SerialClient.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #####################################################################
4 # Software License Agreement (BSD License)
5 #
6 # Copyright (c) 2011, Willow Garage, Inc.
7 # All rights reserved.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 #
13 # * Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # * Redistributions in binary form must reproduce the above
16 # copyright notice, this list of conditions and the following
17 # disclaimer in the documentation and/or other materials provided
18 # with the distribution.
19 # * Neither the name of Willow Garage, Inc. nor the names of its
20 # contributors may be used to endorse or promote products derived
21 # from this software without specific prior written permission.
22 #
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 # POSSIBILITY OF SUCH DAMAGE.
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 Fri Jun 7 2019 22:02:56