mirror of
https://github.com/YikeStone/ros_arduino_bridge.git
synced 2025-08-05 20:14:07 +05:30
Improved initial connection loop in arduino_driver.py
This commit is contained in:
parent
6975ea2e74
commit
9a6229656b
@ -72,36 +72,33 @@ class Arduino:
|
|||||||
while not self.serial_port.isOpen():
|
while not self.serial_port.isOpen():
|
||||||
time.sleep(self.timeout)
|
time.sleep(self.timeout)
|
||||||
|
|
||||||
# Now open the port with the real settings
|
# Now open the port with the real settings. An initial timeout of at least 1.0 seconds seems to work best
|
||||||
self.serial_port = serial.Serial(port=self.port, baudrate=self.baudrate, timeout=self.timeout, writeTimeout=self.writeTimeout)
|
self.serial_port = serial.Serial(port=self.port, baudrate=self.baudrate, timeout=max(1.0, self.timeout))
|
||||||
|
|
||||||
# It can take time for the serial port to wake up
|
# It can take time for the serial port to wake up
|
||||||
max_attempts = 10
|
max_attempts = 10
|
||||||
attempts = 0
|
attempts = 0
|
||||||
timeout = self.timeout
|
timeout = self.timeout
|
||||||
|
|
||||||
# Wake up the serial port
|
self.serial_port.write('\r')
|
||||||
self.serial_port.write('\r\r\r')
|
time.sleep(timeout)
|
||||||
self.serial_port.read()
|
test = self.serial_port.read()
|
||||||
|
|
||||||
# Keep trying for max_attempts
|
# Wake up the serial port
|
||||||
while attempts < max_attempts:
|
while attempts < max_attempts and test == '':
|
||||||
attempts += 1
|
attempts += 1
|
||||||
self.serial_port.write('\r\r\r')
|
self.serial_port.write('\r')
|
||||||
|
time.sleep(timeout)
|
||||||
test = self.serial_port.read()
|
test = self.serial_port.read()
|
||||||
if test != '':
|
|
||||||
break
|
|
||||||
|
|
||||||
rospy.loginfo("Waking up serial port attempt " + str(attempts) + " of " + str(max_attempts))
|
rospy.loginfo("Waking up serial port attempt " + str(attempts) + " of " + str(max_attempts))
|
||||||
|
|
||||||
# Increase timeout by 10%
|
|
||||||
timeout *= 1.1
|
|
||||||
self.serial_port.timeout = timeout
|
|
||||||
self.serial_port.writeTimeout = timeout
|
|
||||||
|
|
||||||
if test == '':
|
if test == '':
|
||||||
raise SerialException
|
raise SerialException
|
||||||
|
|
||||||
|
# Reset the timeout to the user specified timeout
|
||||||
|
self.serial_port.setTimeout(self.timeout)
|
||||||
|
self.serial_port.setWriteTimeout(self.timeout)
|
||||||
|
|
||||||
# Test the connection by reading the baudrate
|
# Test the connection by reading the baudrate
|
||||||
attempts = 0
|
attempts = 0
|
||||||
while self.get_baud() != self.baudrate and attempts < max_attempts:
|
while self.get_baud() != self.baudrate and attempts < max_attempts:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user