publisher例程
按照指定频率向小海龟控制频段发送移动数据
//velocity_publisher.cpp
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
int main(int argc, char **argv){
ros::init(argc, argv, "velocity_publisher");
ros::NodeHandle n;
ros::Publisher turtle_vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel",10);
ros::Rate loop_rate(10);
int count = 0;
while(ros::ok()){
geometry_msgs::Twist vel_msg;
vel_msg.linear.x = 0.5;
vel_msg.angular.z = 0.2;
turtle_vel_pub.publish(vel_msg);
ROS_INFO("Publish turtle velocity command[%0.2f m/s, %0.2f m/s]",
vel_msg.linear.x, vel_msg.angular.z);
loop_rate.sleep();
}
return 0;
}
在CMakeLists.txt
中添加编译链接
add_executable(velocity_publisher src/velocity_publisher.cpp)
target_link_libraries(velocity_publisher ${catkin_LIBRARIES})
subscriber例程
//pose_subscriber.cpp
#include<ros/ros.h>
#include<turtlesim/Pose.h>
void poseCallback(const turtlesim::Pose::ConstPtr &msg){
ROS_INFO("Turtle pose: x:%0.6f, y:%0.6f", msg->x, msg->y);
}
int main(int argc, char **argv){
ros::init(argc, argv, "pose_subscriber");
ros::NodeHandle n;
ros::Subscriber pose_sub = n.subscribe("/turtle1/pose", 10, poseCallback);
ros::spin();
return 0;
}
自定义消息类型
创建一个.msg
后缀的消息文件,内容为消息格式的描述文件,会在编译时翻译成对应语言的数据类型格式。
在哪个包里用,就创建在哪个包里
//person.msg
string name
uint8 sex
uint8 age
uint8 unkown = 0
uint8 male = 1
uint8 female = 2
package.xml
中添加自定义消息类型创建的依赖库
<build_depend>message_generation</build_depend>
<exec_depend>message_generation</exec_depend>
<exec_depend>message_runtime</exec_depend>
CMakeLists.txt
中添加上述依赖包
find_package(catkin REQUIRED COMPONENTS
geometry_msgs
roscpp
rospy
std_msgs
turtlesim
message_generation
)
在下面的区域中添加自定义消息的编译链接
add_message_files(FILES person.msg)
generate_messages(DEPENDENCIES std_msgs) //自定义消息的依赖包
添加编译依赖项
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES learning_topic
CATKIN_DEPENDS geometry_msgs roscpp rospy std_msgs turtlesim message_generation
# DEPENDS system_lib
)
发送和订阅的代码
//person_publisher.cpp
#include<ros/ros.h>
#include<learning_topic/person.h>
int main(int argc, char** argv){
ros::init(argc, argv, "person_publisher");
ros::NodeHandle n;
ros::Publisher person_info_pub = n.advertise<learning_topic::person>("/person_info",10);
ros::Rate loop_rate(1);
while(ros::ok()){
learning_topic::person person_msg;
person_msg.name = "Tom";
person_msg.age = 18;
person_msg.sex = learning_topic::person::male;
person_info_pub.publish(person_msg);
ROS_INFO("Published Person info: name:%s, age:%d, sex:%d",
person_msg.name.c_str(), person_msg.age, person_msg.sex);
loop_rate.sleep();
}
return 0;
}
//person_subscriber.cpp
#include<ros/ros.h>
#include<learning_topic/person.h>
void poseCallback(const learning_topic::person::ConstPtr &msg){
ROS_INFO("Get person info name:%s, age:%d, sex:%d", msg->name.c_str(), msg->age, msg->sex);
}
int main(int argc, char **argv){
ros::init(argc, argv, "person_subscriber");
ros::NodeHandle n;
ros::Subscriber person_sub = n.subscribe("/person_info", 10, poseCallback);
ros::spin();
return 0;
}
添加编译链接,由于与动态生成代码的地方有依赖关系,故教程上说这里各多了一行add_dependencies
但实际测试并不需要,安全起见加上
add_executable(person_publisher src/person_publisher.cpp)
target_link_libraries(person_publisher ${catkin_LIBRARIES})
add_dependencies(person_publisher ${PROJECT_NAME}_generate_messages_cpp)
add_executable(person_subscriber src/person_subscriber.cpp)
target_link_libraries(person_subscriber ${catkin_LIBRARIES})
add_dependencies(person_subscriber ${PROJECT_NAME}_generate_messages_cpp)