moriarty / ros-ev3

How to install ROS (ros_comm) on an ev3 with ev3dev
GNU General Public License v2.0
24 stars 19 forks source link

How to add ev3dev.cpp as a library for my ROS program #7

Closed Tom-Shen closed 8 years ago

Tom-Shen commented 8 years ago

@moriarty I have built a simple advertise program to send my "Move" command, then I need to build a subscribe program to receive my command then operate my EV3 car, I believe I just add the ev3dev.cpp as a library, is it right? how should I do it, how to setup the CMAKELIST.txt?

The advertise program was built under Ubuntu - I have successfully done this, I believe the subscribe program should be built under the Brickstrap Environment, do you have detail instruction how to do it?

Thank you very much for your support, very appreciated!

By the way, I wrote a comment for the ROS-EV3 over Jade on the previous issue page.

moriarty commented 8 years ago

Sorry,

I left out those details from the past issue answer because I couldn't think of a clean or proper way. There are three ways I can think of of the top of my head.

I'll give you the quick-and-dirty solution. At the moment I haven't got time to fully test if this works.

  1. Modify the ev3dev-lang-cpp CMakeList.txt in ev3dev-lang-cpp to make a shared library.

    add_library(ev3dev SHARED ev3dev.cpp)

  2. Put the shared object file libev3dev.so in /usr/lib and the ev3dev.h in /usr/include.
    • Even better, properly modify the CMakeList to install the library and header there, but these are the quick-and-dirty instructions.
    • This needs to be done on your EV3 and in your brickstrap environment if you've already created the SD card.
  3. Add ev3dev to CMakeList.txt as you would a standard system library dependency

    • For example, here are parts of the CMakeList.txt which edited after generate using catkin_create_pkg:

      catkin_create_pkg tutorial std_msgs roscpp rospy

    catkin_package(
     INCLUDE_DIRS include
     LIBRARIES tutorial
     CATKIN_DEPENDS roscpp rospy std_msgs
     DEPENDS ev3dev
    )
    add_executable(talker_node src/talker.cpp)
    target_link_libraries(talker_node
      ${catkin_LIBRARIES}
      ev3dev
    )
  4. I ran catkin_make. Here is the output of ldd showing it was able to find everything.

    (brickstrap)root@alex-linux-desktop:/home/robot/catkin_ws# ldd devel/lib/tutorial/talker_node | grep ev3
       libev3dev.so => /usr/lib/libev3dev.so (0xf6207000)

Again, this is just a quick and dirty solution which I haven't tested but it should get your up and running.

Tom-Shen commented 8 years ago

@moriarty I see.

I have completed my ROS test program, I can compile in ubuntu environment, it contains 2 CPP files, cmakelist.txt, package.xml, I want to build it under brickstrap, which library I should put in the brickstrap, and after put them into correct library, should I just execute catkin_make?

Tom-Shen commented 8 years ago

@moriarty I thought I have solved previous issue, in my program, I need geometry_msgs, how can I add it to my brickstrap Ros-EV3?

Thanks.

moriarty commented 8 years ago

Here is a modified CMakeLists.txt for ev3dev-lang-cpp

cmake_minimum_required(VERSION 2.8)
if (NOT CMAKE_BUILD_TYPE)
    message(STATUS "No build type selected, default to RelWithDebInfo")
    set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Build type")
endif()

project(ev3dev-lang-cpp)

set(EV3DEV_PLATFORM "EV3" CACHE STRING "Target ev3dev platform (EV3/RPI)")
set_property(CACHE EV3DEV_PLATFORM PROPERTY STRINGS "EV3" "RPI")

include_directories(${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")

add_library(ev3dev SHARED ev3dev.cpp)

target_link_libraries(ev3dev pthread)

install(TARGETS ev3dev
        LIBRARY DESTINATION lib
)
install(FILES ev3dev.h
        DESTINATION include
)

#function(add_ev3_executable target sources)
#    add_executable(${target} ${sources})
#    target_link_libraries(${target} ev3dev pthread)
#endfunction()

#enable_testing()

#add_subdirectory(demos)
#add_subdirectory(tests)

I've changed the library type to "shared", linked pthread and added install commands. and commented out the demos and tests.

Now, following the other instructions from ev3dev-lang-cpp, with the additional step to install the library and header files.

For example, it will output something like this:

$ sudo make install
[sudo] password for alex: 
[100%] Built target ev3dev
Install the project...
-- Install configuration: "RelWithDebInfo"
-- Installing: /usr/local/lib/libev3dev.so
-- Installing: /usr/local/include/ev3dev.h

You should run these commands in brickstrap. This will put the library in the standard location inside of the brickstrap environment. On the EV3 itself, it will work but it's a little slow, so you can just copy the libev3dev.so and ev3dev.h which it installed above onto the EV3 when you're ready to test.

Now, with the libev3dev.so and ev3dev.h files in place, you can use them like I mentioned in the comment above.

And just run catkin_make

moriarty commented 8 years ago

You'll need to clone geometry messages into your catkin workspace.

Are you working in a clean workspace or are you still working in the original workspace from step 7 of the ros_comm?

If you know you'll need several dependencies you can add them to the original rosinstall from step 7 so that you know they will be avialable when you generate the sd card image.

The ros_comm package doesn't come with much, just the essentials.

moriarty commented 8 years ago

From step 7:

And this wiki page: http://wiki.ros.org/rosinstall_generator

rosinstall_generator ros_comm geometry_msgs --rosdistro indigo --deps --wet-only --tar > indigo-ros_comm-wet.rosinstall
Tom-Shen commented 8 years ago

Hello, when I build the EV3DEV.CPP, I got below error, I did not change the ev3dev.cpp and ev3dev.h.

/host-rootfs/home/jerrys/Documents/EV3/ev3devlib/ev3dev.h:34:20: error: #include nested too deeply

include "ev3dev.h"

moriarty commented 8 years ago

Interesting-

That will need to wait until I'm near a computer.

moriarty commented 8 years ago

See answer here:

http://stackoverflow.com/questions/6021720/c-nested-include-avoiding-include-nested-too-deeply-error

I don't see that done in: https://github.com/ddemidov/ev3dev-lang-cpp/blob/master/ev3dev.h

moriarty commented 8 years ago

But this is likely pointing to a bigger mistake in my instructions.

Tom-Shen commented 8 years ago

I have successful built the ev3dev library, I do not know by some reason my ev3dev.h was replaced by something else, I have copied from the website again, it works.

Thank you so much for the help, then I should work on the how to add geometry_msgs.

moriarty commented 8 years ago

I've updated the instructions to include ros_comm and common_msgs. I'm waiting for catkin_make_isolated to finish now.

I will also add a step to install ev3dev-lang-cpp using the CMakeLists.txt from the comment above.

Tom-Shen commented 8 years ago

@moriarty thanks!

moriarty commented 8 years ago

@Tom-Shen just a warning:

I tested on brickstrap and compiling worked.

I was then lazy and decided to test on the ev3 itself, there isn't enough ram/swap to run catkin_make.