码迷,mamicode.com
首页 > 移动开发 > 详细

02---控制移动底座5

时间:2017-03-29 23:49:26      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:out   update   自己   模拟   des   子目录   .com   min   运行   

在大多数时间都是依靠ROS节点来发布恰当的Twist消息。举个简单的例子,假设我们想编程使机器人向前移动1m,旋转180度,然后返回开始的位置。我们会尝试用不同的方法来完成这个任务,这些方法很好地表现了ROS不同层次的运动控制

1、通过定时和定速估计距离和角度

我们第一个尝试是用定时的Twist命令去让机器人花一定时间向前移动一定的距离,旋转180度后,然后以相同的速度进行相同时长的向前移动,不出意外的话它会回到开始的位置。最后我们会让机器人旋转180度回到最初的方向。

这段脚本可以在子目录rbx1_nav/nodes下的timed_out_and_back.py中找到。

技术分享

2、在ArbotiX模拟器上进行计时前进并返回运动

为了保证模拟的TurtleBot回到开始的位置,按下“Ctrl-C”让模拟的TurtleBot中正在运行的启动文件停止运行,然后用以下命令让它重新运行:

roslaunch rbx1_bringup fake_turtlebot.launch

如果你愿意的话,可以用对应Pi Robot或者你自己的机器人的文件替换掉fake_turtlebot.launch,这不会使结果有差别。

如果RViz并不是正在运行,那么就让它开始运行:

rosrun rviz rviz -d `rospack find rbx1_nav`/sim.rviz

或者按下Reset按钮删除掉在上一部分留下的Odometry箭头。

最后运行timed_out_and_back.py节点:

rosrun rbx1_nav timed_out_and_back.py

技术分享

3、计时前进并返回运动的脚本

以下是计时前进并返回节点的完整脚本。

技术分享
#!/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 robots speed
        self.cmd_vel = rospy.Publisher(/cmd_vel, Twist, queue_size=1)
        
        # How fast will we update the robots movement?
        rate = 50
        
        # 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 (180 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(2):
            # 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(1)
            
            # Now rotate left roughly 180 degrees  
            
            # Set the angular speed
            move_cmd.angular.z = angular_speed

            # Rotate for a time to go 180 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(1)    
            
        # 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(1)
 
if __name__ == __main__:
    try:
        OutAndBack()
    except:
        rospy.loginfo("Out-and-Back node terminated.")
View Code

 

02---控制移动底座5

标签:out   update   自己   模拟   des   子目录   .com   min   运行   

原文地址:http://www.cnblogs.com/gary-guo/p/6642515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!