ROS通信模式编程

目录

一.话题、服务模式的ROS程序设计

1.创建工作空间

2.在功能包中创建动作编程需要的文件

3.修改CMakeLists.txt的内容

4.修改package.xml中内容

 5.运行

二.ROS分布式通信


一.话题、服务模式的ROS程序设计

1.创建工作空间

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

 

编译工作空间

cd ..
catkin_make

 设置环境变量

创建功能包

catkin_create_pkg learning_communication std_msgs rospy roscpp

编译功能包

cd ..
catkin_make
source ~/comm_ws/devel/setup.bash

2.在功能包中创建动作编程需要的文件

1.创建小乌龟移动的“服务文件”turtleMove.cpp

进入工程包下的src文件。使用命令:cd ~/catkin_ws/src/ros_communication/src

 

/*
本程序是为了实现一个为ROS的动作编程,通过本程序是实现client发布一个位置信息给控制海龟移动的turtlel程序,使海龟移动到目标位置
*/
#include<ros/ros.h>
#include<actionlib/server/simple_action_server.h>
#include"learning_communication/turtleMoveAction.h"
#include<turtlesim/Pose.h>
#include<turtlesim/Spawn.h>
#include<geometry_msgs/Twist.h>
typedef actionlib::SimpleActionServer<learning_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 learning_communication::turtleMoveGoalConstPtr &goal, Server* as)
{
learning_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;
}

2.创建小乌龟“发布目标位置文件”turtleMoveClient.cpp

在同一目录下

#include <actionlib/client/simple_action_client.h>
#include "learning_communication/turtleMoveAction.h"
#include <turtlesim/Pose.h>
#include <turtlesim/Spawn.h>
#include <geometry_msgs/Twist.h>
typedef actionlib::SimpleActionClient<learning_communication::turtleMoveAction>
Client;
struct Myturtle
{
	float x;
	float y;
	float theta;
}turtle_present_pose;
// 当 action 完成后会调用该回调函数一次
void doneCb(const actionlib::SimpleClientGoalState &state,
const learning_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 learning_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
	learning_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;
}

3.在功能包目录下创建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

3.修改CMakeLists.txt的内容

cd ~comm_ws/learn_communication
sudo gedit 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

4.修改package.xml中内容

将文件中 build_depend 一栏,exec_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>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>

 5.运行

打开第一个终端,进入工作空间并编译

cd ~/comm_ws
catkin_make
source ./devel/setup.bash
rosrun learning_communication turtleMove

第二个终端,启动ros roscore

打三个终端,运行小海龟 rosrun turtlesim turtlesim_node

第四个终端,

cd ~/catkin_ws
source ./devel/setup.bash
rosrun learning_communitcation turtleMoveClient

二.ROS分布式通信

1.查看ip地址和主机名称

ifconfig   #查看ip地址
hostname   #查看主机名

 

2.修改 hosts 文件,在该文件中加入对方的IP地址和计算机名:
cd /etc

sudo gedit hosts

3.配置主机IP

添加环境变量  cd ~/.banshrc

export ROS_MASTER_URI=http://192.168.136.131:11311  //主机ip或名称
export ROS_HOSTNAME=192.168.136.131

4.配置从机IP

export ROS_MASTER_URI=http://192.168.136.131:11311  //主机ip或名称
export ROS_HOSTNAME=192.168.136.130     //从机ip或名称

5.控制小海龟

主机上运行小乌龟的仿真器

roscore

rosrun turtlesim turtlesim_node

从机上控制小乌龟运动

rosrun turtlesim turtle_teleop_key

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值