/* goto.c */ /* asserv - Position & speed motor control on a ATmega128. {{{ * * Copyright (C) 2005 Nicolas Schodet * * Robot APB Team/Efrei 2005. * Web: http://assos.efrei.fr/robot/ * Email: robot AT efrei DOT fr * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * }}} */ /** Goto mode: * 0: linear move. * 1: angular move. * 2: position move. */ uint8_t goto_mode; /** Distance for mode 0, ui16. */ uint16_t goto_d; /** Sign of movement (0: forward, 1 backward). */ uint8_t goto_sign; /** Destination position, f24.8. */ int32_t goto_x, goto_y; /** Destination angle, f0.8. */ int8_t goto_a; /** Destination epsillon. */ int32_t goto_eps = 20L << 8; /** Debug values. */ int32_t goto_dx, goto_dy, goto_dl, goto_da; /* +AutoDec */ /* -AutoDec */ /** Linear control mode. */ static inline void goto_linear_mode (void) { uint16_t d; uint32_t dx, dy; /* Compute distance from the start point. * WARNING: this could overflow as dsp_hypot accept a maximum value of * +/- 65535. */ dx = goto_x - postrack_x; dy = goto_y - postrack_y; d = dsp_hypot (dx, dy); /* Change speed. */ if (d > goto_d) { speed_left_aim = 0; speed_right_aim = 0; } else { /* Convert back to f24.8. */ int32_t com = goto_d - d; com <<= 8; speed_distance (goto_sign ? -com : com, 0); } } /** Angular control mode. */ static inline void goto_angular_mode (void) { int8_t angle_diff; int32_t arc; /* Compute angle diff. This works because a full circle is 256. */ angle_diff = goto_a - v32_to_v8 (postrack_a, 2); /* Compute arc. */ arc = angle_diff; /* i8 */ arc *= postrack_footing / 2; /* i24 */ arc <<= 8; /* f24.8 */ /* Change speed. */ speed_distance (0, arc); } /** Position control mode. */ static inline void goto_position_mode (void) { int32_t c, s; /* Project in the robot base. */ goto_dx = goto_x - postrack_x; /* f24.8 */ goto_dy = goto_y - postrack_y; if (goto_dx < goto_eps && goto_dx > -goto_eps && goto_dy < goto_eps && goto_dy > -goto_eps) { speed_left_aim = 0; speed_right_aim = 0; } else { c = dsp_cos_f824 (postrack_a); s = dsp_sin_f824 (postrack_a); goto_dl = dsp_mul_f824 (goto_dx, c) + dsp_mul_f824 (goto_dy, s); goto_da = dsp_mul_f824 (goto_dy, c) - dsp_mul_f824 (goto_dx, s); /* Convert da into a arc. This is a rough aproximation. */ goto_da = goto_da * (postrack_footing / 2) / (goto_dl >> 8); speed_distance (goto_dl, goto_da); } } /** Update the speed according to the desired destination. */ static inline void goto_update (void) { switch (goto_mode) { case 0: goto_linear_mode (); break; case 1: goto_angular_mode (); break; case 2: goto_position_mode (); break; } }