|
@@ -14,26 +14,29 @@ from ros_arduino_msgs.srv import *
|
|
|
from geometry_msgs.msg import Twist
|
|
|
from serial.serialutil import SerialException
|
|
|
from ros_arduino_python.arduino_sensors import *
|
|
|
-from ros_arduino_python.arduino_driver import Arduino
|
|
|
+from ros_arduino_python.arduino_driver import ArduinoDriver
|
|
|
from ros_arduino_python.base_controller import BaseController
|
|
|
|
|
|
class ArduinoROS():
|
|
|
def __init__(self):
|
|
|
rospy.init_node('mobilebase_arduino_node', log_level=rospy.INFO)
|
|
|
|
|
|
- self.is_serial = rospy.get_param("~is_use_serial", True)
|
|
|
- self.serial_port = rospy.get_param("~serial_port", "/dev/ttyACM0")
|
|
|
- self.base_frame = rospy.get_param("~base_frame", "base_footprint")
|
|
|
- self.cmd_topic = rospy.get_param("~cmd_topic", "cmd_vel")
|
|
|
- self.balance_mode = rospy.get_param("~balance_mode", False)
|
|
|
- self.motor_stop_delay = rospy.get_param("~motor_stop_delay", 11)
|
|
|
+ self.is_serial = rospy.get_param("~is_use_serial", True)
|
|
|
+ self.serial_port = rospy.get_param("~serial_port", "/dev/ttyACM0")
|
|
|
+ self.base_frame = rospy.get_param("~base_frame", "base_footprint")
|
|
|
+ self.cmd_topic = rospy.get_param("~cmd_topic", "cmd_vel")
|
|
|
+ self.balance_mode = rospy.get_param("~balance_mode", False)
|
|
|
|
|
|
#根据参数中配置的频率来设置主循环频率,平衡小车模式为100hz,默认支撑轮模式20hz
|
|
|
+ #获取电机没有收到新的移动命令后延迟多长时间停止转动的参数,单位ms
|
|
|
if self.balance_mode:
|
|
|
self.rate = int(rospy.get_param("~balance_main_rate", 100))
|
|
|
+ self.motor_stop_delay = rospy.get_param("~balance_motor_stop_delay", 11);
|
|
|
else:
|
|
|
self.rate = int(rospy.get_param("~default_main_rate", 20))
|
|
|
+ self.motor_stop_delay = rospy.get_param("~default_motor_stop_delay", 100)
|
|
|
|
|
|
+ #配置主循环的频率
|
|
|
loop_rate = rospy.Rate(self.rate)
|
|
|
|
|
|
#初始化控制移动的Twist发布者,用于断开连接时停止小车移动
|
|
@@ -63,14 +66,14 @@ class ArduinoROS():
|
|
|
#当节点退出的时候执行的回调函数
|
|
|
rospy.on_shutdown(self.shutdown)
|
|
|
|
|
|
- # Initialize the controlller
|
|
|
- self.controller = Arduino(self.is_serial, self.serial_port)
|
|
|
+ #初始化ArduinoDriver对象,开始建立通信
|
|
|
+ self.controller = ArduinoDriver(self.is_serial, self.serial_port)
|
|
|
|
|
|
#与下位机Arduino建立连接
|
|
|
self.controller.connect()
|
|
|
|
|
|
- #初始化一个线程锁对象
|
|
|
- mutex = thread.allocate_lock()
|
|
|
+ #配置电机停止转动的延时,延迟参数从配置文件中读取
|
|
|
+ self.controller.update_motor_stop_delay(self.motor_stop_delay)
|
|
|
|
|
|
#获取传感器列表
|
|
|
self.mySensors = list()
|
|
@@ -97,11 +100,8 @@ class ArduinoROS():
|
|
|
#初始化底盘控制器
|
|
|
self.myBaseController = BaseController(self.is_serial,self.controller,self.base_frame,self.cmd_topic,self.balance_mode)
|
|
|
|
|
|
- #配置电机停止转动的延时,从配置文件中读取延迟参数
|
|
|
- if self.balance_mode:
|
|
|
- self.controller.update_motor_stop_delay(self.motor_stop_delay)
|
|
|
- else:
|
|
|
- self.controller.update_motor_stop_delay(100)
|
|
|
+ #初始化一个线程锁对象
|
|
|
+ mutex = thread.allocate_lock()
|
|
|
|
|
|
#开始主循环,控制底盘转动和发布各传感器信息
|
|
|
while not rospy.is_shutdown():
|