SerialClient.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
35 
36 import time
37 
38 import rospy
39 from std_srvs.srv import Empty, EmptyResponse
40 
41 from serial import Serial
42 
43 from rosserial_python import SerialClient as _SerialClient
44 
45 MINIMUM_RESET_TIME = 30
46 
47 class SerialClient(_SerialClient):
48 
49  def __init__(self, *args, **kwargs):
50  # The number of seconds to wait after a sync failure for a sync success before automatically performing a reset.
51  # If 0, no reset is performed.
52  self.auto_reset_timeout = kwargs.pop('auto_reset_timeout', 0)
53  self.lastsync_reset = rospy.Time.now()
54  rospy.Service('~reset_arduino', Empty, self.resetArduino)
55  super(SerialClient, self).__init__(*args, **kwargs)
56 
57  def resetArduino(self, *args, **kwargs):
58  """
59  Forces the Arduino to perform a reset, as though its reset button was pressed.
60  """
61  with self.read_lock, self.write_lock:
62  rospy.loginfo('Beginning Arduino reset on port %s. Closing serial port...' % self.port.portstr)
63  self.port.close()
64  with Serial(self.port.portstr) as arduino:
65  arduino.setDTR(False)
66  time.sleep(3)
67  arduino.flushInput()
68  arduino.setDTR(True)
69  time.sleep(5)
70  rospy.loginfo('Reopening serial port...')
71  self.port.open()
72  rospy.loginfo('Arduino reset complete.')
73  self.lastsync_reset = rospy.Time.now()
74  self.requestTopics()
75  return EmptyResponse()
76 
77  def sendDiagnostics(self, level, msg_text):
78  super(SerialClient, self).sendDiagnostics(level, msg_text)
79  # Reset when we haven't received any data from the Arduino in over N seconds.
80  if self.auto_reset_timeout and (rospy.Time.now() - self.last_read).secs >= self.auto_reset_timeout:
81  if (rospy.Time.now() - self.lastsync_reset).secs < MINIMUM_RESET_TIME:
82  rospy.loginfo('Sync has failed, but waiting for last reset to complete.')
83  else:
84  rospy.loginfo('Sync has failed for over %s seconds. Initiating automatic reset.' % self.auto_reset_timeout)
85  self.resetArduino()
def sendDiagnostics(self, level, msg_text)
Definition: SerialClient.py:77


rosserial_arduino
Author(s): Michael Ferguson, Adam Stambler
autogenerated on Mon Feb 28 2022 23:35:30