libpruio  0.0.2
AM33xx-PRU driver for digital input / output and analog input
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
stepper.bas
Go to the documentation of this file.
1 /'* \file stepper.bas
2 \brief Example: control a stepper motor.
3 
4 This file contains an example on how to use libpruio to control a
5 4-wire stepper motor:
6 
7 - configure 4 pins as output
8 - receive user action in loop
9 - inform user about the current state
10 - change motor direction
11 - change motor speed
12 - stop holded or in power off mode
13 - move a single step (in holded mode)
14 - quit
15 
16 Licence: GPLv3
17 
18 Copyright 2014 by Thomas{ dOt ]Freiherr[ At ]gmx[ DoT }net
19 
20 
21 Compile by:
22 
23 fbc -w all stepper.bas
24 
25 '/
26 
27 '* include libpruio
28 #INCLUDE ONCE "../pruio/pruio.bi"
29 '* include the convenience macros for header pins
30 #INCLUDE ONCE "../pruio/pruio_pins.bi"
31 
32 ' output pins for the motor driver
33 #DEFINE P1 P8_08
34 #DEFINE P2 P8_10
35 #DEFINE P3 P8_12
36 #DEFINE P4 P8_14
37 
38 
39 /'* \brief make the motor move the next step.
40 \param Io pointer to PruIo structure
41 \param Rot direction of rotation (1 or -1)
42 
43 This function sets 4 output pins for a stepper motor driver. It
44 remembers the last step as static variable (starting at 0 = zero) and
45 adds the new position to it. So the Rot parameter should either be 1 or
46 -1 to make the motor move one step in any direction.
47 
48 '/
49 SUB move(BYVAL Io AS PruIo PTR, BYVAL Rot AS BYTE = 1)
50  STATIC AS INTEGER p = 0
51 
52  WITH *Io
53  p += Rot
54  p AND= &b111
55 
56  SELECT CASE AS CONST p
57  CASE 1 : .gpio_out(P1, 1) : .gpio_out(P2, 0) : .gpio_out(P3, 0) : .gpio_out(P4, 0)
58  CASE 2 : .gpio_out(P1, 1) : .gpio_out(P2, 1) : .gpio_out(P3, 0) : .gpio_out(P4, 0)
59  CASE 3 : .gpio_out(P1, 0) : .gpio_out(P2, 1) : .gpio_out(P3, 0) : .gpio_out(P4, 0)
60  CASE 4 : .gpio_out(P1, 0) : .gpio_out(P2, 1) : .gpio_out(P3, 1) : .gpio_out(P4, 0)
61  CASE 5 : .gpio_out(P1, 0) : .gpio_out(P2, 0) : .gpio_out(P3, 1) : .gpio_out(P4, 0)
62  CASE 6 : .gpio_out(P1, 0) : .gpio_out(P2, 0) : .gpio_out(P3, 1) : .gpio_out(P4, 1)
63  CASE 7 : .gpio_out(P1, 0) : .gpio_out(P2, 0) : .gpio_out(P3, 0) : .gpio_out(P4, 1)
64  CASE ELSE : .gpio_out(P1, 1) : .gpio_out(P2, 0) : .gpio_out(P3, 0) : .gpio_out(P4, 1)
65  END SELECT
66  END WITH
67 END SUB
68 
69 
70 ' ***** main *****
71 
72 VAR io = NEW PruIo '*< create a PruIo structure, wakeup devices
73 
74 WITH *io
75  DO ' pseudo loop, just to avoid GOTOs
76  IF .Errr THEN ?"initialisation failed (" & *.Errr & ")" : EXIT DO
77 
78 ' initial output pins settings
79  IF .gpio_set(P1, PRUIO_OUT1) THEN ?"failed setting P1 (" & *.Errr & ")"
80  IF .gpio_set(P2, PRUIO_OUT0) THEN ?"failed setting P2 (" & *.Errr & ")"
81  IF .gpio_set(P3, PRUIO_OUT0) THEN ?"failed setting P3 (" & *.Errr & ")"
82  IF .gpio_set(P4, PRUIO_OUT1) THEN ?"failed setting P4 (" & *.Errr & ")"
83  IF .Errr THEN EXIT DO
84 
85 ' pin config OK, transfer local settings to PRU and start PRU driver
86  IF .config() THEN ?"config failed (" & *.Errr & ")" : EXIT DO
87 
88  ? ' print user informations
89  ?"Controls: (other keys quit, 1 and 3 only when Direction = 0)"
90  ?" 8 = faster"
91  ?" 4 = rotate CW 5 = stop, hold position 6 = rotate CCW"
92  ?" 1 = single step CW 2 = slower 3 = single step CCW"
93  ?" 0 = stop, power off"
94  ?
95  ?"Direction","Sleep" : ?
96 
97  VAR w = 128, d = 0, x = 1, y = CSRLIN() - 1
98  LOCATE y, x, 0 : ?RIGHT(" " & d, 2), RIGHT(" " & w, 3); ' user info
99  DO ' endless loop
100  VAR k = ASC(INKEY())
101  IF k THEN
102  SELECT CASE AS CONST k ' react on user keystrokes
103  CASE ASC("2") : IF w < 512 THEN w SHL= 1
104  CASE ASC("8") : IF w > 1 THEN w SHR= 1
105  CASE ASC("4") : d = 1
106  CASE ASC("7") : d = 2
107  CASE ASC("9") : d = -2
108  CASE ASC("6") : d = -1
109  CASE ASC("0") : d = 0 : .gpio_out(P1, 0) : .gpio_out(P2, 0) : .gpio_out(P3, 0) : .gpio_out(P4, 0)
110  CASE ASC("5") : d = 0 : move(io, d)
111  CASE ASC("1") : IF 0 = d THEN move(io, 1)
112  CASE ASC("3") : IF 0 = d THEN move(io, -1)
113  CASE ELSE : EXIT DO
114  END SELECT
115 
116  LOCATE y, x, 0 : ?RIGHT(" " & d, 2), RIGHT(" " & w, 3); 'user info
117  END IF
118  IF d THEN move(io, d) ' move the motor
119  SLEEP w ' control the frequency = speed
120  LOOP
121  ?
122  ' switch off pins
123  .gpio_out(P1, 0) : .gpio_out(P2, 0) : .gpio_out(P3, 0) : .gpio_out(P4, 0)
124  ' reset pin configurations
125  .gpio_set(P1, PRUIO_PIN_RESET)
126  .gpio_set(P2, PRUIO_PIN_RESET)
127  .gpio_set(P3, PRUIO_PIN_RESET)
128  .gpio_set(P4, PRUIO_PIN_RESET)
129  LOOP UNTIL 1
130 END WITH
131 
132 DELETE io ' reset ADC, PinMux and GPIOs, clear memory
133