ROS教程第五步
程序员文章站
2022-05-24 18:21:58
...
编写一个简单的节点,用于发布消息和接收消息
1. 发布节点文件
#include"ros/ros.h"
#include"runcar/carinfo.h"
#include"geometry_msgs/Twist.h"
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main(int argc,char **argv){
ros::init(argc,argv,"theFirstCar"); // 当前节点的名字
ros::NodeHandle n;
ros::Publisher pub_pos_info = n.advertise<runcar::carinfo>("/car_pos",5);
ros::Publisher pub_ctrl = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel",5);
ros::Rate delay(10);
int count =0;
float x1 = 0;
float y1 = 0;
float linear_x = 2;
float angular_z = 2;
while (ros::ok()){
runcar::carinfo carpos;
carpos.px = x1;
carpos.py = y1;
pub_pos_info.publish(carpos);
ROS_INFO("car1pos: px = %f py = %f [Count = %d]",carpos.px,carpos.py,count);
geometry_msgs::Twist ctrl_msg;
ctrl_msg.linear.x = linear_x;
ctrl_msg.angular.z = angular_z;
pub_ctrl.publish(ctrl_msg);
ROS_INFO("car1ctrl: linear_x = %f angular_z = %f [Count = %d]",ctrl_msg.linear.x,ctrl_msg.angular.z,count);
linear_x += 0.1;
angular_z -= 0.1;
x1 += 0.1;
y1 += 0.1;
count++;
ros::spinOnce();
delay.sleep();
}
}
2. 接收节点文件
#include"ros/ros.h"
#include"runcar/carinfo.h"
#include"geometry_msgs/Twist.h"
#include<string>
using namespace std;
void carpos_callback(const runcar::carinfo::ConstPtr& carpos){
ROS_INFO("carpos_message: px =%f py = %f",carpos->px,carpos->py);
}
void carctrl_callback(const geometry_msgs::Twist::ConstPtr& carctrl){
ROS_INFO("carctrl_message: linear_x = %f angular_z = %f",carctrl->linear.x,carctrl->angular.z);
}
int main(int argc,char **argv){
ros::init(argc,argv,"carReceive"); // 接收的节点名字
ros::NodeHandle n;
ros::Subscriber sub_chat1 = n.subscribe("/car_pos",5,carpos_callback);
ros::Subscriber sub_chat2 = n.subscribe("/turtle1/cmd_vel",5,carctrl_callback);
ros::Rate loop(1);
/*while (ros::ok()){ /// 采取循环方式
ros::spinOnce();
loop.sleep();
}*/
ros::spin(); // 一直保留在这里等待
return 0;
}
3. 消息文件
float64 px
float64 py
float64 vx
float64 vy
float64 ux
float64 uy
float64 u
float64 v
4. CmakeList.txt 编译文件
cmake_minimum_required(VERSION 2.8.3)
project(runcar)
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
geometry_msgs
message_generation
)
add_message_files(
FILES
# Message1.msg
# Message2.msg
carinfo.msg
)
generate_messages(
DEPENDENCIES
# std_msgs # Or other packages containing msgs
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES runcar
# CATKIN_DEPENDS roscpp rospy
# DEPENDS system_lib
)
include_directories(
include
${catkin_INCLUDE_DIRS}
)
add_executable(exec_car1_du src/carrun.cpp)
add_dependencies(exec_car1_du ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(exec_car1_du ${catkin_LIBRARIES})
add_executable(exec_car1_re src/carreceive.cpp)
add_dependencies(exec_car1_re ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(exec_car1_re ${catkin_LIBRARIES})
5. package.xlm文件
<?xml version="1.0"?>
<package format="2">
<name>runcar</name>
<version>0.0.0</version>
<description>The runcar package</description>
<maintainer email="[email protected]">macl</maintainer>
<license>TODO</license>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>rospy</build_export_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>rospy</exec_depend>
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->
</export>
</package>
6. launch文件
<launch>
<node pkg = "turtlesim" name = "car1" type = "turtlesim_node"/>
<node pkg = "runcar" name = "theFirstCar" type = "exec_car1_du"/>
<node pkg = "runcar" name = "carReceive" type = "exec_car1_re" output = "screen"/>
</launch>
推荐阅读
-
PDO防注入原理分析以及注意事项,pdo注入_PHP教程
-
JavaScript鼠标拖拽及多选功能的实现方法教程
-
Access入门教程9.4认识查询设计视图
-
PHP传值到不同页面的三种常见方式及php和html之间传值问题,phphtml_PHP教程
-
LAMP(Apache MySQL PHP)一键安装包教程(CentOS 5 32bit)
-
PHP实现html源码中回车换行输出_PHP教程
-
php正则表达式获取内容所有链接,php正则表达式获取_PHP教程
-
QtQuick 基础教程 第三课: QML State状态与Transition变换
-
php微信开发之上传临时素材,php开发素材_PHP教程
-
session全教程(三)_PHP教程