ROS通信模式编程

目录

一.ROS动作编程:客户端发送一个运动坐标,模拟机器人运动到目标位置的过程

环境准备

​编辑启动程序

二.在两台电脑上演示ROS的分布式通信

​编辑三.参考博客

四.总结


一.ROS动作编程:客户端发送一个运动坐标,模拟机器人运动到目标位置的过程

环境准备

1、创建工作空间

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace



2、编译工作空间

cd ~/catkin_ws/
catkin_make


3、设置环境变量

source devel/setup.bash
echo $ROS_PACKAGE_PATH


4、创建功能包
 

cd ~/catkin_ws/src
catkin_create_pkg ros_communication std_msgs rospy roscpp
cd~/catkin_ws


5、编译功能包

catkin_make
source ~/catkin_ws/devel/setup.bash


6、编写程序
①创建turtle.cpp文件

①创建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文件夹
 

d ~/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(注意需要将电脑网络设置为桥接模式)

hostname


由图可知本电脑的名为ttljq-virtual-machine,实验另一台电脑名为wz-virtual-mechine


2.进行绑定

hostname


将打开的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_MASTER_URI=http://wz-virtual-machine:11311 #主机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.效果如下


三.参考博客


https://blog.csdn.net/weixin_51244852/article/details/115738730
https://blog.csdn.net/qq_42585108/article/details/104852100?

四.总结


本次实验实现了ROS通信模式编程,ROS动作编程:客户端发送一个运动坐标,模拟机器人运动到目标位置的过程。过程中出现了很多问题,但有很多是因为文件名字不正确或者路径不正确,本机与参考博客的命名方式不一样导致会出现很多类似的问题,解决方式就是对应找到相应的文件名称和路径对应参考博客中的命令做出修改。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值