libpruio  0.6.8
Fast and easy Digital/Analog Input/Output for Beaglebones
qep.c
Go to the documentation of this file.
1 
18 #define _GNU_SOURCE 1
19 #include "stdio.h"
20 #include <termios.h>
21 #include <unistd.h>
22 #include <errno.h>
23 #include <sys/types.h>
24 #include "libpruio/pruio.h"
25 #include "libpruio/pruio_pins.h"
26 
28 #define PMX 4095
30 #define VHz 25
32 static const uint8 PINS[3] = {P8_12, P8_11, P8_16};
33 
41 int
42 isleep(unsigned int mseconds)
43 {
44  fd_set set;
45  struct timeval timeout;
46 
47  /* Initialize the file descriptor set. */
48  FD_ZERO(&set);
49  FD_SET(STDIN_FILENO, &set);
50 
51  /* Initialize the timeout data structure. */
52  timeout.tv_sec = 0;
53  timeout.tv_usec = mseconds * 1000;
54 
55  return TEMP_FAILURE_RETRY(select(FD_SETSIZE,
56  &set, NULL, NULL,
57  &timeout));
58 }
59 
61 int main(int argc, char **argv)
62 {
63  pruIo *Io = pruio_new(PRUIO_DEF_ACTIVE, 4, 0x98, 0);
64  do {
65  if (Io->Errr) {
66  printf("initialisation failed (%s)\n", Io->Errr); break;}
67 
68  // configure PWM-1 for symetric output duty 50% and phase shift 1 / 4
69  Io->Pwm->ForceUpDown = 1 << 1;
70  Io->Pwm->AqCtl[0][1][1] = 0x006; //&b000000000110
71  Io->Pwm->AqCtl[1][1][1] = 0x600; //&b011000000000;
72 
74  freq = 50.;
75  if (pruio_pwm_setValue(Io, P9_14, freq, .00)) {
76  printf("failed setting P9_14 (%s)\n", Io->Errr); break;}
77 
78  if (pruio_pwm_setValue(Io, P9_16, freq, .25)) {
79  printf("failed setting P9_16 (%s)\n", Io->Errr); break;}
80 
81  if (pruio_pwm_setValue(Io, P9_42, .5, .00000005)) {
82  printf("failed setting P9_42 (%s)\n", Io->Errr); break;}
83 
84  if (pruio_pwm_Value(Io, P9_14, &realfreq, NULL)) {
85  printf("failed getting PWM value (%s)\n", Io->Errr); break;}
86 
87  uint32 pmax = PMX;
88  if (pruio_qep_config(Io, PINS[0], pmax, VHz, 1., 0)) {
89  printf("QEP pin configuration failed (%s)\n", Io->Errr); break;}
90 
91  if (pruio_config(Io, 1, 0, 0, 4)) {
92  printf("config failed (%s)\n", Io->Errr); break;}
93 
94 //printf("\n input, Hz , PMax= %s", Io->Errr); //freq, realfreq, pmax);
95  static char *t[] = {" A", " A & B", "A, B & I"};
96  uint32 posi, m = -1, p = 0;
97  float_t velo;
98  //printf("\n%s input, %10f Hz (%10f), PMax=%u", t[p], freq, realfreq, pmax);
99 
100  printf("\n p=%u", p);
101  printf("\n m=%i", m);
102  printf("\n %s input", t[p]);
103  printf("\n %s input", t[0]);
104  printf("\n %s input", t[1]);
105  printf("\n %s input", t[2]);
106 
107  struct termios oldt, newt; // make terminal non-blocking
108  tcgetattr(STDIN_FILENO, &oldt);
109  newt = oldt;
110  newt.c_lflag &= ~(ICANON);
111  newt.c_cc[VMIN] = 0;
112  newt.c_cc[VTIME] = 1;
113  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
114 
115  //printf("\n%s input, %10f Hz (%10f), PMax=%u", t[p], 1., 1., 0); //freq, realfreq, pmax);
116  //printf("\n input, Hz , PMax= %s", Io->Errr); //freq, realfreq, pmax);
117 
118 
119  //printf("\n input, Hz , PMax= %s", Io->Errr); //freq, realfreq, pmax);
120  while(1) { // run endless loop
121  fflush(STDIN_FILENO);
122  if (1 == isleep(20)) {
123  switch (getchar()) { // evaluate keystroke
124  case 'a' : case 'A' : m = 0; break;
125  case 'b' : case 'B' : m = 1; break;
126  case 'i' : case 'I' : m = 2; break;
127  case 'p' : case 'P' : m = 3; freq = (freq < 499995.) ? freq + 5. : 500000.; break;
128  case 'm' : case 'M' : m = 3; freq = (freq > 30.) ? freq - 5. : 25.; break;
129  case '*' : m = 3; freq = (freq < 250000.) ? freq * 5. : 500000.; break;
130  case '/' : m = 3; freq = (freq > 50.) ? freq / 5. : 25.; break;
131  case '0' : m = p; pmax = 0; break;
132  case '1' : m = p; pmax = 1023; break;
133  case '4' : m = p; pmax = 4095; break;
134  case '5' : m = p; pmax = 511; break;
135  case '8' : m = p; pmax = 8191; break;
136  case '+' : m = 3; Io->Pwm->AqCtl[0][1][1] = 0x6; break;
137  case '-' : m = 3; Io->Pwm->AqCtl[0][1][1] = 0x9; break;
138  case 13 : m = 3; freq = 50.;
139  if (pruio_pwm_setValue(Io, P9_14, freq, -1.)) {
140  printf("failed setting PWM value (%s)\n", Io->Errr);
141  goto finish;}
142  if (pruio_pwm_Value(Io, P9_14, &realfreq, NULL)) {
143  printf("failed getting PWM value (%s)\n", Io->Errr);
144  goto finish;}
145  break;
146  default: goto finish;
147  };
148  switch (m) { // evaluate command
149  case 3:
150  if (pruio_pwm_setValue(Io, P9_14, freq, -1.)) {
151  printf("failed setting PWM value (%s)\n", Io->Errr);
152  goto finish;}
153  if (pruio_pwm_Value(Io, P9_14, &realfreq, NULL)) {
154  printf("failed getting PWM value (%s)\n", Io->Errr);
155  goto finish;}
156  break;
157  default:
158  p = m;
159  if (pruio_qep_config(Io, PINS[p], pmax, VHz, 1., 0)) { //reconfigure QEP pins
160  printf("QEP pin reconfiguration failed (%s)\n", Io->Errr);
161  goto finish;}
162  };
163  printf("\n%s input, %10f Hz (%10f), PMax=%u", t[p], freq, realfreq, pmax);
164  }
165  if (pruio_qep_Value(Io, PINS[p], &posi, &velo)) { // get new input
166  printf("failed getting QEP Value (%s)\n", Io->Errr); break;}
167 
168  printf("\r Position: %8X , Speed: %7.2f", posi, velo); // info
169  }
170 
171 finish:
172  tcsetattr( STDIN_FILENO, TCSANOW, &oldt ); // reset terminal
173 
174  printf("\n");
175  } while (0);
176 
177  pruio_destroy(Io); /* destroy driver structure */
178  return 0;
179 }
VAR p
The position of the 'x' character (if any).
Definition: oszi.bas:27
VAR m
The step mask.
Definition: oszi.bas:92
unsigned int uint32
32 bit unsigned integer data type.
Definition: pruio.h:41
char * pruio_config(pruIo *Io, uint32 Samp, uint32 Mask, uint32 Tmr, uint16 Mds)
Wrapper function for PruIo::config().
float float_t
float data type.
Definition: pruio.h:42
char * pruio_pwm_setValue(pruIo *Io, uint8 Ball, float_t Hz, float_t Du)
Wrapper function for PwmMod::setValue().
pruIo * pruio_new(uint16 Act, uint8 Av, uint32 OpD, uint8 SaD)
Wrapper function for the constructor PruIo::PruIo().
char * pruio_qep_config(pruIo *Io, uint8 Ball, uint32 PMax, float_t VHz, float_t Scale, uint8 Mo)
Wrapper function for QepMod::config().
char * pruio_pwm_Value(pruIo *Io, uint8 Ball, float_t *Hz, float_t *Du)
Wrapper function for PwmMod::Value().
char * pruio_qep_Value(pruIo *Io, uint8 Ball, uint32 *Posi, float_t *Velo)
Wrapper function for QepMod::Value().
void pruio_destroy(pruIo *Io)
Wrapper function for the destructor PruIo::~PruIo().
unsigned char uint8
8 bit unsigned integer data type.
Definition: pruio.h:39
@ PRUIO_DEF_ACTIVE
Activate all subsystems.
Definition: pruio.h:531
const ANY_PTR NULL
The NULL pointer.
#define P8_16
CPU ball number for pin 16 on header 8.
Definition: pruio_pins.bi:40
#define P9_14
CPU ball number for pin 14 on header 9.
Definition: pruio_pins.bi:109
#define P9_16
CPU ball number for pin 16 on header 9.
Definition: pruio_pins.bi:113
#define P8_12
CPU ball number for pin 12 on header 8.
Definition: pruio_pins.bi:32
#define P9_42
CPU ball number for double pin 42 on header 9 (second ball is 104)
Definition: pruio_pins.bi:163
#define P8_11
CPU ball number for pin 11 on header 8.
Definition: pruio_pins.bi:30
UInt32 pmax
The maximum position value.
Definition: qep.bas:56
CONST_UInt8 PINS[...]
The header pins to use for input (PWMSS-1).
Definition: qep.bas:27
Float_t freq
The current frequency (startvalue here).
Definition: qep.bas:42
UInt32 posi
The current position value.
Definition: qep.bas:65
Float_t velo
The current speed.
Definition: qep.bas:68
Float_t realfreq
The real frequency of PWM output.
Definition: qep.bas:43
#define PMX
Default PMax value.
Definition: qep.c:28
int isleep(unsigned int mseconds)
Wait for keystroke or timeout.
Definition: qep.c:42
int main(int argc, char **argv)
The main function.
Definition: qep.c:61
#define VHz
The frequency for speed measurement.
Definition: qep.c:30
Wrapper structure for PruIo.
Definition: pruio.h:550
char * Errr
Pointer for error messages.
Definition: pruio.h:551
pwmMod * Pwm
Pointer to ePWM module structure (in PWMSS subsystem).
Definition: pruio.h:557
uint16 AqCtl[1+1][PRUIO_AZ_PWMSS+1][2+1]
Initializers for Action Qualifier for ePWM modules (see PWM).
Definition: pruio.h:435
uint16 ForceUpDown
< Switch to force up-down counter for ePWM modules.
Definition: pruio.h:434