|
@@ -25,13 +25,13 @@ class Arduino:
|
|
|
N_ANALOG_PORTS = 10
|
|
|
N_DIGITAL_PORTS = 54
|
|
|
|
|
|
- def __init__(self, is_use_serial,serial_port="/dev/ttyACM0",baudrate=57600,i2c_smbus_num=1,i2c_slave_addr=8,timeout=0.5):
|
|
|
+ def __init__(self, is_use_serial,serial_port="/dev/ttyACM0",i2c_smbus_num=1,i2c_slave_addr=8,timeout=0.5):
|
|
|
self.PID_RATE = 40 #Don't change this ! It is a fixed property of the Arduino PID controller.
|
|
|
self.PID_INTERVAL = 25
|
|
|
+ self.baudrate = 57600
|
|
|
|
|
|
self.is_use_serial = is_use_serial #与下位机arduino的通信方式是否使用串口还是IIC
|
|
|
self.serial_port = serial_port
|
|
|
- self.baudrate = baudrate
|
|
|
|
|
|
self.i2c_smbus_num = i2c_smbus_num
|
|
|
self.i2c_slave_addr = i2c_slave_addr
|
|
@@ -110,27 +110,20 @@ class Arduino:
|
|
|
self.serial_port.open()
|
|
|
|
|
|
def close(self):
|
|
|
- ''' Close the serial port or i2c bus connect.
|
|
|
- '''
|
|
|
self.beep_ring(2)
|
|
|
rospy.sleep(0.5)
|
|
|
self.beep_ring(3)
|
|
|
+
|
|
|
if self.is_use_serial:
|
|
|
self.serial_port.close()
|
|
|
else:
|
|
|
self.i2c_bus.close()
|
|
|
|
|
|
def send(self, cmd):
|
|
|
- ''' This command should not be used on its own: it is called by the execute commands
|
|
|
- below in a thread safe manner.
|
|
|
- '''
|
|
|
self.serial_port.write(cmd + '\r')
|
|
|
|
|
|
def recv(self, timeout=0.5):
|
|
|
timeout = min(timeout, self.timeout)
|
|
|
- ''' This command should not be used on its own: it is called by the execute commands
|
|
|
- below in a thread safe manner
|
|
|
- '''
|
|
|
c = ''
|
|
|
value = ''
|
|
|
attempts = 0
|
|
@@ -146,16 +139,10 @@ class Arduino:
|
|
|
return value
|
|
|
|
|
|
def recv_ack(self):
|
|
|
- ''' This command should not be used on its own: it is called by the execute commands
|
|
|
- below in a thread safe manner.
|
|
|
- '''
|
|
|
ack = self.recv(self.timeout)
|
|
|
return ack == 'OK'
|
|
|
|
|
|
def recv_int(self):
|
|
|
- ''' This command should not be used on its own: it is called by the execute commands
|
|
|
- below in a thread safe manner.
|
|
|
- '''
|
|
|
value = self.recv(self.timeout)
|
|
|
try:
|
|
|
return int(value)
|
|
@@ -163,9 +150,6 @@ class Arduino:
|
|
|
return None
|
|
|
|
|
|
def recv_array(self):
|
|
|
- ''' This command should not be used on its own: it is called by the execute commands
|
|
|
- below in a thread safe manner.
|
|
|
- '''
|
|
|
try:
|
|
|
values = self.recv(self.timeout * self.N_ANALOG_PORTS).split()
|
|
|
return map(float, values)
|
|
@@ -173,8 +157,6 @@ class Arduino:
|
|
|
return []
|
|
|
|
|
|
def execute(self, cmd):
|
|
|
- ''' Thread safe execution of "cmd" on the Arduino returning a single integer value.
|
|
|
- '''
|
|
|
self.mutex.acquire()
|
|
|
|
|
|
try:
|
|
@@ -204,8 +186,6 @@ class Arduino:
|
|
|
return value
|
|
|
|
|
|
def execute_array(self, cmd):
|
|
|
- ''' Thread safe execution of "cmd" on the Arduino returning an float array.
|
|
|
- '''
|
|
|
self.mutex.acquire()
|
|
|
try:
|
|
|
self.serial_port.flushInput()
|
|
@@ -240,8 +220,6 @@ class Arduino:
|
|
|
return values
|
|
|
|
|
|
def execute_ack(self, cmd):
|
|
|
- ''' Thread safe execution of "cmd" on the Arduino returning True if response is ACK.
|
|
|
- '''
|
|
|
self.mutex.acquire()
|
|
|
|
|
|
try:
|