libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
stepper.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
24 
25 import curses
26 from libpruio import *
27 import time
28 
29 
30 def stepper(stdscr):
31  P1 = P8_08 # The first pin of the stepper.
32  P2 = P8_10 # The second pin of the stepper.
33  P3 = P8_12 # The third pin of the stepper.
34  P4 = P8_14 # The fourth pin of the stepper.
35 
36  # Create a ctypes pointer to the pruio structure
37  io = pruio_new(PRUIO_DEF_ACTIVE, 4, 0x98, 0)
38  IO = io.contents # the pointer dereferencing, using contents member
39 
40  def PIN_OUT(a, b, c, d):
41  if pruio_gpio_setValue(io, P1, a): raise AssertionError("setValue P1 error (%s)" % IO.Errr)
42  if pruio_gpio_setValue(io, P2, b): raise AssertionError("setValue P2 error (%s)" % IO.Errr)
43  if pruio_gpio_setValue(io, P3, c): raise AssertionError("setValue P3 error (%s)" % IO.Errr)
44  if pruio_gpio_setValue(io, P4, d): raise AssertionError("setValue P4 error (%s)" % IO.Errr)
45 
46  def move(Rot):
47  move.pos += Rot
48  if (Rot & 1): move.pos &= 7
49  else: move.pos &= 6
50 
51  if move.pos == 1: PIN_OUT(1,0,0,0)
52  elif move.pos == 2: PIN_OUT(1,1,0,0)
53  elif move.pos == 3: PIN_OUT(0,1,0,0)
54  elif move.pos == 4: PIN_OUT(0,1,1,0)
55  elif move.pos == 5: PIN_OUT(0,0,1,0)
56  elif move.pos == 6: PIN_OUT(0,0,1,1)
57  elif move.pos == 7: PIN_OUT(0,0,0,1)
58  else: PIN_OUT(1,0,0,1)
59 
60  move.pos = 0 # initialize func attribute
61  # Clear and refresh the screen for a blank canvas
62  stdscr.clear()
63  stdscr.nodelay(1)
64  try:
65  if IO.Errr: raise AssertionError("pruio_new failed (%s)" % IO.Errr)
66 
67  PIN_OUT(1,0,0,1) # initialize pin config
68 
69  if pruio_config(io, 1, 0x1FE, 0, 4): # start IO mode
70  raise AssertionError("config failed (%s)" % IO.Errr)
71 
72  # print user informations
73  stdscr.addstr(0,0, "Controls: (other keys quit, 1 and 3 only when Direction = 0)\n")
74  stdscr.addstr(1,0, " 8 = faster\n")
75  stdscr.addstr(2,0, " 4 = rotate CW 5 = stop, hold position 6 = rotate CCW\n")
76  stdscr.addstr(3,0, " 1 = single step CW 2 = slower 3 = single step CCW\n")
77  stdscr.addstr(4,0, " 0 = stop, power off\n\n")
78  stdscr.addstr(6,0, " Pins Dir Sleep")
79  w = 128
80  d = 1
81  stdscr.addstr(7,10, "{:2d} {:3d}".format(d, w))
82  while True:
83  if d: move(d)
84  stdscr.addstr(7,0, "{:1d}-{:1d}-{:1d}-{:1d}".format(
85  pruio_gpio_Value(io, P1)
86  , pruio_gpio_Value(io, P2)
87  , pruio_gpio_Value(io, P3)
88  , pruio_gpio_Value(io, P4))) # user information
89  stdscr.refresh()
90 
91  stdscr.timeout(w)
92  c = stdscr.getch()
93  if c != -1:
94  if c == ord('2'):
95  if w < 512: w <<= 1
96  elif c == ord('8'):
97  if w > 4: w >>= 1 # python is slow
98  elif c == ord('4'): d = -1
99  elif c == ord('7'): d = -2
100  elif c == ord('9'): d = 2
101  elif c == ord('6'): d = 1
102  elif c == ord('0'): d = 0; PIN_OUT(0,0,0,0)
103  elif c == ord('5'): d = 0; move(d)
104  elif c == ord('1'):
105  if d == 0: move(-1)
106  elif c == ord('3'):
107  if d == 0: move( 1)
108  else: break
109  stdscr.addstr(7,10, "{:2d} {:3d}".format(d, w))
110  finally:
111  PIN_OUT(0,0,0,0) # reset output pins to low
112  pruio_destroy(io) # we're done
113 
114 if __name__ == "__main__":
115  curses.wrapper(stepper)
char * pruio_config(pruIo *Io, uint32 Samp, uint32 Mask, uint32 Tmr, uint16 Mds)
Wrapper function for PruIo::config().
uint32 pruio_gpio_Value(pruIo *Io, uint8 Ball)
Wrapper function for GpioUdt::Value().
pruIo * pruio_new(uint16 Act, uint8 Av, uint32 OpD, uint8 SaD)
Wrapper function for the constructor PruIo::PruIo().
void pruio_destroy(pruIo *Io)
Wrapper function for the destructor PruIo::~PruIo().
char * pruio_gpio_setValue(pruIo *Io, uint8 Ball, uint8 Modus)
Wrapper function for GpioUdt::setValue().
#define PIN_OUT(a, b, c, d)
Set values of all four output pins.
Definition: stepper.c:73
void move(pruIo *Io, int Rot)
Make the motor move the next step.
Definition: stepper.c:90