From c34c7f4b78ddf31d422454cb440ed31647d9b964 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Mon, 2 May 2011 23:28:00 +0200 Subject: digital/{ai,io}: move timer and chrono to ai --- digital/io/src/Makefile | 4 +- digital/io/src/bot.h | 5 -- digital/io/src/chrono.c | 156 ---------------------------------------- digital/io/src/chrono.h | 97 ------------------------- digital/io/src/main.c | 8 +-- digital/io/src/main_timer.avr.c | 60 ---------------- digital/io/src/main_timer.h | 78 -------------------- digital/io/src/simu.host.c | 4 +- digital/io/src/trace.trc | 2 +- digital/io/src/usdist.c | 2 +- digital/io/src/usdist.h | 2 +- 11 files changed, 11 insertions(+), 407 deletions(-) delete mode 100644 digital/io/src/chrono.c delete mode 100644 digital/io/src/chrono.h delete mode 100644 digital/io/src/main_timer.avr.c delete mode 100644 digital/io/src/main_timer.h (limited to 'digital/io') diff --git a/digital/io/src/Makefile b/digital/io/src/Makefile index 7ac4eefe..a21b0c1b 100644 --- a/digital/io/src/Makefile +++ b/digital/io/src/Makefile @@ -4,7 +4,7 @@ BASE = ../../avr PROGS = io # Sources to compile. io_SOURCES = main.c servo.avr.c eeprom.avr.c pwm.c \ - switch.avr.c chrono.c main_timer.avr.c servo_pos.c \ + switch.avr.c chrono.c timer.avr.c servo_pos.c \ twi_master.c asserv.c mimot.c \ simu.host.c contact.c usdist.c radar.c \ path.c food.c events.host.c \ @@ -12,7 +12,7 @@ io_SOURCES = main.c servo.avr.c eeprom.avr.c pwm.c \ # Modules needed for IO. MODULES = proto uart twi utils adc math/fixed math/geometry path/astar \ trace flash spi -AI_MODULES = twi_master common +AI_MODULES = twi_master common utils # Configuration file. CONFIGFILE = avrconfig.h # IO board use an ATMega128. diff --git a/digital/io/src/bot.h b/digital/io/src/bot.h index 01515574..b4112e92 100644 --- a/digital/io/src/bot.h +++ b/digital/io/src/bot.h @@ -29,11 +29,6 @@ * Specific defines about the robot dimensions and competion rules. */ -/** - * Duration of a match in milliseconds, with margin. - */ -#define MATCH_DURATION_MS (90000 - 2500) - /** * The scaling factor, millimeter per step. */ diff --git a/digital/io/src/chrono.c b/digital/io/src/chrono.c deleted file mode 100644 index b73b627c..00000000 --- a/digital/io/src/chrono.c +++ /dev/null @@ -1,156 +0,0 @@ -/* chrono.c */ -/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{ - * - * Copyright (C) 2008 Dufour Jérémy - * - * APBTeam: - * Web: http://apbteam.org/ - * Email: team AT apbteam DOT org - * - * 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 "common.h" - -#include "bot.h" -#include "main_timer.h" -#include "asserv.h" -#include "twi_master.h" - -#include "modules/utils/utils.h" -#include "modules/host/mex.h" - -#include "chrono.h" - -/** - * Implementation notes. - * This module compute the number of tic of the main loop it should count - * before the match is over (chrono_init). Every tic of the main loop, it - * decrements the counter (chrono_update). When the counter is zero, the - * match is over (chrono_is_match_over, chrono_end_match). - */ - -/** - * Number of overflows of the timer/counter 0 to wait before the match is - * over. - * Basically, it is match_duration / timer_counter_0_overflow_duration. - * Minus one is here for safety reason (because rounding is done at plus one). - */ -#define CHRONO_MATCH_OVERFLOW_COUNT (MATCH_DURATION_MS / MT_TC0_PERIOD - 1) - -/** - * Duration of a loop to emulate from the original behaviour, in ms. - */ -#define CHRONO_LOOP_DURATION_MS 4 - -/** - * Time to wait before resetting slaves board, in ms. - */ -#define CHRONO_WAIT_BEFORE_RESET_MS 1000 - -/** - * Number of time to overflow before the end of the match. - */ -static uint32_t chrono_ov_count_; - -/** - * Status of the chrono module. - * Set to 0 if the module is disabled, otherwise set to a non 0 value. - */ -static uint8_t chrono_enabled_ = 0; - - -void -chrono_init (void) -{ - /* Enable chrono. */ - chrono_enable (); - /* Set the overflow counter to the maximum of overflow before the end of - * the match. */ - chrono_ov_count_ = CHRONO_MATCH_OVERFLOW_COUNT; -} - -void -chrono_update (void) -{ - /* Decrement overflow counter if it is possible. */ - if (chrono_enabled_ && chrono_ov_count_) - chrono_ov_count_--; -} - -uint8_t -chrono_is_match_over (void) -{ - if (!chrono_enabled_ || chrono_ov_count_) - return 0; - else - return 1; -} - -void -chrono_enable (void) -{ - chrono_enabled_ = 1; -} - -void -chrono_disable (void) -{ - chrono_enabled_ = 0; -} - -uint8_t -chrono_enabled (void) -{ - return chrono_enabled_; -} - -uint32_t -chrono_remaining_time (void) -{ - return chrono_ov_count_ * MT_TC0_PERIOD; -} - -void -chrono_end_match (uint8_t block) -{ - /* Make sure previous command has been acknowledged. If not, retransmit - * until acknowledged */ - while (!twi_master_sync ()) - utils_delay_ms (CHRONO_LOOP_DURATION_MS); - - /* Make the bot stop moving */ - asserv_stop_motor (); - - /* Wait until complete */ - while (!twi_master_sync ()) - utils_delay_ms (CHRONO_LOOP_DURATION_MS); - - /* Wait CHRONO_WAIT_BEFORE_RESET ms before reseting */ - utils_delay_ms (CHRONO_WAIT_BEFORE_RESET_MS); - /* Reset the asserv board */ - asserv_reset (); - /* Block indefinitely */ - if (block) - while (42) -#ifdef HOST - { - mex_node_wait (); - } -#else - ; -#endif -} diff --git a/digital/io/src/chrono.h b/digital/io/src/chrono.h deleted file mode 100644 index e2b09ac8..00000000 --- a/digital/io/src/chrono.h +++ /dev/null @@ -1,97 +0,0 @@ -#ifndef chrono_h -#define chrono_h -/* chrono.h */ -/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{ - * - * Copyright (C) 2008 Dufour Jérémy - * - * APBTeam: - * Web: http://apbteam.org/ - * Email: team AT apbteam DOT org - * - * 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. - * - * }}} */ - -/** - * @file Module to manage the chrono responsible to stop the bot after 90s. - * - * It is based on the main timer (time/counter 0) to know when to stop the - * bot. - * - * The main loop should never last more than the 4.44ms defined, otherwise, - * this module will not be precise at all! - */ - -/** - * Initialize the chrono module. - * It setups it for a duration of MATCH_DURATION_MS. - */ -void -chrono_init (void); - -/** - * Update chrono module. - * You must call this function every overflow of the main timer. - */ -void -chrono_update (void); - -/** - * Enable chrono module. - * You should call this function when a match start. - */ -void -chrono_enable (void); - -/** - * Disable chrono module. - */ -void -chrono_disable (void); - -/** - * Is chrono module enabled? - * @return 0 if not enabled, other values otherwise. - */ -uint8_t -chrono_enabled (void); - -/** - * Match over? - * @return - * - 0 if the match is not finished yet. - * - 1 if the match is over. - */ -uint8_t -chrono_is_match_over (void); - -/** - * How much time remains before the end of the match. - * @return remaining time in ms. - */ -uint32_t -chrono_remaining_time (void); - -/** - * End the match. - * This function is responsible of resetting the asserv board to stop the bot - * from moving and put the io board in a state where it will not do something. - * @param block blocking function until hardware reset? - */ -void -chrono_end_match (uint8_t block); - -#endif /* chrono_h */ diff --git a/digital/io/src/main.c b/digital/io/src/main.c index b29d1fea..be04366f 100644 --- a/digital/io/src/main.c +++ b/digital/io/src/main.c @@ -39,7 +39,7 @@ #include #endif /* HOST */ -#include "main_timer.h" +#include "timer.h" #include "simu.host.h" #include "asserv.h" @@ -261,7 +261,7 @@ main_init (void) /* Enable interrupts */ sei (); /* Main timer */ - main_timer_init (); + timer_init (); /* Load parameters */ eeprom_load_param (); /* Dirty fix */ @@ -305,14 +305,14 @@ main_loop (void) { /* Wait for an overflow of the main timer (4.444ms) */ { - uint8_t timer_count = main_timer_wait (); + uint8_t timer_count = timer_wait (); if (main_stats_timer_) proto_send1b('M', timer_count); if (timer_count == 1) { /* Main timer has reached overflow earlier! We are late and this is really bad. */ - TRACE (TRACE_MAIN_TIMER__LATE); + TRACE (TRACE_TIMER__LATE); } } diff --git a/digital/io/src/main_timer.avr.c b/digital/io/src/main_timer.avr.c deleted file mode 100644 index bcb185b6..00000000 --- a/digital/io/src/main_timer.avr.c +++ /dev/null @@ -1,60 +0,0 @@ -/* main_timer.avr.c */ -/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{ - * - * Copyright (C) 2009 Dufour Jérémy - * - * APBTeam: - * Web: http://apbteam.org/ - * Email: team AT apbteam DOT org - * - * 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 "common.h" - -#include "main_timer.h" - -#include "modules/utils/utils.h" -#include "io.h" - -void -main_timer_init (void) -{ - /* Configuration of the timer/counter 0: - * - top = 0xff, - * - prescaler = 256, - * -> Fov = F_io / (prescaler * (TOP + 1)) - * -> Tov = 1 / Fov = 4.444 ms. - * Note: if you change the TCCR0 register value, please also update - * MT_TC0_PRESCALER and MT_TC0_TOP. */ - TCCR0 = regv (FOC0, WGM00, COM01, COM0, WGM01, CS02, CS01, CS00, - 0, 0, 0, 0, 0, 1, 1, 0); -} - -uint8_t -main_timer_wait (void) -{ - /* Let's pretend we have reached overflow before calling this function. */ - uint8_t count_before_ov = 1; - /* Loop until an overflow of the timer occurs. */ - while (!(TIFR & _BV (TOV0))) - /* We have not reached overflow. */ - count_before_ov = 0; - /* Write 1 to clear overflow. */ - TIFR = _BV (TOV0); - - return count_before_ov; -} diff --git a/digital/io/src/main_timer.h b/digital/io/src/main_timer.h deleted file mode 100644 index b04c1775..00000000 --- a/digital/io/src/main_timer.h +++ /dev/null @@ -1,78 +0,0 @@ -#ifndef main_timer_h -#define main_timer_h -/* main_timer.h */ -/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{ - * - * Copyright (C) 2009 Dufour Jérémy - * - * APBTeam: - * Web: http://apbteam.org/ - * Email: team AT apbteam DOT org - * - * 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. - * - * }}} */ - -/** - * @file Main timer. - * The main timer is responsible for making the main loop executed regularly - * to a specific time (at least). - * - * Sometimes, the main loop can be executed less frequently that we want - * because it takes too much time! This is bad and should be avoided. - * - * The main timer used the timer/counter 0 of the AVR. - */ - -/** - * Prescaler configured for timer/counter 0. - * If you want to change this value, you also need to change the TCCR0 - * register. - */ -#define MT_TC0_PRESCALER 256L -/** - * Top configured for timer/counter 0. - * If you want to change this value, you also need to change the TCCR0 - * register. - */ -#define MT_TC0_TOP 255 -/** - * Period of timer/counter 0 (in millisecond). - */ -#define MT_TC0_PERIOD \ - (1000.0 / (AC_FREQ / (MT_TC0_PRESCALER * (MT_TC0_TOP + 1)))) - -/** - * Initialize the main timer to 4.444 ms. - * This function setup the timer/counter 0 configuration register. - */ -void -main_timer_init (void); - -/** - * Wait until the main timer overflows. - * @return - * - 0 if we are on time (we have not reached overflow before calling this - * function). - * - 1 if we have already reached overflow. - * @warning if this function return 1, it means we are late and the main loop - * is lasting more than the time configured. Consequence, some important - * functions (like the chronometer for match duration) will not work - * correctly! - */ -uint8_t -main_timer_wait (void); - -#endif /* main_timer_h */ diff --git a/digital/io/src/simu.host.c b/digital/io/src/simu.host.c index 276c3b14..f1b7e0d2 100644 --- a/digital/io/src/simu.host.c +++ b/digital/io/src/simu.host.c @@ -214,13 +214,13 @@ switch_update (void) } void -main_timer_init (void) +timer_init (void) { simu_init (); } uint8_t -main_timer_wait (void) +timer_wait (void) { mex_node_wait_date (mex_node_date () + 4); simu_step (); diff --git a/digital/io/src/trace.trc b/digital/io/src/trace.trc index 4cdcf8e0..1da5012b 100644 --- a/digital/io/src/trace.trc +++ b/digital/io/src/trace.trc @@ -1,4 +1,4 @@ -EVENT main_timer__late "MainTimer: we are late." +EVENT timer__late "Timer: we are late." EVENT fsm__handle_event from 1 event 1 to 1 "FSM: %i =%i=> %i." EVENT move__go_to xd 2 yd 2 "Move: (%d, %d)." EVENT food__best food 1 "Food best: %d." diff --git a/digital/io/src/usdist.c b/digital/io/src/usdist.c index df26ea79..d6fd9730 100644 --- a/digital/io/src/usdist.c +++ b/digital/io/src/usdist.c @@ -24,7 +24,7 @@ * }}} */ #include "common.h" #include "usdist.h" -#include "main_timer.h" +#include "timer.h" #include "modules/adc/adc.h" #include "modules/utils/utils.h" diff --git a/digital/io/src/usdist.h b/digital/io/src/usdist.h index 541d1c93..9a4a6646 100644 --- a/digital/io/src/usdist.h +++ b/digital/io/src/usdist.h @@ -42,7 +42,7 @@ #define USDIST_MM_TOO_FAR 650 /** Measuring period in cycles. */ -#define USDIST_PERIOD_CYCLE (uint8_t) (8.0 / MT_TC0_PERIOD) +#define USDIST_PERIOD_CYCLE (uint8_t) (8.0 / TIMER_PERIOD_MS) /** Array containing the last measures in millimeters. */ extern uint16_t usdist_mm[USDIST_NB]; -- cgit v1.2.3