一.ROS动作编程:客户端发送一个运动坐标,模拟机器人运动到目标位置的过程环境准备
创建工作空间
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
编译工作空间
cd ~/catkin_ws/
catkin_make
设置环境变量
source devel/setup.bash
echo $ROS_PACKAGE_PATH
创建功能包
cd ~/catkin_ws/src
catkin_create_pkg ros_communication std_msgs rospy roscpp
cd~/catkin_ws
编译功能包
catkin_make
source ~/catkin_ws/devel/setup.bash
编写程序
创建turtle.cpp文件
cd ~/catkin_ws/src/ros_communication/src
touch turtleMove.cpp
gedit turtleMove.cpp
在创建的turtleMove.cpp文件写入如下代码
#include<ros/ros.h>
#include<actionlib/server/simple_action_server.h>
#include"ros_communication/turtleMoveAction.h"
#include<turtlesim/Pose.h>
#include<turtlesim/Spawn.h>
#include<geometry_msgs/Twist.h>
typedef actionlib::SimpleActionServer<ros_communication::turtleMoveAction> Server;
struct Myturtle
{
float x;
float y;
float theta;
}turtle_original_pose,turtle_target_pose;
ros::Publisher turtle_vel;
void posecallback(const turtlesim::PoseConstPtr &msg)
{
ROS_INFO("turtle1_position:(%f,%f,%f)",msg->x,msg->y,msg->theta);
turtle_original_pose.x=msg->x;
turtle_original_pose.y=msg->y;
turtle_original_pose.theta=msg->theta;
}
// 收到 action 的 goal 后调用该回调函数
void execute(const ros_communication::turtleMoveGoalConstPtr &goal, Server* as)
{
ros_communication::turtleMoveFeedback feedback;
ROS_INFO("TurtleMove is working.");
turtle_target_pose.x=goal->turtle_target_x;
turtle_target_pose.y=goal->turtle_target_y;
turtle_target_pose.theta=goal->turtle_target_theta;
geometry_msgs::Twist vel_msgs;
float break_flag;
while(1)
{
ros::Rate r(10);
vel_msgs.angular.z = 4.0 *(atan2(turtle_target_pose.y-turtle_original_pose.y,
turtle_target_pose.x-turtle_original_pose.x)-turtle_original_pose.theta);
vel_msgs.linear.x = 0.5 *sqrt(pow(turtle_target_pose.x-turtle_original_pose.x, 2) +pow(turtle_target_pose.y-turtle_original_pose.y, 2));
break_flag=sqrt(pow(turtle_target_pose.x-turtle_original_pose.x, 2)+pow(turtle_target_pose.y-turtle_original_pose.y, 2));
turtle_vel.publish(vel_msgs);
feedback.present_turtle_x=turtle_original_pose.x;
feedback.present_turtle_y=turtle_original_pose.y;
feedback.present_turtle_theta=turtle_original_pose.theta;
as->publishFeedback(feedback);
ROS_INFO("break_flag=%f",break_flag);
if(break_flag<0.1) break;
r.sleep();
}
// 当 action 完成后,向客户端返回结果
ROS_INFO("TurtleMove is finished.");
as->setSucceeded();
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "turtleMove");
ros::NodeHandle n,turtle_node;
ros::Subscriber sub =
turtle_node.subscribe("turtle1/pose",10,&posecallback); //订阅小乌龟的位置信息
turtle_vel =turtle_node.advertise<geometry_msgs::Twist>("turtle1/cmd_vel",10);//发布控制小乌龟运动的速度
// 定义一个服务器
Server server(n, "turtleMove", boost::bind(&execute, _1,&server), false);
// 服务器开始运行
server.start();
ROS_INFO("server has started.");
ros::spin();
return 0;
}
创建小乌龟目标位置文件turtleMoveClient.cpp
touch turtleMoveClient.cpp
gedit turtleMoveClient.cpp
在创建的turtleMoveClient.cpp文件中写入如下代码
#include <actionlib/client/simple_action_client.h>
#include "ros_communication/turtleMoveAction.h"
#include <turtlesim/Pose.h>
#include <turtlesim/Spawn.h>
#include <geometry_msgs/Twist.h>
typedef actionlib::SimpleActionClient<ros_communication::turtleMoveAction>
Client;
struct Myturtle
{
float x;
float y;
float theta;
}turtle_present_pose;
// 当 action 完成后会调用该回调函数一次
void doneCb(const actionlib::SimpleClientGoalState &state,
const ros_communication::turtleMoveResultConstPtr &result)
{
ROS_INFO("Yay! The turtleMove is finished!");
ros::shutdown();
}
// 当 action 激活后会调用该回调函数一次
void activeCb()
{
ROS_INFO("Goal just went active");
}
// 收到 feedback 后调用该回调函数
void feedbackCb(const ros_communication::turtleMoveFeedbackConstPtr &feedback)
{
ROS_INFO(" present_pose : %f %f %f",
feedback->present_turtle_x,
feedback->present_turtle_y,feedback->present_turtle_theta);
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "turtleMoveClient");
// 定义一个客户端
Client client("turtleMove", true);
// 等待服务器端
ROS_INFO("Waiting for action server to start.");
client.waitForServer();
ROS_INFO("Action server started, sending goal.");
// 创建一个 action 的 goal
ros_communication::turtleMoveGoal goal;
goal.turtle_target_x = 1;
goal.turtle_target_y = 1;
goal.turtle_target_theta = 0;
// 发送 action 的 goal 给服务器端,并且设置回调函数
client.sendGoal(goal, &doneCb, &activeCb, &feedbackCb);
ros::spin();
return 0;
}
在功能包目录下创建action文件夹
cd ~/catkin_ws/src/ros_communication
mkdir action
cd action
touch turtleMove.action
gedit turtleMove.action
在创建的turtleMove.action文件中写入如下代码
# Define the goal
float64 turtle_target_x # Specify Turtle's target position
float64 turtle_target_y
float64 turtle_target_theta
---
# Define the result
float64 turtle_final_x
float64 turtle_final_y
float64 turtle_final_theta
---
# Define a feedback message
float64 present_turtle_x
float64 present_turtle_y
float64 present_turtle_theta
修改CMakeLists.txt的内容
cd ~/catkin_ws/src/ros_communication/src
sudo gedit CMakeLists.txt
在CMakeLists.txt文件后加入如下代码
add_executable(turtleMoveClient src/turtleMoveClient.cpp)
target_link_libraries(turtleMoveClient ${catkin_LIBRARIES})
add_dependencies(turtleMoveClient ${PROJECT_NAME}_gencpp)
add_executable(turtleMove src/turtleMove.cpp)
target_link_libraries(turtleMove ${catkin_LIBRARIES})
add_dependencies(turtleMove ${PROJECT_NAME}_gencpp)
找到find_package()函数做如下修改
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
actionlib_msgs
actionlib
)
找到add_action_files函数,默认是用#注释掉了的。这里我们取消掉该部分的注释,再修改文以下代码
add_action_files(
FILES
turtleMove.action
)
找到 generate_messages 函数一项,默认也是#注释,同样取消掉该部分注释,并修改为以下代码
generate_messages(
DEPENDENCIES
std_msgs
actionlib_msgs
)
找到 catkin_package 函数一项,修改为如下代码
# INCLUDE_DIRS include
# LIBRARIES comm
# CATKIN_DEPENDS roscpp rospy std_msgs
# DEPENDS system_lib
CATKIN_DEPENDS roscpp rospy std_msgs
message_runtime
配置package.xml文件
gedit package.xml
找到build_depend一项,修改为以下代码
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>message_generation</build_depend>
<build_depend>actionlib</build_depend>
<build_depend>actionlib_msgs</build_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>rospy</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
将文件中 exec_depend 一栏、替换为如下代码
<exec_depend>roscpp</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<exec_depend>message_runtime</exec_depend>
<exec_depend>actionlib</exec_depend>
<exec_depend>actionlib_msgs</exec_depend>
启动程序
编译并设置环境变量
cd ~/catkin_ws
catkin_make
source ~/catkin_ws/devel/setup.bash
新建终端1
roscore
新建终端2
source ~/catkin_ws/devel/setup.bash
rosrun turtlesim turtlesim_node
新建终端3
source ~/catkin_ws/devel/setup.bash
rosrun ros_communication turtleMove
新建终端4
source ~/catkin_ws/devel/setup.bash
rosrun ros_communication turtleMoveClient
效果如下
二.在两台电脑上演示ROS的分布式通信
要求两台电脑连同一局域网
1.获取两台电脑的IP和主机名
ifconfig
由图可知本电脑的IP为192.168.176.65,实验另一台电脑IP为192.168.176.92(注意需要将电脑网络设置为桥接模式)
由图可知本电脑的名为ttljq-virtual-machine,实验另一台电脑名为wz-virtual-mechine
2.进行绑定
sudo gedit /etc/hosts
将打开的hosts文件加入对方的IP地址和hostname(注意中间要用tab键间隔)
3.重启网络
sudo /etc/init.d/networking restart
如图则重启网络成功
4.下载两个包
sudo apt-get install chrony
sudo apt-get install openssh-server
5.查看ssh是否启动成功
ps -e|grep ssh
出现sshd则表明启动成功
6.检测通信
两台电脑互相ping,ping通则证明可以通信,可以ping IP地址也可以ping hostname
ping 192.168.176.92
或
ping wz-virtual-mechine
7.两台机器配置
从机配置(本电脑为从机,队友为主机)
export ROS_MASTER_URI=http://wz-virtual-machine:11311
export ROS_HOSTNAME=ttljq-virtual-mechine #从机IP或名称
主机配置
export ROS_HOSTNAME=wz-virtual-machine #主机IP或名称
export ROS_MASTER_URI=http://wz-virtual-machine:11311 #主机ip或名称
8.通信测试
主机运行小海龟仿真器
roscore
rosrun turtlesim turtlesim_nod
从机上使用“rostopic list”命令查看ROS系统中的话题列表,打开小乌龟键盘控制节点,然后控制小乌龟运动
rostopic list
rosrun turtlesim turtle_teleop_key
9.效果如下
三、总结
ROS通信编程实验是机器人学习和开发过程中的重要环节,通过实践掌握ROS通信机制和技术要点,对于提升个人能力和促进机器人技术的发展具有重要意义。
四.参考博客
https://blog.csdn.net/ssj925319/article/details/109231145
【嵌入式】Windows10&Ubantu16.04&树莓派3B+安装opencv教程_opencv 嵌入式 树莓派-CSDN博客
https://blog.csdn.net/qq_46467126/article/details/120629602