通过ros节点发布Twist Messages控制机器人--10
原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/
1.到目前为止,我们已经从命令行移动机器人,但大多数时间你将依靠一个ros节点发布适当的Twist消息。作为一个简单的例子,假设你想让你的机器人向前移动一个1米大约180度,然后回到起点。我们将尝试完成这项任务,这将很好地说明不同层次的ros运动控制。
启动tulterbot机器人:
roslaunch rbx1_bringup fake_turtlebot.launch
2.在rviz视图窗口查看机器人:
rosrun rviz rviz -d `rospack find rbx1_nav`/sim.rviz
3.运行timed_out_and_back.py节点:
rosrun rbx1_nav timed_out_and_back.py
4.通过rqt_graph查看消息订阅的框图:
rosrun rqt_graph rqt_graph
5.分析timed_out_and_back.py节点代码:
#!/usr/bin/env python import rospy
from geometry_msgs.msg import Twist
from math import pi class OutAndBack():
def __init__(self):
# Give the node a name
rospy.init_node('out_and_back', anonymous=False)
# Set rospy to execute a shutdown function when exiting
rospy.on_shutdown(self.shutdown) # Publisher to control the robot's speed
self.cmd_vel = rospy.Publisher('/cmd_vel', Twist, queue_size=) # How fast will we update the robot's movement?
rate = # Set the equivalent ROS rate variable
r = rospy.Rate(rate) # Set the forward linear speed to 0.2 meters per second
linear_speed = 0.2 # Set the travel distance to 1.0 meters
goal_distance = 1.0 # How long should it take us to get there?
linear_duration = goal_distance / linear_speed # Set the rotation speed to 1.0 radians per second
angular_speed = 1.0 # Set the rotation angle to Pi radians ( degrees)
goal_angle = pi # How long should it take to rotate?
angular_duration = goal_angle / angular_speed # Loop through the two legs of the trip
for i in range():
# Initialize the movement command
move_cmd = Twist() # Set the forward speed
move_cmd.linear.x = linear_speed # Move forward for a time to go the desired distance
ticks = int(linear_duration * rate) for t in range(ticks):
self.cmd_vel.publish(move_cmd)
r.sleep() # Stop the robot before the rotation
move_cmd = Twist()
self.cmd_vel.publish(move_cmd)
rospy.sleep() # Now rotate left roughly degrees # Set the angular speed
move_cmd.angular.z = angular_speed # Rotate for a time to go degrees
ticks = int(goal_angle * rate) for t in range(ticks):
self.cmd_vel.publish(move_cmd)
r.sleep() # Stop the robot before the next leg
move_cmd = Twist()
self.cmd_vel.publish(move_cmd)
rospy.sleep() # Stop the robot
self.cmd_vel.publish(Twist()) def shutdown(self):
# Always stop the robot when shutting down the node.
rospy.loginfo("Stopping the robot...")
self.cmd_vel.publish(Twist())
rospy.sleep() if __name__ == '__main__':
try:
OutAndBack()
except:
rospy.loginfo("Out-and-Back node terminated.")
6.等以上节点运行完成后。可以运行下一个节点;
rosrun rbx1_nav nav_square.py
查看节点订阅框图:
7.分析nav_square.py节点的源码:
#!/usr/bin/env python import rospy
from geometry_msgs.msg import Twist, Point, Quaternion
import tf
from rbx1_nav.transform_utils import quat_to_angle, normalize_angle
from math import radians, copysign, sqrt, pow, pi class NavSquare():
def __init__(self):
# Give the node a name
rospy.init_node('nav_square', anonymous=False) # Set rospy to execute a shutdown function when terminating the script
rospy.on_shutdown(self.shutdown) # How fast will we check the odometry values?
rate = # Set the equivalent ROS rate variable
r = rospy.Rate(rate) # Set the parameters for the target square
goal_distance = rospy.get_param("~goal_distance", 1.0) # meters
goal_angle = rospy.get_param("~goal_angle", radians()) # degrees converted to radians
linear_speed = rospy.get_param("~linear_speed", 0.2) # meters per second
angular_speed = rospy.get_param("~angular_speed", 0.7) # radians per second
angular_tolerance = rospy.get_param("~angular_tolerance", radians()) # degrees to radians # Publisher to control the robot's speed
self.cmd_vel = rospy.Publisher('/cmd_vel', Twist, queue_size=) # The base frame is base_footprint for the TurtleBot but base_link for Pi Robot
self.base_frame = rospy.get_param('~base_frame', '/base_link') # The odom frame is usually just /odom
self.odom_frame = rospy.get_param('~odom_frame', '/odom') # Initialize the tf listener
self.tf_listener = tf.TransformListener() # Give tf some time to fill its buffer
rospy.sleep() # Set the odom frame
self.odom_frame = '/odom' # Find out if the robot uses /base_link or /base_footprint
try:
self.tf_listener.waitForTransform(self.odom_frame, '/base_footprint', rospy.Time(), rospy.Duration(1.0))
self.base_frame = '/base_footprint'
except (tf.Exception, tf.ConnectivityException, tf.LookupException):
try:
self.tf_listener.waitForTransform(self.odom_frame, '/base_link', rospy.Time(), rospy.Duration(1.0))
self.base_frame = '/base_link'
except (tf.Exception, tf.ConnectivityException, tf.LookupException):
rospy.loginfo("Cannot find transform between /odom and /base_link or /base_footprint")
rospy.signal_shutdown("tf Exception") # Initialize the position variable as a Point type
position = Point() # Cycle through the four sides of the square
for i in range():
# Initialize the movement command
move_cmd = Twist() # Set the movement command to forward motion
move_cmd.linear.x = linear_speed # Get the starting position values
(position, rotation) = self.get_odom() x_start = position.x
y_start = position.y # Keep track of the distance traveled
distance = # Enter the loop to move along a side
while distance < goal_distance and not rospy.is_shutdown():
# Publish the Twist message and sleep cycle
self.cmd_vel.publish(move_cmd) r.sleep() # Get the current position
(position, rotation) = self.get_odom() # Compute the Euclidean distance from the start
distance = sqrt(pow((position.x - x_start), ) +
pow((position.y - y_start), )) # Stop the robot before rotating
move_cmd = Twist()
self.cmd_vel.publish(move_cmd)
rospy.sleep(1.0) # Set the movement command to a rotation
move_cmd.angular.z = angular_speed # Track the last angle measured
last_angle = rotation # Track how far we have turned
turn_angle = # Begin the rotation
while abs(turn_angle + angular_tolerance) < abs(goal_angle) and not rospy.is_shutdown():
# Publish the Twist message and sleep cycle
self.cmd_vel.publish(move_cmd) r.sleep() # Get the current rotation
(position, rotation) = self.get_odom() # Compute the amount of rotation since the last lopp
delta_angle = normalize_angle(rotation - last_angle) turn_angle += delta_angle
last_angle = rotation move_cmd = Twist()
self.cmd_vel.publish(move_cmd)
rospy.sleep(1.0) # Stop the robot when we are done
self.cmd_vel.publish(Twist()) def get_odom(self):
# Get the current transform between the odom and base frames
try:
(trans, rot) = self.tf_listener.lookupTransform(self.odom_frame, self.base_frame, rospy.Time())
except (tf.Exception, tf.ConnectivityException, tf.LookupException):
rospy.loginfo("TF Exception")
return return (Point(*trans), quat_to_angle(Quaternion(*rot))) def shutdown(self):
# Always stop the robot when shutting down the node
rospy.loginfo("Stopping the robot...")
self.cmd_vel.publish(Twist())
rospy.sleep() if __name__ == '__main__':
try:
NavSquare()
except rospy.ROSInterruptException:
rospy.loginfo("Navigation terminated.")
通过ros节点发布Twist Messages控制机器人--10的更多相关文章
- ROS 多台计算机联网控制机器人
0. 时间同步 sudo apt-get install chrony 1. ubuntu自带的有openssh-client 可以通过如下指令 ssh username@host 来连接同一局域网内 ...
- ROS主题发布订阅控制真实的机器人下位机
先模拟控制小乌龟 新建cmd_node.ccpp文件: #include"ros/ros.h" #include"geometry_msgs/Twist.h" ...
- ROS中发布激光扫描消息
激光雷达工作时会先在当前位置发出激光并接收反射光束,解析得到距离信息,而后激光发射器会转过一个角度分辨率对应的角度再次重复这个过程.限于物理及机械方面的限制,激光雷达通常会有一部分“盲区”.使用激光雷 ...
- ROS学习笔记三(理解ROS节点)
要求已经在Linux系统中安装一个学习用的ros软件包例子: sudo apt-get install ros-indigo-ros-tutorials ROS图形概念概述 nodes:节点,一个节点 ...
- ROS节点理解--5
理解 ROS节点(原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/) Description: 本教程主要介绍 ROS 图(graph)概念 并讨 ...
- 通过joystick遥感和按键控制机器人--11
原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 1.首先安装joystick遥控器驱动: sudo apt-get install ros-indigo ...
- (五)ROS节点
一. 理解ROS 节点: ROS的节点: 可以说是一个可运行的程序.当然这个程序可不简单.因为它可以接受来自ROS网络上其他可运行程序的输出信息,也可以发送信息给ROS网络,被其他 ROS 可运行程序 ...
- ROS学习(六)—— 理解ROS节点
一.准备工作 下载一个轻量级的模拟器 sudo apt-get install ros-kinetic-ros-tutorials 二.图概念的理解 1.Nodes:一个节点就是一个可执行文件,用来与 ...
- ROS Learning-007 beginner_Tutorials ROS节点
ROS Indigo beginner_Tutorials-06 ROS节点 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04.4 LT ...
随机推荐
- C++-继承名称的掩盖
/////////////////////////////////////////////////////////////////////////////// // // FileName : eff ...
- java抽象类与接口 详解
在面向对象的概念中,我们知道所有的对象都是通过类来描绘的,但是并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类. 抽象类往往用来表征我们在对问题 ...
- xlistview的java(头)
package com.bwie.xlistviews; import com.bwie.test.R; import android.content.Context;import android.u ...
- 修改Centos SSH远程端口
1. 在防火墙添加开放端口10000:本机防火墙和云防火墙 本机防火墙:-A INPUT -m state --state NEW -m tcp -p tcp --dport 10000 -j ACC ...
- JAVA 打印九九乘法表
/** * * @author liangxiaoyu * @version 1.0 *2015-09-18 */public class JJ { public static void main( ...
- Java面向对象的三大特征
Java面向对象的三大特征 java面向对象的三大特征:“封装.继承.多态”.更多Java技术知识,请登陆疯狂软件教育官网.微信搜索微信号:疯狂软件,参加2015年优惠活动,有机会获得优惠劵和代金劵. ...
- XML元素和结点的区别
1.区别介绍 Element是Node的扩展,所以也更实用一些.例如,用Element可以方便的获得Node的属性getAttribute(String attrName)如果用Node,可以得到一个 ...
- java读取大容量excel之二(空格、空值问题)
最近在项目中发现,对于Excel2007(底层根本是xml) ,使用<java读取大容量excel之一>中的方式读取,若待读取的excel2007文件中某一列是空值,(注意,所谓的空值是什 ...
- weblogic服务器的简单使用(一)
一.前言 现在的公司开发的项目基于的平台是weblogic8.1.5,虽然版本是旧了点,但是用到的功能还是很多的,如JNDI.t3协议.EJB2.0.线程池.连接池.Ant部署.java远程调试. 发 ...
- JS - The react framework
这几天因为赶时间 , 所以理解上可能有许多的误差 , 如果你不幸点进来了 , 请不要看我的代码 , 这几天我会重新修改 , 然后把错误的不足的 全部修正一下 . /hwr/src/index.js i ...