Howto Write and build ROS program on Ubuntu
Herewe will give an example to illustrate howto write and buildROS(Groovy) program on Ubuntu. The complete example can be downloaded at: http://download.csdn.net/detail/owldestiny/5286602 .
Beforeyou go, make sure you have install and configure ROS following theinstruction Howto Install and Configure ROS on Ubuntu.
Theoverall program structure of the program is:
│ └── ros_test
│ ├── bin
│ ├── build
│ ├── CMakeLists.txt
│ ├── CMakeLists.txt.user
│ ├── lib
│ ├── msg
│ │ └── test.msg
│ ├── package.xml
│ └── src
│ └── main.cpp
Theprogram is in the directory /program/ros_workspace/src/test/ros_test.
TheCMakeLists.txt is:
=========================================================================
cmake_minimum_required(VERSION2.8.3)
project(test)
#========================================================================
#cmakesettings
set(LIBRARY_OUTPUT_PATH${PROJECT_SOURCE_DIR}/lib)
set(EXECUTABLE_OUTPUT_PATH${PROJECT_SOURCE_DIR}/bin)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/
/program/ros_workspace/devel/include)#to include the generated file from msgs
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/srcPROJECT_SOURCES)
#========================================================================
#========================================================================
#ros
find_package(catkinREQUIRED
COMPONENTSroscpp std_msgs sensor_msgs message_generation) #build compoment
#message(STATUS"catkin_INCLUDE_DIRS:" ${catkin_INCLUDE_DIRS})
#message(STATUS"catkin_LIBRARIES:" ${catkin_LIBRARIES})
#message(STATUS"catkin_DEFINITIONS:" ${catkin_DEFINITIONS})
#message(STATUS"catkin_INSTALL:" ${catkin_PACKAGE_BIN_DESTINATION})
#rosmsgs
add_message_files(FILEStest.msg)
generate_messages(DEPENDENCIESstd_msgs)
#========================================================================
add_executable(ros_test${PROJECT_SOURCES})
target_link_libraries(ros_test${catkin_LIBRARIES})
install(TARGETSros_test
ARCHIVE DESTINATION /program/ros_workspace/bin
LIBRARY DESTINATION /program/ros_workspace/bin
RUNTIME DESTINATION /program/ros_workspace/bin
)
=========================================================================
Thepackage.xml is:
=========================================================================
<package>
<name>test</name>
<version>0.0.1</version>
<description>
Thisis the test program of ros.
</description>
<maintaineremail="chen0510566@163.com">Chen Cheng</maintainer>
<license>BSD</license>
<buildtool_depend>catkin</buildtool_depend>
<!--
<build_depend>roscpp</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>sensor_msgs</build_depend>
<run_depend>roscpp</run_depend>
<run_depend>std_msgs</run_depend>
<run_depend>sensor_msgs</run_depend>
-->
<!--<build_depend></build_depend>-->
<!--<run_depend></run_depend>-->
<!--<test_depend></test_depend>-->
</package>
=========================================================================
Themain.cpp is:
=========================================================================
#include<iostream>
#include<ros/ros.h>
#include<std_msgs/String.h>
#include<test/test.h>
intmain(int argc, char** argv)
{
ros::init(argc, argv, "test");
ros::NodeHandle n;
ros::Publisher chatter_pub =n.advertise<std_msgs::String>("chatter", 1000);
test::test testmsg;
ros::Rate loop_rate(10);
intcount = 0;
while (ros::ok())
{
std_msgs::String msg;
std::stringstream ss;
ss << "hello world " << count;
msg.data = ss.str();
ROS_INFO("%s", msg.data.c_str());
chatter_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++count;
}
return(0);
}
=========================================================================
Thetest.msg is:
=========================================================================
Headerheader
float32f1
float32f2
uint8n1
=========================================================================
Tobuild the program, you can use either cmake commands or catkin_makecommands.
Buildthe program by cmake:
$cd/program/ros_workspace/src/test/ros_test/build
$cmake..
$make
Here you may get the error info test/test.h no suchfile, that's because the test.h is generated in the project builddirectory, the CMakeLists.txt set the file in ros_workspace/develdirectory, which is generated by catkin_make. If you usegenerate_messages in the CMakeLists.txt, the generated file will bein build/devel/include and build/devel/lib directory even if you haveset the output directory for the binary. If there is nogenerate_messages, the binary will be in the directory you set.
Buildthe program by catkin_make:
$cd/program/ros_workspace
$catkin_make
The generated binary files is in/program/ros_workspace/devel/lib and include files in/program/ros_workspace/devel/include. Maybe you have to runcatkin_make twice to build the projects, especially for the projectsthat generate msgs.
Toinstall the binaries to specific directories, follow the commands:
$cd/program/ros_workspace/build
$makeinstall