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
82b2bbb546
commit
6975ea2e74
@ -75,41 +75,41 @@ class Arduino:
|
|||||||
# Now open the port with the real settings
|
# Now open the port with the real settings
|
||||||
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=self.timeout, writeTimeout=self.writeTimeout)
|
||||||
|
|
||||||
# When an Arduino is first plugged in, 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 = 1
|
attempts = 0
|
||||||
timeout = self.timeout
|
timeout = self.timeout
|
||||||
|
|
||||||
# Wake up the serial port
|
# Wake up the serial port
|
||||||
|
self.serial_port.write('\r\r\r')
|
||||||
|
self.serial_port.read()
|
||||||
|
|
||||||
|
# Keep trying for max_attempts
|
||||||
while attempts < max_attempts:
|
while attempts < max_attempts:
|
||||||
self.serial_port.write('\r')
|
attempts += 1
|
||||||
time.sleep(timeout)
|
self.serial_port.write('\r\r\r')
|
||||||
test = self.serial_port.read()
|
test = self.serial_port.read()
|
||||||
if test != '':
|
if test != '':
|
||||||
break
|
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%
|
|
||||||
|
# Increase timeout by 10%
|
||||||
timeout *= 1.1
|
timeout *= 1.1
|
||||||
self.serial_port.timeout = timeout
|
self.serial_port.timeout = timeout
|
||||||
self.serial_port.writeTimeout = timeout
|
self.serial_port.writeTimeout = timeout
|
||||||
self.serial_port.flushInput()
|
|
||||||
self.serial_port.flushOutput()
|
|
||||||
attempts += 1
|
|
||||||
|
|
||||||
if test == '':
|
if test == '':
|
||||||
raise SerialException
|
raise SerialException
|
||||||
|
|
||||||
if timeout != self.timeout:
|
|
||||||
rospy.loginfo("Found best timeout to be " + str(timeout) + " seconds")
|
|
||||||
|
|
||||||
# Test the connection by reading the baudrate
|
# Test the connection by reading the baudrate
|
||||||
attepmpts = 0
|
attempts = 0
|
||||||
while self.get_baud() != self.baudrate and attempts < max_attempts:
|
while self.get_baud() != self.baudrate and attempts < max_attempts:
|
||||||
attempts += 1
|
attempts += 1
|
||||||
self.serial_port.flushInput()
|
self.serial_port.flushInput()
|
||||||
self.serial_port.flushOutput()
|
self.serial_port.flushOutput()
|
||||||
rospy.loginfo("Connecting...")
|
rospy.loginfo("Connecting...")
|
||||||
|
time.sleep(timeout)
|
||||||
try:
|
try:
|
||||||
self.serial_port.inWaiting()
|
self.serial_port.inWaiting()
|
||||||
rospy.loginfo("Connected at " + str(self.baudrate))
|
rospy.loginfo("Connected at " + str(self.baudrate))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user