机器人前行、旋转的service编写
发布日期:2022-02-06 00:27:04 浏览次数:23 分类:技术文章

本文共 4048 字,大约阅读时间需要 13 分钟。

文章目录

一、机器人前行

1.forward.srv

在这里插入图片描述

请求参数为:
1.前行的距离
2.前行的速度
3.设定的时间,即机器人在给定的时间内运行完成

返回的参数:

1.bool值,是否到达指定位置
2.字符串

2.forward.cpp

//该例程执行/forward服务#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
ros::Subscriber pose_sub;ros::Publisher linear_velocity_pub;nav_msgs::Odometry nowps;geometry_msgs::Twist lin_msg,definite_lin;float add_line,time_out;bool pubCommand;std::string msg;bool forwardCallback(forward_demo::forward::Request &req, forward_demo::forward::Response &res){ lin_msg.linear.x = req.linear_velocity; add_line = req.line; time_out = req.time; ROS_INFO("Publish line [%f] , linear_velocity [%f], time[%f] ", req.line,req.linear_velocity,req.time); ros::Time begin = ros::Time::now(); ROS_INFO_STREAM("The beginning of time :"<
= time_out) { ROS_INFO_STREAM("Time out !"); pubCommand = false; msg = "Time out !"; break; } if(add_line > 0) { if(distance <(add_line-0.6)){ linear_velocity_pub.publish(lin_msg);} else if(distance < add_line) { definite_lin.linear.x = lin_msg.linear.x * (add_line-distance)/0.6; if(definite_lin.linear.x > -0.06 && definite_lin.linear.x<0){ definite_lin.linear.x = -0.04;} if(definite_lin.linear.x < 0.06 && definite_lin.linear.x>0){ definite_lin.linear.x = 0.04;} linear_velocity_pub.publish(definite_lin); } else { finalps.pose.pose.position.x = nowps.pose.pose.position.x; finalps.pose.pose.position.y = nowps.pose.pose.position.y; pubCommand = true; msg = "Change the state successfully !"; ROS_INFO_STREAM("The first pose X :"<
<<"; Y :"<
<<" The final pose X :"<
<<"; Y :"<
("/mobile_base/commands/velocity",10); ROS_INFO("Ready to receive command"); ros::spin(); return 0;}

示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

二、机器人旋转

1.rotate.srv

在这里插入图片描述

2.rotate.cpp

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
ros::Publisher angular_velocity_pub;ros::Subscriber imu_sub;geometry_msgs::Twist ang_msg,definite_ang;double nowangle;float add_angle;bool pubCommand;std::string msg;float time_out;bool rotateCallback(rotate_demo::rotate::Request &req, rotate_demo::rotate::Response &res){ ang_msg.angular.z = req.angular_velocity; add_angle = req.angle; time_out = req.time; ROS_INFO("Publish angle [%f] , angle_velocity [%f] , time[%f] ", req.angle,req.angular_velocity,req.time); ros::Time begin =ros::Time::now(); ROS_INFO_STREAM("The beginning of time :"<
= time_out) { ROS_INFO_STREAM("Time out !"); pubCommand = false; msg = "Time out !"; break; } if(angle_fabs <= 3.1415926 && add_angle > 0) { if(angle_fabs < add_angle-0.5){ angular_velocity_pub.publish(ang_msg);} else if(angle_fabs < add_angle) { definite_ang.angular.z = ang_msg.angular.z*(add_angle-angle_fabs)/0.5; if(definite_ang.angular.z > -0.06 && definite_ang.angular.z < 0) { definite_ang.angular.z = -0.05;} if(definite_ang.angular.z < 0.06 && definite_ang.angular.z > 0) { definite_ang.angular.z = 0.05;} angular_velocity_pub.publish(definite_ang); } else { finalangle = nowangle; pubCommand = true; msg = "Change the state successfully !"; ROS_INFO_STREAM("The initial position yaw :"<
<<" The final position yaw :"<
3.1415926 && add_angle > 0) { if(6.2831852-angle_fabs < add_angle-0.5){ angular_velocity_pub.publish(ang_msg);} else if(6.2831852-angle_fabs < add_angle) { definite_ang.angular.z = ang_msg.angular.z*(add_angle-(6.2831852-angle_fabs))/0.5; if(definite_ang.angular.z > -0.06 && definite_ang.angular.z < 0) { definite_ang.angular.z = -0.05;} if(definite_ang.angular.z < 0.06 && definite_ang.angular.z > 0) { definite_ang.angular.z = 0.05;} angular_velocity_pub.publish(definite_ang); } else { finalangle = nowangle; pubCommand = true; msg = "Change the state successfully !"; ROS_INFO_STREAM("The initial position yaw :"<
<<" The final position yaw :"<
("/mobile_base/commands/velocity",10); ROS_INFO("Ready to receive command"); ros::spin(); return 0;}

小结

周末愉快!

转载地址:https://blog.csdn.net/weixin_46181372/article/details/109923139 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:粒子滤波学习记录
下一篇:ROS 中 boost::bind( ) 的使用

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月06日 00时54分52秒