fix display and support beacon in robotsim
[aversive.git] / projects / microb2010 / mainboard / main.c
1 /*
2  *  Copyright Droids Corporation
3  *  Olivier Matz <zer0@droids-corp.org>
4  *
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.
9  *
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.
14  *
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
18  *
19  *  Revision : $Id: main.c,v 1.10 2009-11-08 17:24:33 zer0 Exp $
20  *
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include <aversive.h>
27 #include <aversive/pgmspace.h>
28 #include <aversive/wait.h>
29 #include <aversive/error.h>
30 #include <aversive/eeprom.h>
31
32 #include <ax12.h>
33 #include <uart.h>
34 #include <spi.h>
35 #include <i2c.h>
36 #include <encoders_spi.h>
37 #include <pwm_ng.h>
38 #include <timer.h>
39 #include <scheduler.h>
40 #include <clock_time.h>
41 #include <adc.h>
42
43 #include <pid.h>
44 #include <quadramp.h>
45 #include <control_system_manager.h>
46 #include <trajectory_manager.h>
47 #include <vect_base.h>
48 #include <lines.h>
49 #include <polygon.h>
50 #include <obstacle_avoidance.h>
51 #include <blocking_detection_manager.h>
52 #include <robot_system.h>
53 #include <position_manager.h>
54
55 #include <parse.h>
56 #include <rdline.h>
57
58 #include "../common/eeprom_mapping.h"
59 #include "../common/i2c_commands.h"
60
61 #include "main.h"
62 #include "robotsim.h"
63 #include "ax12_user.h"
64 #include "strat.h"
65 #include "cmdline.h"
66 #include "sensor.h"
67 #include "actuator.h"
68 #include "cs.h"
69 #include "strat_base.h"
70 #include "strat_db.h"
71 #include "strat_avoid.h"
72 #include "i2c_protocol.h"
73 #include "beacon.h"
74
75
76 /* 0 means "programmed"
77  * ---- with 16 Mhz quartz
78  * CKSEL 3-0 : 0111
79  * SUT 1-0 : 10
80  * CKDIV8 : 1
81  * ---- bootloader
82  * BOOTZ 1-0 : 01 (4K bootloader)
83  * BOOTRST : 0 (reset on bootloader)
84  * ---- jtag
85  * jtagen : 0
86  */
87
88 struct genboard gen;
89 struct mainboard mainboard;
90 volatile struct cobboard cobboard;
91 volatile struct ballboard ballboard;
92 volatile struct beaconboard beaconboard;
93
94 #ifndef HOST_VERSION
95 /***********************/
96
97 void bootloader(void)
98 {
99 #define BOOTLOADER_ADDR 0x3f000
100         if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) {
101                 printf_P(PSTR("Bootloader is not present\r\n"));
102                 return;
103         }
104         cli();
105         BRAKE_ON();
106         /* ... very specific :( */
107         TIMSK0 = 0;
108         TIMSK1 = 0;
109         TIMSK2 = 0;
110         TIMSK3 = 0;
111         TIMSK4 = 0;
112         TIMSK5 = 0;
113         EIMSK = 0;
114         UCSR0B = 0;
115         UCSR1B = 0;
116         UCSR2B = 0;
117         UCSR3B = 0;
118         SPCR = 0;
119         TWCR = 0;
120         ACSR = 0;
121         ADCSRA = 0;
122
123         EIND = 1;
124         __asm__ __volatile__ ("ldi r31,0xf8\n");
125         __asm__ __volatile__ ("ldi r30,0x00\n");
126         __asm__ __volatile__ ("eijmp\n");
127
128         /* never returns */
129 }
130
131 void do_time_monitor(void *dummy)
132 {
133         uint16_t seconds;
134         seconds = eeprom_read_word(EEPROM_TIME_ADDRESS);
135         seconds ++;
136         eeprom_write_word(EEPROM_TIME_ADDRESS, seconds);
137 }
138
139 void do_led_blink(void *dummy)
140 {
141         static uint8_t a = 0;
142
143         if (mainboard.flags & DO_ERRBLOCKING) {
144                 if (a & 1)
145                         LED1_ON();
146                 else
147                         LED1_OFF();
148         }
149         else {
150                 if (a & 4)
151                         LED1_ON();
152                 else
153                         LED1_OFF();
154         }
155         a++;
156 }
157
158 static void main_timer_interrupt(void)
159 {
160         static uint8_t cpt = 0;
161         cpt++;
162         sei();
163         if ((cpt & 0x3) == 0)
164                 scheduler_interrupt();
165 }
166 #endif
167
168 int main(void)
169 {
170 #ifndef HOST_VERSION
171         /* brake */
172         BRAKE_DDR();
173         BRAKE_OFF();
174
175         /* CPLD reset on PG3 */
176         DDRG |= 1<<3;
177         PORTG &= ~(1<<3); /* implicit */
178
179         /* LEDS */
180         DDRJ |= 0x0c;
181         DDRL = 0xc0;
182         LED1_OFF();
183         LED2_OFF();
184         LED3_OFF();
185         LED4_OFF();
186 #endif
187
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;
194
195         beaconboard.oppx = I2C_OPPONENT_NOT_THERE;
196
197         /* UART */
198         uart_init();
199         uart_register_rx_event(CMDLINE_UART, emergency);
200 #ifndef HOST_VERSION
201 #if CMDLINE_UART == 3
202         fdevopen(uart3_dev_send, uart3_dev_recv);
203 #elif CMDLINE_UART == 1
204         fdevopen(uart1_dev_send, uart1_dev_recv);
205 #endif
206
207         /* check eeprom to avoid to run the bad program */
208         if (eeprom_read_byte(EEPROM_MAGIC_ADDRESS) !=
209             EEPROM_MAGIC_MAINBOARD) {
210                 int c;
211                 sei();
212                 printf_P(PSTR("Bad eeprom value ('f' to force)\r\n"));
213                 c = uart_recv(CMDLINE_UART);
214                 if (c == 'f')
215                         eeprom_write_byte(EEPROM_MAGIC_ADDRESS, EEPROM_MAGIC_MAINBOARD);
216                 wait_ms(100);
217                 bootloader();
218         }
219 #endif /* ! HOST_VERSION */
220
221         /* LOGS */
222         error_register_emerg(mylog);
223         error_register_error(mylog);
224         error_register_warning(mylog);
225         error_register_notice(mylog);
226         error_register_debug(mylog);
227
228 #ifndef HOST_VERSION
229         /* SPI + ENCODERS */
230         encoders_spi_init(); /* this will also init spi hardware */
231
232         /* I2C */
233         i2c_init(I2C_MODE_MASTER, I2C_MAINBOARD_ADDR);
234         i2c_protocol_init();
235         i2c_register_recv_event(i2c_recvevent);
236         i2c_register_send_event(i2c_sendevent);
237
238         /* TIMER */
239         timer_init();
240         timer0_register_OV_intr(main_timer_interrupt);
241
242         /* PWM */
243         PWM_NG_TIMER_16BITS_INIT(1, TIMER_16_MODE_PWM_10,
244                                  TIMER1_PRESCALER_DIV_1);
245         PWM_NG_TIMER_16BITS_INIT(4, TIMER_16_MODE_PWM_10,
246                                  TIMER4_PRESCALER_DIV_1);
247
248         PWM_NG_INIT16(&gen.pwm1_4A, 4, A, 10, PWM_NG_MODE_SIGNED,
249                       &PORTD, 4);
250         PWM_NG_INIT16(&gen.pwm2_4B, 4, B, 10, PWM_NG_MODE_SIGNED |
251                       PWM_NG_MODE_SIGN_INVERTED, &PORTD, 5);
252         PWM_NG_INIT16(&gen.pwm3_1A, 1, A, 10, PWM_NG_MODE_SIGNED,
253                       &PORTD, 6);
254         PWM_NG_INIT16(&gen.pwm4_1B, 1, B, 10, PWM_NG_MODE_SIGNED,
255                       &PORTD, 7);
256
257
258         /* servos */
259         PWM_NG_TIMER_16BITS_INIT(3, TIMER_16_MODE_PWM_10,
260                                  TIMER1_PRESCALER_DIV_256);
261         PWM_NG_INIT16(&gen.servo1, 3, C, 10, PWM_NG_MODE_NORMAL,
262                       NULL, 0);
263         PWM_NG_TIMER_16BITS_INIT(5, TIMER_16_MODE_PWM_10,
264                                  TIMER1_PRESCALER_DIV_256);
265         PWM_NG_INIT16(&gen.servo2, 5, A, 10, PWM_NG_MODE_NORMAL,
266                       NULL, 0);
267         PWM_NG_INIT16(&gen.servo3, 5, B, 10, PWM_NG_MODE_NORMAL,
268                       NULL, 0);
269         PWM_NG_INIT16(&gen.servo4, 5, C, 10, PWM_NG_MODE_NORMAL,
270                       NULL, 0);
271         support_balls_deploy(); /* init pwm for servos */
272 #endif /* !HOST_VERSION */
273
274         /* SCHEDULER */
275         scheduler_init();
276 #ifdef HOST_VERSION
277         hostsim_init();
278         robotsim_init();
279 #endif
280
281 #ifndef HOST_VERSION
282         scheduler_add_periodical_event_priority(do_led_blink, NULL,
283                                                 100000L / SCHEDULER_UNIT,
284                                                 LED_PRIO);
285 #endif /* !HOST_VERSION */
286
287         /* all cs management */
288         microb_cs_init();
289
290         /* TIME */
291         time_init(TIME_PRIO);
292
293         /* sensors, will also init hardware adc */
294         sensor_init();
295
296         /* beacon */
297         beacon_init();
298
299 #ifndef HOST_VERSION
300         /* start i2c slave polling */
301         scheduler_add_periodical_event_priority(i2c_poll_slaves, NULL,
302                                                 8000L / SCHEDULER_UNIT, I2C_POLL_PRIO);
303 #endif /* !HOST_VERSION */
304
305         /* strat */
306         gen.logs[0] = E_USER_STRAT;
307         gen.log_level = 5;
308
309         /* strat-related event */
310         scheduler_add_periodical_event_priority(strat_event, NULL,
311                                                 25000L / SCHEDULER_UNIT,
312                                                 STRAT_PRIO);
313
314 #ifndef HOST_VERSION
315         /* eeprom time monitor */
316         scheduler_add_periodical_event_priority(do_time_monitor, NULL,
317                                                 1000000L / SCHEDULER_UNIT,
318                                                 EEPROM_TIME_PRIO);
319 #endif /* !HOST_VERSION */
320
321         sei();
322
323         strat_db_init();
324         test_strat_avoid();
325
326         printf_P(PSTR("\r\n"));
327         printf_P(PSTR("Respect et robustesse.\r\n"));
328 #ifndef HOST_VERSION
329         {
330                 uint16_t seconds;
331                 seconds = eeprom_read_word(EEPROM_TIME_ADDRESS);
332                 printf_P(PSTR("Running since %d mn %d\r\n"), seconds/60, seconds%60);
333         }
334 #endif
335
336 #ifdef HOST_VERSION
337         strat_reset_pos(400, COLOR_Y(400), COLOR_A(-90));
338 #endif
339
340         cmdline_interact();
341
342         return 0;
343 }