|
@@ -8,9 +8,8 @@
|
|
|
History:
|
|
|
20200706:init this file.
|
|
|
"""
|
|
|
-import os
|
|
|
import rospy
|
|
|
-import thread
|
|
|
+import os, thread
|
|
|
from ros_arduino_msgs.srv import *
|
|
|
from geometry_msgs.msg import Twist
|
|
|
from serial.serialutil import SerialException
|
|
@@ -22,40 +21,36 @@ 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.serial_baud = int(rospy.get_param("~serial_baud", 57600))
|
|
|
- self.i2c_smbus_num = rospy.get_param("~i2c_smbus_num", 1)
|
|
|
- self.i2c_slave_addr = rospy.get_param("~i2c_slave_addr", 8)
|
|
|
- self.timeout = rospy.get_param("~connect_timeout", 0.5)
|
|
|
- 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.is_serial = rospy.get_param("~is_use_serial", True)
|
|
|
+ self.serial_port = rospy.get_param("~serial_port", "/dev/ttyACM0")
|
|
|
+ self.serial_baud = int(rospy.get_param("~serial_baud", 57600))
|
|
|
+ self.i2c_smbus_num = rospy.get_param("~i2c_smbus_num", 1)
|
|
|
+ self.i2c_slave_addr = rospy.get_param("~i2c_slave_addr", 8)
|
|
|
+ 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.rate = int(rospy.get_param("~main_rate", 50))
|
|
|
+ self.rate = int(rospy.get_param("~main_rate", 100))
|
|
|
loop_rate = rospy.Rate(self.rate)
|
|
|
|
|
|
#初始化控制移动的Twist发布者,用于断开连接时停止小车移动
|
|
|
self.cmd_vel_pub = rospy.Publisher(self.cmd_topic, Twist, queue_size=1)
|
|
|
|
|
|
- # A service to turn set the direction of a digital pin (0 = input, 1 = output)
|
|
|
+ #设置数字引脚的输入输出模式 (0 = input, 1 = output)
|
|
|
rospy.Service('~digital_set_direction', DigitalSetDirection, self.DigitalSetDirectionHandler)
|
|
|
|
|
|
- # A service to turn a digital sensor on or off
|
|
|
+ #设置数字引脚的高低电平
|
|
|
rospy.Service('~digital_write', DigitalWrite, self.DigitalWriteHandler)
|
|
|
|
|
|
- # A service to read the value of a digital sensor
|
|
|
+ #读取数字引脚高低电平的服务
|
|
|
rospy.Service('~digital_read', DigitalRead, self.DigitalReadHandler)
|
|
|
|
|
|
- # A service to set pwm values for the pins
|
|
|
- rospy.Service('~analog_write', AnalogWrite, self.AnalogWriteHandler)
|
|
|
-
|
|
|
- # A service to read the value of an analog sensor
|
|
|
+ #读取模拟引脚数值的服务
|
|
|
rospy.Service('~analog_read', AnalogRead, self.AnalogReadHandler)
|
|
|
|
|
|
- # A service to control camera angle
|
|
|
+ #控制摄像头转动的服务
|
|
|
rospy.Service('~camera_control', CameraControl, self.CameraControlHandler)
|
|
|
|
|
|
# A service to get new pid params
|
|
@@ -68,12 +63,12 @@ class ArduinoROS():
|
|
|
rospy.on_shutdown(self.shutdown)
|
|
|
|
|
|
# Initialize the controlller
|
|
|
- self.controller = Arduino(self.is_serial, self.serial_port, self.serial_baud, self.i2c_smbus_num, self.i2c_slave_addr, self.timeout)
|
|
|
+ self.controller = Arduino(self.is_serial, self.serial_port, self.serial_baud, self.i2c_smbus_num, self.i2c_slave_addr)
|
|
|
|
|
|
#与下位机建立连接
|
|
|
self.controller.connect()
|
|
|
|
|
|
- # Reserve a thread lock
|
|
|
+ #初始化一个线程锁对象
|
|
|
mutex = thread.allocate_lock()
|
|
|
|
|
|
#获取传感器列表
|
|
@@ -138,10 +133,6 @@ class ArduinoROS():
|
|
|
value = self.controller.digital_read(req.pin)
|
|
|
return DigitalReadResponse(value)
|
|
|
|
|
|
- def AnalogWriteHandler(self, req):
|
|
|
- self.controller.analog_write(req.pin, req.value)
|
|
|
- return AnalogWriteResponse()
|
|
|
-
|
|
|
def AnalogReadHandler(self, req):
|
|
|
value = self.controller.analog_read(req.pin)
|
|
|
return AnalogReadResponse(value)
|
|
@@ -152,7 +143,7 @@ class ArduinoROS():
|
|
|
|
|
|
def GetInfraredDistanceHandler(self, req):
|
|
|
value = self.controller.detect_distance()
|
|
|
- return GetInfraredDistanceResponse(value[0]/100.0,value[1]/100.0,value[2]/100.0)
|
|
|
+ return GetInfraredDistanceResponse(value[0]/100.0, value[1]/100.0, value[2]/100.0)
|
|
|
|
|
|
def DynamicPIDHandler(self, req):
|
|
|
self.controller.update_pid(req.value1, req.value2, 0, 50,
|
|
@@ -166,12 +157,13 @@ class ArduinoROS():
|
|
|
self.controller.close()
|
|
|
os._exit(0)
|
|
|
|
|
|
+
|
|
|
if __name__ == '__main__':
|
|
|
try:
|
|
|
myArduino = ArduinoROS()
|
|
|
except SerialException:
|
|
|
rospy.logerr("ERROR trying to open serial port.")
|
|
|
os._exit(1)
|
|
|
- else:
|
|
|
+ except Exception:
|
|
|
rospy.logerr("Get other unknown error !")
|
|
|
os._exit(2)
|