libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
stepper.bas
Go to the documentation of this file.
1 
24 
25 ' include libpruio
26 #INCLUDE ONCE "BBB/pruio.bi"
27 ' include the convenience macros for header pins
28 #INCLUDE ONCE "BBB/pruio_pins.bi"
29 
30 
31 #DEFINE P1 P8_08
32 
33 #DEFINE P2 P8_10
34 
35 #DEFINE P3 P8_12
36 
37 #DEFINE P4 P8_14
38 
39 
40 
50 SUB move(BYVAL Io AS PruIo PTR, BYVAL Rot AS BYTE = 1)
51  STATIC AS INTEGER p = 0
52 
53  WITH *Io
54  p += Rot
55  p AND= IIF(Rot AND &b1, &b111, &b110)
56 
57  SELECT CASE AS CONST p
58  CASE 1 : .Gpio->setValue(P1, 1) : .Gpio->setValue(P2, 0) : .Gpio->setValue(P3, 0) : .Gpio->setValue(P4, 0)
59  CASE 2 : .Gpio->setValue(P1, 1) : .Gpio->setValue(P2, 1) : .Gpio->setValue(P3, 0) : .Gpio->setValue(P4, 0)
60  CASE 3 : .Gpio->setValue(P1, 0) : .Gpio->setValue(P2, 1) : .Gpio->setValue(P3, 0) : .Gpio->setValue(P4, 0)
61  CASE 4 : .Gpio->setValue(P1, 0) : .Gpio->setValue(P2, 1) : .Gpio->setValue(P3, 1) : .Gpio->setValue(P4, 0)
62  CASE 5 : .Gpio->setValue(P1, 0) : .Gpio->setValue(P2, 0) : .Gpio->setValue(P3, 1) : .Gpio->setValue(P4, 0)
63  CASE 6 : .Gpio->setValue(P1, 0) : .Gpio->setValue(P2, 0) : .Gpio->setValue(P3, 1) : .Gpio->setValue(P4, 1)
64  CASE 7 : .Gpio->setValue(P1, 0) : .Gpio->setValue(P2, 0) : .Gpio->setValue(P3, 0) : .Gpio->setValue(P4, 1)
65  CASE ELSE : .Gpio->setValue(P1, 1) : .Gpio->setValue(P2, 0) : .Gpio->setValue(P3, 0) : .Gpio->setValue(P4, 1)
66  END SELECT
67  END WITH
68 END SUB
69 
70 
71 ' ***** main *****
72 
73 VAR io = NEW PruIo
74 
75 WITH *io
76  DO ' pseudo loop, just to avoid GOTOs
77  IF .Errr THEN ?"initialisation failed (" & *.Errr & ")" : EXIT DO
78 
79 ' initial output pin settings
80  IF .Gpio->config(P1, PRUIO_GPIO_OUT1) THEN ?"failed setting P1 (" & *.Errr & ")"
81  IF .Gpio->config(P2, PRUIO_GPIO_OUT0) THEN ?"failed setting P2 (" & *.Errr & ")"
82  IF .Gpio->config(P3, PRUIO_GPIO_OUT0) THEN ?"failed setting P3 (" & *.Errr & ")"
83  IF .Gpio->config(P4, PRUIO_GPIO_OUT1) THEN ?"failed setting P4 (" & *.Errr & ")"
84  IF .Errr THEN EXIT DO
85 
86 ' pin config OK, transfer local settings to PRU and start PRU driver
87  IF .config() THEN ?"config failed (" & *.Errr & ")" : EXIT DO
88 
89  ? ' print user informations
90  ?"Controls: (other keys quit, 1 and 3 only when Direction = 0)"
91  ?" 8 = faster"
92  ?" 4 = rotate CW 5 = stop, hold position 6 = rotate CCW"
93  ?" 1 = single step CW 2 = slower 3 = single step CCW"
94  ?" 0 = stop, power off"
95  ?
96  ?"Pins","Direction","Sleep" : ?
97 
98  VAR w = 128 _
99  , d = 0 _
100  , x = 16 _
101  , y = CSRLIN() - 1
102  LOCATE y, x, 0 : ?RIGHT(" " & d, 2), RIGHT(" " & w, 3); ' user info
103  DO ' endless loop
104  VAR k = ASC(INKEY())
105  IF k THEN
106  SELECT CASE AS CONST k ' react on user keystrokes
107  CASE ASC("2") : IF w < 512 THEN w SHL= 1 ' faster
108  CASE ASC("8") : IF w > 1 THEN w SHR= 1 ' slower
109  CASE ASC("4") : d = -1 ' half step CW
110  CASE ASC("7") : d = -2 ' half step CCW
111  CASE ASC("9") : d = 2 ' full step CW
112  CASE ASC("6") : d = 1 ' full step CCW
113  CASE ASC("5") : d = 0 : move(io, d) ' powered stop
114  CASE ASC("1") : IF 0 = d THEN move(io, -1) ' single step CW
115  CASE ASC("3") : IF 0 = d THEN move(io, 1) ' single step CCW
116  CASE ASC("0") : d = 0 ' unpowered stop
117  .Gpio->setValue(P1, 0)
118  .Gpio->setValue(P2, 0)
119  .Gpio->setValue(P3, 0)
120  .Gpio->setValue(P4, 0)
121  CASE ELSE : EXIT DO ' finish
122  END SELECT
123 
124  LOCATE y, x, 0
125  ?RIGHT(" " & d, 2), RIGHT(" " & w, 3); ' user info
126  END IF
127  IF d THEN move(io, d) ' move the motor
128  LOCATE y, 1, 0
129  ? .Gpio->Value(P1) & "-" _
130  & .Gpio->Value(P2) & "-" _
131  & .Gpio->Value(P3) & "-" _
132  & .Gpio->Value(P4);
133  SLEEP w ' control the frequency = speed
134  LOOP UNTIL .Errr : ?
135  IF .Errr THEN ?"abborted: " & *.Errr
136 
137  .Gpio->setValue(P1, 0) ' switch off pins
138  .Gpio->setValue(P2, 0)
139  .Gpio->setValue(P3, 0)
140  .Gpio->setValue(P4, 0)
141  IF .Errr THEN ?"zeroing pins failed: : " & *.Errr
142 
143  IF .Errr THEN ?"re-setting pins failed: : " & *.Errr
144  LOOP UNTIL 1
145 END WITH
146 
147 DELETE io ' reset ADC, PinMux and GPIOs, clear memory
148 
149 '' help Doxygen to document the main code
150 '&/** The main function. */
151 '&int main() {PruIo::PruIo(); GpioUdt::config(); PruIo::config(); GpioUdt::setValue(); GpioUdt::Value(); PruIo::~PruIo();}
152