2 * Copyright Droids Corporation
3 * Olivier Matz <zer0@droids-corp.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * Revision : $Id: main.c,v 1.10 2009-11-08 17:24:33 zer0 Exp $
27 #include <aversive/pgmspace.h>
28 #include <aversive/wait.h>
29 #include <aversive/error.h>
30 #include <aversive/eeprom.h>
36 #include <encoders_spi.h>
39 #include <scheduler.h>
40 #include <clock_time.h>
45 #include <control_system_manager.h>
46 #include <trajectory_manager.h>
47 #include <vect_base.h>
50 #include <obstacle_avoidance.h>
51 #include <blocking_detection_manager.h>
52 #include <robot_system.h>
53 #include <position_manager.h>
58 #include "../common/eeprom_mapping.h"
59 #include "../common/i2c_commands.h"
63 #include "ax12_user.h"
69 #include "strat_base.h"
71 #include "strat_avoid.h"
72 #include "i2c_protocol.h"
76 /* 0 means "programmed"
77 * ---- with 16 Mhz quartz
82 * BOOTZ 1-0 : 01 (4K bootloader)
83 * BOOTRST : 0 (reset on bootloader)
89 struct mainboard mainboard;
90 volatile struct cobboard cobboard;
91 volatile struct ballboard ballboard;
92 volatile struct beaconboard beaconboard;
95 /***********************/
99 #define BOOTLOADER_ADDR 0x3f000
100 if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) {
101 printf_P(PSTR("Bootloader is not present\r\n"));
106 /* ... very specific :( */
124 __asm__ __volatile__ ("ldi r31,0xf8\n");
125 __asm__ __volatile__ ("ldi r30,0x00\n");
126 __asm__ __volatile__ ("eijmp\n");
131 void do_time_monitor(void *dummy)
134 seconds = eeprom_read_word(EEPROM_TIME_ADDRESS);
136 eeprom_write_word(EEPROM_TIME_ADDRESS, seconds);
139 void do_led_blink(void *dummy)
141 static uint8_t a = 0;
143 if (mainboard.flags & DO_ERRBLOCKING) {
158 static void main_timer_interrupt(void)
160 static uint8_t cpt = 0;
163 if ((cpt & 0x3) == 0)
164 scheduler_interrupt();
175 /* CPLD reset on PG3 */
177 PORTG &= ~(1<<3); /* implicit */
188 memset(&gen, 0, sizeof(gen));
189 memset(&mainboard, 0, sizeof(mainboard));
190 mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
191 DO_POS | DO_POWER | DO_BD | DO_ERRBLOCKING;
192 ballboard.lcob = I2C_COB_NONE;
193 ballboard.rcob = I2C_COB_NONE;
195 beaconboard.oppx = I2C_OPPONENT_NOT_THERE;
196 beaconboard.posx = I2C_BEACON_NOT_FOUND;
200 uart_register_rx_event(CMDLINE_UART, emergency);
202 #if CMDLINE_UART == 3
203 fdevopen(uart3_dev_send, uart3_dev_recv);
204 #elif CMDLINE_UART == 1
205 fdevopen(uart1_dev_send, uart1_dev_recv);
208 /* check eeprom to avoid to run the bad program */
209 if (eeprom_read_byte(EEPROM_MAGIC_ADDRESS) !=
210 EEPROM_MAGIC_MAINBOARD) {
213 printf_P(PSTR("Bad eeprom value ('f' to force)\r\n"));
214 c = uart_recv(CMDLINE_UART);
216 eeprom_write_byte(EEPROM_MAGIC_ADDRESS, EEPROM_MAGIC_MAINBOARD);
220 #endif /* ! HOST_VERSION */
223 error_register_emerg(mylog);
224 error_register_error(mylog);
225 error_register_warning(mylog);
226 error_register_notice(mylog);
227 error_register_debug(mylog);
231 encoders_spi_init(); /* this will also init spi hardware */
234 i2c_init(I2C_MODE_MASTER, I2C_MAINBOARD_ADDR);
236 i2c_register_recv_event(i2c_recvevent);
237 i2c_register_send_event(i2c_sendevent);
241 timer0_register_OV_intr(main_timer_interrupt);
244 PWM_NG_TIMER_16BITS_INIT(1, TIMER_16_MODE_PWM_10,
245 TIMER1_PRESCALER_DIV_1);
246 PWM_NG_TIMER_16BITS_INIT(4, TIMER_16_MODE_PWM_10,
247 TIMER4_PRESCALER_DIV_1);
249 PWM_NG_INIT16(&gen.pwm1_4A, 4, A, 10, PWM_NG_MODE_SIGNED,
251 PWM_NG_INIT16(&gen.pwm2_4B, 4, B, 10, PWM_NG_MODE_SIGNED |
252 PWM_NG_MODE_SIGN_INVERTED, &PORTD, 5);
253 PWM_NG_INIT16(&gen.pwm3_1A, 1, A, 10, PWM_NG_MODE_SIGNED,
255 PWM_NG_INIT16(&gen.pwm4_1B, 1, B, 10, PWM_NG_MODE_SIGNED,
260 PWM_NG_TIMER_16BITS_INIT(3, TIMER_16_MODE_PWM_10,
261 TIMER1_PRESCALER_DIV_256);
262 PWM_NG_INIT16(&gen.servo1, 3, C, 10, PWM_NG_MODE_NORMAL,
264 PWM_NG_TIMER_16BITS_INIT(5, TIMER_16_MODE_PWM_10,
265 TIMER1_PRESCALER_DIV_256);
266 PWM_NG_INIT16(&gen.servo2, 5, A, 10, PWM_NG_MODE_NORMAL,
268 PWM_NG_INIT16(&gen.servo3, 5, B, 10, PWM_NG_MODE_NORMAL,
270 PWM_NG_INIT16(&gen.servo4, 5, C, 10, PWM_NG_MODE_NORMAL,
272 support_balls_deploy(); /* init pwm for servos */
273 #endif /* !HOST_VERSION */
283 scheduler_add_periodical_event_priority(do_led_blink, NULL,
284 100000L / SCHEDULER_UNIT,
286 #endif /* !HOST_VERSION */
288 /* all cs management */
292 time_init(TIME_PRIO);
294 /* sensors, will also init hardware adc */
301 /* start i2c slave polling */
302 scheduler_add_periodical_event_priority(i2c_poll_slaves, NULL,
303 8000L / SCHEDULER_UNIT, I2C_POLL_PRIO);
304 #endif /* !HOST_VERSION */
307 gen.logs[0] = E_USER_STRAT;
310 /* strat-related event */
311 scheduler_add_periodical_event_priority(strat_event, NULL,
312 25000L / SCHEDULER_UNIT,
316 /* eeprom time monitor */
317 scheduler_add_periodical_event_priority(do_time_monitor, NULL,
318 1000000L / SCHEDULER_UNIT,
320 #endif /* !HOST_VERSION */
326 printf_P(PSTR("\r\n"));
327 printf_P(PSTR("Respect et robustesse.\r\n"));
331 seconds = eeprom_read_word(EEPROM_TIME_ADDRESS);
332 printf_P(PSTR("Running since %d mn %d\r\n"), seconds/60, seconds%60);
337 strat_reset_pos(400, COLOR_Y(400), COLOR_A(-90));