6. Hello World ROS - C++
Tương tự như phần hướng dẫn ROS cơ bản trong Python, chúng ta có thể tạo các node ROS trong code C++. Các node được tạo ra cũng 2 node cơ bản là talker và listener và kết nối với nhau qua topic /chatter.

Bước 1. Bạn tạo thư mục src
của package hello_world
roscd hello_world
mkdir -p src
Bước 2. Các bạn tạo file talker.cpp
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
/**
* This tutorial demonstrates simple sending of messages over the ROS system.
*/
int main(int argc, char **argv)
{
ros::init(argc, argv, "talker");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
ros::Rate loop_rate(10);
/**
* A count of how many messages we have sent. This is used to create
* a unique string for each message.
*/
int count = 0;
while (ros::ok())
{
/**
* This is a message object. You stuff it with data, and then publish it.
*/
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;
}
Bước 3. Tạo node subscriber (listener.cpp)
#include "ros/ros.h"
#include "std_msgs/String.h"
/**
* This tutorial demonstrates simple receipt of messages over the ROS system.
*/
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]", msg->data.c_str());
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "listener");
/**
* NodeHandle is the main access point to communications with the ROS system.
* The first NodeHandle constructed will fully initialize this node, and the last
* NodeHandle destructed will close down the node.
*/
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
ros::spin();
return 0;
}
Bạn có thể đọc thêm phần giải thích code cụ thể ở đây:
Bước 4. Build package
Bạn chỉnh sửa lại file package.xml như sau:
<?xml version="1.0"?>
<package format="2">
<name>hello_world</name>
<version>0.1.0</version>
<description>The hello_world example package</description>
<maintainer email="your_email">your_name</maintainer>
<license>Apache 2.0</license>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_export_depend>roscpp</build_export_depend>
<exec_depend>roscpp</exec_depend>
</package>
Sau đó bạn chỉnh sửa lại file CMakeList.txt như sau
# %Tag(FULLTEXT)%
cmake_minimum_required(VERSION 2.8.3)
project(hello_world)
## Find catkin and any catkin packages
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg)
## Declare a catkin package
catkin_package()
## Build talker and listener
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_executable(listener src/listener.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
# %EndTag(FULLTEXT)%
dẫn đường dẫn đến folder và compile catkin_ws
cd catkin_ws
catkin_make
Last updated
Was this helpful?