Laelaps  2.3.5
RoadNarrows Robotics Small Outdoor Mobile Robot Project
Gpio.py
Go to the documentation of this file.
1 #
2 # Module: Laelaps.Gpio
3 #
4 # Package: RoadNarrows Laelaps Robotic Mobile Platform Package
5 #
6 # Link: https://github.com/roadnarrows-robotics/laelaps
7 #
8 # File: Gpio.py
9 #
10 ## \file
11 ##
12 ## $LastChangedDate: 2016-02-02 13:47:13 -0700 (Tue, 02 Feb 2016) $
13 ## $Rev: 4293 $
14 ##
15 ## \brief Laelaps Odroid GPIO control.
16 ##
17 ## \author: Robin Knight (robin.knight@roadnarrows.com)
18 ##
19 ## \par Copyright
20 ## \h_copy 2015-2017. RoadNarrows LLC.\n
21 ## http://www.roadnarrows.com\n
22 ## All Rights Reserved
23 #
24 # @EulaBegin@
25 #
26 # Permission is hereby granted, without written agreement and without
27 # license or royalty fees, to use, copy, modify, and distribute this
28 # software and its documentation for any purpose, provided that
29 # (1) The above copyright notice and the following two paragraphs
30 # appear in all copies of the source code and (2) redistributions
31 # including binaries reproduces these notices in the supporting
32 # documentation. Substantial modifications to this software may be
33 # copyrighted by their authors and need not follow the licensing terms
34 # described here, provided that the new terms are clearly indicated in
35 # all files where they apply.
36 #
37 # IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
38 # OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
39 # PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
40 # DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
41 # EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
42 # THE POSSIBILITY OF SUCH DAMAGE.
43 #
44 # THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
45 # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
46 # FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
47 # "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
48 # PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
49 #
50 # @EulaEnd@
51 #
52 
53 """
54 Laelaps Odroid GPIO control.
55 """
56 
57 import sys
58 
59 from Laelaps.SysConf import *
60 
61 #
62 ## \brief Read value of a GPIO pin.
63 ##
64 ## \param gpio Exported GPIO number.
65 ##
66 ## \return On success returns '0' or '1'. On error, '' is returned.
67 #
68 def gpioReadValue(gpio):
69  valFileName = "/sys/class/gpio/gpio%d/value" % (gpio)
70  try:
71  fp = open(valFileName, "r")
72  except IOError as inst:
73  print >>sys.stderr, "Error:", inst
74  return ''
75  try:
76  val = fp.read(1)
77  except IOError as inst:
78  print >>sys.stderr, "Error:", inst
79  val = ''
80  fp.close()
81  return val
82 
83 #
84 ## \brief Write a value to a GPIO pin.
85 ##
86 ## \param gpio Exported GPIO number.
87 ## \param value Value to write.
88 ## If '0', 0, or False, value written is '0'.
89 ## If '1', non-zero, or True, value written is '1'.
90 ##
91 ## \return On success returns '0' or '1'. On error, '' is returned.
92 #
93 def gpioWriteValue(gpio, value):
94  valFileName = "/sys/class/gpio/gpio%d/value" % (gpio)
95  try:
96  fp = open(valFileName, "w")
97  except IOError as inst:
98  print >>sys.stderr, "Error:", inst
99  return ''
100  if (value == '0') or (value == '1'):
101  val = value
102  elif value:
103  val = '1'
104  else:
105  val = '0'
106  try:
107  val = fp.write(val+'\n')
108  except IOError as inst:
109  print >>sys.stderr, "Error:", inst
110  val = ''
111  fp.close()
112  return val
113 
114 #
115 ## \brief Laelaps specific chip select function using digital GPIO.
116 ##
117 ## \note Deprecated
118 ##
119 ## The Laelaps uses GPIO 173 to select the front or rear motor controllers.
120 ## For the Odroid, this pin is also the RTS line. Hardware flow control is not
121 ## used, so this pin is available for signalling.
122 ##
123 ## \param port Opened serial port (not used).
124 ## \param addrSel Address of motor controller to be selected.
125 ## \param addrLast Address of last motor controller selected.
126 #
127 def MotorCtlrChipSelectGpio(port, addrSel, addrLast):
128  if addrSel != addrLast:
129  if addrSel == MotorCtlrAddrFront:
130  val = 1
131  else:
132  val = 0
133  gpioWriteValue(GpioMotorCtlrCs, val);
134 
135 #
136 ## \brief Laelaps specific chip select function using RTS.
137 ##
138 ## \note Deprecated
139 ##
140 ## \note The RTS signal in current Odroid distro does not work, so this function
141 ## is useless.
142 ##
143 ## \param port Opened serial port.
144 ## \param addrSel Address of motor controller to be selected.
145 ## \param addrLast Address of last motor controller selected.
146 #
147 def MotorCtlrChipselectRts(port, addrSel, addrLast):
148  if addrSel != addrLast:
149  if addrSel == MotorCtlrAddrFront:
150  level = True
151  else:
152  level = False
153  port.setRTS(level)
154 
155 #
156 ## \brief Enable/disable power to Laelaps motor controllers.
157 ##
158 ## \param state True to enable, False to disable.
159 ##
160 def enableMotorCtlrsPower(state):
161  gpioWriteValue(GpioMotorCtlrEn, state);
162 
163 #
164 ## \brief Test if the motor controllers are powered
165 ##
166 ## \return Returns True if powered, False if unpowered.
167 ##
168 def areMotorCtlrsPowered():
169  if gpioReadValue(GpioMotorCtlrEn) == '1':
170  return True
171  else:
172  return False