Laelaps  2.3.5
RoadNarrows Robotics Small Outdoor Mobile Robot Project
laelaps_stop.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 ###############################################################################
4 #
5 # Package: RoadNarrows Robotics Laelaps Robotic Mobile Platform Package
6 #
7 # File: laelaps_stop
8 #
9 ## \file
10 ##
11 ## $LastChangedDate: 2015-09-25 18:32:17 -0600 (Fri, 25 Sep 2015) $
12 ## $Rev: 4109 $
13 ##
14 ## \brief Stop the Laelaps.
15 ##
16 ## \author Robin Knight (robin.knight@roadnarrows.com)
17 ##
18 ## \par Copyright
19 ## \h_copy 2015-2017. RoadNarrows LLC.\n
20 ## http://www.roadnarrows.com\n
21 ## All Rights Reserved
22 ##
23 # @EulaBegin@
24 #
25 # Unless otherwise stated explicitly, all materials contained are copyrighted
26 # and may not be used without RoadNarrows LLC's written consent,
27 # except as provided in these terms and conditions or in the copyright
28 # notice (documents and software) or other proprietary notice provided with
29 # the relevant materials.
30 #
31 # IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY
32 # MEMBERS/EMPLOYEES/CONTRACTORS OF ROADNARROWS OR DISTRIBUTORS OF THIS SOFTWARE
33 # BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
34 # CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
35 # DOCUMENTATION, EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN
36 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #
38 # THE AUTHORS AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
39 # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
40 # FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
41 # "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
42 # PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
43 #
44 # @EulaEnd@
45 #
46 ###############################################################################
47 
48 import os
49 import sys
50 import time
51 import getopt
52 
53 import Laelaps.SysConf as SysConf
54 import Laelaps.WatchDog as WatchDog
55 import Laelaps.RoboClaw as RoboClaw
56 
57 Argv0 = os.path.basename(__file__)
58 CliArgs = {}
59 
60 ##
61 ## \brief Unit test command-line exception class.
62 ##
63 ## Raise usage exception.
64 ##
65 class usage(Exception):
66 
67  ##
68  ## \brief Constructor.
69  ##
70  ## \param msg Error message string.
71  ##
72  def __init__(self, msg):
73  ## error message attribute
74  self.msg = msg
75 
76 #
77 ## \brief Print 'try' message'
78 #
79 def printTry():
80  print "Try '%s --help' for more information." % (Argv0)
81 
82 #
83 ## \brief Print usage error.
84 ##
85 ## \param emsg Error message string.
86 #
87 def printUsageErr(emsg):
88  if emsg:
89  print "%s: Error: %s" % (Argv0, emsg)
90  else:
91  print "%s: Error" % (Argv0)
92  printTry()
93 
94 ## \brief Print Command-Line Usage Message.
95 def printUsage():
96  print \
97 """
98 usage: %s [OPTIONS]
99  %s --help
100 
101 Stop all Laelaps motors.
102 
103 Options and arguments:
104 -h, --help : Display this help and exit.
105 
106 Exit Status:
107  Returns exits status of 0 on success. An exit status of 128 indicates usage
108  error.
109 """ % (Argv0, Argv0)
110 
111 #
112 ## \brief Get command-line options
113 ##
114 ## \param [in,out] kwargs Command-line keyword dictionary.
115 ##
116 ## \return Parsed keyword arguments.
117 #
118 def getOptions(kwargs):
119  argv = sys.argv
120 
121  # defaults
122  kwargs['debug'] = False
123 
124  # parse command-line options
125  try:
126  opts, args = getopt.getopt(argv[1:], "?h", ['help', ''])
127  except getopt.error, msg:
128  raise usage(msg)
129  for opt, optarg in opts:
130  if opt in ('-h', '--help', '-?'):
131  printUsage()
132  sys.exit(0)
133 
134  return kwargs
135 
136 #
137 # Parse command-line options and arguments
138 #
139 try:
140  CliArgs = getOptions(CliArgs)
141 except usage, e:
142  printUsageErr(e.msg)
143  sys.exit(128)
144 
145 DoStop = True
146 
147 #
148 # WatchDog subprocessor control the enable lines to the motor controllers.
149 #
150 wd = WatchDog.WatchDog()
151 
152 if wd.open(SysConf.SensorDevName):
153  wd.cmdGetFwVersion()
154  ret = wd.cmdReadEnables()
155  if ret['rc'] == 'ok':
156  if not ret['enables']['motor_ctlr_en']:
157  print "Motor controllers already disabled, nothing to stop."
158  DoStop = False
159 else:
160  print 'Error: Failed to open connection to watchdog subprocessor.'
161  print ' Continuing...'
162 
163 
164 if DoStop:
165  motorctlr = RoboClaw.RoboClaw()
166 
167  #
168  # Stop all motors.
169  #
170  try:
171  motorctlr.open(SysConf.MotorCtlrDevName, SysConf.MotorCtlrBaudRate)
172  motorctlr.setMixedSpeed(SysConf.MotorCtlrAddrFront, 0, 0)
173  motorctlr.setMixedSpeed(SysConf.MotorCtlrAddrRear, 0, 0)
174  motorctlr.close()
175  except RoboClaw.RoboClawException as inst:
176  print 'Error:', inst.message
177 
178 if wd.isOpen():
179  wd.close()
Unit test command-line exception class.
Definition: laelaps_stop.py:65
msg
error message attribute
Definition: laelaps_stop.py:74
def __init__(self, msg)
Constructor.
Definition: laelaps_stop.py:72