-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalibrate.c
146 lines (103 loc) · 4.54 KB
/
calibrate.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
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include "config.h"
#include "servo.h"
#include "sound.h"
static FILE *param;
static int wait_button(char *prompt) {
int i = 0;
pthread_t thread;
if(prompt != NULL) {
// free()'d by play_file...
if(pthread_create(&thread, NULL, (void *)play_file, (void *)strdup(prompt))) {
fprintf(stderr,"ERROR: ERROR PLAYING SOUND FOR CALIBRATION EVENT: %s\n", prompt);
return 0;
}
pthread_detach(thread);
}
while(servo_limit1(RIGHT_SERVO)) {
usleep(500);
if(i > 500)
return 0;
i++;
}
while(! servo_limit1(RIGHT_SERVO))
usleep(500);
return 1;
}
void manual_calibration() {
fprintf(stderr,"ACTION: MOVE PUCK TO UPPER LEFT CORNER. PRESS CALIBRATE KEY WHEN DONE.\n");
while(! wait_button("/exhibit/sounds/upper_left.wav"));
float left_top_x = X(SERVO_HOME_IN + TICKS_TO_INCHES(servo_position(LEFT_SERVO), INCHES_PER_REV_LEFT), SERVO_HOME_IN + TICKS_TO_INCHES(servo_position(RIGHT_SERVO), INCHES_PER_REV_RIGHT));
float left_top_y = Y(SERVO_HOME_IN + TICKS_TO_INCHES(servo_position(LEFT_SERVO), INCHES_PER_REV_LEFT), SERVO_HOME_IN + TICKS_TO_INCHES(servo_position(RIGHT_SERVO), INCHES_PER_REV_RIGHT));
fprintf(stderr,"ACTION: MOVE PUCK TO LOWER LEFT CORNER. PRESS CALIBRATE KEY WHEN DONE.\n");
while(! wait_button("/exhibit/sounds/lower_left.wav"));
// float left_bottom_x = X(SERVO_HOME_IN + TICKS_TO_INCHES(servo_position(LEFT_SERVO), INCHES_PER_REV_LEFT), SERVO_HOME_IN + TICKS_TO_INCHES(servo_position(RIGHT_SERVO), INCHES_PER_REV_RIGHT));
float left_bottom_y = Y(SERVO_HOME_IN + TICKS_TO_INCHES(servo_position(LEFT_SERVO), INCHES_PER_REV_LEFT), SERVO_HOME_IN + TICKS_TO_INCHES(servo_position(RIGHT_SERVO), INCHES_PER_REV_RIGHT));
fprintf(stderr,"ACTION: MOVE PUCK TO UPPER RIGHT CORNER. PRESS CALIBRATE KEY WHEN DONE.\n");
while(! wait_button("/exhibit/sounds/upper_right.wav"));
float right_top_x = X(SERVO_HOME_IN + TICKS_TO_INCHES(servo_position(LEFT_SERVO), INCHES_PER_REV_LEFT), SERVO_HOME_IN + TICKS_TO_INCHES(servo_position(RIGHT_SERVO), INCHES_PER_REV_RIGHT));
// float right_top_y = Y(SERVO_HOME_IN + TICKS_TO_INCHES(servo_position(LEFT_SERVO), INCHES_PER_REV_LEFT), SERVO_HOME_IN + TICKS_TO_INCHES(servo_position(RIGHT_SERVO), INCHES_PER_REV_RIGHT));
MAP_WIDTH_IN = right_top_x - left_top_x;
MAP_HEIGHT_IN = left_top_y - left_bottom_y;
MAP_OFFSET_TOP = WORKSPACE_HEIGHT_IN - left_top_y;
MAP_OFFSET_BOTTOM = left_bottom_y;
MAP_OFFSET_LEFT = left_top_x;
MAP_OFFSET_RIGHT = WORKSPACE_WIDTH_IN - right_top_x;
fprintf(stderr,"NOTICE: SAVING MAP PARAMETERS TO DISK...");
param = fopen("calibrate.dat", "w+");
if(param == NULL)
fprintf(stderr, "FAILED.\n");
else {
fprintf(param, "%f\n", MAP_WIDTH_IN);
fprintf(param, "%f\n", MAP_HEIGHT_IN);
fprintf(param, "%f\n", MAP_OFFSET_TOP);
fprintf(param, "%f\n", MAP_OFFSET_BOTTOM);
fprintf(param, "%f\n", MAP_OFFSET_LEFT);
fprintf(param, "%f\n", MAP_OFFSET_RIGHT);
fflush(param);
fclose(param);
fprintf(stderr,"DONE.\n");
}
return;
}
void calibrate() {
char buffer[1024];
fprintf(stderr,"ACTION: MOVE PUCK TO HOME POSITION. PRESS CALIBRATE KEY WHEN DONE.\n");
while(! wait_button("/exhibit/sounds/center.wav"));
servo_resetposition(LEFT_SERVO);
servo_resetposition(RIGHT_SERVO);
param = fopen("calibrate.dat", "r");
if(param == NULL) {
play_file(strdup("/exhibit/sounds/recalibrate.wav"));
sleep(1);
fprintf(stderr,"NOTICE: NO CALIBRATION FILE WAS FOUND. MANUAL CALIBRATION WILL NOW OCCUR.\n");
manual_calibration();
} else {
fprintf(stderr,"NOTICE: A CALIBRATION FILE WAS FOUND. USING PARAMETERS FOUND FROM LAST CALIBRATION...\n");
fgets(buffer, 1024, param);
MAP_WIDTH_IN = atof(buffer);
fgets(buffer, 1024, param);
MAP_HEIGHT_IN = atof(buffer);
fgets(buffer, 1024, param);
MAP_OFFSET_TOP = atof(buffer);
fgets(buffer, 1024, param);
MAP_OFFSET_BOTTOM = atof(buffer);
fgets(buffer, 1024, param);
MAP_OFFSET_LEFT = atof(buffer);
fgets(buffer, 1024, param);
MAP_OFFSET_RIGHT = atof(buffer);
fclose(param);
}
fprintf(stderr,"NOTICE: MAP WIDTH=%f\nNOTICE: MAP HEIGHT=%f\nNOTICE: TOP OFFSET=%f\nNOTICE: BOTTOM OFFSET=%f\nNOTICE: LEFT OFFSET=%f\nNOTICE: RIGHT OFFSET=%f\n", MAP_WIDTH_IN, MAP_HEIGHT_IN, MAP_OFFSET_TOP, MAP_OFFSET_BOTTOM, MAP_OFFSET_LEFT, MAP_OFFSET_RIGHT);
servo_init();
servo_move(LEFT_SERVO, 0, SERVO_MAX_VEL_SLEW);
servo_move(RIGHT_SERVO, 0, SERVO_MAX_VEL_SLEW);
while(servo_moving(LEFT_SERVO) || servo_moving(RIGHT_SERVO))
usleep(500);
return;
}