test_pi_gpio.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 #
4 # A test of GPIO Lines on Rapberry Pi that requires loopback connector
5 #
6 # This test requires a standalone Raspberry Pi with GPIO pins connected in pairs
7 # where output and input test array index is the same
8 # Wire for test1outputs[] and test1inputs[]
9 # Wire for test2outputs[] and test2inputs[]
10 #
11 #
12 import time
13 import os
14 
15 import sys
16 import getopt
17 import RPi.GPIO as GPIO
18 
19 # Configure Pi GPIO lines we will use for pure GPIO IO input and output tests
20 # test1outputs = []
21 # test1inputs = []
22 # test2outputs = []
23 # test2inputs = []
24 
25 # GPIO Lines must be connected between same indexes for test1 and for test2
26 test1outputs = [ 2, 4,14,17,22,10,11,24,8, 5, 6,19,26]
27 test1inputs = [ 3,27,15,18,23, 9,21,25,7,13,12,16,20]
28 test2outputs = [ 2, 4,14,17,22,10,11,24,8, 5, 6,19,26]
29 test2inputs = [ 3,27,15,18,23, 9,21,25,7,13,12,16,20]
30 
31 # Version for this background monitor script
32 scriptVersion = '20181029.1'
33 
34 # Get a starting time in unix time in seconds for this script
35 scriptStartTime = time.time()
36 
37 # Define Paths used by this tool all up front when possible
38 # IMPORTANT SUPPORT INFO: These paths must EXACTLY match usages in config_server.cpp!
39 logFilePath = '/home/ubuntu'
40 
41 # set to 1 for showing some info on progress.
42 # there are many more debugs that can be enabled with modifications
43 g_simpleDebug = 0
44 
45 # Define the LEDS in terms of GPIO lines
46 statusLed=12
47 
48 GPIO.setwarnings(False)
49 
50 # configure GPIO lines we will use for control
51 GPIO.setmode(GPIO.BCM)
52 
53 # --------------------------- Helper Routines ------------------------------
54 # Log output from this script to a log file
55 def logLine(line):
56  try:
57  f = open(logFilePath, 'a')
58  try:
59  lineForLog = time.asctime(time.gmtime()) + ' ' + line + '\n'
60  f.write(lineForLog)
61  finally:
62  f.close()
63  except IOError:
64  pass
65 
66 # Blink a single led the number of times requested with requested blink period
67 # The led line is first set low then high and after done are all set to final value
68 def blinkLed( ledGpioLine, numBlinks, timeOfBlinks, finalLedState ):
69  for x in range (0, numBlinks):
70  GPIO.output(ledGpioLine, 1)
71  time.sleep(timeOfBlinks)
72  GPIO.output(ledGpioLine, 0)
73  time.sleep(timeOfBlinks)
74  GPIO.output(ledGpioLine, finalLedState)
75 
76 def printUsage():
77  print('usage: python testPiGpio.py')
78 
79 # ----------------------------- Start Main ----------------------------------
80 #
81 # Setup for log lines to a file
82 if not os.path.isfile(logFilePath):
83  os.system('touch ' + logFilePath + ' 2> /dev/null');
84 
85 retValue = 0;
86 
87 logLine(os.path.basename(__file__) + ' version ' + scriptVersion + ' test starting now.')
88 
89 # Get options for this run of the script
90 try:
91  opts, args = getopt.getopt(sys.argv[1:], "hrsf")
92 except getopt.GetoptError as err:
93  print(str(err)) # will print something like "option -a not recognized"
94  printUsage()
95  logLine(os.path.basename(__file__) + ' Pi Connector test exiting due to bad option entererd.')
96  sys.exit(2)
97 
98 output = None
99 verbose = False
100 # ...
101 
102 
103 # -------------------------- mainloop: Background Loop -------------------------------
104 
105 # Wrap to catch exceptions such as KeyboardInterrupt so we can indicate in log when done
106 try:
107  # Start of the main program ========================================================
108 
109  bitError = 0
110 
111  print("Test of Raspberry Pi GPIO lines. Version: " + str(scriptVersion))
112  print("(The test fixture to connect associated GPIO lines is required)")
113  print("")
114 
115  # ------------------------ Check first direction of GPIO pins --------------------
116  for i in range(0, len(test1outputs)):
117  if g_simpleDebug == 1:
118  print("Dir 1 Test: Output port " + str(test1outputs[i]) + " input port " + str(test1inputs[i]))
119  GPIO.setup(test1inputs[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
120  GPIO.setup(test1outputs[i], GPIO.OUT)
121 
122  print("Testing forward direction for digital GPIO connectivity...")
123  time.sleep(0.1)
124 
125  # first we set one level then next pass test the other level
126  print("Testing first direction for one level")
127  for i in range(0, len(test1outputs)):
128  # Set an output bit and read it back
129  if ((i & 1) == True):
130  # print("Set output port " + str(test1outputs[i]) + " to 1 ")
131  GPIO.output(test1outputs[i], 1)
132  else:
133  # print("Set output port " + str(test1outputs[i]) + " to 0 ")
134  GPIO.output(test1outputs[i], 0)
135 
136  time.sleep(0.1)
137  for i in range(0, len(test1inputs)):
138  # Set an output bit and read it back
139  # print("Test input port " + str(test1inputs[i]))
140  if ((i & 1) == True):
141  if GPIO.input(test1inputs[i]) != 1:
142  print("FAILURE! Readback of port " + str(test1inputs[i]) + " set by port " + str(test1outputs[i]) + " was not 1!")
143  bitError |= 1 << i
144  else:
145  if GPIO.input(test1inputs[i]) != 0:
146  print("FAILURE! Readback of port " + str(test1inputs[i]) + " set by port " + str(test1outputs[i]) + " was not 0!")
147  bitError |= 1 << i
148 
149  # now set the one level for this direction
150  print("Testing first direction for other level")
151  for i in range(0, len(test1outputs)):
152  # Set an output bit and read it back
153  if ((i & 1) == True):
154  # print("Set output port " + str(test1outputs[i]) + " to 0 ")
155  GPIO.output(test1outputs[i], 0)
156  else:
157  # print("Set output port " + str(test1outputs[i]) + " to 1 ")
158  GPIO.output(test1outputs[i], 1)
159 
160  time.sleep(0.1)
161  for i in range(0, len(test1inputs)):
162  # Test an input bit and read it back
163  # print("Test input port " + str(test1inputs[i]))
164  if ((i & 1) == True):
165  if GPIO.input(test1inputs[i]) != 0:
166  print("FAILURE! Readback of port " + str(test1inputs[i]) + " set by port " + str(test1outputs[i]) + " was not 0!")
167  bitError |= 1 << i
168  else:
169  if GPIO.input(test1inputs[i]) != 1:
170  print("FAILURE! Readback of port " + str(test1inputs[i]) + " set by port " + str(test1outputs[i]) + " was not 1!")
171  bitError |= 1 << i
172 
173 
174  print("Testing reverse direction for digital GPIO connectivity...")
175  for i in range(0, len(test2outputs)):
176  if g_simpleDebug == 1:
177  print("Dir 2 Test: Output port " + str(test2outputs[i]) + " input port " + str(test2inputs[i]))
178  GPIO.setup(test2inputs[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
179  GPIO.setup(test2outputs[i], GPIO.OUT)
180 
181 
182  # first we set one level then next pass test the other level
183  print("Testing reverse direction for one level")
184  for i in range(0, len(test2outputs)):
185  # Set an output bit and read it back
186  if ((i & 1) == True):
187  # print("Set output port " + str(test2outputs[i]) + " to 1 ")
188  GPIO.output(test2outputs[i], 1)
189  else:
190  # print("Set output port " + str(test2outputs[i]) + " to 0 ")
191  GPIO.output(test2outputs[i], 0)
192 
193  time.sleep(0.1)
194  for i in range(0, len(test2inputs)):
195  # Set an output bit and read it back
196  # print("Test input port " + str(test2inputs[i]))
197  if ((i & 1) == True):
198  if GPIO.input(test2inputs[i]) != 1:
199  print("FAILURE! Readback of port " + str(test2inputs[i]) + " set by port " + str(test2outputs[i]) + " was not 1!")
200  bitError |= 1 << i
201  else:
202  if GPIO.input(test1inputs[i]) != 0:
203  print("FAILURE! Readback of port " + str(test2inputs[i]) + " set by port " + str(test2outputs[i]) + " was not 0!")
204  bitError |= 1 << i
205 
206  # now set the one level for this direction
207  print("Testing reverse direction for other level")
208  for i in range(0, len(test2outputs)):
209  # Set an output bit and read it back
210  if ((i & 1) == True):
211  # print("Set output port " + str(test2outputs[i]) + " to 0 ")
212  GPIO.output(test2outputs[i], 0)
213  else:
214  # print("Set output port " + str(test2outputs[i]) + " to 1 ")
215  GPIO.output(test2outputs[i], 1)
216 
217  time.sleep(0.1)
218  for i in range(0, len(test2inputs)):
219  # Test an input bit and read it back
220  # print("Test input port " + str(test2inputs[i]))
221  if ((i & 1) == True):
222  if GPIO.input(test2inputs[i]) != 0:
223  print("FAILURE! Readback of port " + str(test2inputs[i]) + " set by port " + str(test2outputs[i]) + " was not 0!")
224  bitError |= 1 << i
225  else:
226  if GPIO.input(test2inputs[i]) != 1:
227  print("FAILURE! Readback of port " + str(test2inputs[i]) + " set by port " + str(test2outputs[i]) + " was not 1!")
228  bitError |= 1 << i
229 
230  # Show results
231  if (bitError == 0):
232  print("")
233  print("All tests passed!")
234  else:
235  print("")
236  print("One or more failures were detected on this pass!")
237  print(" Note that the test fixture to connect associated GPIO lines is required")
238  print(" Intermittent failures are often a sign that some other task is controlling GPIO")
239 
240 
241  # End of the main program ==============================================================
242 
243 except KeyboardInterrupt:
244  logLine(os.path.basename(__file__) + ' keyboard exception. background monitor done.')
245  raise
246 except:
247  logLine(os.path.basename(__file__) + ' exception or program exit. background monitor done.')
248 
249 sys.exit(0)
250 
251 
252 
253 
test_pi_gpio.printUsage
def printUsage()
Definition: test_pi_gpio.py:76
test_pi_gpio.blinkLed
def blinkLed(ledGpioLine, numBlinks, timeOfBlinks, finalLedState)
Definition: test_pi_gpio.py:68
test_pi_gpio.logLine
def logLine(line)
Definition: test_pi_gpio.py:55


ubiquity_motor
Author(s):
autogenerated on Thu Nov 16 2023 03:30:56