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