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 DEBUG(E_TRAJECTORY, "stop");
190 __trajectory_goto_d_a_rel(traj, 0, 0, READY,
191 UPDATE_A | UPDATE_D | RESET_D | RESET_A);
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)
198 struct quadramp_filter *q_d, *q_a;
200 DEBUG(E_TRAJECTORY, "hardstop");
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);
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);
214 /************ GOTO XY, USE EVENTS */
216 /** goto a x,y point, using a trajectory event */
217 void trajectory_goto_xy_abs(struct trajectory *traj, double x, double y)
219 DEBUG(E_TRAJECTORY, "Goto XY");
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);
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)
231 DEBUG(E_TRAJECTORY, "Goto XY_F");
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);
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)
243 DEBUG(E_TRAJECTORY, "Goto XY_B");
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);
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)
256 double x = position_get_x_double(traj->position);
257 double y = position_get_y_double(traj->position);
259 DEBUG(E_TRAJECTORY, "Goto DA rel");
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;
268 traj->state = RUNNING_XY_START;
269 trajectory_manager_event(traj);
270 schedule_event(traj);
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)
278 double x = position_get_x_double(traj->position);
279 double y = position_get_y_double(traj->position);
281 DEBUG(E_TRAJECTORY, "Goto XY rel");
287 vect2_cart2pol(&c, &p);
288 p.theta += position_get_a_rad_double(traj->position);;
289 vect2_pol2cart(&p, &traj->target.cart);
291 traj->target.cart.x += x;
292 traj->target.cart.y += y;
294 traj->state = RUNNING_XY_START;
295 trajectory_manager_event(traj);
296 schedule_event(traj);
299 /************ FUNCS FOR GETTING TRAJ STATE */
301 uint8_t trajectory_angle_finished(struct trajectory *traj)
303 return cs_get_consign(traj->csm_angle) ==
304 cs_get_filtered_consign(traj->csm_angle);
307 uint8_t trajectory_distance_finished(struct trajectory *traj)
309 if (traj->state == RUNNING_CLITOID_CURVE)
312 return cs_get_consign(traj->csm_distance) ==
313 cs_get_filtered_consign(traj->csm_distance) ;
316 /** return true if the position consign is equal to the filtered
317 * position consign (after quadramp filter), for angle and
319 uint8_t trajectory_finished(struct trajectory *traj)
321 return trajectory_angle_finished(traj) &&
322 trajectory_distance_finished(traj);
325 /** return true if traj is nearly finished */
326 uint8_t trajectory_in_window(struct trajectory *traj, double d_win, double a_win_rad)
328 switch(traj->state) {
330 case RUNNING_XY_ANGLE_OK:
331 case RUNNING_XY_F_ANGLE_OK:
332 case RUNNING_XY_B_ANGLE_OK:
333 /* if robot coordinates are near the x,y target */
334 return is_robot_in_xy_window(traj, d_win);
337 return is_robot_in_angle_window(traj, a_win_rad);
340 return is_robot_in_dist_window(traj, d_win);
343 return is_robot_in_dist_window(traj, d_win) &&
344 is_robot_in_angle_window(traj, a_win_rad);
346 case RUNNING_XY_START:
347 case RUNNING_XY_F_START:
348 case RUNNING_XY_B_START:
349 case RUNNING_XY_ANGLE:
350 case RUNNING_XY_F_ANGLE:
351 case RUNNING_XY_B_ANGLE:
357 /*********** *TRAJECTORY EVENT FUNC */
359 /** event called for xy trajectories */
360 void trajectory_manager_xy_event(struct trajectory *traj)
363 double x = position_get_x_double(traj->position);
364 double y = position_get_y_double(traj->position);
365 double a = position_get_a_rad_double(traj->position);
366 int32_t d_consign=0, a_consign=0;
368 /* These vectors contain target position of the robot in
369 * its own coordinates */
370 vect2_cart v2cart_pos;
371 vect2_pol v2pol_target;
373 /* step 1 : process new commands to quadramps */
375 switch (traj->state) {
376 case RUNNING_XY_START:
377 case RUNNING_XY_ANGLE:
378 case RUNNING_XY_ANGLE_OK:
379 case RUNNING_XY_F_START:
380 case RUNNING_XY_F_ANGLE:
381 case RUNNING_XY_F_ANGLE_OK:
382 case RUNNING_XY_B_START:
383 case RUNNING_XY_B_ANGLE:
384 case RUNNING_XY_B_ANGLE_OK:
386 /* process the command vector from current position to
387 * absolute target. */
388 v2cart_pos.x = traj->target.cart.x - x;
389 v2cart_pos.y = traj->target.cart.y - y;
390 vect2_cart2pol(&v2cart_pos, &v2pol_target);
391 v2pol_target.theta = simple_modulo_2pi(v2pol_target.theta - a);
393 /* asked to go backwards */
394 if (traj->state >= RUNNING_XY_B_START &&
395 traj->state <= RUNNING_XY_B_ANGLE_OK ) {
396 v2pol_target.r = -v2pol_target.r;
397 v2pol_target.theta = simple_modulo_2pi(v2pol_target.theta + M_PI);
400 /* if we don't need to go forward */
401 if (traj->state >= RUNNING_XY_START &&
402 traj->state <= RUNNING_XY_ANGLE_OK ) {
403 /* If the target is behind the robot, we need to go
404 * backwards. 0.52 instead of 0.5 because we prefer to
406 if ((v2pol_target.theta > 0.52*M_PI) ||
407 (v2pol_target.theta < -0.52*M_PI ) ) {
408 v2pol_target.r = -v2pol_target.r;
409 v2pol_target.theta = simple_modulo_2pi(v2pol_target.theta + M_PI);
413 /* If the robot is correctly oriented to start moving in distance */
414 /* here limit dist speed depending on v2pol_target.theta */
415 if (ABS(v2pol_target.theta) > traj->a_start_rad) // || ABS(v2pol_target.r) < traj->d_win)
416 set_quadramp_speed(traj, 0, traj->a_speed);
418 coef = (traj->a_start_rad - ABS(v2pol_target.theta)) / traj->a_start_rad;
419 set_quadramp_speed(traj, traj->d_speed * coef, traj->a_speed);
422 d_consign = (int32_t)(v2pol_target.r * (traj->position->phys.distance_imp_per_mm));
423 d_consign += rs_get_distance(traj->robot);
426 /* XXX here we specify 2.2 instead of 2.0 to avoid oscillations */
427 a_consign = (int32_t)(v2pol_target.theta *
428 (traj->position->phys.distance_imp_per_mm) *
429 (traj->position->phys.track_mm) / 2.2);
430 a_consign += rs_get_angle(traj->robot);
435 /* hmmm quite odd, delete the event */
436 DEBUG(E_TRAJECTORY, "GNI ???");
442 /* step 2 : update state, or delete event if we reached the
445 /* XXX if target is our pos !! */
447 switch (traj->state) {
448 case RUNNING_XY_START:
449 case RUNNING_XY_F_START:
450 case RUNNING_XY_B_START:
452 DEBUG(E_TRAJECTORY, "-> ANGLE");
456 case RUNNING_XY_ANGLE:
457 case RUNNING_XY_F_ANGLE:
458 case RUNNING_XY_B_ANGLE: {
459 struct quadramp_filter *q_a;
460 q_a = traj->csm_angle->consign_filter_params;
461 /* if d_speed is not 0, we are in start_angle_win */
462 if (get_quadramp_distance_speed(traj)) {
463 if (is_robot_in_xy_window(traj, traj->d_win)) {
466 /* ANGLE -> ANGLE_OK */
468 DEBUG(E_TRAJECTORY, "-> ANGLE_OK");
473 case RUNNING_XY_ANGLE_OK:
474 case RUNNING_XY_F_ANGLE_OK:
475 case RUNNING_XY_B_ANGLE_OK:
476 /* If we reached the destination */
477 if (is_robot_in_xy_window(traj, traj->d_win)) {
486 /* step 3 : send the processed commands to cs */
488 EVT_DEBUG(E_TRAJECTORY,"EVENT XY d_cur=%" PRIi32 ", d_consign=%" PRIi32 ", d_speed=%" PRIi32 ", "
489 "a_cur=%" PRIi32 ", a_consign=%" PRIi32 ", a_speed=%" PRIi32,
490 rs_get_distance(traj->robot), d_consign, get_quadramp_distance_speed(traj),
491 rs_get_angle(traj->robot), a_consign, get_quadramp_angle_speed(traj));
493 cs_set_consign(traj->csm_angle, a_consign);
494 cs_set_consign(traj->csm_distance, d_consign);
498 * Compute the fastest distance and angle speeds matching the radius
499 * from current traj_speed
501 void circle_get_da_speed_from_radius(struct trajectory *traj,
506 /* speed_d = coef * speed_a */
508 double speed_d2, speed_a2;
510 coef = 2. * radius_mm / traj->position->phys.track_mm;
512 speed_d2 = traj->a_speed * coef;
513 if (speed_d2 < traj->d_speed) {
515 *speed_a = traj->a_speed;
518 speed_a2 = traj->d_speed / coef;
519 *speed_d = traj->d_speed;
524 /* trajectory event for circles */
526 void trajectory_manager_circle_event(struct trajectory *traj)
529 double x = position_get_x_double(traj->position);
530 double y = position_get_y_double(traj->position);
531 double a = position_get_a_rad_double(traj->position);
532 int32_t d_consign = 0, a_consign = 0;
533 double angle_to_center_rad;
534 double coef_p, coef_d;
535 double d_speed, a_speed;
537 /* These vectors contain target position of the robot in
538 * its own coordinates */
539 vect2_cart v2cart_pos;
540 vect2_pol v2pol_target;
542 /* step 1 : process new commands to quadramps */
544 /* process the command vector from current position to the
545 * center of the circle. */
546 v2cart_pos.x = traj->target.circle.center.x - x;
547 v2cart_pos.y = traj->target.circle.center.y - y;
548 vect2_cart2pol(&v2cart_pos, &v2pol_target);
549 v2pol_target.theta = simple_modulo_2pi(v2pol_target.theta - a);
552 radius = traj->target.circle.radius;
554 coef_p = v2pol_target.r / radius;
555 coef_p = 1. * coef_p;
557 angle_to_center_rad = v2pol_target.theta - (M_PI / 2.);
558 angle_to_center_rad = simple_modulo_2pi(angle_to_center_rad);
559 if (angle_to_center_rad > 0.5)
560 angle_to_center_rad = 0.5;
561 if (angle_to_center_rad < -0.5)
562 angle_to_center_rad = -0.5;
563 coef_d = exp(5*angle_to_center_rad);
566 circle_get_da_speed_from_radius(traj, radius / (coef_p * coef_d),
568 set_quadramp_speed(traj, d_speed, a_speed);
570 EVT_DEBUG(E_TRAJECTORY, "angle=%2.2f radius=%2.2f r=%2.2f coef_p=%2.2f coef_d=%2.2f "
571 "d_speed=%2.2f a_speed=%2.2f",
572 angle_to_center_rad, radius, v2pol_target.r,
573 coef_p, coef_d, d_speed, a_speed);
575 /* XXX check flags */
576 d_consign = 400000 + rs_get_distance(traj->robot);
577 a_consign = 400000 + rs_get_angle(traj->robot);
580 /* a_consign = (int32_t)(v2pol_target.theta * */
581 /* (traj->position->phys.distance_imp_per_mm) * */
582 /* (traj->position->phys.track_mm) / 2.0); */
583 /* a_consign += rs_get_angle(traj->robot); */
585 /* step 2 : update state, or delete event if we reached the
588 /* /\* output angle -> delete event *\/ */
589 /* if (a_consign >= traj->target.circle.dest_angle) { */
590 /* a_consign = traj->target.circle.dest_angle; */
591 /* delete_event(traj); */
594 /* step 3 : send the processed commands to cs */
596 /* EVT_DEBUG(E_TRAJECTORY,"EVENT CIRCLE d_cur=%" PRIi32 ", d_consign=%" PRIi32 */
597 /* ", d_speed=%" PRIi32 ", a_cur=%" PRIi32 ", a_consign=%" PRIi32 */
598 /* ", a_speed=%" PRIi32 ", radius = %f", */
599 /* rs_get_distance(traj->robot), d_consign, get_quadramp_distance_speed(traj), */
600 /* rs_get_angle(traj->robot), a_consign, get_quadramp_angle_speed(traj), */
603 cs_set_consign(traj->csm_angle, a_consign);
604 cs_set_consign(traj->csm_distance, d_consign);
607 /* trajectory event for lines */
608 static void trajectory_manager_line_event(struct trajectory *traj)
610 double x = position_get_x_double(traj->position);
611 double y = position_get_y_double(traj->position);
612 double a = position_get_a_rad_double(traj->position);
613 double advance, dist_to_line;
614 point_t robot, proj, target_pt;
615 int32_t d_consign = 0, a_consign = 0;
616 vect2_cart v2cart_pos;
617 vect2_pol v2pol_target;
622 /* target point on the line is further on the line */
623 proj_pt_line(&robot, &traj->target.line.line, &proj);
624 dist_to_line = pt_norm(&robot, &proj);
625 if (dist_to_line > traj->target.line.advance)
628 advance = traj->target.line.advance - dist_to_line;
629 target_pt.x = proj.x + advance * cos(traj->target.line.angle);
630 target_pt.y = proj.y + advance * sin(traj->target.line.angle);
633 v2cart_pos.x = target_pt.x - x;
634 v2cart_pos.y = target_pt.y - y;
635 vect2_cart2pol(&v2cart_pos, &v2pol_target);
636 v2pol_target.theta = simple_modulo_2pi(v2pol_target.theta - a);
638 /* If the robot is correctly oriented to start moving in distance */
639 /* here limit dist speed depending on v2pol_target.theta */
640 if (ABS(v2pol_target.theta) > traj->a_start_rad) // || ABS(v2pol_target.r) < traj->d_win)
641 set_quadramp_speed(traj, 0, traj->a_speed);
644 coef = (traj->a_start_rad - ABS(v2pol_target.theta)) / traj->a_start_rad;
645 set_quadramp_speed(traj, traj->d_speed * coef, traj->a_speed);
648 /* position consign is infinite */
649 d_consign = pos_mm2imp(traj, v2pol_target.r);
650 d_consign += rs_get_distance(traj->robot);
652 /* angle consign (1.1 to avoid oscillations) */
653 a_consign = pos_rd2imp(traj, v2pol_target.theta) / 1.1;
654 a_consign += rs_get_angle(traj->robot);
656 EVT_DEBUG(E_TRAJECTORY, "target.x=%2.2f target.y=%2.2f "
657 "a_consign=%"PRIi32" d_consign=%"PRIi32,
658 target_pt.x, target_pt.y, a_consign, d_consign);
660 cs_set_consign(traj->csm_angle, a_consign);
661 cs_set_consign(traj->csm_distance, d_consign);
663 /* we reached dest, start clitoid */
664 if (traj->state == RUNNING_CLITOID_LINE &&
667 traj->target.line.turn_pt.x,
668 traj->target.line.turn_pt.y) <
669 xy_norm(proj.x + cos(traj->target.line.angle),
670 proj.y + sin(traj->target.line.angle),
671 traj->target.line.turn_pt.x,
672 traj->target.line.turn_pt.y)) {
678 /* trajectory event */
679 void trajectory_manager_event(void * param)
681 struct trajectory *traj = (struct trajectory *)param;
683 switch (traj->state) {
684 case RUNNING_XY_START:
685 case RUNNING_XY_ANGLE:
686 case RUNNING_XY_ANGLE_OK:
687 case RUNNING_XY_F_START:
688 case RUNNING_XY_F_ANGLE:
689 case RUNNING_XY_F_ANGLE_OK:
690 case RUNNING_XY_B_START:
691 case RUNNING_XY_B_ANGLE:
692 case RUNNING_XY_B_ANGLE_OK:
693 trajectory_manager_xy_event(traj);
697 trajectory_manager_circle_event(traj);
701 case RUNNING_CLITOID_LINE:
702 trajectory_manager_line_event(traj);
710 /*********** *CIRCLE */
712 /* make the robot orbiting around (x,y) on a circle whose radius is
713 * radius_mm, and exit when relative destination angle is reached. The
714 * flags set if we go forward or backwards, and CW/CCW. */
715 void trajectory_circle_rel(struct trajectory *traj,
725 traj->target.circle.center.x = x;
726 traj->target.circle.center.y = y;
727 traj->target.circle.radius = radius_mm;
728 traj->target.circle.flags = flags;
730 /* convert in steps */
731 dst_angle = RAD(rel_a_deg) *
732 (traj->position->phys.distance_imp_per_mm) *
733 (traj->position->phys.track_mm) / 2.0;
735 traj->target.circle.dest_angle = rs_get_angle(traj->robot);
736 traj->target.circle.dest_angle += dst_angle;
738 DEBUG(E_TRAJECTORY, "Circle rel (x,y)=%2.2f,%2.2f r=%2.2f flags=%x dst_angle=%"PRIi32"",
739 x, y, radius_mm, flags, traj->target.circle.dest_angle);
741 traj->state = RUNNING_CIRCLE;
742 trajectory_manager_event(traj);
743 schedule_event(traj);
746 /* return the distance in millimeters that corresponds to an angle in
747 * degree and a radius in mm */
748 /* static */double circle_get_dist_from_degrees(double radius_mm, double a_deg)
750 double a_rad = RAD(a_deg);
751 return a_rad * radius_mm;
755 * Start a circle of specified radius around the specified center
756 * (relative with d,a). The distance is specified in mm.
758 void trajectory_circle(struct trajectory *traj,
759 double center_d_mm, double center_a_rad,
760 double radius_mm, double dist_mm)
764 /* DEBUG(E_TRAJECTORY, "CIRCLE to d=%f a_rad=%f", center_d_mm, */
766 /* delete_event(traj); */
767 /* traj->state = RUNNING_CIRCLE; */
773 * Start a circle of specified radius around the specified center
774 * (absolute). The distance is specified in mm.
776 void trajectory_circle_abs_dist_mm(struct trajectory *traj,
777 double x_rel_mm, double y_rel_mm,
778 double radius_mm, double dist_mm)
783 * Start a circle of specified radius around the specified center
784 * (absolute). The distance is specified in degrees.
786 void trajectory_circle_abs_dist_deg(struct trajectory *traj,
787 double x_rel_mm, double y_rel_mm,
788 double radius_mm, double dist_degrees)
793 /*********** *LINE */
796 static void __trajectory_line_abs(struct trajectory *traj,
797 double x1, double y1,
798 double x2, double y2,
803 /* find the line EQ */
808 pts2line(&p1, &p2, &traj->target.line.line);
810 /* find the line angle */
811 traj->target.line.angle = atan2(y2-y1, x2-x1);
812 traj->target.line.advance = advance;
814 DEBUG(E_TRAJECTORY, "Line rel (a,b,c)=%2.2f,%2.2f,%2.2f",
815 traj->target.line.line.a,
816 traj->target.line.line.b,
817 traj->target.line.line.c,
818 traj->target.line.angle);
823 void trajectory_line_abs(struct trajectory *traj,
824 double x1, double y1,
825 double x2, double y2,
829 __trajectory_line_abs(traj, x1, y1, x2, y2, advance);
830 traj->state = RUNNING_LINE;
831 trajectory_manager_event(traj);
832 schedule_event(traj);
838 * process clitoid parameters
840 * - alpha: total angle
841 * - beta: circular part of angle (lower than alpha)
842 * - R: the radius of the circle (must be != 0)
843 * - Vd: linear speed to use (in imp per cs period)
844 * - Amax: maximum angular acceleration
845 * - d_inter: distance in mm until the intersection of the
848 * return 0 on success: in this case these parameters are filled:
849 * - Aa_out: the angular acceleration to configure in quadramp
850 * - Va_out: the angular speed to configure in quadramp
851 * - remain_d_mm_out: remaining distance before start to turn
853 static int8_t calc_clitoid(struct trajectory *traj,
854 double x, double y, double a_rad,
855 double alpha_deg, double beta_deg, double R_mm,
856 double Vd, double Amax, double d_inter_mm,
857 double *Aa_out, double *Va_out, double *remain_d_mm_out)
861 double t, tau, d_mm, alpha_rad, beta_rad;
865 line_t line1_int, line2_int;
866 point_t robot, intersect, pt2, center, proj, M;
871 if (fabs(alpha_deg) <= fabs(beta_deg)) {
872 DEBUG(E_TRAJECTORY, "alpha is smaller than beta");
876 /* get angular speed Va */
877 Vd_mm_s = speed_imp2mm(traj, Vd);
878 DEBUG(E_TRAJECTORY, "Vd_mm_s=%2.2f", Vd_mm_s);
879 Va_rd_s = Vd_mm_s / R_mm;
880 Va = speed_rd2imp(traj, Va_rd_s);
881 DEBUG(E_TRAJECTORY, "Va_rd_s=%2.2f Va=%2.2f", Va_rd_s, Va);
883 /* process 't', the time in seconds that we will take to do
884 * the first clothoid */
885 alpha_rad = RAD(alpha_deg);
886 beta_rad = RAD(beta_deg);
887 t = fabs(((alpha_rad - beta_rad) * R_mm) / Vd_mm_s);
888 DEBUG(E_TRAJECTORY, "R_mm=%2.2f a_rad=%2.2f alpha_rad=%2.2f beta_rad=%2.2f t=%2.2f",
889 R_mm, a_rad, alpha_rad, beta_rad, t);
891 /* process the angular acceleration */
892 Aa_rd_s2 = Va_rd_s / t;
893 Aa = acc_rd2imp(traj, Aa_rd_s2);
894 DEBUG(E_TRAJECTORY, "Aa_rd_s2=%2.2f Aa=%2.2f", Aa_rd_s2, Aa);
896 /* exit if the robot cannot physically do it */
898 DEBUG(E_TRAJECTORY, "greater than max acceleration");
902 /* define line1 and line2 */
905 intersect.x = x + cos(a_rad) * d_inter_mm;
906 intersect.y = y + sin(a_rad) * d_inter_mm;
907 pts2line(&robot, &intersect, &line1);
908 pt2.x = intersect.x + cos(a_rad + alpha_rad);
909 pt2.y = intersect.y + sin(a_rad + alpha_rad);
910 pts2line(&intersect, &pt2, &line2);
911 DEBUG(E_TRAJECTORY, "intersect=(%2.2f, %2.2f)",
912 intersect.x, intersect.y);
914 /* L and A are the parameters of the clothoid, xm and ym are
915 * the relative coords (starting from the beginning of
916 * clothoid) of the crossing point between the clothoid and
919 A = R_mm * sqrt(fabs(alpha_rad - beta_rad));
922 - (pow(L, 5) / (40. * pow(A, 4)))
923 + (pow(L, 9) / (3456. * pow(A, 8)))
924 - (pow(L, 13) / (599040. * pow(A, 12)));
926 (pow(L, 3) / (6. * pow(A, 2)))
927 - (pow(L, 7) / (336. * pow(A, 6)))
928 + (pow(L, 11) / (42240. * pow(A, 10)))
929 - (pow(L, 15) / (9676800. * pow(A, 14)));
930 DEBUG(E_TRAJECTORY, "relative xm,ym = (%2.2f, %2.2f)",
933 /* the center of the circle is at d_mm when we have to start
935 tau = (alpha_rad - beta_rad) / 2.;
936 d_mm = ym + (R_mm * cos(tau));
937 DEBUG(E_TRAJECTORY, "d_mm=%2.2f", d_mm);
939 /* translate line1 */
940 memcpy(&line1_int, &line1, sizeof(line1_int));
941 memcpy(&line2_int, &line2, sizeof(line2_int));
942 v.x = intersect.x - robot.x;
943 v.y = intersect.y - robot.y;
948 vect_resize(&v, d_mm);
949 line_translate(&line1_int, &v);
950 DEBUG(E_TRAJECTORY, "translate line1 by %2.2f,%2.2f", v.x, v.y);
952 /* translate line2_int */
953 v.x = intersect.x - pt2.x;
954 v.y = intersect.y - pt2.y;
959 vect_resize(&v, d_mm);
960 line_translate(&line2_int, &v);
961 DEBUG(E_TRAJECTORY, "translate line2 by %2.2f,%2.2f", v.x, v.y);
963 /* find the center of the circle, at the intersection of the
964 * new translated lines */
965 if (intersect_line(&line1_int, &line2_int, ¢er) != 1) {
966 DEBUG(E_TRAJECTORY, "cannot find circle center");
969 DEBUG(E_TRAJECTORY, "center=(%2.2f,%2.2f)", center.x, center.y);
971 /* M is the same point than xm, ym but in absolute coords */
973 M.x = center.x + cos(a_rad + M_PI/2 + tau) * R_mm;
974 M.y = center.y + sin(a_rad + M_PI/2 + tau) * R_mm;
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;
980 DEBUG(E_TRAJECTORY, "absolute M = (%2.2f, %2.2f)", M.x, M.y);
982 /* project M on line 1 */
983 proj_pt_line(&M, &line1, &proj);
984 DEBUG(E_TRAJECTORY, "proj M = (%2.2f, %2.2f)", proj.x, proj.y);
986 /* process remaining distance before start turning */
987 remain_d_mm = d_inter_mm - (pt_norm(&proj, &intersect) + xm);
988 DEBUG(E_TRAJECTORY, "remain_d=%2.2f", remain_d_mm);
989 if (remain_d_mm < 0) {
990 DEBUG(E_TRAJECTORY, "too late, cannot turn");
997 *remain_d_mm_out = remain_d_mm;
1001 /* after the line, start the clothoid */
1002 static void start_clitoid(struct trajectory *traj)
1004 double Aa = traj->target.line.Aa;
1005 double Va = traj->target.line.Va;
1006 double a_rad = traj->target.line.alpha;
1007 double R_mm = traj->target.line.R;
1010 DEBUG(E_TRAJECTORY, "%s() Va=%2.2f Aa=%2.2f",
1011 __FUNCTION__, Va, Aa);
1013 d = fabs(R_mm * a_rad);
1014 d *= 3.; /* margin to avoid deceleration */
1015 trajectory_d_a_rel(traj, d, DEG(a_rad));
1016 set_quadramp_acc(traj, traj->d_acc, Aa);
1017 set_quadramp_speed(traj, traj->d_speed, Va);
1018 traj->state = RUNNING_CLITOID_CURVE;
1023 * do a superb curve joining line1 to line2 which is composed of:
1024 * - a clothoid starting from line1
1026 * - another clothoid up to line2
1027 * this curve is called a clitoid (hehe)
1029 * the function assumes that the initial linear speed is Vd and
1030 * angular speed is 0.
1032 * - x,y,a_deg: starting position
1033 * - advance: parameter for line following
1034 * - alpha: total angle
1035 * - beta: circular part of angle (lower than alpha)
1036 * - R: the radius of the circle (must be != 0)
1037 * - Vd: linear speed to use (in imp per cs period)
1038 * - Amax: maximum angular acceleration
1039 * - d_inter: distance in mm until the intersection of the
1042 * return 0 if trajectory can be loaded, then it is processed in
1045 int8_t trajectory_clitoid(struct trajectory *traj,
1046 double x, double y, double a_deg, double advance,
1047 double alpha_deg, double beta_deg, double R_mm,
1050 double remain = 0, Aa = 0, Va = 0, Vd;
1051 double turnx, turny;
1052 double a_rad = RAD(a_deg);
1055 if (calc_clitoid(traj, x, y, a_rad, alpha_deg, beta_deg, R_mm,
1056 Vd, traj->a_acc, d_inter_mm,
1057 &Aa, &Va, &remain) < 0) {
1058 DEBUG(E_TRAJECTORY, "%s() calc_clitoid returned an error",
1064 turnx = x + cos(a_rad) * remain;
1065 turny = y + sin(a_rad) * remain;
1066 traj->target.line.Aa = Aa;
1067 traj->target.line.Va = Va;
1068 traj->target.line.alpha = RAD(alpha_deg);
1069 traj->target.line.R = R_mm;
1070 traj->target.line.turn_pt.x = turnx;
1071 traj->target.line.turn_pt.y = turny;
1072 DEBUG(E_TRAJECTORY, "%s() turn_pt=%2.2f,%2.2f",
1073 __FUNCTION__, turnx, turny);
1075 __trajectory_line_abs(traj, x, y, turnx, turny,
1077 traj->state = RUNNING_CLITOID_LINE;
1078 trajectory_manager_event(traj);
1079 schedule_event(traj);