Laelaps  2.3.5
RoadNarrows Robotics Small Outdoor Mobile Robot Project
laelaps_imu.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_imu.py
8 #
9 ## \file
10 ##
11 ## $LastChangedDate: 2015-09-25 18:32:17 -0600 (Fri, 25 Sep 2015) $
12 ## $Rev: 4109 $
13 ##
14 ## \brief Read and prints basic IMU data.
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 serial
51 import struct
52 import time
53 import getopt
54 
55 import Laelaps.SysConf as SysConf
56 import Laelaps.Imu as Imu
57 
58 Argv0 = os.path.basename(__file__)
59 CliArgs = {}
60 
61 ##
62 ## \brief Unit test command-line exception class.
63 ##
64 ## Raise usage exception.
65 ##
66 class usage(Exception):
67 
68  ##
69  ## \brief Constructor.
70  ##
71  ## \param msg Error message string.
72  ##
73  def __init__(self, msg):
74  ## error message attribute
75  self.msg = msg
76 
77 #
78 ## \brief Print 'try' message'
79 #
80 def printTry():
81  print "Try '%s --help' for more information." % (Argv0)
82 
83 #
84 ## \brief Print usage error.
85 ##
86 ## \param emsg Error message string.
87 #
88 def printUsageErr(emsg):
89  if emsg:
90  print "%s: Error: %s" % (Argv0, emsg)
91  else:
92  print "%s: Error" % (Argv0)
93  printTry()
94 
95 ## \brief Print Command-Line Usage Message.
96 def printUsage():
97  print \
98 """
99 usage: %s [OPTIONS]
100  %s --help
101 
102 Monitor Laelaps Inertial Measurement Unit.
103 
104 Options and arguments:
105 -h, --help : Display this help and exit.
106 
107 Exit Status:
108  Returns exits status of 0 on success. An exit status of 128 indicates usage
109  error.
110 """ % (Argv0, Argv0)
111 
112 #
113 ## \brief Get command-line options
114 ##
115 ## \param [in,out] kwargs Command-line keyword dictionary.
116 ##
117 ## \return Parsed keyword arguments.
118 #
119 def getOptions(kwargs):
120  argv = sys.argv
121 
122  # defaults
123  kwargs['debug'] = False
124 
125  # parse command-line options
126  try:
127  opts, args = getopt.getopt(argv[1:], "?h", ['help', ''])
128  except getopt.error, msg:
129  raise usage(msg)
130  for opt, optarg in opts:
131  if opt in ('-h', '--help', '-?'):
132  printUsage()
133  sys.exit(0)
134 
135  return kwargs
136 
137 #
138 # Parse command-line options and arguments
139 #
140 try:
141  CliArgs = getOptions(CliArgs)
142 except usage, e:
143  printUsageErr(e.msg)
144  sys.exit(128)
145 
146 print "LaeLaps IMU"
147 print
148 
149 imu = Imu.Imu()
150 
151 imu.open(SysConf.ImuSymName, baudrate=SysConf.ImuBaudRate)
152 
153 ident = imu.readIdent()
154 
155 print "IMU Identity:"
156 print " Version: %u" % (ident['version'])
157 print " Multi-Type: %u" % (ident['multi_type'])
158 print " MSP Version: %u" % (ident['msp_version'])
159 print " Caps: 0x%04x" % (ident['caps'])
160 
161 while True:
162  try:
163  meas = imu.readRawImu('si', 'si')
164 
165  print "accel[x,y,z] (m/s^2) = %.4f, %.4f, %.4f" % \
166  (meas['accel'][Imu.X], meas['accel'][Imu.Y], meas['accel'][Imu.Z])
167  print "gyro[x,y,z](radians/s)) = %.4f, %.4f, %.4f" % \
168  (meas['gyro'][Imu.X], meas['gyro'][Imu.Y], meas['gyro'][Imu.Z])
169 
170  rpy = imu.readAttitude('si')
171 
172  print "roll,pitch,yaw(radians) = %.4f, %.4f, %.4f" % \
173  (rpy[Imu.ROLL], rpy[Imu.PITCH], rpy[Imu.YAW])
174 
175  except KeyboardInterrupt:
176  break
def __init__(self, msg)
Constructor.
Definition: laelaps_imu.py:73
Unit test command-line exception class.
Definition: laelaps_imu.py:66
msg
error message attribute
Definition: laelaps_imu.py:75