Howto Write and build ROS program on Ubuntu

Source: Internet
Author: User

Howto Write and build ROS program on Ubuntu

Herewe would 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 Ubu Ntu.


Theoverall program structure to 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::P ublisher chatter_pub =n.advertise<std_msgs::string> ("chatter", 1000);
Test::test testmsg;

Ros::rate Loop_rate (a);
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, can use either CMake commands or catkin_makecommands.

Buildthe by CMake:

$CD/program/ros_workspace/src/test/ros_test/build

$cmake.

$make

Here's the error info test/test.h no suchfile, that's because the test.h is generated in the project BuildDirect Ory, the CMakeLists.txt set the file in Ros_workspace/develdirectory, which are generated by Catkin_make. If you are usegenerate_messages in the CMakeLists.txt, the generated file would bein Build/devel/include and Build/devel/lib di Rectory even if you are haveset the output directory for the binary. If There is nogenerate_messages, the binary would be in the directory for your set.


Buildthe by Catkin_make:

$CD/program/ros_workspace

$catkin _make

The generated binary the files are 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.


Toinstall the binaries to specific directories, follow the commands:

$CD/program/ros_workspace/build

$makeinstall

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.