机器人前行、旋转的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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月06日 00时54分52秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
理解String.intern()和String类常量池疑难解析例子
2019-04-26
06 Python之函数调用与定义
2019-04-26
07 Python之Numpy库
2019-04-26
08 Python之Pandas库选择查看、选择
2019-04-26
如何在junit中使用SpringFramework的Ioc容器
2019-04-26
一个案例教你理解Spring面向切面编程(Spring Aop)
2019-04-26
手把手教你整合SSM框架
2019-04-26
自己造个简单数据校验的注解@Value和@Mail
2019-04-26
Poj百练 4148:生理周期 (分类:枚举)
2019-04-26
微信公众号介绍_以及注册订阅号---微信公众号开发工作笔记0001
2019-04-26
Vue模板语法---vue工作笔记0003
2019-04-26
Vue计算属性之基本使用---vue工作笔记0004
2019-04-26
Vue监视---vue工作笔记0005
2019-04-26
Vue条件渲染---vue工作笔记0008
2019-04-26
Vue事件处理_vue的事件处理超级方便_功能强大---vue工作笔记0011
2019-04-26
Vue表单数据自动收集---vue工作笔记0012
2019-04-26
Vue生命周期---vue工作笔记0013
2019-04-26