gclib examples  0
Example Projects for the Communications API for Galil controllers and PLCs
jog.cpp
Go to the documentation of this file.
1 
4 #include "examples.h"
5 
6 #ifdef _WIN32
7  #include <conio.h>
8 #elif __linux__
9  #include <ncurses.h>
10 #endif
11 
12 #include <iostream> //std::cout
13 using namespace std;
14 
25 GReturn jog(GCon g)
26 {
27  char buf[G_SMALL_BUFFER]; //traffic buffer
28  if (g == 0) //Bad connection
29  {
30  cerr << "There was an error with the connection." << endl;
31  return G_CONNECTION_NOT_ESTABLISHED;
32  }
33 
34 #ifdef __linux__
35  //These functions set up the ncurses library to capture keyboard input
36  initscr(); // Initialization of ncurses library
37  cbreak(); // Capture one character at a time
38  noecho(); // Do not write back entered characters to the console
39 #endif
40 
41  e(GCmd(g, "ST")); // stop all motors
42  e(GMotionComplete(g, "A")); //Wait for motion to complete
43  e(GCmd(g, "SHA")); // Set servo here
44  e(GCmd(g, "DPA=0")); // Start position at absolute zero
45  e(GCmd(g, "JGA=0")); // Start jogging with 0 speed
46  e(GCmd(g, "BGA")); // Begin motion on A Axis
47 
48  int isJogging = 1;
49  int speed = 0;
50 
51  char instructions[] = "Enter a character on the keyboard to change the"
52  " motor's speed:\n<q> Quit\n<a> -2000 counts/s\n"
53  "<s> -500 counts/s\n<d> +500 counts/s\n<f> "
54  "+2000 counts/s\n<r> Direction Reversal\n";
55 
56 #ifdef _WIN32
57  cout << instructions;
58 #elif __linux__
59  printw(instructions); //Print instructions to console
60 #endif
61 
62  while (isJogging)
63  {
64  sprintf(buf, "JGA=%d", speed);
65  e(GCmd(g, buf)); // Set speed
66 #ifdef _WIN32
67  cout << "Jog Speed: " << speed << "\n";
68  switch (_getch()) //Capture keypress
69 #elif __linux__
70  sprintf(buf, "Jog Speed: %d\n", speed);
71  printw(buf); //Print speed to console
72  switch (getch()) //Capture keypress
73 #endif
74  {
75  case 'q': //Quit Jogging
76  isJogging = 0;
77  break;
78  case 'a': //Large speed change in negative direction
79  speed -= 2000;
80  break;
81  case 's': //Small speed change in negative direction
82  speed -= 500;
83  break;
84  case 'd': //Small speed change in positive direction
85  speed += 500;
86  break;
87  case 'f': //Large speed change in positive direction
88  speed += 2000;
89  break;
90  case 'r': //Reverse direction of speed
91  speed *= -1;
92  break;
93  }
94  }
95 
96 #if __linux__
97  endwin(); //Restores terminal to previous state
98 #endif
99  e(GCmd(g, "ST")); // Stop all motors
100  e(GMotionComplete(g, "A")); //Wait for motion to complete
101 
102  return GALIL_EXAMPLE_OK;
103 }
GReturn jog(GCon g)
Puts controller into Jog Mode and accepts user input to adjust the speed.
Definition: jog.cpp:25
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: examples.h:22