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