/* 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. * 4: counter move. * 10: we fuck the wall. */ uint8_t goto_mode; /** Distance for mode 0, ui16. */ uint16_t goto_d; /** Sign of movement (0: forward, 1 backward). */ uint8_t goto_sign; /** Counter aim. */ int16_t goto_counter_left, goto_counter_right; /** Destination/start position, f24.8. */ int32_t goto_x, goto_y; /** Destination angle, f0.8. */ int8_t goto_a; /** Travel speed for fixed speed movements, i8. */ int8_t goto_s; /** Destination epsillon. */ int32_t goto_eps = 20L << 8; /** Debug values. */ int32_t goto_dx, goto_dy, goto_dl, goto_da; /** Movement finished. */ uint8_t goto_finish; /* +AutoDec */ /* -AutoDec */ /** Linear control mode. */ static void goto_linear_mode (void) { uint16_t d; int32_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 = 0; speed_right = 0; speed_left_aim = 0; speed_right_aim = 0; goto_finish = 1; } else { /* Convert back to f24.8. */ int32_t com = goto_d - d; com <<= 8; speed_distance (goto_sign ? -com : com, 0); } } /** Setup linear mode. */ static inline void goto_linear (int16_t d) { motor_mode = 2; goto_mode = 0; goto_sign = d >> 15; goto_d = d; if (goto_sign) goto_d = -goto_d; goto_x = postrack_x; goto_y = postrack_y; goto_finish = 0; } /** Angular control mode. */ static void goto_angular_mode (void) { int32_t angle_diff; /* Compute angle diff. */ angle_diff = v8_to_v32 (0, goto_a, 0, 0) - postrack_a; angle_diff <<= 8; angle_diff >>= 8; /* Small angles. */ if (0x1000L > angle_diff && angle_diff > -0x1000L) { speed_left = 0; speed_right = 0; speed_left_aim = 0; speed_right_aim = 0; goto_finish = 1; } else { /* Compute arc. */ goto_da = dsp_mul_f824 (angle_diff, postrack_footing_2pi); /* Set speed. */ speed_distance (0, goto_da); } } /** Setup angular mode. */ static inline void goto_angular (int8_t a) { motor_mode = 2; goto_mode = 1; goto_a = a; goto_finish = 0; } /** Position control mode. */ static void goto_position_mode (void) { int32_t c, s, arc; int32_t dla, daa; 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 = 0; speed_right = 0; speed_left_aim = 0; speed_right_aim = 0; goto_finish = 1; } else { /* Project in the robot base. */ 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); if (goto_dl > 0) dla = goto_dl; else dla = -goto_dl; if (goto_da > 0) daa = goto_da; else daa = -goto_da; /* If very big angle (> 83 °), rotate. */ if (daa > dla * 8) { /* Compute arc. */ arc = postrack_footing_2pi / 4; speed_distance (0, goto_da > 0 ? arc : -arc); } /* If big angle (> 1.7°), rotate. */ else if (daa * 32 > dla) { /* Compute arc. This is a rough aproximation. */ arc = goto_da / (goto_dl >> 8) * (postrack_footing / 2); speed_distance (0, arc); } /* Is small angle (< 0.44°), strait ahead. */ else if (daa * 128 < dla) { speed_distance (goto_dl, 0); } /* Else, curve. */ else { speed_distance (goto_dl, 0); if ((goto_da >= 0 && goto_dl >= 0) || (goto_da < 0 && goto_dl < 0)) { speed_left_aim--; speed_right_aim++; } else { speed_left_aim++; speed_right_aim--; } } } } /** Setup position mode. */ static inline void goto_position (int32_t x, int32_t y) { motor_mode = 2; goto_mode = 2; goto_x = x; goto_y = y; goto_finish = 0; } /** Position control mode, `exact' method. */ static inline void goto_position_exact_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; goto_finish = 1; } 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); } } /** Counter control mode. */ static void goto_counter_mode (void) { int16_t dl, dr; int32_t dl248, dr248; dl = goto_counter_left - counter_left; dr = goto_counter_right - counter_right; if (dl < 3 && dl > -3 && dr < 3 && dr > -3) { goto_finish = 1; } dl248 = dl; dr248 = dr; speed_distance_lr (dl248 << 8, dr248 << 8); } /** Setup counter mode. */ static inline void goto_counter (int16_t l, int16_t r) { motor_mode = 2; goto_mode = 4; goto_counter_left = speed_left_counter + l; goto_counter_right = speed_right_counter + r; goto_finish = 0; } /** We fuck the wall mode. */ static void goto_ftw_mode (void) { /* Change speed. */ if (PINA & _BV (0)) { speed_left_aim = goto_s; } else { speed_left = 0; speed_left_aim = 0; } if (PINA & _BV (7)) { speed_right_aim = goto_s; } else { speed_right = 0; speed_right_aim = 0; } if (!(PINA & (_BV (0) | _BV (7)))) goto_finish = 1; } /** Setup ftw mode. */ static inline void goto_ftw (int8_t s) { motor_mode = 2; goto_mode = 10; goto_s = s; goto_finish = 0; } /** Update the speed according to the desired destination. */ static 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; case 4: goto_counter_mode (); break; case 10: goto_ftw_mode (); break; } }