All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
stepper.c
Go to the documentation of this file.
#define _GNU_SOURCE 1
#include "stdio.h"
#include <termios.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h>
#include "../c_wrapper/pruio.h"
#include "../c_wrapper/pruio_pins.h"
#define P1 P8_08
#define P2 P8_10
#define P3 P8_12
#define P4 P8_14
int
isleep(unsigned int mseconds)
{
fd_set set;
struct timeval timeout;
/* Initialize the file descriptor set. */
FD_ZERO(&set);
FD_SET(STDIN_FILENO, &set);
/* Initialize the timeout data structure. */
timeout.tv_sec = 0;
timeout.tv_usec = mseconds * 1000;
return TEMP_FAILURE_RETRY(select(FD_SETSIZE,
&set, NULL, NULL,
&timeout));
}
#define PIN_OUT(a, b, c, d) \
if (pruio_gpio_setValue(Io, P1, a)) {printf("setValue P1 error (%s)\n", Io->Errr); break;} \
if (pruio_gpio_setValue(Io, P2, b)) {printf("setValue P2 error (%s)\n", Io->Errr); break;} \
if (pruio_gpio_setValue(Io, P3, c)) {printf("setValue P3 error (%s)\n", Io->Errr); break;} \
if (pruio_gpio_setValue(Io, P4, d)) {printf("setValue P4 error (%s)\n", Io->Errr); break;}
void
move(pruIo *Io, int Rot) {
static int p = 0;
p += Rot;
p &= Rot & 1 ? 7 : 6 ;
switch (p){
case 1: PIN_OUT(1,0,0,0); break;
case 2: PIN_OUT(1,1,0,0); break;
case 3: PIN_OUT(0,1,0,0); break;
case 4: PIN_OUT(0,1,1,0); break;
case 5: PIN_OUT(0,0,1,0); break;
case 6: PIN_OUT(0,0,1,1); break;
case 7: PIN_OUT(0,0,0,1); break;
default: PIN_OUT(1,0,0,1)
}
}
int main(int argc, char **argv)
{
pruIo *Io = pruio_new(PRUIO_DEF_ACTIVE, 0x98, 0, 1);
do {
if (Io->Errr) {
printf("initialisation failed (%s)\n", Io->Errr); break;}
PIN_OUT(1,0,0,1) // initialize pin config
//' pin config OK, transfer local settings to PRU and start PRU driver
if (pruio_config(Io, 1, 0x1FE, 0, 4)) {
printf("config failed (%s)\n", Io->Errr); break;}
// print user informations
printf("Controls: (other keys quit, 1 and 3 only when Direction = 0)\n");
printf(" 8 = faster\n");
printf(" 4 = rotate CW 5 = stop, hold position 6 = rotate CCW\n");
printf(" 1 = single step CW 2 = slower 3 = single step CCW\n");
printf(" 0 = stop, power off\n\n");
printf("Pins\t\tKey\t\tDirection\tSleep\n");
struct termios oldt, newt; // make terminal non-blocking
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON );
newt.c_cc[VMIN] = 0;
newt.c_cc[VTIME] = 1;
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
int w = 128, d = 0;
printf("%1d-%1d-%1d-%1d\t\t%s\t\t%2d\t\t%3d",
pruio_gpio_Value(Io, P1), pruio_gpio_Value(Io, P2), pruio_gpio_Value(Io, P3), pruio_gpio_Value(Io, P4),
"", d, w); // user information
fflush(STDIN_FILENO);
while(1) { // run endless loop
if (1 == isleep(w)) {
switch (getchar()) { // evaluate keystroke
case '2': if (w < 512) w <<= 1; break;
case '8': if (w > 1) w >>= 1; break;
case '4': d = 1; break;
case '7': d = 2; break;
case '9': d = -2; break;
case '6': d = -1; break;
case '0': d = 0; PIN_OUT(0,0,0,0); break;
case '5': d = 0; move(Io, d); break;
case '1': if (d == 0) move(Io, 1); break;
case '3': if (d == 0) move(Io, -1); break;
default: goto finish;
};
printf("\t\t%2d\t\t%3d", d, w); // user information
}
if (d) move(Io, d); // move the motor
printf("\r%1d-%1d-%1d-%1d\t\t"
, pruio_gpio_Value(Io, P1)
, pruio_gpio_Value(Io, P2)
, pruio_gpio_Value(Io, P3)
, pruio_gpio_Value(Io, P4)); // user information
fflush(STDIN_FILENO);
}
finish:
tcsetattr( STDIN_FILENO, TCSANOW, &oldt ); // reset terminal
PIN_OUT(0,0,0,0) // switch off pins
printf("\n");
} while (0);
pruio_destroy(Io); /* destroy driver structure */
return 0;
}
P2
#define P2
The second pin of the stepper.
Definition: stepper.c:39
P1
#define P1
The first pin of the stepper.
Definition: stepper.c:37
P3
#define P3
The third pin of the stepper.
Definition: stepper.c:41
p
VAR p
The position of the 'x' character (if any).
Definition: oszi.bas:28
pruIo
Wrapper structure for PruIo.
Definition: pruio.h:403
pruio_new
pruIo * pruio_new(uint16 Act, uint8 Av, uint32 OpD, uint8 SaD)
Wrapper function for the constructor PruIo::PruIo().
pruIo::Errr
char * Errr
pointer for error messages
Definition: pruio.h:411
pruio_destroy
void pruio_destroy(pruIo *Io)
Wrapper function for the destructor PruIo::~PruIo.
PIN_OUT
#define PIN_OUT(a, b, c, d)
Set values of all four output pins.
Definition: stepper.c:73
PRUIO_DEF_ACTIVE
activate all devices
Definition: pruio.h:385
move
void move(pruIo *Io, int Rot)
Make the motor move the next step.
Definition: stepper.c:90
main
int main(int argc, char **argv)
The main function.
Definition: stepper.c:109
pruio_gpio_Value
uint32 pruio_gpio_Value(pruIo *Io, uint8 Ball)
Wrapper function for GpioUdt::Value().
P4
#define P4
The fourth pin of the stepper.
Definition: stepper.c:43
d
VAR d
The direction value.
Definition: stepper.bas:99
isleep
int isleep(unsigned int mseconds)
Wait for keystroke or timeout.
Definition: stepper.c:54
pruio_config
char * pruio_config(pruIo *Io, uint32 Samp, uint32 Mask, uint32 Tmr, uint16 Mds)
Wrapper function for PruIo::config().
w
VAR w
The required window width.
Definition: oszi.bas:29