| gclib 1.35.5 Communications API for Galil controllers and PLCs |

jog.cpp
Go to the documentation of this file.
1
8 #include "examples.h"
9
10 #ifdef _WIN32
11 #include <conio.h>
12 #elif __linux__
13 #include <ncurses.h>
14 #endif
15
16 #include <iostream> //std::cout
17 using namespace std;
18
29 GReturn jog(GCon g)
30 {
31 char buf[G_SMALL_BUFFER]; //traffic buffer
32 if (g == 0) //Bad connection
33 {
34 cerr << "There was an error with the connection." << endl;
35 return G_CONNECTION_NOT_ESTABLISHED;
36 }
37
38 #ifdef __linux__
39 //These functions set up the ncurses library to capture keyboard input
40 initscr(); // Initialization of ncurses library
41 cbreak(); // Capture one character at a time
42 noecho(); // Do not write back entered characters to the console
43 #endif
44
45 e(GCmd(g, "ST")); // stop all motors
46 e(GMotionComplete(g, "A")); //Wait for motion to complete
47 e(GCmd(g, "SHA")); // Set servo here
48 e(GCmd(g, "DPA=0")); // Start position at absolute zero
49 e(GCmd(g, "JGA=0")); // Start jogging with 0 speed
50 e(GCmd(g, "BGA")); // Begin motion on A Axis
51
52 int isJogging = 1;
53 int speed = 0;
54
55 char instructions[] = "Enter a character on the keyboard to change the"
56 " motor's speed:\n<q> Quit\n<a> -2000 counts/s\n"
57 "<s> -500 counts/s\n<d> +500 counts/s\n<f> "
58 "+2000 counts/s\n<r> Direction Reversal\n";
59
60 #ifdef _WIN32
61 cout << instructions;
62 #elif __linux__
63 printw(instructions); //Print instructions to console
64 #endif
65
66 while (isJogging)
67 {
68 sprintf(buf, "JGA=%d", speed);
69 e(GCmd(g, buf)); // Set speed
70 #ifdef _WIN32
71 cout << "Jog Speed: " << speed << "\n";
72 switch (_getch()) //Capture keypress
73 #elif __linux__
74 sprintf(buf, "Jog Speed: %d\n", speed);
75 printw(buf); //Print speed to console
76 switch (getch()) //Capture keypress
77 #endif
78 {
79 case 'q': //Quit Jogging
80 isJogging = 0;
81 break;
82 case 'a': //Large speed change in negative direction
83 speed -= 2000;
84 break;
85 case 's': //Small speed change in negative direction
86 speed -= 500;
87 break;
88 case 'd': //Small speed change in positive direction
89 speed += 500;
90 break;
91 case 'f': //Large speed change in positive direction
92 speed += 2000;
93 break;
94 case 'r': //Reverse direction of speed
95 speed *= -1;
96 break;
97 }
98 }
99
100 #if __linux__
101 endwin(); //Restores terminal to previous state
102 #endif
103 e(GCmd(g, "ST")); // Stop all motors
104 e(GMotionComplete(g, "A")); //Wait for motion to complete
105
106 return GALIL_EXAMPLE_OK;
107 }
examples.h
GMotionComplete
GCLIB_DLL_EXPORTED GReturn GCALL GMotionComplete(GCon g, GCStringIn axes)
Blocking call that returns once all axes specified have completed their motion.
Definition: gclibo.c:300
GCmd
GCLIB_DLL_EXPORTED GReturn GCALL GCmd(GCon g, GCStringIn command)
Wrapper around GCommand for use when the return value is not desired.
Definition: gclibo.c:237
GReturn
int GReturn
Every function returns a value of type GReturn. See gclib_errors.h for possible values.
Definition: gclib.h:93
G_SMALL_BUFFER
#define G_SMALL_BUFFER
Most reads from Galil are small. This value will easily hold most, e.g. TH, TZ, etc.
Definition: gclib.h:89
GCon
void * GCon
Connection handle. Unique for each connection in process. Assigned a non-zero value in GOpen().
Definition: gclib.h:94
G_CONNECTION_NOT_ESTABLISHED
#define G_CONNECTION_NOT_ESTABLISHED
Function was called with no connection.
Definition: gclib_errors.h:61
e
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.
Definition: examples.h:33
jog
GReturn jog(GCon g)
Puts controller into Jog Mode and accepts user input to adjust the speed.
Definition: jog.cpp:29