文章目录
- 前言
- 一、初始配置
- 二、运动控制
- 三、移动机器人
- 总结
前言
在对Epuck2机器人进行完固件更新及IP地址查询后,接下来通过ROS来对Epuck2机器人进行运动控制。
一、初始配置
(1)创建一个 catkin 工作空间
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
cd ~/catkin_ws/
catkin_make
source ./devel/setup.bash
(2)移至~/catkin_ws/src 并克隆 ROS e-puck2 驱动程序仓库:
cd ~/catkin_ws/src
git clone -b e-puck2_wifi https://github.com/gctronic/epuck_driver_cpp
(3)安装依赖
sudo apt-get install ros-kinetic-gmapping
sudo apt-get install ros-kinetic-rviz-imu-plugin
(4)打开终端,进入 catkin 工作区目录(~/catkin_ws)并发出命令
catkin_make
Epuck2档位置于位置 15 进行 WiFi 通信
二、运动控制
(1)通讯:启动 epuck2 前,将 epuck2 机器人和计算机连接到同一个WiFi 网络
(2)启动
roscore
打开终端并发出以下命令:
roslaunch epuck_driver_cpp epuck2_controller.launch epuck2_address:='172.20.10.8'
'172.20.10.8’是 epuck2 IP 地址,需要根据您的机器人进行相应更改。
如果一切顺利,机器人将准备好交换数据,rviz 将打开,显示从 e-puck2 驱动
程序节点发布的主题中收集的信息。
三、移动机器人
(1)单机器人
第一种方式是使用 rviz 界面:
在界面的左下方有一个 Teleop 面板,其中包含一个交互式方块,旨在与差动驱动机器人一起使用。通过单击此方块,您将移动机器人,例如通过单击右上角部分,然后机器人将向右移动
第二种方式是使用 ros-melodic-turtlebot-teleop ROS 包。
第三种方式是直接发布/mobile_base/cmd_vel 主题,比如下命令
rostopic pub -1 /mobile_base/cmd_vel geometry_msgs/Twist -- '[0.0, 0.0, 0.0]' '[0.0, 0.0, 4.0]'
(2)多机器人
代码如下:具体移动可见分享视频
import roslib;
roslib.load_manifest('turbot3')
import rospy
from geometry_msgs.msg import Twist
from std_msgs.msg import String
import sys, select, os
import math
if os.name == 'nt':
import msvcrt
else:
import tty, termios
class Teleop:
def __init__(self):
pub0 = rospy.Publisher('/epuck_robot_0/mobile_base/cmd_vel', Twist,queue_size=10)
pub1 = rospy.Publisher('/epuck_robot_1/mobile_base/cmd_vel', Twist,queue_size=10)
#pub2 = rospy.Publisher('epuck_robot_2/mobile_base/cmd_vel', Twist)
#pub3 = rospy.Publisher('epuck_robot_3/mobile_base/cmd_vel', Twist)
#pub4 = rospy.Publisher('epuck_robot_4/mobile_base/cmd_vel', Twist)
rospy.init_node('turbot3')
rate = rospy.Rate(rospy.get_param('~hz', 1))
self.cmd = None
pi = math.pi
def move(forward, turn):
cmd = Twist()
cmd.linear.x = forward
cmd.linear.y = 0
cmd.linear.z = 0
cmd.angular.z = 0
cmd.angular.z = 0
cmd.angular.z = turn
self.cmd = cmd
return self.cmd
def getKey():
if os.name == 'nt':
return msvcrt.getch()
tty.setraw(sys.stdin.fileno())
rlist, _, _ = select.select([sys.stdin], [], [], 0.1)
if rlist:
key = sys.stdin.read(1)
else:
key = ''
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings)
return key
while not rospy.is_shutdown():
key = getKey()
if (key == '\x03'):
break
else:
str = "%s" % rospy.get_time()
rospy.loginfo(str)
pub0.publish(move(0, 0)) # stop
pub1.publish(move(0, 0))
#pub2.publish(move(0, 0))
#pub3.publish(move(0, 0))
#pub4.publish(move(0, 0))
rospy.sleep(3)
pub0.publish(move(0, pi / 6)) # left 90
pub1.publish(move(0, pi / 6))
#pub2.publish(move(0, pi / 6))
#pub3.publish(move(0, pi / 6))
#pub4.publish(move(0, pi / 6))
rospy.sleep(3)
pub0.publish(move(0, 0)) # stop
pub1.publish(move(0, 0))
#pub2.publish(move(0, 0))
#pub3.publish(move(0, 0))
#pub4.publish(move(0, 0))
rospy.sleep(3)
pub0.publish(move(0.6, 0))
pub1.publish(move(0.6, 0))
#pub2.publish(move(0.05, 0))
#pub3.publish(move(0.05, 0))
#pub4.publish(move(0.05, 0))
rospy.sleep(3)
pub0.publish(move(0, 0)) # stop
pub1.publish(move(0, 0))
#pub2.publish(move(0, 0))
#pub3.publish(move(0, 0))
#pub4.publish(move(0, 0))
rospy.sleep(3)
pub0.publish(move(0, -pi / 6))
pub1.publish(move(0, -pi / 6))
#pub2.publish(move(0, -pi / 6))
#pub3.publish(move(0, -pi / 6))
#pub4.publish(move(0, -pi / 6))
rospy.sleep(6)
pub0.publish(move(0, 0))
pub1.publish(move(0, 0))
#pub2.publish(move(0, 0))
#pub3.publish(move(0, 0))
#pub4.publish(move(0, 0))
rospy.sleep(3)
pub0.publish(move(0.6, 0))
pub1.publish(move(0.6, 0))
#pub2.publish(move(0.05, 0))
#pub3.publish(move(0.05, 0))
#pub4.publish(move(0.05, 0))
rospy.sleep(3)
pub0.publish(move(0, 0))
pub1.publish(move(0, 0))
#pub2.publish(move(0, 0))
#pub3.publish(move(0, 0))
#pub4.publish(move(0, 0))
rospy.sleep(3)
pub0.publish(move(0, pi / 6))
pub1.publish(move(0, pi / 6))
#pub2.publish(move(0, pi / 6))
#pub3.publish(move(0, pi / 6))
#pub4.publish(move(0, pi / 6))
rospy.sleep(3)
pub0.publish(move(0, 0))
pub1.publish(move(0, 0))
#pub2.publish(move(0, 0))
#pub3.publish(move(0, 0))
#pub4.publish(move(0, 0))
rospy.sleep(3)
pub0.publish(move(-0.6, 0))
pub1.publish(move(0.6, 0))
#pub2.publish(move(-0.05, 0))
#pub3.publish(move(0.05, 0))
#pub4.publish(move(-0.05, 0))
rospy.sleep(3)
pub0.publish(move(0, 0))
pub1.publish(move(0, 0))
#pub2.publish(move(0, 0))
#pub3.publish(move(0, 0))
#pub4.publish(move(0, 0))
rospy.sleep(3)
pub0.publish(move(0.6, 0))
pub1.publish(move(-0.6, 0))
#pub2.publish(move(0.05, 0))
#pub3.publish(move(-0.05, 0))
#pub4.publish(move(0.05, 0))
rospy.sleep(6)
pub0.publish(move(0, 0))
pub1.publish(move(0, 0))
#pub2.publish(move(0, 0))
#pub3.publish(move(0, 0))
#pub4.publish(move(0, 0))
rospy.sleep(3)
pub0.publish(move(-0.6, 0))
pub1.publish(move(0.6, 0))
#pub2.publish(move(-0.05, 0))
#pub3.publish(move(0.05, 0))
#pub4.publish(move(-0.05, 0))
rospy.sleep(3)
twist = Twist()
twist.linear.x = 0.0;twist.linear.y = 0.0;twist.linear.z = 0.0
twist.angular.x = 0.0;twist.angular.y = 0.0;twist.angular.z = 0.0
pub0.publish(twist)
pub1.publish(twist)
#pub2.publish(twist)
#pub3.publish(twist)
#pub4.publish(twist)
if __name__ == '__main__':
if os.name != 'nt':
settings = termios.tcgetattr(sys.stdin)
Teleop()
总结
以上就是Epuck2 在 ROS 下的运动控制,本文仅仅简单介绍了Epuck2的简单控制,后面想办法将编队算法应用在Epuck2机器人上进行测试。