save
[aversive.git] / modules / devices / robot / trajectory_manager / trajectory_manager_core.c
1 /*
2  *  Copyright Droids Corporation, Microb Technology, Eirbot (2005)
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: trajectory_manager.c,v 1.4.4.17 2009-05-18 12:28:36 zer0 Exp $
19  *
20  */
21
22 /* Trajectory Manager v3 - zer0 - for Eurobot 2010 */
23
24 #include <string.h>
25 #include <stdlib.h>
26 #include <math.h>
27
28 #include <aversive.h>
29 #include <aversive/error.h>
30 #include <scheduler.h>
31 #include <vect2.h>
32 #include <vect_base.h>
33 #include <lines.h>
34
35 #include <position_manager.h>
36 #include <robot_system.h>
37 #include <control_system_manager.h>
38 #include <quadramp.h>
39
40 #include <trajectory_manager.h>
41 #include "trajectory_manager_utils.h"
42 #include "trajectory_manager_core.h"
43
44 /************ SIMPLE TRAJS, NO EVENT */
45
46 #define UPDATE_D 1
47 #define UPDATE_A 2
48 #define RESET_D  4
49 #define RESET_A  8
50
51 static uint8_t evt_debug_cpt = 0;
52 #define EVT_DEBUG(args...) do {                         \
53                 if (((evt_debug_cpt ++) & 0x07) == 0) { \
54                         DEBUG(args);                    \
55                 }                                       \
56         } while (0)
57
58 static void start_clitoid(struct trajectory *traj);
59
60 /**
61  * update angle and/or distance
62  * this function is not called directly by the user
63  *   traj  : pointer to the trajectory structure
64  *   d_mm  : distance in mm
65  *   a_rad : angle in radian
66  *   flags : what to update (UPDATE_A, UPDATE_D)
67  */
68 void __trajectory_goto_d_a_rel(struct trajectory *traj, double d_mm,
69                                double a_rad, uint8_t state, uint8_t flags)
70 {
71         int32_t a_consign, d_consign;
72
73         DEBUG(E_TRAJECTORY, "Goto DA/RS rel to d=%f a_rad=%f", d_mm, a_rad);
74         delete_event(traj);
75         traj->state = state;
76         if (flags & UPDATE_A) {
77                 if (flags & RESET_A) {
78                         a_consign = 0;
79                 }
80                 else {
81                         a_consign = (int32_t)(a_rad * (traj->position->phys.distance_imp_per_mm) *
82                                               (traj->position->phys.track_mm) / 2);
83                 }
84                 a_consign +=  rs_get_angle(traj->robot);
85                 traj->target.pol.angle = a_consign;
86                 cs_set_consign(traj->csm_angle, a_consign);
87         }
88         if (flags & UPDATE_D) {
89                 if (flags & RESET_D) {
90                         d_consign = 0;
91                 }
92                 else {
93                         d_consign = (int32_t)((d_mm) * (traj->position->phys.distance_imp_per_mm));
94                 }
95                 d_consign += rs_get_distance(traj->robot);
96                 traj->target.pol.distance = d_consign;
97                 cs_set_consign(traj->csm_distance, d_consign);
98         }
99 }
100
101 /** go straight forward (d is in mm) */
102 void trajectory_d_rel(struct trajectory *traj, double d_mm)
103 {
104         __trajectory_goto_d_a_rel(traj, d_mm, 0, RUNNING_D,
105                                   UPDATE_D | UPDATE_A | RESET_A);
106 }
107
108 /** update distance consign without changing angle consign */
109 void trajectory_only_d_rel(struct trajectory *traj, double d_mm)
110 {
111         __trajectory_goto_d_a_rel(traj, d_mm, 0, RUNNING_D, UPDATE_D);
112 }
113
114 /** turn by 'a' degrees */
115 void trajectory_a_rel(struct trajectory *traj, double a_deg_rel)
116 {
117         __trajectory_goto_d_a_rel(traj, 0, RAD(a_deg_rel), RUNNING_A,
118                                   UPDATE_A | UPDATE_D | RESET_D);
119 }
120
121 /** turn by 'a' degrees */
122 void trajectory_a_abs(struct trajectory *traj, double a_deg_abs)
123 {
124         double posa = position_get_a_rad_double(traj->position);
125         double a;
126
127         a = RAD(a_deg_abs) - posa;
128         a = modulo_2pi(a);
129         __trajectory_goto_d_a_rel(traj, 0, a, RUNNING_A,
130                                   UPDATE_A | UPDATE_D | RESET_D);
131 }
132
133 /** turn the robot until the point x,y is in front of us */
134 void trajectory_turnto_xy(struct trajectory *traj, double x_abs_mm, double y_abs_mm)
135 {
136         double posx = position_get_x_double(traj->position);
137         double posy = position_get_y_double(traj->position);
138         double posa = position_get_a_rad_double(traj->position);
139
140         DEBUG(E_TRAJECTORY, "Goto Turn To xy %f %f", x_abs_mm, y_abs_mm);
141         __trajectory_goto_d_a_rel(traj, 0,
142                         simple_modulo_2pi(atan2(y_abs_mm - posy, x_abs_mm - posx) - posa),
143                                   RUNNING_A,
144                                   UPDATE_A | UPDATE_D | RESET_D);
145 }
146
147 /** turn the robot until the point x,y is behind us */
148 void trajectory_turnto_xy_behind(struct trajectory *traj, double x_abs_mm, double y_abs_mm)
149 {
150         double posx = position_get_x_double(traj->position);
151         double posy = position_get_y_double(traj->position);
152         double posa = position_get_a_rad_double(traj->position);
153
154         DEBUG(E_TRAJECTORY, "Goto Turn To xy %f %f", x_abs_mm, y_abs_mm);
155         __trajectory_goto_d_a_rel(traj, 0,
156                         modulo_2pi(atan2(y_abs_mm - posy, x_abs_mm - posx) - posa + M_PI),
157                                   RUNNING_A,
158                                   UPDATE_A | UPDATE_D | RESET_D);
159 }
160
161 /** update angle consign without changing distance consign */
162 void trajectory_only_a_rel(struct trajectory *traj, double a_deg)
163 {
164         __trajectory_goto_d_a_rel(traj, 0, RAD(a_deg), RUNNING_A,
165                                   UPDATE_A);
166 }
167
168 /** update angle consign without changing distance consign */
169 void trajectory_only_a_abs(struct trajectory *traj, double a_deg_abs)
170 {
171         double posa = position_get_a_rad_double(traj->position);
172         double a;
173
174         a = RAD(a_deg_abs) - posa;
175         a = modulo_2pi(a);
176         __trajectory_goto_d_a_rel(traj, 0, a, RUNNING_A, UPDATE_A);
177 }
178
179 /** turn by 'a' degrees */
180 void trajectory_d_a_rel(struct trajectory *traj, double d_mm, double a_deg)
181 {
182         __trajectory_goto_d_a_rel(traj, d_mm, RAD(a_deg),
183                                   RUNNING_AD, UPDATE_A | UPDATE_D);
184 }
185
186 /** set relative angle and distance consign to 0 */
187 void trajectory_stop(struct trajectory *traj)
188 {
189         __trajectory_goto_d_a_rel(traj, 0, 0, READY,
190                                   UPDATE_A | UPDATE_D | RESET_D | RESET_A);
191 }
192
193 /** set relative angle and distance consign to 0, and break any
194  * deceleration ramp in quadramp filter */
195 void trajectory_hardstop(struct trajectory *traj)
196 {
197         struct quadramp_filter *q_d, *q_a;
198
199         q_d = traj->csm_distance->consign_filter_params;
200         q_a = traj->csm_angle->consign_filter_params;
201         __trajectory_goto_d_a_rel(traj, 0, 0, READY,
202                                   UPDATE_A | UPDATE_D | RESET_D | RESET_A);
203
204         q_d->previous_var = 0;
205         q_d->previous_out = rs_get_distance(traj->robot);
206         q_a->previous_var = 0;
207         q_a->previous_out = rs_get_angle(traj->robot);
208 }
209
210
211 /************ GOTO XY, USE EVENTS */
212
213 /** goto a x,y point, using a trajectory event */
214 void trajectory_goto_xy_abs(struct trajectory *traj, double x, double y)
215 {
216         DEBUG(E_TRAJECTORY, "Goto XY");
217         delete_event(traj);
218         traj->target.cart.x = x;
219         traj->target.cart.y = y;
220         traj->state = RUNNING_XY_START;
221         trajectory_manager_event(traj);
222         schedule_event(traj);
223 }
224
225 /** go forward to a x,y point, using a trajectory event */
226 void trajectory_goto_forward_xy_abs(struct trajectory *traj, double x, double y)
227 {
228         DEBUG(E_TRAJECTORY, "Goto XY_F");
229         delete_event(traj);
230         traj->target.cart.x = x;
231         traj->target.cart.y = y;
232         traj->state = RUNNING_XY_F_START;
233         trajectory_manager_event(traj);
234         schedule_event(traj);
235 }
236
237 /** go backward to a x,y point, using a trajectory event */
238 void trajectory_goto_backward_xy_abs(struct trajectory *traj, double x, double y)
239 {
240         DEBUG(E_TRAJECTORY, "Goto XY_B");
241         delete_event(traj);
242         traj->target.cart.x = x;
243         traj->target.cart.y = y;
244         traj->state = RUNNING_XY_B_START;
245         trajectory_manager_event(traj);
246         schedule_event(traj);
247 }
248
249 /** go forward to a d,a point, using a trajectory event */
250 void trajectory_goto_d_a_rel(struct trajectory *traj, double d, double a)
251 {
252         vect2_pol p;
253         double x = position_get_x_double(traj->position);
254         double y = position_get_y_double(traj->position);
255
256         DEBUG(E_TRAJECTORY, "Goto DA rel");
257
258         delete_event(traj);
259         p.r = d;
260         p.theta = RAD(a) + position_get_a_rad_double(traj->position);
261         vect2_pol2cart(&p, &traj->target.cart);
262         traj->target.cart.x += x;
263         traj->target.cart.y += y;
264
265         traj->state = RUNNING_XY_START;
266         trajectory_manager_event(traj);
267         schedule_event(traj);
268 }
269
270 /** go forward to a x,y relative point, using a trajectory event */
271 void trajectory_goto_xy_rel(struct trajectory *traj, double x_rel_mm, double y_rel_mm)
272 {
273         vect2_cart c;
274         vect2_pol p;
275         double x = position_get_x_double(traj->position);
276         double y = position_get_y_double(traj->position);
277
278         DEBUG(E_TRAJECTORY, "Goto XY rel");
279
280         delete_event(traj);
281         c.x = x_rel_mm;
282         c.y = y_rel_mm;
283
284         vect2_cart2pol(&c, &p);
285         p.theta += position_get_a_rad_double(traj->position);;
286         vect2_pol2cart(&p, &traj->target.cart);
287
288         traj->target.cart.x += x;
289         traj->target.cart.y += y;
290
291         traj->state = RUNNING_XY_START;
292         trajectory_manager_event(traj);
293         schedule_event(traj);
294 }
295
296 /************ FUNCS FOR GETTING TRAJ STATE */
297
298 uint8_t trajectory_angle_finished(struct trajectory *traj)
299 {
300         return cs_get_consign(traj->csm_angle) ==
301                 cs_get_filtered_consign(traj->csm_angle);
302 }
303
304 uint8_t trajectory_distance_finished(struct trajectory *traj)
305 {
306         if (traj->state == RUNNING_CLITOID_CURVE)
307                 return 1;
308
309         return cs_get_consign(traj->csm_distance) ==
310                 cs_get_filtered_consign(traj->csm_distance) ;
311 }
312
313 /** return true if the position consign is equal to the filtered
314  * position consign (after quadramp filter), for angle and
315  * distance. */
316 uint8_t trajectory_finished(struct trajectory *traj)
317 {
318         return trajectory_angle_finished(traj) &&
319                 trajectory_distance_finished(traj);
320 }
321
322 /** return true if traj is nearly finished */
323 uint8_t trajectory_in_window(struct trajectory *traj, double d_win, double a_win_rad)
324 {
325         switch(traj->state) {
326
327         case RUNNING_XY_ANGLE_OK:
328         case RUNNING_XY_F_ANGLE_OK:
329         case RUNNING_XY_B_ANGLE_OK:
330                 /* if robot coordinates are near the x,y target */
331                 return is_robot_in_xy_window(traj, d_win);
332
333         case RUNNING_A:
334                 return is_robot_in_angle_window(traj, a_win_rad);
335
336         case RUNNING_D:
337                 return is_robot_in_dist_window(traj, d_win);
338
339         case RUNNING_AD:
340                 return is_robot_in_dist_window(traj, d_win) &&
341                         is_robot_in_angle_window(traj, a_win_rad);
342
343         case RUNNING_XY_START:
344         case RUNNING_XY_F_START:
345         case RUNNING_XY_B_START:
346         case RUNNING_XY_ANGLE:
347         case RUNNING_XY_F_ANGLE:
348         case RUNNING_XY_B_ANGLE:
349         default:
350                 return 0;
351         }
352 }
353
354 /*********** *TRAJECTORY EVENT FUNC */
355
356 /** event called for xy trajectories */
357 void trajectory_manager_xy_event(struct trajectory *traj)
358 {
359         double coef = 1.0;
360         double x = position_get_x_double(traj->position);
361         double y = position_get_y_double(traj->position);
362         double a = position_get_a_rad_double(traj->position);
363         int32_t d_consign=0, a_consign=0;
364
365         /* These vectors contain target position of the robot in
366          * its own coordinates */
367         vect2_cart v2cart_pos;
368         vect2_pol v2pol_target;
369
370         /* step 1 : process new commands to quadramps */
371
372         switch (traj->state) {
373         case RUNNING_XY_START:
374         case RUNNING_XY_ANGLE:
375         case RUNNING_XY_ANGLE_OK:
376         case RUNNING_XY_F_START:
377         case RUNNING_XY_F_ANGLE:
378         case RUNNING_XY_F_ANGLE_OK:
379         case RUNNING_XY_B_START:
380         case RUNNING_XY_B_ANGLE:
381         case RUNNING_XY_B_ANGLE_OK:
382
383                 /* process the command vector from current position to
384                  * absolute target. */
385                 v2cart_pos.x = traj->target.cart.x - x;
386                 v2cart_pos.y = traj->target.cart.y - y;
387                 vect2_cart2pol(&v2cart_pos, &v2pol_target);
388                 v2pol_target.theta = simple_modulo_2pi(v2pol_target.theta - a);
389
390                 /* asked to go backwards */
391                 if (traj->state >= RUNNING_XY_B_START &&
392                     traj->state <= RUNNING_XY_B_ANGLE_OK ) {
393                         v2pol_target.r = -v2pol_target.r;
394                         v2pol_target.theta = simple_modulo_2pi(v2pol_target.theta + M_PI);
395                 }
396
397                 /* if we don't need to go forward */
398                 if (traj->state >= RUNNING_XY_START &&
399                     traj->state <= RUNNING_XY_ANGLE_OK ) {
400                         /* If the target is behind the robot, we need to go
401                          * backwards. 0.52 instead of 0.5 because we prefer to
402                          * go forward */
403                         if ((v2pol_target.theta > 0.52*M_PI) ||
404                             (v2pol_target.theta < -0.52*M_PI ) ) {
405                                 v2pol_target.r = -v2pol_target.r;
406                                 v2pol_target.theta = simple_modulo_2pi(v2pol_target.theta + M_PI);
407                         }
408                 }
409
410                 /* If the robot is correctly oriented to start moving in distance */
411                 /* here limit dist speed depending on v2pol_target.theta */
412                 if (ABS(v2pol_target.theta) > traj->a_start_rad) // || ABS(v2pol_target.r) < traj->d_win)
413                         set_quadramp_speed(traj, 0, traj->a_speed);
414                 else {
415                         coef = (traj->a_start_rad - ABS(v2pol_target.theta)) / traj->a_start_rad;
416                         set_quadramp_speed(traj, traj->d_speed * coef, traj->a_speed);
417                 }
418
419                 d_consign = (int32_t)(v2pol_target.r * (traj->position->phys.distance_imp_per_mm));
420                 d_consign += rs_get_distance(traj->robot);
421
422                 /* angle consign */
423                 /* XXX here we specify 2.2 instead of 2.0 to avoid oscillations */
424                 a_consign = (int32_t)(v2pol_target.theta *
425                                       (traj->position->phys.distance_imp_per_mm) *
426                                       (traj->position->phys.track_mm) / 2.2);
427                 a_consign += rs_get_angle(traj->robot);
428
429                 break;
430
431         default:
432                 /* hmmm quite odd, delete the event */
433                 DEBUG(E_TRAJECTORY, "GNI ???");
434                 delete_event(traj);
435                 traj->state = READY;
436         }
437
438
439         /* step 2 : update state, or delete event if we reached the
440          * destination */
441
442         /* XXX if target is our pos !! */
443
444         switch (traj->state) {
445         case RUNNING_XY_START:
446         case RUNNING_XY_F_START:
447         case RUNNING_XY_B_START:
448                 /* START -> ANGLE */
449                 DEBUG(E_TRAJECTORY, "-> ANGLE");
450                 traj->state ++;
451                 break;
452
453         case RUNNING_XY_ANGLE:
454         case RUNNING_XY_F_ANGLE:
455         case RUNNING_XY_B_ANGLE: {
456                 struct quadramp_filter *q_a;
457                 q_a = traj->csm_angle->consign_filter_params;
458                 /* if d_speed is not 0, we are in start_angle_win */
459                 if (get_quadramp_distance_speed(traj)) {
460                         if (is_robot_in_xy_window(traj, traj->d_win)) {
461                                 delete_event(traj);
462                         }
463                         /* ANGLE -> ANGLE_OK */
464                         traj->state ++;
465                         DEBUG(E_TRAJECTORY, "-> ANGLE_OK");
466                 }
467                 break;
468         }
469
470         case RUNNING_XY_ANGLE_OK:
471         case RUNNING_XY_F_ANGLE_OK:
472         case RUNNING_XY_B_ANGLE_OK:
473                 /* If we reached the destination */
474                 if (is_robot_in_xy_window(traj, traj->d_win)) {
475                         delete_event(traj);
476                 }
477         break;
478
479         default:
480                 break;
481         }
482
483         /* step 3 : send the processed commands to cs */
484
485         EVT_DEBUG(E_TRAJECTORY,"EVENT XY d_cur=%" PRIi32 ", d_consign=%" PRIi32 ", d_speed=%" PRIi32 ", "
486               "a_cur=%" PRIi32 ", a_consign=%" PRIi32 ", a_speed=%" PRIi32,
487               rs_get_distance(traj->robot), d_consign, get_quadramp_distance_speed(traj),
488               rs_get_angle(traj->robot), a_consign, get_quadramp_angle_speed(traj));
489
490         cs_set_consign(traj->csm_angle, a_consign);
491         cs_set_consign(traj->csm_distance, d_consign);
492 }
493
494 /*
495  * Compute the fastest distance and angle speeds matching the radius
496  * from current traj_speed
497  */
498 void circle_get_da_speed_from_radius(struct trajectory *traj,
499                                      double radius_mm,
500                                      double *speed_d,
501                                      double *speed_a)
502 {
503         /* speed_d = coef * speed_a */
504         double coef;
505         double speed_d2, speed_a2;
506
507         coef = 2. * radius_mm / traj->position->phys.track_mm;
508
509         speed_d2 = traj->a_speed * coef;
510         if (speed_d2 < traj->d_speed) {
511                 *speed_d = speed_d2;
512                 *speed_a = traj->a_speed;
513         }
514         else {
515                 speed_a2 = traj->d_speed / coef;
516                 *speed_d = traj->d_speed;
517                 *speed_a = speed_a2;
518         }
519 }
520
521 /* trajectory event for circles */
522 /* XXX static */
523 void trajectory_manager_circle_event(struct trajectory *traj)
524 {
525         double radius;
526         double x = position_get_x_double(traj->position);
527         double y = position_get_y_double(traj->position);
528         double a = position_get_a_rad_double(traj->position);
529         int32_t d_consign = 0, a_consign = 0;
530         double angle_to_center_rad;
531         double coef_p, coef_d;
532         double d_speed, a_speed;
533
534         /* These vectors contain target position of the robot in
535          * its own coordinates */
536         vect2_cart v2cart_pos;
537         vect2_pol v2pol_target;
538
539         /* step 1 : process new commands to quadramps */
540
541         /* process the command vector from current position to the
542          * center of the circle. */
543         v2cart_pos.x = traj->target.circle.center.x - x;
544         v2cart_pos.y = traj->target.circle.center.y - y;
545         vect2_cart2pol(&v2cart_pos, &v2pol_target);
546         v2pol_target.theta = simple_modulo_2pi(v2pol_target.theta - a);
547
548         /* radius consign */
549         radius = traj->target.circle.radius;
550
551         coef_p = v2pol_target.r / radius;
552         coef_p = 1. * coef_p;
553
554         angle_to_center_rad = v2pol_target.theta - (M_PI / 2.);
555         angle_to_center_rad = simple_modulo_2pi(angle_to_center_rad);
556         if (angle_to_center_rad > 0.5)
557                 angle_to_center_rad = 0.5;
558         if (angle_to_center_rad < -0.5)
559                 angle_to_center_rad = -0.5;
560         coef_d = exp(5*angle_to_center_rad);
561         coef_d = coef_d;
562
563         circle_get_da_speed_from_radius(traj, radius / (coef_p * coef_d),
564                                         &d_speed, &a_speed);
565         set_quadramp_speed(traj, d_speed, a_speed);
566
567         EVT_DEBUG(E_TRAJECTORY, "angle=%2.2f radius=%2.2f r=%2.2f coef_p=%2.2f coef_d=%2.2f "
568               "d_speed=%2.2f a_speed=%2.2f",
569                   angle_to_center_rad, radius, v2pol_target.r,
570               coef_p, coef_d, d_speed, a_speed);
571
572         /* XXX check flags */
573         d_consign = 400000 + rs_get_distance(traj->robot);
574         a_consign = 400000 + rs_get_angle(traj->robot);
575
576         /* angle consign */
577 /*      a_consign = (int32_t)(v2pol_target.theta * */
578 /*                            (traj->position->phys.distance_imp_per_mm) * */
579 /*                            (traj->position->phys.track_mm) / 2.0); */
580 /*      a_consign += rs_get_angle(traj->robot); */
581
582         /* step 2 : update state, or delete event if we reached the
583          * destination */
584
585 /*      /\* output angle -> delete event *\/ */
586 /*      if (a_consign >= traj->target.circle.dest_angle) { */
587 /*              a_consign = traj->target.circle.dest_angle; */
588 /*              delete_event(traj); */
589 /*      } */
590
591         /* step 3 : send the processed commands to cs */
592
593 /*      EVT_DEBUG(E_TRAJECTORY,"EVENT CIRCLE d_cur=%" PRIi32 ", d_consign=%" PRIi32 */
594 /*                ", d_speed=%" PRIi32 ", a_cur=%" PRIi32 ", a_consign=%" PRIi32 */
595 /*                ", a_speed=%" PRIi32 ", radius = %f", */
596 /*                rs_get_distance(traj->robot), d_consign, get_quadramp_distance_speed(traj), */
597 /*                rs_get_angle(traj->robot), a_consign, get_quadramp_angle_speed(traj), */
598 /*                radius); */
599
600         cs_set_consign(traj->csm_angle, a_consign);
601         cs_set_consign(traj->csm_distance, d_consign);
602 }
603
604 /* trajectory event for lines */
605 static void trajectory_manager_line_event(struct trajectory *traj)
606 {
607         double x = position_get_x_double(traj->position);
608         double y = position_get_y_double(traj->position);
609         double a = position_get_a_rad_double(traj->position);
610         double advance, dist_to_line;
611         point_t robot, proj, target_pt;
612         int32_t d_consign = 0, a_consign = 0;
613         vect2_cart v2cart_pos;
614         vect2_pol v2pol_target;
615
616         robot.x = x;
617         robot.y = y;
618
619         /* target point on the line is further on the line */
620         proj_pt_line(&robot, &traj->target.line.line, &proj);
621         dist_to_line = pt_norm(&robot, &proj);
622         if (dist_to_line > traj->target.line.advance)
623                 advance = 0;
624         else
625                 advance = traj->target.line.advance - dist_to_line;
626         target_pt.x = proj.x + advance * cos(traj->target.line.angle);
627         target_pt.y = proj.y + advance * sin(traj->target.line.angle);
628
629         /* target vector */
630         v2cart_pos.x = target_pt.x - x;
631         v2cart_pos.y = target_pt.y - y;
632         vect2_cart2pol(&v2cart_pos, &v2pol_target);
633         v2pol_target.theta = simple_modulo_2pi(v2pol_target.theta - a);
634
635         /* If the robot is correctly oriented to start moving in distance */
636         /* here limit dist speed depending on v2pol_target.theta */
637         if (ABS(v2pol_target.theta) > traj->a_start_rad) // || ABS(v2pol_target.r) < traj->d_win)
638                 set_quadramp_speed(traj, 0, traj->a_speed);
639         else {
640                 double coef;
641                 coef = (traj->a_start_rad - ABS(v2pol_target.theta)) / traj->a_start_rad;
642                 set_quadramp_speed(traj, traj->d_speed * coef, traj->a_speed);
643         }
644
645         /* position consign is infinite */
646         d_consign = pos_mm2imp(traj, v2pol_target.r);
647         d_consign += rs_get_distance(traj->robot);
648
649         /* angle consign (1.1 to avoid oscillations) */
650         a_consign = pos_rd2imp(traj, v2pol_target.theta) / 1.1;
651         a_consign += rs_get_angle(traj->robot);
652
653         EVT_DEBUG(E_TRAJECTORY, "target.x=%2.2f target.y=%2.2f "
654                   "a_consign=%"PRIi32" d_consign=%"PRIi32,
655                   target_pt.x, target_pt.y, a_consign, d_consign);
656
657         cs_set_consign(traj->csm_angle, a_consign);
658         cs_set_consign(traj->csm_distance, d_consign);
659
660         /* we reached dest, start clitoid */
661         if (traj->state == RUNNING_CLITOID_LINE &&
662             xy_norm(proj.x,
663                     proj.y,
664                     traj->target.line.turn_pt.x,
665                     traj->target.line.turn_pt.y) <
666             xy_norm(proj.x + cos(traj->target.line.angle),
667                     proj.y + sin(traj->target.line.angle),
668                     traj->target.line.turn_pt.x,
669                     traj->target.line.turn_pt.y)) {
670                 start_clitoid(traj);
671         }
672 }
673
674
675 /* trajectory event */
676 void trajectory_manager_event(void * param)
677 {
678         struct trajectory *traj = (struct trajectory *)param;
679
680         switch (traj->state) {
681         case RUNNING_XY_START:
682         case RUNNING_XY_ANGLE:
683         case RUNNING_XY_ANGLE_OK:
684         case RUNNING_XY_F_START:
685         case RUNNING_XY_F_ANGLE:
686         case RUNNING_XY_F_ANGLE_OK:
687         case RUNNING_XY_B_START:
688         case RUNNING_XY_B_ANGLE:
689         case RUNNING_XY_B_ANGLE_OK:
690                 trajectory_manager_xy_event(traj);
691                 break;
692
693         case RUNNING_CIRCLE:
694                 trajectory_manager_circle_event(traj);
695                 break;
696
697         case RUNNING_LINE:
698         case RUNNING_CLITOID_LINE:
699                 trajectory_manager_line_event(traj);
700                 break;
701
702         default:
703                 break;
704         }
705 }
706
707 /*********** *CIRCLE */
708
709 /* make the robot orbiting around (x,y) on a circle whose radius is
710  * radius_mm, and exit when relative destination angle is reached. The
711  * flags set if we go forward or backwards, and CW/CCW. */
712 void trajectory_circle_rel(struct trajectory *traj,
713                            double x, double y,
714                            double radius_mm,
715                            double rel_a_deg,
716                            uint8_t flags)
717 {
718         double dst_angle;
719
720         delete_event(traj);
721
722         traj->target.circle.center.x = x;
723         traj->target.circle.center.y = y;
724         traj->target.circle.radius = radius_mm;
725         traj->target.circle.flags = flags;
726
727         /* convert in steps  */
728         dst_angle = RAD(rel_a_deg) *
729                 (traj->position->phys.distance_imp_per_mm) *
730                 (traj->position->phys.track_mm) / 2.0;
731
732         traj->target.circle.dest_angle = rs_get_angle(traj->robot);
733         traj->target.circle.dest_angle += dst_angle;
734
735         DEBUG(E_TRAJECTORY, "Circle rel (x,y)=%2.2f,%2.2f r=%2.2f flags=%x dst_angle=%"PRIi32"",
736               x, y, radius_mm, flags, traj->target.circle.dest_angle);
737
738         traj->state = RUNNING_CIRCLE;
739         trajectory_manager_event(traj);
740         schedule_event(traj);
741 }
742
743 /* return the distance in millimeters that corresponds to an angle in
744  * degree and a radius in mm */
745 /* static  */double circle_get_dist_from_degrees(double radius_mm, double a_deg)
746 {
747         double a_rad = RAD(a_deg);
748         return a_rad * radius_mm;
749 }
750
751 /*
752  * Start a circle of specified radius around the specified center
753  * (relative with d,a). The distance is specified in mm.
754  */
755 void trajectory_circle(struct trajectory *traj,
756                        double center_d_mm, double center_a_rad,
757                        double radius_mm, double dist_mm)
758 {
759 /*      double */
760
761 /*      DEBUG(E_TRAJECTORY, "CIRCLE to d=%f a_rad=%f", center_d_mm, */
762 /*            center_a_rad); */
763 /*      delete_event(traj); */
764 /*      traj->state = RUNNING_CIRCLE; */
765
766
767 }
768
769 /*
770  * Start a circle of specified radius around the specified center
771  * (absolute). The distance is specified in mm.
772  */
773 void trajectory_circle_abs_dist_mm(struct trajectory *traj,
774                                    double x_rel_mm, double y_rel_mm,
775                                    double radius_mm, double dist_mm)
776 {
777 }
778
779 /*
780  * Start a circle of specified radius around the specified center
781  * (absolute). The distance is specified in degrees.
782  */
783 void trajectory_circle_abs_dist_deg(struct trajectory *traj,
784                                     double x_rel_mm, double y_rel_mm,
785                                     double radius_mm, double dist_degrees)
786 {
787
788 }
789
790 /*********** *LINE */
791
792 /* Follow a line */
793 static void __trajectory_line_abs(struct trajectory *traj,
794                                   double x1, double y1,
795                                   double x2, double y2,
796                                   double advance)
797 {
798         point_t p1, p2;
799
800         /* find the line EQ */
801         p1.x = x1;
802         p1.y = y1;
803         p2.x = x2;
804         p2.y = y2;
805         pts2line(&p1, &p2, &traj->target.line.line);
806
807         /* find the line angle */
808         traj->target.line.angle = atan2(y2-y1, x2-x1);
809         traj->target.line.advance = advance;
810
811         DEBUG(E_TRAJECTORY, "Line rel (a,b,c)=%2.2f,%2.2f,%2.2f",
812               traj->target.line.line.a,
813               traj->target.line.line.b,
814               traj->target.line.line.c,
815               traj->target.line.angle);
816
817 }
818
819 /* Follow a line */
820 void trajectory_line_abs(struct trajectory *traj,
821                          double x1, double y1,
822                          double x2, double y2,
823                          double advance)
824 {
825         delete_event(traj);
826         __trajectory_line_abs(traj, x1, y1, x2, y2, advance);
827         traj->state = RUNNING_LINE;
828         trajectory_manager_event(traj);
829         schedule_event(traj);
830 }
831
832 /*** CLOTHOID */
833
834 /**
835  * process clitoid parameters
836  *
837  * - alpha: total angle
838  * - beta: circular part of angle (lower than alpha)
839  * - R: the radius of the circle (must be != 0)
840  * - Vd: linear speed to use (in imp per cs period)
841  * - Amax: maximum angular acceleration
842  * - d_inter: distance in mm until the intersection of the
843  *            2 lines
844  *
845  * return 0 on success: in this case these parameters are filled:
846  * - Aa_out: the angular acceleration to configure in quadramp
847  * - Va_out: the angular speed to configure in quadramp
848  * - remain_d_mm_out: remaining distance before start to turn
849  */
850 static int8_t calc_clitoid(struct trajectory *traj,
851                             double x, double y, double a_rad,
852                             double alpha_deg, double beta_deg, double R_mm,
853                             double Vd, double Amax, double d_inter_mm,
854                             double *Aa_out, double *Va_out, double *remain_d_mm_out)
855 {
856         double Vd_mm_s;
857         double Va, Va_rd_s;
858         double t, tau, d_mm, alpha_rad, beta_rad;
859         double remain_d_mm;
860         double Aa, Aa_rd_s2;
861         line_t line1, line2;
862         line_t line1_int, line2_int;
863         point_t robot, intersect, pt2, center, proj, M;
864         vect_t v;
865         double xm, ym, L, A;
866
867         /* param check */
868         if (fabs(alpha_deg) <= fabs(beta_deg)) {
869                 DEBUG(E_TRAJECTORY, "alpha is smaller than beta");
870                 return -1;
871         }
872
873         /* get angular speed Va */
874         Vd_mm_s = speed_imp2mm(traj, Vd);
875         DEBUG(E_TRAJECTORY, "Vd_mm_s=%2.2f", Vd_mm_s);
876         Va_rd_s = Vd_mm_s / R_mm;
877         Va = speed_rd2imp(traj, Va_rd_s);
878         DEBUG(E_TRAJECTORY, "Va_rd_s=%2.2f Va=%2.2f", Va_rd_s, Va);
879
880         /* process 't', the time in seconds that we will take to do
881          * the first clothoid */
882         alpha_rad = RAD(alpha_deg);
883         beta_rad = RAD(beta_deg);
884         t = fabs(((alpha_rad - beta_rad) * R_mm) / Vd_mm_s);
885         DEBUG(E_TRAJECTORY, "R_mm=%2.2f alpha_rad=%2.2f beta_rad=%2.2f t=%2.2f",
886               R_mm, alpha_rad, beta_rad, t);
887
888         /* process the angular acceleration */
889         Aa_rd_s2 = Va_rd_s / t;
890         Aa = acc_rd2imp(traj, Aa_rd_s2);
891         DEBUG(E_TRAJECTORY, "Aa_rd_s2=%2.2f Aa=%2.2f", Aa_rd_s2, Aa);
892
893         /* exit if the robot cannot physically do it */
894         if (Aa > Amax) {
895                 DEBUG(E_TRAJECTORY, "greater than max acceleration");
896                 return -1;
897         }
898
899         /* define line1 and line2 */
900         robot.x = x;
901         robot.y = y;
902         intersect.x = x + cos(a_rad) * d_inter_mm;
903         intersect.y = y + sin(a_rad) * d_inter_mm;
904         pts2line(&robot, &intersect, &line1);
905         pt2.x = intersect.x + cos(a_rad + alpha_rad);
906         pt2.y = intersect.y + sin(a_rad + alpha_rad);
907         pts2line(&intersect, &pt2, &line2);
908         DEBUG(E_TRAJECTORY, "intersect=(%2.2f, %2.2f)",
909               intersect.x, intersect.y);
910
911         /* L and A are the parameters of the clothoid, xm and ym are
912          * the relative coords (starting from the beginning of
913          * clothoid) of the crossing point between the clothoid and
914          * the circle. */
915         L = Vd_mm_s * t;
916         A = R_mm * sqrt(fabs(alpha_rad - beta_rad));
917         xm =
918                 L
919                 - (pow(L, 5) / (40. * pow(A, 4)))
920                 + (pow(L, 9) / (3456. * pow(A, 8)))
921                 - (pow(L, 13) / (599040. * pow(A, 12)));
922         ym =
923                 (pow(L, 3) / (6. * pow(A, 2)))
924                 - (pow(L, 7) / (336. * pow(A, 6)))
925                 + (pow(L, 11) / (42240. * pow(A, 10)))
926                 - (pow(L, 15) / (9676800. * pow(A, 14)));
927         DEBUG(E_TRAJECTORY, "relative xm,ym = (%2.2f, %2.2f)",
928               xm, ym);
929
930         /* the center of the circle is at d_mm when we have to start
931          * the clothoid */
932         tau = (alpha_rad - beta_rad) / 2.;
933         d_mm = ym + (R_mm * cos(tau));
934         DEBUG(E_TRAJECTORY, "d_mm=%2.2f", d_mm);
935
936         /* translate line1 */
937         memcpy(&line1_int, &line1, sizeof(line1_int));
938         memcpy(&line2_int, &line2, sizeof(line2_int));
939         v.x = intersect.x - robot.x;
940         v.y = intersect.y - robot.y;
941         if (alpha_rad > 0)
942                 vect_rot_trigo(&v);
943         else
944                 vect_rot_retro(&v);
945         vect_resize(&v, d_mm);
946         line_translate(&line1_int, &v);
947         DEBUG(E_TRAJECTORY, "translate line1 by %2.2f,%2.2f", v.x, v.y);
948
949         /* translate line2_int */
950         v.x = intersect.x - pt2.x;
951         v.y = intersect.y - pt2.y;
952         if (alpha_rad < 0)
953                 vect_rot_trigo(&v);
954         else
955                 vect_rot_retro(&v);
956         vect_resize(&v, d_mm);
957         line_translate(&line2_int, &v);
958         DEBUG(E_TRAJECTORY, "translate line2 by %2.2f,%2.2f", v.x, v.y);
959
960         /* find the center of the circle, at the intersection of the
961          * new translated lines */
962         if (intersect_line(&line1_int, &line2_int, &center) != 1) {
963                 DEBUG(E_TRAJECTORY, "cannot find circle center");
964                 return -1;
965         }
966         DEBUG(E_TRAJECTORY, "center=(%2.2f,%2.2f)", center.x, center.y);
967
968         /* M is the same point than xm, ym but in absolute coords */
969         M.x = center.x + cos(a_rad - M_PI/2 + tau) * R_mm;
970         M.y = center.y + sin(a_rad - M_PI/2 + tau) * R_mm;
971         DEBUG(E_TRAJECTORY, "absolute M = (%2.2f, %2.2f)", M.x, M.y);
972
973         /* project M on line 1 */
974         proj_pt_line(&M, &line1, &proj);
975         DEBUG(E_TRAJECTORY, "proj M = (%2.2f, %2.2f)", proj.x, proj.y);
976
977         /* process remaining distance before start turning */
978         remain_d_mm = d_inter_mm - (pt_norm(&proj, &intersect) + xm);
979         DEBUG(E_TRAJECTORY, "remain_d=%2.2f", remain_d_mm);
980         if (remain_d_mm < 0) {
981                 DEBUG(E_TRAJECTORY, "too late, cannot turn");
982                 return -1;
983         }
984
985         /* return result */
986         *Aa_out = Aa;
987         *Va_out = Va;
988         *remain_d_mm_out = remain_d_mm;
989         return 0;
990 }
991
992 /* after the line, start the clothoid */
993 static void start_clitoid(struct trajectory *traj)
994 {
995         double Aa = traj->target.line.Aa;
996         double Va = traj->target.line.Va;
997         double a_rad = traj->target.line.alpha;
998         double R_mm = traj->target.line.R;
999         double d;
1000
1001         delete_event(traj);
1002         DEBUG(E_TRAJECTORY, "%s() Va=%2.2f Aa=%2.2f",
1003               __FUNCTION__, Va, Aa);
1004         traj->state = RUNNING_CLITOID_CURVE;
1005         d = fabs(R_mm * a_rad);
1006         d *= 3.; /* margin to avoid deceleration */
1007         trajectory_d_a_rel(traj, d, DEG(a_rad));
1008         set_quadramp_acc(traj, traj->d_acc, Aa);
1009         set_quadramp_speed(traj, traj->d_speed, Va);
1010 }
1011
1012
1013 /**
1014  * do a superb curve joining line1 to line2 which is composed of:
1015  *   - a clothoid starting from line1
1016  *   - a circle
1017  *   - another clothoid up to line2
1018  * this curve is called a clitoid (hehe)
1019  *
1020  * the function assumes that the initial linear speed is Vd and
1021  * angular speed is 0.
1022  *
1023  * - x,y,a: starting position
1024  * - advance: parameter for line following
1025  * - alpha: total angle
1026  * - beta: circular part of angle (lower than alpha)
1027  * - R: the radius of the circle (must be != 0)
1028  * - Vd: linear speed to use (in imp per cs period)
1029  * - Amax: maximum angular acceleration
1030  * - d_inter: distance in mm until the intersection of the
1031  *            2 lines
1032  *
1033  * return 0 if trajectory can be loaded, then it is processed in
1034  * background.
1035  */
1036 int8_t trajectory_clitoid(struct trajectory *traj,
1037                           double x, double y, double a, double advance,
1038                           double alpha_deg, double beta_deg, double R_mm,
1039                           double d_inter_mm)
1040 {
1041         double remain = 0, Aa = 0, Va = 0, Vd;
1042         double turnx, turny;
1043
1044         Vd = traj->d_speed;
1045         if (calc_clitoid(traj, x, y, a, alpha_deg, beta_deg, R_mm,
1046                          Vd, traj->a_acc, d_inter_mm,
1047                          &Aa, &Va, &remain) < 0)
1048                 return -1;
1049
1050         delete_event(traj);
1051         turnx = x + cos(a) * remain;
1052         turny = y + sin(a) * remain;
1053         traj->target.line.Aa = Aa;
1054         traj->target.line.Va = Va;
1055         traj->target.line.alpha = RAD(alpha_deg);
1056         traj->target.line.R = R_mm;
1057         traj->target.line.turn_pt.x = turnx;
1058         traj->target.line.turn_pt.y = turny;
1059         DEBUG(E_TRAJECTORY, "%s() turn_pt=%2.2f,%2.2f",
1060               __FUNCTION__, turnx, turny);
1061
1062         __trajectory_line_abs(traj, x, y, turnx, turny,
1063                               advance);
1064         traj->state = RUNNING_CLITOID_LINE;
1065         trajectory_manager_event(traj);
1066         schedule_event(traj);
1067         return 0;
1068 }