libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
stepper.c
Go to the documentation of this file.
1 
26 #define _GNU_SOURCE 1
27 #include "stdio.h"
28 #include <termios.h>
29 #include <unistd.h>
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <sys/time.h>
33 #include "libpruio/pruio.h"
34 #include "libpruio/pruio_pins.h"
35 
37 #define P1 P8_08
39 #define P2 P8_10
41 #define P3 P8_12
43 #define P4 P8_14
44 
45 
53 int
54 isleep(unsigned int mseconds)
55 {
56  fd_set set;
57  struct timeval timeout;
58 
59  /* Initialize the file descriptor set. */
60  FD_ZERO(&set);
61  FD_SET(STDIN_FILENO, &set);
62 
63  /* Initialize the timeout data structure. */
64  timeout.tv_sec = 0;
65  timeout.tv_usec = mseconds * 1000;
66 
67  return TEMP_FAILURE_RETRY(select(FD_SETSIZE,
68  &set, NULL, NULL,
69  &timeout));
70 }
71 
73 #define PIN_OUT(a, b, c, d) \
74  if (pruio_gpio_setValue(Io, P1, a)) {printf("setValue P1 error (%s)\n", Io->Errr); break;} \
75  if (pruio_gpio_setValue(Io, P2, b)) {printf("setValue P2 error (%s)\n", Io->Errr); break;} \
76  if (pruio_gpio_setValue(Io, P3, c)) {printf("setValue P3 error (%s)\n", Io->Errr); break;} \
77  if (pruio_gpio_setValue(Io, P4, d)) {printf("setValue P4 error (%s)\n", Io->Errr); break;}
78 
89 void
90 move(pruIo *Io, int Rot) {
91  static int p = 0;
92 
93  p += Rot;
94  p &= Rot & 1 ? 7 : 6 ;
95 
96  switch (p){
97  case 1: PIN_OUT(1,0,0,0); break;
98  case 2: PIN_OUT(1,1,0,0); break;
99  case 3: PIN_OUT(0,1,0,0); break;
100  case 4: PIN_OUT(0,1,1,0); break;
101  case 5: PIN_OUT(0,0,1,0); break;
102  case 6: PIN_OUT(0,0,1,1); break;
103  case 7: PIN_OUT(0,0,0,1); break;
104  default: PIN_OUT(1,0,0,1)
105  }
106 }
107 
109 int main(int argc, char **argv)
110 {
111  pruIo *Io = pruio_new(PRUIO_DEF_ACTIVE, 4, 0x98, 0);
112  do {
113  if (Io->Errr) {
114  printf("initialisation failed (%s)\n", Io->Errr); break;}
115 
116  PIN_OUT(1,0,0,1) // initialize pin config
117 
118  //' pin config OK, transfer local settings to PRU and start PRU driver
119  if (pruio_config(Io, 1, 0x1FE, 0, 4)) {
120  printf("config failed (%s)\n", Io->Errr); break;}
121 
122  // print user informations
123  printf("Controls: (other keys quit, 1 and 3 only when Direction = 0)\n");
124  printf(" 8 = faster\n");
125  printf(" 4 = rotate CW 5 = stop, hold position 6 = rotate CCW\n");
126  printf(" 1 = single step CW 2 = slower 3 = single step CCW\n");
127  printf(" 0 = stop, power off\n\n");
128  printf("Pins\t\tKey\t\tDirection\tSleep\n");
129 
130  struct termios oldt, newt; // make terminal non-blocking
131  tcgetattr( STDIN_FILENO, &oldt );
132  newt = oldt;
133  newt.c_lflag &= ~( ICANON );
134  newt.c_cc[VMIN] = 0;
135  newt.c_cc[VTIME] = 1;
136  tcsetattr( STDIN_FILENO, TCSANOW, &newt );
137 
138  int w = 128, d = 0;
139  printf("%1d-%1d-%1d-%1d\t\t%s\t\t%2d\t\t%3d",
141  "", d, w); // user information
142  fflush(STDIN_FILENO);
143  while(1) { // run endless loop
144  if (1 == isleep(w)) {
145  switch (getchar()) { // evaluate keystroke
146  case '2': if (w < 512) w <<= 1; break;
147  case '8': if (w > 1) w >>= 1; break;
148  case '4': d = -1; break;
149  case '7': d = -2; break;
150  case '9': d = 2; break;
151  case '6': d = 1; break;
152  case '0': d = 0; PIN_OUT(0,0,0,0); break;
153  case '5': d = 0; move(Io, d); break;
154  case '1': if (d == 0) move(Io, -1); break;
155  case '3': if (d == 0) move(Io, 1); break;
156  default: goto finish;
157  };
158  printf("\t\t%2d\t\t%3d", d, w); // user information
159  }
160 
161  if (d) move(Io, d); // move the motor
162 
163  printf("\r%1d-%1d-%1d-%1d\t\t"
164  , pruio_gpio_Value(Io, P1)
165  , pruio_gpio_Value(Io, P2)
166  , pruio_gpio_Value(Io, P3)
167  , pruio_gpio_Value(Io, P4)); // user information
168  fflush(STDIN_FILENO);
169  }
170 
171 finish:
172  tcsetattr( STDIN_FILENO, TCSANOW, &oldt ); // reset terminal
173 
174  PIN_OUT(0,0,0,0) // switch off pins
175 
176  printf("\n");
177  } while (0);
178 
179  pruio_destroy(Io); /* destroy driver structure */
180  return 0;
181 }
VAR p
The position of the 'x' character (if any).
Definition: oszi.bas:27
VAR w
The required window width.
Definition: oszi.bas:28
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().
@ PRUIO_DEF_ACTIVE
Activate all subsystems.
Definition: pruio.h:531
const ANY_PTR NULL
The NULL pointer.
float_t d
the measured duty cycle
#define P3
The third pin of the stepper.
Definition: stepper.c:41
int isleep(unsigned int mseconds)
Wait for keystroke or timeout.
Definition: stepper.c:54
int main(int argc, char **argv)
The main function.
Definition: stepper.c:109
#define P1
The first pin of the stepper.
Definition: stepper.c:37
#define PIN_OUT(a, b, c, d)
Set values of all four output pins.
Definition: stepper.c:73
#define P4
The fourth pin of the stepper.
Definition: stepper.c:43
void move(pruIo *Io, int Rot)
Make the motor move the next step.
Definition: stepper.c:90
#define P2
The second pin of the stepper.
Definition: stepper.c:39
Wrapper structure for PruIo.
Definition: pruio.h:550
char * Errr
Pointer for error messages.
Definition: pruio.h:551