ROS通信编程

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

环境准备

创建工作空间

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

编译工作空间

cd ~/catkin_ws/
catkin_make

创建功能包

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

编译功能包

catkin_make
source ~/catkin_ws/devel/setup.bash

编写程序

创建cpp文件

cd ~/catkin_ws/src/ros_communication/src
touch turtleMove.cpp
gedit 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;
}

创建小乌龟目标位置文件

touch turtleMoveClient.cpp
gedit 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

加入如下代码

# 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

剩余部分如下。

运行完成。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值