/* main.c */ /* asserv - Position & speed motor control on a ATmega128. {{{ * * Copyright (C) 2004 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. * * }}} */ #include "dsp.h" #include #include #include #include #include #include "pwm.c" #include "timer.c" #include "counter.c" #include "speed.c" #include "postrack.c" /** Motor mode : * 0 - pwm setup; * 1 - speed control; * 2 - position control. */ int8_t motor_mode; /** Main loop counter. */ uint8_t motor_loop_cpt; /** Statistics about speed control. */ uint8_t motor_stat_speed, motor_stat_speed_cpt; /** Statistics about pwm values. */ uint8_t motor_stat_pwm, motor_stat_pwm_cpt; /** Report of timer. */ uint8_t motor_stat_timer, motor_stat_timer_cpt; /** Report of counters. */ uint8_t motor_stat_counter, motor_stat_counter_cpt; /** Report position. */ uint8_t motor_stat_postrack, motor_stat_postrack_cpt; /** Record timer value at different stage of computing. Used for performance * analisys. */ uint8_t motor_timer_0, motor_timer_1, motor_timer_2, motor_timer_3; /* +AutoDec */ /* Main loop. */ static void main_loop (void); /* -AutoDec */ /** Entry point. */ int main (void) { DDRD = 0x60; pwm_init (); timer_init (); counter_init (); speed_init (); postrack_init (); rs232_init (); proto_send0 ('z'); sei (); main_loop (); return 0; } /** Main loop. */ static void main_loop (void) { while (1) { motor_timer_0 = timer_read (); while (!timer_pending ()) counter_poll (); counter_update (); motor_timer_3 = timer_read (); postrack_update (); motor_timer_2 = timer_read (); /* Speed control. */ if (motor_mode >= 1) { speed_update (); speed_compute_left_pwm (); speed_compute_right_pwm (); } motor_timer_1 = timer_read (); /* Pwm setup. */ pwm_update (); /* Stats. */ if (motor_stat_counter && !--motor_stat_counter_cpt) { proto_send2w ('C', counter_left, counter_right); motor_stat_counter_cpt = motor_stat_counter; } if (motor_stat_postrack && !--motor_stat_postrack_cpt) { proto_send1d ('X', postrack_x); proto_send1d ('Y', postrack_y); proto_send1d ('A', postrack_a); motor_stat_postrack_cpt = motor_stat_postrack; } if (motor_stat_speed && !--motor_stat_speed_cpt) { proto_send4w ('S', speed_left_e_old, speed_left_int, speed_right_e_old, speed_right_int); motor_stat_speed_cpt = motor_stat_speed; } if (motor_stat_pwm && !--motor_stat_pwm_cpt) { proto_send2w ('W', pwm_left, pwm_right); motor_stat_pwm_cpt = motor_stat_pwm; } if (motor_stat_timer && !--motor_stat_timer_cpt) { proto_send4b ('T', motor_timer_3, motor_timer_2, motor_timer_1, motor_timer_0); motor_stat_timer_cpt = motor_stat_timer; } /* Misc. */ if ((motor_loop_cpt & 7) == 0) { if (rs232_poll ()) proto_accept (rs232_getc ()); } motor_loop_cpt++; } } /** Handle incoming messages. */ void proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) { #define c(cmd, size) (cmd << 8 | size) switch (c (cmd, size)) { case c ('z', 0): reset (); break; case c ('w', 0): speed_restart (); motor_mode = 0; pwm_left = 0; pwm_right = 0; break; case c ('w', 4): speed_restart (); motor_mode = 0; pwm_left = args[0] << 8 | args[1]; pwm_right = args[2] << 8 | args[3]; break; case c ('s', 2): motor_mode = 1; speed_left_aim = args[0]; speed_right_aim = args[1]; break; case c ('a', 1): speed_acc_cpt = speed_acc = args[0]; break; case c ('p', 2): speed_kp = args[0] << 8 | args[1]; break; case c ('i', 2): speed_ki = args[0] << 8 | args[1]; break; case c ('f', 2): postrack_set_footing (args[0] << 8 | args[1]); break; case c ('C', 1): motor_stat_counter_cpt = motor_stat_counter = args[0]; break; case c ('X', 1): motor_stat_postrack_cpt = motor_stat_postrack = args[0]; break; case c ('S', 1): motor_stat_speed_cpt = motor_stat_speed = args[0]; break; case c ('W', 1): motor_stat_pwm_cpt = motor_stat_pwm = args[0]; break; case c ('T', 1): motor_stat_timer_cpt = motor_stat_timer = args[0]; break; default: proto_send0 ('e'); return; } proto_send (cmd, size, args); #undef c }