-
Notifications
You must be signed in to change notification settings - Fork 0
/
gb_standalone.c
255 lines (218 loc) · 6.61 KB
/
gb_standalone.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*############################################################################
# Title: gb-standalone.c
# Author: Chris Johnson
# Date: 9/3/19
# Description: standalone functionality for guide box
#
#
#############################################################################*/
/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/time.h>
#include <time.h>
#include "gb_commands.h"
void domessage(char *av0, char *message);
void displayTelem(int ttyfd);
/*############################################################################
# Title: main
# Author: C.Johnson
# Date: 9/4/19
# Args: (int argc, char ** argv)
# Description: uses command line options to figure out how to run the program
#
#############################################################################*/
int main(int argc, char ** argv)
{
char strPort[100], strAxis[20];
int doinit=0, dohome=0, domove=0, dohelp=0, reset_ltx=0;
int value=0, opt, iaxis, ttyfd, isFilter=0, test, isnet=0;
int axisinit=0, portinit=0, valinit=0, dotelem=0;
char resp[10000];
while ((opt = getopt(argc, argv, "p:ihLmta:tv:?")) != -1)
{
switch (opt)
{
case 'p': //Set Port
sprintf(strPort, "%s", optarg);
portinit = 1;
break;
case 'n': //set as network
isnet = 1;
case 'i': //Initialize
doinit = 1;
break;
case 'h': //home
dohome = 1;
break;
case 'L': //reset lantronix
reset_ltx = 1;
break;
case 'm': //move
domove = 1;
break;
case 'a': //Set Axis
sprintf(strAxis, "%s", optarg);
axisinit = 1;
break;
case 'v': //Set Value
value=atoi(optarg);
valinit = 1;
break;
case 't': //get telemetry
dotelem = 1;
break;
case '?': //HELP!!!
dohelp=1;
break;
default: /* '?' */
domessage(argv[0], NULL);
exit(EXIT_FAILURE);
}
}
//print the help
if (dohelp)
{
domessage(argv[0], "lookin for help?");
}
//no port specified... do not continue
if(!portinit)
{
domessage(argv[0], "ERROR!!! Must specify valid port");
}
if(reset_ltx)
{
fprintf(stderr, "\nOpening Port %s\n", strPort);
lantronix_reset(strPort);
}
//check and see if port opens
if(!isnet)
ttyfd = ttyOpen( strPort );
else
ttyfd = net_ttyOpen( strPort );
if(ttyfd == -1)
{
fprintf(stderr, "\nError Opening Port %s\n", strPort);
exit(EXIT_FAILURE);
}
//initialize motor chain on [port]
if (doinit)
{
printf("INITIALIZING!!!\n");
guider_init( ttyfd );
ttyClose( ttyfd );
exit(0);
}
//grab telemetry
if(dotelem)
{
displayTelem(ttyfd);
ttyClose( ttyfd );
exit(0);
}
//validate [axis] before we proceed
if(!axisinit)
{
ttyClose( ttyfd );
domessage(argv[0], "ERROR!!! Must specify valid axis");
}
iaxis = validateAxis(strAxis, &isFilter);
if(!iaxis)
{
ttyClose( ttyfd );
domessage(argv[0], "ERROR!!! invalid axis");
}
//home axis [axis]
else if (dohome)
{
printf("HOMING %s!!!\n", strAxis);
stageHome( ttyfd, strAxis );
ttyClose( ttyfd );
exit(0);
}
//move axis [axis] to position [value]
else if (domove)
{
if((!axisinit)||(!valinit))
{
ttyClose( ttyfd );
domessage(argv[0], "ERROR!!! Must specify valid axis and value");
}
printf("MOVING %s to %i!!!\n", strAxis, value);
test=stageGoTo(ttyfd, strAxis, value);
printf("moved stage %i\n", test);
}
else
{
domessage(argv[0], "ERROR!!! Unknown Command");
}
ttyClose( ttyfd );
}
/*############################################################################
# Title: displayTelemetry(char *av0, char *message)
# Author: C.Johnson
# Date: 9/4/19
# Args: char *av0 = pointer to name of command issued to start program
# char *message = optional message to append to beginning
# Description: prints the help message
#
#############################################################################*/
void displayTelem(int ttyfd)
{
MSTATUS allmotors[7];
int ix,ix2;
doTelemetry(ttyfd, allmotors, 1);
for(ix=0;ix<7;ix++)
{
printf("******Stage %i*******\n", ix);
printf("\tactive=%i\n",allmotors[ix].isActive);
for(ix2=0;ix2<4;ix2++)
{
printf("\t\twords[%i]=%i\n", ix2, allmotors[ix].words[ix2]);
}
printf("\tuserbits=%i\n", allmotors[ix].userbits);
printf("\tmotor_num=%i\n", allmotors[ix].motor_num);
printf("\tpos=%i\n", allmotors[ix].pos);
printf("\tname=%s\n", allmotors[ix].name);
printf("\tfnum=%i\n", allmotors[ix].fnum);
}
}
/*############################################################################
# Title: domessage(char *av0, char *message)
# Author: C.Johnson
# Date: 9/4/19
# Args: char *av0 = pointer to name of command issued to start program
# char *message = optional message to append to beginning
# Description: prints the help message
#
#############################################################################*/
void domessage(char *av0, char *message)
{
if(message != NULL)
fprintf(stderr, "\n%s\n", message);
fprintf(stderr, "\nVATT Guide Box Command Interface v.42\n");
fprintf(stderr, "code name: Don't Panic!\n\n");
fprintf(stderr, "Usage: %s -p[port] -m -a[axis] -v[value]\n",av0);
fprintf(stderr, " -p: set port to [port] exe. /dev/ttyUSB0\n");
fprintf(stderr, " -i: initialize [ignores all other arguments]\n");
fprintf(stderr, " -h: home axis [axis]\n");
fprintf(stderr, " -m: move axis [axis]to value [value]\n");
fprintf(stderr, " -a: set axis to [axis] exe. FWHEEL_LOWER\n");
fprintf(stderr, " -v: set axis value to [value] exe. 200\n");
fprintf(stderr, " -t: get telemetry data\n");
fprintf(stderr, " -?: print this help message\n");
fprintf(stderr, "\nValid Axis:\n");
fprintf(stderr, " OFFSET_X\n");
fprintf(stderr, " OFFSET_Y\n");
fprintf(stderr, " OFFSET_FOCUS\n");
fprintf(stderr, " OFFSET_MIRRORS\n");
fprintf(stderr, " OFFSET_FWHEEL\n");
fprintf(stderr, " FWHEEL_LOWER\n");
fprintf(stderr, " FWHEEL_UPPER\n");
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
}