2 * Copyright Droids Corporation, Microb Technology, Eirbot (2005)
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.
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.
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
18 * Revision : $Id: trajectory_manager.c,v 1.4.4.17 2009-05-18 12:28:36 zer0 Exp $
22 /* Trajectory Manager v3 - zer0 - for Eurobot 2010 */
29 #include <aversive/error.h>
30 #include <scheduler.h>
32 #include <vect_base.h>
35 #include <position_manager.h>
36 #include <robot_system.h>
37 #include <control_system_manager.h>
40 #include <trajectory_manager.h>
41 #include "trajectory_manager_utils.h"
42 #include "trajectory_manager_core.h"
44 /************ SIMPLE TRAJS, NO EVENT */
51 static uint8_t evt_debug_cpt = 0;
52 #define EVT_DEBUG(args...) do { \
53 if (((evt_debug_cpt ++) & 0x07) == 0) { \
58 static void start_clitoid(struct trajectory *traj);
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)
68 void __trajectory_goto_d_a_rel(struct trajectory *traj, double d_mm,
69 double a_rad, uint8_t state, uint8_t flags)
71 int32_t a_consign, d_consign;
73 DEBUG(E_TRAJECTORY, "Goto DA/RS rel to d=%f a_rad=%f", d_mm, a_rad);
76 if (flags & UPDATE_A) {
77 if (flags & RESET_A) {
81 a_consign = (int32_t)(a_rad * (traj->position->phys.distance_imp_per_mm) *
82 (traj->position->phys.track_mm) / 2);
84 a_consign += rs_get_angle(traj->robot);
85 traj->target.pol.angle = a_consign;
86 cs_set_consign(traj->csm_angle, a_consign);
88 if (flags & UPDATE_D) {
89 if (flags & RESET_D) {
93 d_consign = (int32_t)((d_mm) * (traj->position->phys.distance_imp_per_mm));
95 d_consign += rs_get_distance(traj->robot);
96 traj->target.pol.distance = d_consign;
97 cs_set_consign(traj->csm_distance, d_consign);
101 /** go straight forward (d is in mm) */
102 void trajectory_d_rel(struct trajectory *traj, double d_mm)
104 __trajectory_goto_d_a_rel(traj, d_mm, 0, RUNNING_D,
105 UPDATE_D | UPDATE_A | RESET_A);
108 /** update distance consign without changing angle consign */
109 void trajectory_only_d_rel(struct trajectory *traj, double d_mm)
111 __trajectory_goto_d_a_rel(traj, d_mm, 0, RUNNING_D, UPDATE_D);
114 /** turn by 'a' degrees */
115 void trajectory_a_rel(struct trajectory *traj, double a_deg_rel)
117 __trajectory_goto_d_a_rel(traj, 0, RAD(a_deg_rel), RUNNING_A,
118 UPDATE_A | UPDATE_D | RESET_D);
121 /** turn by 'a' degrees */
122 void trajectory_a_abs(struct trajectory *traj, double a_deg_abs)
124 double posa = position_get_a_rad_double(traj->position);
127 a = RAD(a_deg_abs) - posa;
129 __trajectory_goto_d_a_rel(traj, 0, a, RUNNING_A,
130 UPDATE_A | UPDATE_D | RESET_D);
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)
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);
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),
144 UPDATE_A | UPDATE_D | RESET_D);
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)
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);
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),
158 UPDATE_A | UPDATE_D | RESET_D);
161 /** update angle consign without changing distance consign */
162 void trajectory_only_a_rel(struct trajectory *traj, double a_deg)
164 __trajectory_goto_d_a_rel(traj, 0, RAD(a_deg), RUNNING_A,
168 /** update angle consign without changing distance consign */
169 void trajectory_only_a_abs(struct trajectory *traj, double a_deg_abs)
171 double posa = position_get_a_rad_double(traj->position);
174 a = RAD(a_deg_abs) - posa;
176 __trajectory_goto_d_a_rel(traj, 0, a, RUNNING_A, UPDATE_A);
179 /** turn by 'a' degrees */
180 void trajectory_d_a_rel(struct trajectory *traj, double d_mm, double a_deg)
182 __trajectory_goto_d_a_rel(traj, d_mm, RAD(a_deg),
183 RUNNING_AD, UPDATE_A | UPDATE_D);
186 /** set relative angle and distance consign to 0 */
187 void trajectory_stop(struct trajectory *traj)
189 __trajectory_goto_d_a_rel(traj, 0, 0, READY,
190 UPDATE_A | UPDATE_D | RESET_D | RESET_A);
193 /** set relative angle and distance consign to 0, and break any
194 * deceleration ramp in quadramp filter */
195 void trajectory_hardstop(struct trajectory *traj)
197 struct quadramp_filter *q_d, *q_a;
199 q_d = traj->csm_distance->consign_filter_params;
200 q_a = traj->csm_angle->consign_filter_params;
201 __trajectory_goto_d_a_rel(traj, 0, 0, READY,
202 UPDATE_A | UPDATE_D | RESET_D | RESET_A);
204 q_d->previous_var = 0;
205 q_d->previous_out = rs_get_distance(traj->robot);
206 q_a->previous_var = 0;
207 q_a->previous_out = rs_get_angle(traj->robot);
211 /************ GOTO XY, USE EVENTS */
213 /** goto a x,y point, using a trajectory event */
214 void trajectory_goto_xy_abs(struct trajectory *traj, double x, double y)
216 DEBUG(E_TRAJECTORY, "Goto XY");
218 traj->target.cart.x = x;
219 traj->target.cart.y = y;
220 traj->state = RUNNING_XY_START;
221 trajectory_manager_event(traj);
222 schedule_event(traj);
225 /** go forward to a x,y point, using a trajectory event */
226 void trajectory_goto_forward_xy_abs(struct trajectory *traj, double x, double y)
228 DEBUG(E_TRAJECTORY, "Goto XY_F");
230 traj->target.cart.x = x;
231 traj->target.cart.y = y;
232 traj->state = RUNNING_XY_F_START;
233 trajectory_manager_event(traj);
234 schedule_event(traj);
237 /** go backward to a x,y point, using a trajectory event */
238 void trajectory_goto_backward_xy_abs(struct trajectory *traj, double x, double y)
240 DEBUG(E_TRAJECTORY, "Goto XY_B");
242 traj->target.cart.x = x;
243 traj->target.cart.y = y;
244 traj->state = RUNNING_XY_B_START;
245 trajectory_manager_event(traj);
246 schedule_event(traj);
249 /** go forward to a d,a point, using a trajectory event */
250 void trajectory_goto_d_a_rel(struct trajectory *traj, double d, double a)
253 double x = position_get_x_double(traj->position);
254 double y = position_get_y_double(traj->position);
256 DEBUG(E_TRAJECTORY, "Goto DA rel");
260 p.theta = RAD(a) + position_get_a_rad_double(traj->position);
261 vect2_pol2cart(&p, &traj->target.cart);
262 traj->target.cart.x += x;
263 traj->target.cart.y += y;
265 traj->state = RUNNING_XY_START;
266 trajectory_manager_event(traj);
267 schedule_event(traj);
270 /** go forward to a x,y relative point, using a trajectory event */
271 void trajectory_goto_xy_rel(struct trajectory *traj, double x_rel_mm, double y_rel_mm)
275 double x = position_get_x_double(traj->position);
276 double y = position_get_y_double(traj->position);
278 DEBUG(E_TRAJECTORY, "Goto XY rel");
284 vect2_cart2pol(&c, &p);
285 p.theta += position_get_a_rad_double(traj->position);;
286 vect2_pol2cart(&p, &traj->target.cart);
288 traj->target.cart.x += x;
289 traj->target.cart.y += y;
291 traj->state = RUNNING_XY_START;
292 trajectory_manager_event(traj);
293 schedule_event(traj);
296 /************ FUNCS FOR GETTING TRAJ STATE */
298 uint8_t trajectory_angle_finished(struct trajectory *traj)
300 return cs_get_consign(traj->csm_angle) ==
301 cs_get_filtered_consign(traj->csm_angle);
304 uint8_t trajectory_distance_finished(struct trajectory *traj)
306 if (traj->state == RUNNING_CLITOID_CURVE)
309 return cs_get_consign(traj->csm_distance) ==
310 cs_get_filtered_consign(traj->csm_distance) ;
313 /** return true if the position consign is equal to the filtered
314 * position consign (after quadramp filter), for angle and
316 uint8_t trajectory_finished(struct trajectory *traj)
318 return trajectory_angle_finished(traj) &&
319 trajectory_distance_finished(traj);
322 /** return true if traj is nearly finished */
323 uint8_t trajectory_in_window(struct trajectory *traj, double d_win, double a_win_rad)
325 switch(traj->state) {
327 case RUNNING_XY_ANGLE_OK:
328 case RUNNING_XY_F_ANGLE_OK:
329 case RUNNING_XY_B_ANGLE_OK:
330 /* if robot coordinates are near the x,y target */
331 return is_robot_in_xy_window(traj, d_win);
334 return is_robot_in_angle_window(traj, a_win_rad);
337 return is_robot_in_dist_window(traj, d_win);
340 return is_robot_in_dist_window(traj, d_win) &&
341 is_robot_in_angle_window(traj, a_win_rad);
343 case RUNNING_XY_START:
344 case RUNNING_XY_F_START:
345 case RUNNING_XY_B_START:
346 case RUNNING_XY_ANGLE:
347 case RUNNING_XY_F_ANGLE:
348 case RUNNING_XY_B_ANGLE:
354 /*********** *TRAJECTORY EVENT FUNC */
356 /** event called for xy trajectories */
357 void trajectory_manager_xy_event(struct trajectory *traj)
360 double x = position_get_x_double(traj->position);
361 double y = position_get_y_double(traj->position);
362 double a = position_get_a_rad_double(traj->position);
363 int32_t d_consign=0, a_consign=0;
365 /* These vectors contain target position of the robot in
366 * its own coordinates */
367 vect2_cart v2cart_pos;
368 vect2_pol v2pol_target;
370 /* step 1 : process new commands to quadramps */
372 switch (traj->state) {
373 case RUNNING_XY_START:
374 case RUNNING_XY_ANGLE:
375 case RUNNING_XY_ANGLE_OK:
376 case RUNNING_XY_F_START:
377 case RUNNING_XY_F_ANGLE:
378 case RUNNING_XY_F_ANGLE_OK:
379 case RUNNING_XY_B_START:
380 case RUNNING_XY_B_ANGLE:
381 case RUNNING_XY_B_ANGLE_OK:
383 /* process the command vector from current position to
384 * absolute target. */
385 v2cart_pos.x = traj->target.cart.x - x;
386 v2cart_pos.y = traj->target.cart.y - y;
387 vect2_cart2pol(&v2cart_pos, &v2pol_target);
388 v2pol_target.theta = simple_modulo_2pi(v2pol_target.theta - a);
390 /* asked to go backwards */
391 if (traj->state >= RUNNING_XY_B_START &&
392 traj->state <= RUNNING_XY_B_ANGLE_OK ) {
393 v2pol_target.r = -v2pol_target.r;
394 v2pol_target.theta = simple_modulo_2pi(v2pol_target.theta + M_PI);
397 /* if we don't need to go forward */
398 if (traj->state >= RUNNING_XY_START &&
399 traj->state <= RUNNING_XY_ANGLE_OK ) {
400 /* If the target is behind the robot, we need to go
401 * backwards. 0.52 instead of 0.5 because we prefer to
403 if ((v2pol_target.theta > 0.52*M_PI) ||
404 (v2pol_target.theta < -0.52*M_PI ) ) {
405 v2pol_target.r = -v2pol_target.r;
406 v2pol_target.theta = simple_modulo_2pi(v2pol_target.theta + M_PI);
410 /* If the robot is correctly oriented to start moving in distance */
411 /* here limit dist speed depending on v2pol_target.theta */
412 if (ABS(v2pol_target.theta) > traj->a_start_rad) // || ABS(v2pol_target.r) < traj->d_win)
413 set_quadramp_speed(traj, 0, traj->a_speed);
415 coef = (traj->a_start_rad - ABS(v2pol_target.theta)) / traj->a_start_rad;
416 set_quadramp_speed(traj, traj->d_speed * coef, traj->a_speed);
419 d_consign = (int32_t)(v2pol_target.r * (traj->position->phys.distance_imp_per_mm));
420 d_consign += rs_get_distance(traj->robot);
423 /* XXX here we specify 2.2 instead of 2.0 to avoid oscillations */
424 a_consign = (int32_t)(v2pol_target.theta *
425 (traj->position->phys.distance_imp_per_mm) *
426 (traj->position->phys.track_mm) / 2.2);
427 a_consign += rs_get_angle(traj->robot);
432 /* hmmm quite odd, delete the event */
433 DEBUG(E_TRAJECTORY, "GNI ???");
439 /* step 2 : update state, or delete event if we reached the
442 /* XXX if target is our pos !! */
444 switch (traj->state) {
445 case RUNNING_XY_START:
446 case RUNNING_XY_F_START:
447 case RUNNING_XY_B_START:
449 DEBUG(E_TRAJECTORY, "-> ANGLE");
453 case RUNNING_XY_ANGLE:
454 case RUNNING_XY_F_ANGLE:
455 case RUNNING_XY_B_ANGLE: {
456 struct quadramp_filter *q_a;
457 q_a = traj->csm_angle->consign_filter_params;
458 /* if d_speed is not 0, we are in start_angle_win */
459 if (get_quadramp_distance_speed(traj)) {
460 if (is_robot_in_xy_window(traj, traj->d_win)) {
463 /* ANGLE -> ANGLE_OK */
465 DEBUG(E_TRAJECTORY, "-> ANGLE_OK");
470 case RUNNING_XY_ANGLE_OK:
471 case RUNNING_XY_F_ANGLE_OK:
472 case RUNNING_XY_B_ANGLE_OK:
473 /* If we reached the destination */
474 if (is_robot_in_xy_window(traj, traj->d_win)) {
483 /* step 3 : send the processed commands to cs */
485 EVT_DEBUG(E_TRAJECTORY,"EVENT XY d_cur=%" PRIi32 ", d_consign=%" PRIi32 ", d_speed=%" PRIi32 ", "
486 "a_cur=%" PRIi32 ", a_consign=%" PRIi32 ", a_speed=%" PRIi32,
487 rs_get_distance(traj->robot), d_consign, get_quadramp_distance_speed(traj),
488 rs_get_angle(traj->robot), a_consign, get_quadramp_angle_speed(traj));
490 cs_set_consign(traj->csm_angle, a_consign);
491 cs_set_consign(traj->csm_distance, d_consign);
495 * Compute the fastest distance and angle speeds matching the radius
496 * from current traj_speed
498 void circle_get_da_speed_from_radius(struct trajectory *traj,
503 /* speed_d = coef * speed_a */
505 double speed_d2, speed_a2;
507 coef = 2. * radius_mm / traj->position->phys.track_mm;
509 speed_d2 = traj->a_speed * coef;
510 if (speed_d2 < traj->d_speed) {
512 *speed_a = traj->a_speed;
515 speed_a2 = traj->d_speed / coef;
516 *speed_d = traj->d_speed;
521 /* trajectory event for circles */
523 void trajectory_manager_circle_event(struct trajectory *traj)
526 double x = position_get_x_double(traj->position);
527 double y = position_get_y_double(traj->position);
528 double a = position_get_a_rad_double(traj->position);
529 int32_t d_consign = 0, a_consign = 0;
530 double angle_to_center_rad;
531 double coef_p, coef_d;
532 double d_speed, a_speed;
534 /* These vectors contain target position of the robot in
535 * its own coordinates */
536 vect2_cart v2cart_pos;
537 vect2_pol v2pol_target;
539 /* step 1 : process new commands to quadramps */
541 /* process the command vector from current position to the
542 * center of the circle. */
543 v2cart_pos.x = traj->target.circle.center.x - x;
544 v2cart_pos.y = traj->target.circle.center.y - y;
545 vect2_cart2pol(&v2cart_pos, &v2pol_target);
546 v2pol_target.theta = simple_modulo_2pi(v2pol_target.theta - a);
549 radius = traj->target.circle.radius;
551 coef_p = v2pol_target.r / radius;
552 coef_p = 1. * coef_p;
554 angle_to_center_rad = v2pol_target.theta - (M_PI / 2.);
555 angle_to_center_rad = simple_modulo_2pi(angle_to_center_rad);
556 if (angle_to_center_rad > 0.5)
557 angle_to_center_rad = 0.5;
558 if (angle_to_center_rad < -0.5)
559 angle_to_center_rad = -0.5;
560 coef_d = exp(5*angle_to_center_rad);
563 circle_get_da_speed_from_radius(traj, radius / (coef_p * coef_d),
565 set_quadramp_speed(traj, d_speed, a_speed);
567 EVT_DEBUG(E_TRAJECTORY, "angle=%2.2f radius=%2.2f r=%2.2f coef_p=%2.2f coef_d=%2.2f "
568 "d_speed=%2.2f a_speed=%2.2f",
569 angle_to_center_rad, radius, v2pol_target.r,
570 coef_p, coef_d, d_speed, a_speed);
572 /* XXX check flags */
573 d_consign = 400000 + rs_get_distance(traj->robot);
574 a_consign = 400000 + rs_get_angle(traj->robot);
577 /* a_consign = (int32_t)(v2pol_target.theta * */
578 /* (traj->position->phys.distance_imp_per_mm) * */
579 /* (traj->position->phys.track_mm) / 2.0); */
580 /* a_consign += rs_get_angle(traj->robot); */
582 /* step 2 : update state, or delete event if we reached the
585 /* /\* output angle -> delete event *\/ */
586 /* if (a_consign >= traj->target.circle.dest_angle) { */
587 /* a_consign = traj->target.circle.dest_angle; */
588 /* delete_event(traj); */
591 /* step 3 : send the processed commands to cs */
593 /* EVT_DEBUG(E_TRAJECTORY,"EVENT CIRCLE d_cur=%" PRIi32 ", d_consign=%" PRIi32 */
594 /* ", d_speed=%" PRIi32 ", a_cur=%" PRIi32 ", a_consign=%" PRIi32 */
595 /* ", a_speed=%" PRIi32 ", radius = %f", */
596 /* rs_get_distance(traj->robot), d_consign, get_quadramp_distance_speed(traj), */
597 /* rs_get_angle(traj->robot), a_consign, get_quadramp_angle_speed(traj), */
600 cs_set_consign(traj->csm_angle, a_consign);
601 cs_set_consign(traj->csm_distance, d_consign);
604 /* trajectory event for lines */
605 static void trajectory_manager_line_event(struct trajectory *traj)
607 double x = position_get_x_double(traj->position);
608 double y = position_get_y_double(traj->position);
609 double a = position_get_a_rad_double(traj->position);
610 double advance, dist_to_line;
611 point_t robot, proj, target_pt;
612 int32_t d_consign = 0, a_consign = 0;
613 vect2_cart v2cart_pos;
614 vect2_pol v2pol_target;
619 /* target point on the line is further on the line */
620 proj_pt_line(&robot, &traj->target.line.line, &proj);
621 dist_to_line = pt_norm(&robot, &proj);
622 if (dist_to_line > traj->target.line.advance)
625 advance = traj->target.line.advance - dist_to_line;
626 target_pt.x = proj.x + advance * cos(traj->target.line.angle);
627 target_pt.y = proj.y + advance * sin(traj->target.line.angle);
630 v2cart_pos.x = target_pt.x - x;
631 v2cart_pos.y = target_pt.y - y;
632 vect2_cart2pol(&v2cart_pos, &v2pol_target);
633 v2pol_target.theta = simple_modulo_2pi(v2pol_target.theta - a);
635 /* If the robot is correctly oriented to start moving in distance */
636 /* here limit dist speed depending on v2pol_target.theta */
637 if (ABS(v2pol_target.theta) > traj->a_start_rad) // || ABS(v2pol_target.r) < traj->d_win)
638 set_quadramp_speed(traj, 0, traj->a_speed);
641 coef = (traj->a_start_rad - ABS(v2pol_target.theta)) / traj->a_start_rad;
642 set_quadramp_speed(traj, traj->d_speed * coef, traj->a_speed);
645 /* position consign is infinite */
646 d_consign = pos_mm2imp(traj, v2pol_target.r);
647 d_consign += rs_get_distance(traj->robot);
649 /* angle consign (1.1 to avoid oscillations) */
650 a_consign = pos_rd2imp(traj, v2pol_target.theta) / 1.1;
651 a_consign += rs_get_angle(traj->robot);
653 EVT_DEBUG(E_TRAJECTORY, "target.x=%2.2f target.y=%2.2f "
654 "a_consign=%"PRIi32" d_consign=%"PRIi32,
655 target_pt.x, target_pt.y, a_consign, d_consign);
657 cs_set_consign(traj->csm_angle, a_consign);
658 cs_set_consign(traj->csm_distance, d_consign);
660 /* we reached dest, start clitoid */
661 if (traj->state == RUNNING_CLITOID_LINE &&
664 traj->target.line.turn_pt.x,
665 traj->target.line.turn_pt.y) <
666 xy_norm(proj.x + cos(traj->target.line.angle),
667 proj.y + sin(traj->target.line.angle),
668 traj->target.line.turn_pt.x,
669 traj->target.line.turn_pt.y)) {
675 /* trajectory event */
676 void trajectory_manager_event(void * param)
678 struct trajectory *traj = (struct trajectory *)param;
680 switch (traj->state) {
681 case RUNNING_XY_START:
682 case RUNNING_XY_ANGLE:
683 case RUNNING_XY_ANGLE_OK:
684 case RUNNING_XY_F_START:
685 case RUNNING_XY_F_ANGLE:
686 case RUNNING_XY_F_ANGLE_OK:
687 case RUNNING_XY_B_START:
688 case RUNNING_XY_B_ANGLE:
689 case RUNNING_XY_B_ANGLE_OK:
690 trajectory_manager_xy_event(traj);
694 trajectory_manager_circle_event(traj);
698 case RUNNING_CLITOID_LINE:
699 trajectory_manager_line_event(traj);
707 /*********** *CIRCLE */
709 /* make the robot orbiting around (x,y) on a circle whose radius is
710 * radius_mm, and exit when relative destination angle is reached. The
711 * flags set if we go forward or backwards, and CW/CCW. */
712 void trajectory_circle_rel(struct trajectory *traj,
722 traj->target.circle.center.x = x;
723 traj->target.circle.center.y = y;
724 traj->target.circle.radius = radius_mm;
725 traj->target.circle.flags = flags;
727 /* convert in steps */
728 dst_angle = RAD(rel_a_deg) *
729 (traj->position->phys.distance_imp_per_mm) *
730 (traj->position->phys.track_mm) / 2.0;
732 traj->target.circle.dest_angle = rs_get_angle(traj->robot);
733 traj->target.circle.dest_angle += dst_angle;
735 DEBUG(E_TRAJECTORY, "Circle rel (x,y)=%2.2f,%2.2f r=%2.2f flags=%x dst_angle=%"PRIi32"",
736 x, y, radius_mm, flags, traj->target.circle.dest_angle);
738 traj->state = RUNNING_CIRCLE;
739 trajectory_manager_event(traj);
740 schedule_event(traj);
743 /* return the distance in millimeters that corresponds to an angle in
744 * degree and a radius in mm */
745 /* static */double circle_get_dist_from_degrees(double radius_mm, double a_deg)
747 double a_rad = RAD(a_deg);
748 return a_rad * radius_mm;
752 * Start a circle of specified radius around the specified center
753 * (relative with d,a). The distance is specified in mm.
755 void trajectory_circle(struct trajectory *traj,
756 double center_d_mm, double center_a_rad,
757 double radius_mm, double dist_mm)
761 /* DEBUG(E_TRAJECTORY, "CIRCLE to d=%f a_rad=%f", center_d_mm, */
763 /* delete_event(traj); */
764 /* traj->state = RUNNING_CIRCLE; */
770 * Start a circle of specified radius around the specified center
771 * (absolute). The distance is specified in mm.
773 void trajectory_circle_abs_dist_mm(struct trajectory *traj,
774 double x_rel_mm, double y_rel_mm,
775 double radius_mm, double dist_mm)
780 * Start a circle of specified radius around the specified center
781 * (absolute). The distance is specified in degrees.
783 void trajectory_circle_abs_dist_deg(struct trajectory *traj,
784 double x_rel_mm, double y_rel_mm,
785 double radius_mm, double dist_degrees)
790 /*********** *LINE */
793 static void __trajectory_line_abs(struct trajectory *traj,
794 double x1, double y1,
795 double x2, double y2,
800 /* find the line EQ */
805 pts2line(&p1, &p2, &traj->target.line.line);
807 /* find the line angle */
808 traj->target.line.angle = atan2(y2-y1, x2-x1);
809 traj->target.line.advance = advance;
811 DEBUG(E_TRAJECTORY, "Line rel (a,b,c)=%2.2f,%2.2f,%2.2f",
812 traj->target.line.line.a,
813 traj->target.line.line.b,
814 traj->target.line.line.c,
815 traj->target.line.angle);
820 void trajectory_line_abs(struct trajectory *traj,
821 double x1, double y1,
822 double x2, double y2,
826 __trajectory_line_abs(traj, x1, y1, x2, y2, advance);
827 traj->state = RUNNING_LINE;
828 trajectory_manager_event(traj);
829 schedule_event(traj);
835 * process clitoid parameters
837 * - alpha: total angle
838 * - beta: circular part of angle (lower than alpha)
839 * - R: the radius of the circle (must be != 0)
840 * - Vd: linear speed to use (in imp per cs period)
841 * - Amax: maximum angular acceleration
842 * - d_inter: distance in mm until the intersection of the
845 * return 0 on success: in this case these parameters are filled:
846 * - Aa_out: the angular acceleration to configure in quadramp
847 * - Va_out: the angular speed to configure in quadramp
848 * - remain_d_mm_out: remaining distance before start to turn
850 static int8_t calc_clitoid(struct trajectory *traj,
851 double x, double y, double a_rad,
852 double alpha_deg, double beta_deg, double R_mm,
853 double Vd, double Amax, double d_inter_mm,
854 double *Aa_out, double *Va_out, double *remain_d_mm_out)
858 double t, tau, d_mm, alpha_rad, beta_rad;
862 line_t line1_int, line2_int;
863 point_t robot, intersect, pt2, center, proj, M;
868 if (fabs(alpha_deg) <= fabs(beta_deg)) {
869 DEBUG(E_TRAJECTORY, "alpha is smaller than beta");
873 /* get angular speed Va */
874 Vd_mm_s = speed_imp2mm(traj, Vd);
875 DEBUG(E_TRAJECTORY, "Vd_mm_s=%2.2f", Vd_mm_s);
876 Va_rd_s = Vd_mm_s / R_mm;
877 Va = speed_rd2imp(traj, Va_rd_s);
878 DEBUG(E_TRAJECTORY, "Va_rd_s=%2.2f Va=%2.2f", Va_rd_s, Va);
880 /* process 't', the time in seconds that we will take to do
881 * the first clothoid */
882 alpha_rad = RAD(alpha_deg);
883 beta_rad = RAD(beta_deg);
884 t = fabs(((alpha_rad - beta_rad) * R_mm) / Vd_mm_s);
885 DEBUG(E_TRAJECTORY, "R_mm=%2.2f alpha_rad=%2.2f beta_rad=%2.2f t=%2.2f",
886 R_mm, alpha_rad, beta_rad, t);
888 /* process the angular acceleration */
889 Aa_rd_s2 = Va_rd_s / t;
890 Aa = acc_rd2imp(traj, Aa_rd_s2);
891 DEBUG(E_TRAJECTORY, "Aa_rd_s2=%2.2f Aa=%2.2f", Aa_rd_s2, Aa);
893 /* exit if the robot cannot physically do it */
895 DEBUG(E_TRAJECTORY, "greater than max acceleration");
899 /* define line1 and line2 */
902 intersect.x = x + cos(a_rad) * d_inter_mm;
903 intersect.y = y + sin(a_rad) * d_inter_mm;
904 pts2line(&robot, &intersect, &line1);
905 pt2.x = intersect.x + cos(a_rad + alpha_rad);
906 pt2.y = intersect.y + sin(a_rad + alpha_rad);
907 pts2line(&intersect, &pt2, &line2);
908 DEBUG(E_TRAJECTORY, "intersect=(%2.2f, %2.2f)",
909 intersect.x, intersect.y);
911 /* L and A are the parameters of the clothoid, xm and ym are
912 * the relative coords (starting from the beginning of
913 * clothoid) of the crossing point between the clothoid and
916 A = R_mm * sqrt(fabs(alpha_rad - beta_rad));
919 - (pow(L, 5) / (40. * pow(A, 4)))
920 + (pow(L, 9) / (3456. * pow(A, 8)))
921 - (pow(L, 13) / (599040. * pow(A, 12)));
923 (pow(L, 3) / (6. * pow(A, 2)))
924 - (pow(L, 7) / (336. * pow(A, 6)))
925 + (pow(L, 11) / (42240. * pow(A, 10)))
926 - (pow(L, 15) / (9676800. * pow(A, 14)));
927 DEBUG(E_TRAJECTORY, "relative xm,ym = (%2.2f, %2.2f)",
930 /* the center of the circle is at d_mm when we have to start
932 tau = (alpha_rad - beta_rad) / 2.;
933 d_mm = ym + (R_mm * cos(tau));
934 DEBUG(E_TRAJECTORY, "d_mm=%2.2f", d_mm);
936 /* translate line1 */
937 memcpy(&line1_int, &line1, sizeof(line1_int));
938 memcpy(&line2_int, &line2, sizeof(line2_int));
939 v.x = intersect.x - robot.x;
940 v.y = intersect.y - robot.y;
945 vect_resize(&v, d_mm);
946 line_translate(&line1_int, &v);
947 DEBUG(E_TRAJECTORY, "translate line1 by %2.2f,%2.2f", v.x, v.y);
949 /* translate line2_int */
950 v.x = intersect.x - pt2.x;
951 v.y = intersect.y - pt2.y;
956 vect_resize(&v, d_mm);
957 line_translate(&line2_int, &v);
958 DEBUG(E_TRAJECTORY, "translate line2 by %2.2f,%2.2f", v.x, v.y);
960 /* find the center of the circle, at the intersection of the
961 * new translated lines */
962 if (intersect_line(&line1_int, &line2_int, ¢er) != 1) {
963 DEBUG(E_TRAJECTORY, "cannot find circle center");
966 DEBUG(E_TRAJECTORY, "center=(%2.2f,%2.2f)", center.x, center.y);
968 /* M is the same point than xm, ym but in absolute coords */
969 M.x = center.x + cos(a_rad - M_PI/2 + tau) * R_mm;
970 M.y = center.y + sin(a_rad - M_PI/2 + tau) * R_mm;
971 DEBUG(E_TRAJECTORY, "absolute M = (%2.2f, %2.2f)", M.x, M.y);
973 /* project M on line 1 */
974 proj_pt_line(&M, &line1, &proj);
975 DEBUG(E_TRAJECTORY, "proj M = (%2.2f, %2.2f)", proj.x, proj.y);
977 /* process remaining distance before start turning */
978 remain_d_mm = d_inter_mm - (pt_norm(&proj, &intersect) + xm);
979 DEBUG(E_TRAJECTORY, "remain_d=%2.2f", remain_d_mm);
980 if (remain_d_mm < 0) {
981 DEBUG(E_TRAJECTORY, "too late, cannot turn");
988 *remain_d_mm_out = remain_d_mm;
992 /* after the line, start the clothoid */
993 static void start_clitoid(struct trajectory *traj)
995 double Aa = traj->target.line.Aa;
996 double Va = traj->target.line.Va;
997 double a_rad = traj->target.line.alpha;
998 double R_mm = traj->target.line.R;
1002 DEBUG(E_TRAJECTORY, "%s() Va=%2.2f Aa=%2.2f",
1003 __FUNCTION__, Va, Aa);
1004 traj->state = RUNNING_CLITOID_CURVE;
1005 d = fabs(R_mm * a_rad);
1006 d *= 3.; /* margin to avoid deceleration */
1007 trajectory_d_a_rel(traj, d, DEG(a_rad));
1008 set_quadramp_acc(traj, traj->d_acc, Aa);
1009 set_quadramp_speed(traj, traj->d_speed, Va);
1014 * do a superb curve joining line1 to line2 which is composed of:
1015 * - a clothoid starting from line1
1017 * - another clothoid up to line2
1018 * this curve is called a clitoid (hehe)
1020 * the function assumes that the initial linear speed is Vd and
1021 * angular speed is 0.
1023 * - x,y,a: starting position
1024 * - advance: parameter for line following
1025 * - alpha: total angle
1026 * - beta: circular part of angle (lower than alpha)
1027 * - R: the radius of the circle (must be != 0)
1028 * - Vd: linear speed to use (in imp per cs period)
1029 * - Amax: maximum angular acceleration
1030 * - d_inter: distance in mm until the intersection of the
1033 * return 0 if trajectory can be loaded, then it is processed in
1036 int8_t trajectory_clitoid(struct trajectory *traj,
1037 double x, double y, double a, double advance,
1038 double alpha_deg, double beta_deg, double R_mm,
1041 double remain = 0, Aa = 0, Va = 0, Vd;
1042 double turnx, turny;
1045 if (calc_clitoid(traj, x, y, a, alpha_deg, beta_deg, R_mm,
1046 Vd, traj->a_acc, d_inter_mm,
1047 &Aa, &Va, &remain) < 0)
1051 turnx = x + cos(a) * remain;
1052 turny = y + sin(a) * remain;
1053 traj->target.line.Aa = Aa;
1054 traj->target.line.Va = Va;
1055 traj->target.line.alpha = RAD(alpha_deg);
1056 traj->target.line.R = R_mm;
1057 traj->target.line.turn_pt.x = turnx;
1058 traj->target.line.turn_pt.y = turny;
1059 DEBUG(E_TRAJECTORY, "%s() turn_pt=%2.2f,%2.2f",
1060 __FUNCTION__, turnx, turny);
1062 __trajectory_line_abs(traj, x, y, turnx, turny,
1064 traj->state = RUNNING_CLITOID_LINE;
1065 trajectory_manager_event(traj);
1066 schedule_event(traj);