libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
qep.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
16 
17 import curses
18 from libpruio import *
19 import time
20 
21 
22 def qep(stdscr):
23  # Clear and refresh the screen for a blank canvas
24  stdscr.clear()
25  stdscr.nodelay(1)
26 
27  PMX = 4095 # Default PMax value.
28  VHz = 25 # The frequency for speed measurement.
29  PINS = (P8_12, P8_11, P8_16) # The header pins to use for input (PWMSS-1).
30 
31  # Create a ctypes pointer to the pruio structure
32  io = pruio_new(PRUIO_DEF_ACTIVE, 4, 0x98, 0)
33  try:
34  IO = io.contents # the pointer dereferencing, using contents member
35  if IO.Errr: raise AssertionError("pruio_new failed (%s)" % IO.Errr)
36 
37  # configure PWM-1 for symetric output duty 50% and phase shift 1 / 4
38  IO.Pwm.contents.ForceUpDown = 1 << 1
39  AqCtl = IO.Pwm.contents.AqCtl
40  AqC_A = 3+1 # offset index [0][1][1]
41  AqC_B = 3*3+3+1 # offset index [1][1][1]
42  AqCtl[AqC_A] = 0b000000000110
43  AqCtl[AqC_B] = 0b011000000000
44 
45  realfreq = c_float(0.0)
46  freq = 50.
47  if pruio_pwm_setValue(io, P9_14, freq, .00):
48  raise AssertionError("failed setting P9_14 (%s)" % IO.Errr)
49 
50  if pruio_pwm_setValue(io, P9_16, freq, .25):
51  raise AssertionError("failed setting P9_16 (%s)" % IO.Errr)
52 
53  if pruio_pwm_setValue(io, P9_42, .5, .00000005):
54  raise AssertionError("failed setting P9_42 (%s)" % IO.Errr)
55 
56  if pruio_pwm_Value(io, P9_14, byref(realfreq), None):
57  raise AssertionError("failed getting PWM value (%s)" % IO.Errr)
58 
59  pmax = PMX
60  if pruio_qep_config(io, PINS[0], pmax, VHz, 1., 0):
61  raise AssertionError("QEP pin configuration failed (%s)" % IO.Errr)
62  if pruio_config(io, 1, 0, 0, 4): # start IO mode
63  raise AssertionError("config failed (%s)" % IO.Errr)
64 
65  t = (" A", " A & B", "A, B & I")
66  posi = c_uint(0)
67  velo = c_float(0)
68  m = -1
69  p = 0
70 
71  stdscr.addstr(0,0, " p=%u" % p)
72  stdscr.addstr(1,0, " m=%i" % m)
73  stdscr.addstr(2,0, " %s input" % t[p])
74  stdscr.addstr(3,0, " %s input" % t[0])
75  stdscr.addstr(4,0, " %s input" % t[1])
76  stdscr.addstr(5,0, " %s input" % t[2])
77  while True:
78  if pruio_qep_Value(io, PINS[p], byref(posi), byref(velo)): # get new input
79  raise AssertionError("failed getting QEP Value (%s)" % IO.Errr)
80  stdscr.addstr(7,0, "Position: %8X , Speed: %7.2f" % (posi.value, velo.value)) # info
81  stdscr.timeout(200)
82  c = stdscr.getch()
83  if c != -1:
84  if c == ord('a') or c == ord('A'): m = 0
85  elif c == ord('b') or c == ord('B'): m = 1
86  elif c == ord('i') or c == ord('I'): m = 2
87  elif c == ord('p') or c == ord('P'): m = 3; freq = 500000. if freq >= 499995. else freq + 5.
88  elif c == ord('m') or c == ord('M'): m = 3; freq = 25. if freq <= 30. else freq - 5.
89  elif c == ord('*'): m = 3; freq = 500000. if freq >= 250000. else freq * 5.
90  elif c == ord('/'): m = 3; freq = 25. if freq <= 50. else freq / 5.
91  elif c == ord('0'): m = p; pmax = 0
92  elif c == ord('1'): m = p; pmax = 1023
93  elif c == ord('4'): m = p; pmax = 4095
94  elif c == ord('5'): m = p; pmax = 511
95  elif c == ord('8'): m = p; pmax = 8191
96  elif c == ord('+'): m = 3; AqCtl[AqC_A] = 0x6
97  elif c == ord('-'): m = 3; AqCtl[AqC_B] = 0x9
98  elif c == 10:
99  m = 3; freq = 50.
100  if pruio_pwm_setValue(io, P9_14, freq, -1.):
101  raise AssertionError("failed setting PWM value (%s)" % IO.Errr)
102  if pruio_pwm_Value(io, P9_14, byref(realfreq), None):
103  raise AssertionError("failed getting PWM value (%s)" % IO.Errr)
104  else:
105  break
106 
107  if m == 3:
108  if pruio_pwm_setValue(io, P9_14, freq, -1.):
109  raise AssertionError("failed setting PWM value (%s)" % IO.Errr)
110  if pruio_pwm_Value(io, P9_14, byref(realfreq), None):
111  raise AssertionError("failed getting PWM value (%s)" % IO.Errr)
112  else:
113  p = m
114  if pruio_qep_config(io, PINS[p], pmax, VHz, 1., 0): # reconfigure QEP pins
115  raise AssertionError("QEP pin reconfiguration failed (%s)" % IO.Errr)
116  stdscr.addstr(7,50, "%s input, %10f Hz (%10f), PMax=%u" % (t[p], freq, realfreq.value, pmax))
117  finally:
118  pruio_destroy(io) # we're done
119 
120 if __name__ == "__main__":
121  curses.wrapper(qep)
Definition: qep.py:1
char * pruio_config(pruIo *Io, uint32 Samp, uint32 Mask, uint32 Tmr, uint16 Mds)
Wrapper function for PruIo::config().
char * pruio_pwm_setValue(pruIo *Io, uint8 Ball, float_t Hz, float_t Du)
Wrapper function for PwmMod::setValue().
pruIo * pruio_new(uint16 Act, uint8 Av, uint32 OpD, uint8 SaD)
Wrapper function for the constructor PruIo::PruIo().
char * pruio_qep_config(pruIo *Io, uint8 Ball, uint32 PMax, float_t VHz, float_t Scale, uint8 Mo)
Wrapper function for QepMod::config().
char * pruio_pwm_Value(pruIo *Io, uint8 Ball, float_t *Hz, float_t *Du)
Wrapper function for PwmMod::Value().
char * pruio_qep_Value(pruIo *Io, uint8 Ball, uint32 *Posi, float_t *Velo)
Wrapper function for QepMod::Value().
void pruio_destroy(pruIo *Io)
Wrapper function for the destructor PruIo::~PruIo().