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