mkdir hello_world_tutorial
cd hello_world_tutorial
hello_world_node.cpp
// Include the ROS C++ APIs
#include <ros/ros.h>
// Standard C++ entry point
int main(int argc, char** argv) {
// Announce this program to the ROS master as a "node" called "hello_world_node"
ros::init(argc, argv, "hello_world_node");
// Start the node resource managers (communication, time, etc)
ros::start();
// Broadcast a simple log message
ROS_INFO_STREAM("Hello, world!");
// Process ROS callbacks until receiving a SIGINT (ctrl-c)
ros::spin();
// Stop the node's resources
ros::shutdown();
// Exit tranquilly
return 0;
}
hello_world_node.cpp The source file(s) to compile
-o hello_world_node The name of the output file (the executable, in this case)
-I/opt/ros/hydro/include An instruction to look for C++ header files in /opt/ros/hydro/include
-L/opt/ros/hydro/lib An instruction to look for static libraries in /opt/ros/hydro/lib
-Wl,-rpath,/opt/ros/hydro/lib An instruction to look for shared libraries in /opt/ros/hydro/lib
-lroscpp Link against the library libroscpp.so (ROS C++ bindings)
-lrosconsole Link against the library librosconsole.so (ROS distributed logging)
-lrostime Link against the library librostime.so (ROS time measurement)
3.运行
./hello_world_node
4.常见错误及解决
4.1 ERROR: ROS_MASTER_URI is not defined (Bad Environment)
[FATAL] [1392021564.231775029]: ROS_MASTER_URI is not defined in the
environment. Either type the following or (preferrably) add this to your
~/.bashrc file in order set up your local machine as a ROS master:
export ROS_MASTER_URI=http://localhost:11311then, type'roscore'in another shell to actually launch the master program.
解决:
source /opt/ros/hydro/setup.sh
./hello_world_node
ERROR: Failed to contact master (Good Environment, but no ROS Master)
[ERROR] [1392014787.460431497]: [registerPublisher] Failed to contact master at [localhost:11311]. Retrying...