17 |
17 |
#include <string.h>
|
18 |
18 |
#include <wireless.h>
|
19 |
19 |
|
20 |
|
#define TARGET_POSITION_STOP_DISTANCE_THRESHOLD (15)
|
|
20 |
#define TARGET_POSITION_STOP_DISTANCE_THRESHOLD (40.0)
|
21 |
21 |
|
22 |
22 |
typedef struct {
|
23 |
23 |
unsigned char msgId; //is this necessary?
|
... | ... | |
27 |
27 |
/* Globals (internal) */
|
28 |
28 |
static UserHandler user_handlers[USER_DEFINED_MSG_TOTAL];
|
29 |
29 |
|
30 |
|
static int robot_x, robot_y;
|
|
30 |
static unsigned robot_x, robot_y;
|
31 |
31 |
static volatile int updated_robot_pos_ready;
|
32 |
|
#define ERROR_MAG_TO_MOTOR_DIFFENTIAL_CONST (80)
|
|
32 |
#define ERROR_MAG_TO_MOTOR_DIFFENTIAL_CONST (1)
|
33 |
33 |
|
34 |
34 |
/* Internal function prototypes */
|
35 |
|
static unsigned int two_bytes_to_int(char high, char low);
|
|
35 |
static unsigned two_bytes_to_int(unsigned char high, unsigned char low);
|
36 |
36 |
static void colonet_handle_receive(char type, int source, unsigned char* packet, int length);
|
37 |
|
static void move_to_position_routine(int x, int y);
|
|
37 |
static void move_to_position_routine(unsigned x, unsigned y);
|
38 |
38 |
|
39 |
39 |
static PacketGroupHandler colonet_pgh;
|
40 |
40 |
|
|
41 |
/* two_bytes_to_int(char a, char b)
|
|
42 |
* Returns int of form [high][low]
|
|
43 |
*/
|
|
44 |
static unsigned int two_bytes_to_int(unsigned char high, unsigned char low) {
|
|
45 |
return (high<<8) | low;
|
|
46 |
}
|
|
47 |
|
41 |
48 |
/* Public functions */
|
42 |
49 |
int colonet_init() {
|
43 |
50 |
colonet_pgh.groupCode = COLONET_PACKET_GROUP_ID;
|
... | ... | |
77 |
84 |
}
|
78 |
85 |
|
79 |
86 |
void request_abs_position() {
|
80 |
|
usb_puts("requesting_abs_position\n");
|
|
87 |
//usb_puts("requesting_abs_position\n");
|
81 |
88 |
|
82 |
89 |
ColonetRobotServerPacket pkt;
|
83 |
90 |
pkt.client_id = -1;
|
... | ... | |
85 |
92 |
wl_send_global_packet(colonet_pgh.groupCode, 0, (char*)&pkt, sizeof(pkt), 0);
|
86 |
93 |
}
|
87 |
94 |
|
88 |
|
static int distsquared(int x1, int y1,int x2, int y2) {
|
89 |
|
return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
|
|
95 |
static float sqrt_approx(float x) {
|
|
96 |
float x2 = x*x;
|
|
97 |
float x3 = x*x2;
|
|
98 |
|
|
99 |
return 0.00014*x3 - 0.0078*x2 + 0.29*x + 0.84;
|
90 |
100 |
}
|
91 |
101 |
|
|
102 |
static float dist(float x1, float y1, float x2, float y2) {
|
|
103 |
return sqrt_approx((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
|
|
104 |
}
|
|
105 |
|
92 |
106 |
/* cubic approximation of arctan. */
|
93 |
107 |
/*static void arctan(float t) {
|
94 |
108 |
float t3 = t*t*t;
|
... | ... | |
116 |
130 |
motor2_set(motor2_dir, ml);
|
117 |
131 |
}
|
118 |
132 |
|
119 |
|
static float sqrt_approx(float x) {
|
120 |
|
float x2 = x*x;
|
121 |
|
float x3 = x*x2;
|
|
133 |
static void move_to_position_routine(unsigned target_x, unsigned target_y) {
|
|
134 |
usb_puts("move to absolute position routine!\n");
|
|
135 |
|
|
136 |
updated_robot_pos_ready = 0;
|
|
137 |
request_abs_position(); // While we're doing this computation, server can be reporting next position.
|
122 |
138 |
|
123 |
|
return 0.00014*x3 - 0.0078*x2 + 0.29*x + 0.84;
|
124 |
|
}
|
|
139 |
int count = 0;
|
|
140 |
while (!updated_robot_pos_ready) {
|
|
141 |
wl_do();
|
|
142 |
if (count++ == 5000) { // in case the server missed it...
|
|
143 |
request_abs_position();
|
|
144 |
count = 0;
|
|
145 |
}
|
|
146 |
}
|
125 |
147 |
|
126 |
|
static void move_to_position_routine(int target_x, int target_y) {
|
127 |
|
int last_x = robot_x, last_y = robot_y;
|
|
148 |
// usb_puts("got past first loop.\n");
|
128 |
149 |
|
|
150 |
unsigned last_x = robot_x, last_y = robot_y;
|
|
151 |
|
129 |
152 |
char buf[40];
|
130 |
|
sprintf(buf, "cur dist is %d\n", distsquared(robot_x, robot_y, target_x, target_y));
|
131 |
|
usb_puts(buf);
|
132 |
|
sprintf(buf, "radius squared is %d\n", TARGET_POSITION_STOP_DISTANCE_THRESHOLD*TARGET_POSITION_STOP_DISTANCE_THRESHOLD);
|
133 |
|
usb_puts(buf);
|
|
153 |
//sprintf(buf, "cur dist is %f\n", dist((float)robot_x, (float)robot_y, (float)target_x, (float)target_y));
|
|
154 |
//usb_puts(buf);
|
|
155 |
//sprintf(buf, "radius squared is %f\n", TARGET_POSITION_STOP_DISTANCE_THRESHOLD);
|
|
156 |
//usb_puts(buf);
|
134 |
157 |
|
135 |
|
while (distsquared(robot_x, robot_y, target_x, target_y) >
|
136 |
|
TARGET_POSITION_STOP_DISTANCE_THRESHOLD*TARGET_POSITION_STOP_DISTANCE_THRESHOLD) {
|
|
158 |
// usb_puts("entering while loop.\n");
|
|
159 |
|
|
160 |
while (dist(robot_x, robot_y, target_x, target_y) > TARGET_POSITION_STOP_DISTANCE_THRESHOLD) {
|
|
161 |
|
|
162 |
// usb_puts("inside while loop.\n");
|
|
163 |
|
|
164 |
updated_robot_pos_ready = 0;
|
137 |
165 |
request_abs_position(); // While we're doing this computation, server can be reporting next position.
|
138 |
166 |
|
139 |
|
int count = 0;
|
140 |
|
while (!updated_robot_pos_ready) { // ghetto condition variable
|
141 |
|
wl_do();
|
|
167 |
// usb_puts("after request_abs_position.\n");
|
142 |
168 |
|
143 |
|
if (count++ == 10000) { // in case the server missed it...
|
144 |
|
request_abs_position();
|
145 |
|
count = 0;
|
146 |
|
}
|
147 |
|
}
|
148 |
|
|
149 |
169 |
int cur_robot_x = robot_x;
|
150 |
170 |
int cur_robot_y = robot_y;
|
151 |
171 |
|
|
172 |
//usb_puts("after cur_robot_x/y = robot_x/y.\n");
|
|
173 |
|
|
174 |
|
|
175 |
int r_x = target_x - cur_robot_x;
|
|
176 |
int r_y = target_y - cur_robot_y;
|
|
177 |
|
|
178 |
int v_x = cur_robot_x - last_x;
|
|
179 |
int v_y = cur_robot_y - last_y;
|
|
180 |
|
|
181 |
int e_x = r_x - v_x;
|
|
182 |
int e_y = r_y - v_y;
|
|
183 |
|
|
184 |
sprintf(buf, "vx:%d vy:%d rx:%d ry:%d ex:%d ey:%d\n", v_x, v_y, r_x, r_y, e_x, e_y);
|
|
185 |
usb_puts(buf);
|
|
186 |
|
|
187 |
int e_mag = e_x*e_x + e_y*e_y;
|
|
188 |
|
|
189 |
int motor_differential = e_mag;// / 100;
|
|
190 |
|
|
191 |
/*
|
152 |
192 |
sprintf(buf, "Current position: %d %d\n", cur_robot_x, cur_robot_y);
|
153 |
193 |
usb_puts(buf);
|
154 |
194 |
|
155 |
195 |
sprintf(buf, "Target position: %d %d\n", target_x, target_y);
|
156 |
196 |
usb_puts(buf);
|
157 |
197 |
|
158 |
|
updated_robot_pos_ready = 0;
|
159 |
|
request_abs_position(); // While we're doing this computation, server can be reporting next position.
|
160 |
|
|
161 |
198 |
float r_x = (float)target_x - cur_robot_x;
|
162 |
199 |
float r_y = (float)target_y - cur_robot_y;
|
163 |
200 |
float r_mag = sqrt_approx(r_x * r_x + r_y * r_y);
|
... | ... | |
177 |
214 |
sprintf(buf, "Error: <%f,%f>; mag: %f\n", e_x, e_y, e_mag);
|
178 |
215 |
usb_puts(buf);
|
179 |
216 |
|
180 |
|
/* Motor differential proportional to magnitude of directional error. */
|
181 |
|
int motor_differential = (int)(e_mag * ERROR_MAG_TO_MOTOR_DIFFENTIAL_CONST);
|
|
217 |
// Motor differential proportional to magnitude of directional error.
|
|
218 |
// int motor_differential = (int)(e_mag * ERROR_MAG_TO_MOTOR_DIFFENTIAL_CONST);
|
|
219 |
*/
|
|
220 |
|
182 |
221 |
|
183 |
|
/* Determine left or right by transforming error vector to robot axes */
|
|
222 |
int p_x = v_y;
|
|
223 |
int p_y = -v_x;
|
|
224 |
|
|
225 |
int mat[2][2];
|
|
226 |
mat[0][0] = v_y;
|
|
227 |
mat[0][1] = -v_x;
|
|
228 |
|
|
229 |
int e_trans_x = mat[0][0] * e_x + mat[0][1] * e_y;
|
|
230 |
|
|
231 |
/*
|
|
232 |
// Determine left or right by transforming error vector to robot axes
|
184 |
233 |
// Perpendicular
|
185 |
234 |
float p_x = v_y;
|
186 |
235 |
float p_y = -v_x;
|
187 |
236 |
|
188 |
|
/* Inverse matrix */
|
|
237 |
// Inverse matrix
|
189 |
238 |
float coeff = 1.0 / (p_x * v_y - v_x * p_y);
|
190 |
239 |
float mat[2][2];
|
191 |
240 |
mat[0][0] = v_y * coeff;
|
... | ... | |
195 |
244 |
|
196 |
245 |
float e_trans_x = mat[0][0] * e_x + mat[0][1] * e_y;
|
197 |
246 |
//float e_trans_y = mat[1][0] * e_x + mat[1][1] * e_y; //Not needed for our purposes.
|
|
247 |
*/
|
198 |
248 |
|
|
249 |
|
199 |
250 |
if (e_trans_x < 0) {
|
200 |
251 |
motor_differential = -motor_differential;
|
201 |
252 |
}
|
... | ... | |
207 |
258 |
last_y = cur_robot_y;
|
208 |
259 |
|
209 |
260 |
set_motors_with_differential(motor_differential);
|
|
261 |
|
|
262 |
count = 0;
|
|
263 |
while (!updated_robot_pos_ready) { // ghetto condition variable
|
|
264 |
wl_do();
|
|
265 |
if (count++ == 5000) { // in case the server missed it...
|
|
266 |
request_abs_position();
|
|
267 |
count = 0;
|
|
268 |
}
|
|
269 |
}
|
210 |
270 |
}
|
211 |
|
|
|
271 |
|
212 |
272 |
usb_puts("reached destination!\n");
|
213 |
273 |
}
|
214 |
274 |
|
... | ... | |
225 |
285 |
static void colonet_handle_receive(char type, int wl_source, unsigned char* packet, int length) {
|
226 |
286 |
ColonetRobotServerPacket* pkt= (ColonetRobotServerPacket*)packet;
|
227 |
287 |
unsigned char* args; //up to 7 char args
|
228 |
|
unsigned int int_args[3]; //up to 3 int (2-byte) args
|
|
288 |
unsigned int_args[3]; //up to 3 int (2-byte) args
|
229 |
289 |
|
230 |
290 |
char buf[40];
|
231 |
291 |
|
... | ... | |
320 |
380 |
/* break; */
|
321 |
381 |
|
322 |
382 |
case SERVER_REPORT_POSITION_TO_ROBOT:
|
323 |
|
robot_x = int_args[0];
|
324 |
|
robot_y = int_args[1];
|
|
383 |
robot_x = (unsigned)int_args[0];
|
|
384 |
robot_y = (unsigned)int_args[1];
|
325 |
385 |
|
326 |
386 |
updated_robot_pos_ready = 1;
|
327 |
387 |
|
328 |
|
sprintf(buf, "pos is: %d %d\n", robot_x, robot_y);
|
|
388 |
sprintf(buf, "pos is: %u %u\n", robot_x, robot_y);
|
329 |
389 |
usb_puts(buf);
|
330 |
390 |
break;
|
331 |
391 |
|
332 |
392 |
case MOVE_TO_ABSOLUTE_POSITION:
|
333 |
393 |
usb_puts("move to abs position2!\n");
|
334 |
|
move_to_position_routine(int_args[0], int_args[1]);
|
|
394 |
move_to_position_routine((unsigned)int_args[0], (unsigned)int_args[1]);
|
335 |
395 |
break;
|
336 |
396 |
|
337 |
397 |
//Buzzer
|
... | ... | |
440 |
500 |
usb_puts(buf);
|
441 |
501 |
}
|
442 |
502 |
}
|
443 |
|
|
444 |
|
/* two_bytes_to_int(char a, char b)
|
445 |
|
* Returns int of form [high][low]
|
446 |
|
*/
|
447 |
|
static unsigned int two_bytes_to_int(char high, char low) {
|
448 |
|
return (((unsigned int)high)<<8) + (unsigned int)low;
|
449 |
|
}
|