Newer
Older
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//system include
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
//user includes
#include "joystick.h"
#include "frontend_common.h"
#include "frontend_tracker.h"
#include "frontend_param.h"
//Path to the joystick device
#define JOYSTICK_PATH "/dev/input/js0"
//Variables used to calculate the allowed setpoint change per iteration of the main loop
#define WAIT_TIME_USEC 5000
#define METER_P_SEC 0.66f
#define SETPOINT_THRESHOLD (((float) WAIT_TIME_USEC) / 1000000) * METER_P_SEC
//Variables used to update setpoints through the frontend
#define QUAD_X 9
#define QUAD_Y 10
#define QUAD_Alt 11
#define QUAD_Yaw 12
#define PARAM 0
//Variables that define the bounds that the user can fly between
#define MAX_Y 1.2f
#define MIN_Y -1.2f
#define MAX_X 1.5f
#define MIN_X -1.5f
#define MAX_Alt -0.7f
#define MIN_Alt -1.5f
#define MAX_Yaw 160f
#define MIN_Yaw -160f
#define DELTA 0.05f
#define MIDPOINT 0.5f
//Checks whether a value from the joystick is recognized as a midpoint and hence the quad will not move
#define isMidPoint(value) (value < (MIDPOINT + DELTA) && value > (MIDPOINT - DELTA)) ? 1 : 0
int exit_flag = 0;
//Connection to backend
struct backend_conn *conn = NULL;
/**
* Handler function for exit signals. This allows the backend to properly close.
*/
static void sig_exit_handler(int s) {
printf("Exiting with condition %d\n\r", s);
exit_flag = 1;
}
//Adjusts the setpoints so that there is not a jump once the value is not recognized
float adjustJoystickValue(float value) {
if (value > 0.5f) {
return value - DELTA;
}
return value + DELTA;
}
int main(int argc, char *argv[]) {
//Pointer to joystick object
struct joystick *js;
//Struct containing VRPN information
struct frontend_tracker_data td;
//Struct containing setpoint update information for frontend
struct frontend_param_data pd;
//Whether or not the quad has taken off
int takeoff = 0;
//Temporary status variable used to hold return values
int status = 0;
//Whether or not we have gotten the current position of the quad upon start of user control
int initialize_setpoint = 0;
//Joystick channel values
float js_throttle, js_yaw, js_pitch, js_roll, js_left_switch, js_right_switch;
//Quad setpoint related variables (Note - quad_Alt is Z)
float quad_X, quad_Y, quad_Alt, quad_yaw;
float temp_setpoint = 0;
//Quad setpoint changed variables
int quad_X_modified, quad_Y_modified, quad_Alt_modified, quad_yaw_modified;
quad_X_modified = quad_Y_modified = quad_Alt_modified = quad_yaw_modified = 0;
//Setup exit signals
signal(SIGINT, sig_exit_handler);
signal(SIGABRT, sig_exit_handler);
signal(SIGQUIT, sig_exit_handler);
signal(SIGTERM, sig_exit_handler);
//Initialize joystick
js = joystick_init(JOYSTICK_PATH, JS_NONBLOCKING);
if (!js) {
printf("Aborting because joystick did not initialize correctly...\n");
return 1;
}
//Prints out info about the device connected
joystick_info(js);
//Create a connection to the backend
conn = ucart_backendConnect();
if (conn == NULL) {
printf("Failed to connect to backend, exitting...\r\n");
joystick_destroy(js);
return 1;
}
printf("Manual Assist Mode Starting...\r\n");
printf("Setpoint Threshold = %f\r\n", SETPOINT_THRESHOLD);
while (!exit_flag) {
//Read to update values
if (joystick_read(js) == 1) {
//Get values from joystick
js_throttle = joystick_get_throttle(js);
//NOTE - yaw control is not yet supported
js_yaw = joystick_get_yaw(js);
js_pitch = joystick_get_pitch(js);
js_roll = joystick_get_roll(js);
js_left_switch = joystick_get_left_switch(js);
js_right_switch = joystick_get_right_switch(js);
//Print debug info using the left switch on the controller
if (js_left_switch > 0.5f) {
printf("Throttle: %f\r\n", js_throttle);
printf("Yaw: %f\r\n", js_yaw);
printf("Pitch: %f\r\n", js_pitch);
printf("Roll: %f\r\n", js_roll);
printf("Left Switch: %f\r\n", js_left_switch);
printf("Right Switch: %f\r\n", js_right_switch);
}
//Right switch controls takeoff and touch down
if (!takeoff && js_right_switch > .5) {
//TODO - may not want to use system calls...
//call takeoff script
printf("TAKING OFF...\r\n");
status = system("./scripts/take_off.sh");
if (status == -1) {
printf("System call failed...\r\n");
exit_flag = 1;
}
takeoff = 1;
usleep(5000000);
printf("You now have control...\r\n");
} else if (takeoff && js_right_switch < .5) {
//TODO - may not want to use system calls...
//call touchdown script
printf("LANDING QUAD...\r\n");
status = system("./scripts/touch_down.sh");
if (status == -1) {
printf("System call failed...\r\n");
exit_flag = 1;
}
takeoff = 0;
usleep(5000000);
initialize_setpoint = 0;
quad_X_modified = quad_Y_modified = quad_Alt_modified = quad_yaw_modified = 0;
printf("You have control, but you must takeoff first...\r\n");
}
if (takeoff) {
if (!initialize_setpoint) {
//grab setpoint
initialize_setpoint = 1;
status = frontend_track(conn, &td);
if (status) {
exit_flag = 1;
} else {
quad_X = td.longitudinal;
quad_Y = td.lateral;
quad_Alt = td.height;
quad_yaw = td.yaw;
}
} else {
//check all joystick values to make sure that they are not in the middle
if (!isMidPoint(js_throttle)) {
js_throttle = adjustJoystickValue(js_throttle);
quad_Alt_modified = 1;
temp_setpoint = -(js_throttle - 0.5f);
quad_Alt += temp_setpoint * SETPOINT_THRESHOLD;
if (quad_Alt < MIN_Alt) {
quad_Alt = MIN_Alt;
} else if (quad_Alt > MAX_Alt) {
quad_Alt = MAX_Alt;
}
}
if (!isMidPoint(js_pitch)) {
js_pitch = adjustJoystickValue(js_pitch);
quad_X_modified = 1;
temp_setpoint = (js_pitch - 0.5f);
quad_X += temp_setpoint * SETPOINT_THRESHOLD;
if (quad_X < MIN_X) {
quad_X = MIN_X;
} else if (quad_X > MAX_X) {
quad_X = MAX_X;
}
}
if (!isMidPoint(js_roll)) {
js_roll = adjustJoystickValue(js_roll);
quad_Y_modified = 1;
temp_setpoint = (js_roll - 0.5f);
quad_Y += temp_setpoint * SETPOINT_THRESHOLD;
if (quad_Y < MIN_Y) {
quad_Y = MIN_Y;
} else if (quad_Y > MAX_Y) {
quad_Y = MAX_Y;
}
}
//if (!isMidPoint(js_yaw)) {
//TODO - not yet supported
//js_yaw = adjustJoystickValue(js_yaw);
//quad_yaw_modified = 1;
//temp_setpoint = (js_yaw - 0.5f);
//quad_yaw += temp_setpoint * SETPOINT_THRESHOLD;
//if (quad_yaw < MIN_Yaw) {
// quad_yaw = MIN_Yaw;
//} else if (quad_yaw > MAX_Yaw) {
// quad_yaw = MAX_Yaw;
//}
//}
//check if any of the setpoints have been changed,
//if so send a message to backend
if (quad_Alt_modified) {
pd.block = QUAD_Alt;
pd.param = PARAM;
pd.value = quad_Alt;
status = frontend_setparam(conn, &pd);
if (status) {
printf("frontend_setparam failed...\r\n");
exit_flag = 1;
}
}
if (quad_X_modified) {
pd.block = QUAD_X;
pd.param = PARAM;
pd.value = quad_X;
status = frontend_setparam(conn, &pd);
if (status) {
printf("frontend_setparam failed...\r\n");
exit_flag = 1;
}
}
if (quad_Y_modified) {
pd.block = QUAD_Y;
pd.param = PARAM;
pd.value = quad_Y;
status = frontend_setparam(conn, &pd);
if (status) {
printf("frontend_setparam failed...\r\n");
exit_flag = 1;
}
}
//if (quad_yaw_modified) {
//TODO - not yet supported
//pd.block = QUAD_Yaw;
//pd.param = PARAM;
//pd.value = quad_yaw;
//status = frontend_setparam(conn, &pd);
//if (status) {
// printf("frontend_setparam failed...\r\n");
// exit_flag = 1;
//}
//}
quad_X_modified = quad_Y_modified = quad_Alt_modified = quad_yaw_modified = 0;
}
}
} else {
printf("Aborting because joystick could not be read...\r\n");
exit_flag = 1;
}
usleep(5000);
}
printf("Manual Assist Mode Exiting...\r\n");
//If quad is still in the air when exitting, make sure to land it
if (takeoff) {
printf("LANDING QUAD...\r\n");
status = system("./scripts/touch_down.sh");
if (status == -1) {
printf("System call failed...\r\n");
exit_flag = 1;
}
takeoff = 0;
usleep(5000000);
}
//Cleanup joystick
joystick_destroy(js);
//Cleanup backend connection
if (conn) {
ucart_backendDisconnect(conn);
conn = NULL;
}
return 0;
}