add a get_State() func
[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
74
75 /* 0 means "programmed"
76  * ---- with 16 Mhz quartz
77  * CKSEL 3-0 : 0111
78  * SUT 1-0 : 10
79  * CKDIV8 : 1
80  * ---- bootloader
81  * BOOTZ 1-0 : 01 (4K bootloader)
82  * BOOTRST : 0 (reset on bootloader)
83  * ---- jtag
84  * jtagen : 0
85  */
86
87 struct genboard gen;
88 struct mainboard mainboard;
89 volatile struct cobboard cobboard;
90 volatile struct ballboard ballboard;
91
92 #ifndef HOST_VERSION
93 /***********************/
94
95 void bootloader(void)
96 {
97 #define BOOTLOADER_ADDR 0x3f000
98         if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) {
99                 printf_P(PSTR("Bootloader is not present\r\n"));
100                 return;
101         }
102         cli();
103         BRAKE_ON();
104         /* ... very specific :( */
105         TIMSK0 = 0;
106         TIMSK1 = 0;
107         TIMSK2 = 0;
108         TIMSK3 = 0;
109         TIMSK4 = 0;
110         TIMSK5 = 0;
111         EIMSK = 0;
112         UCSR0B = 0;
113         UCSR1B = 0;
114         UCSR2B = 0;
115         UCSR3B = 0;
116         SPCR = 0;
117         TWCR = 0;
118         ACSR = 0;
119         ADCSRA = 0;
120
121         EIND = 1;
122         __asm__ __volatile__ ("ldi r31,0xf8\n");
123         __asm__ __volatile__ ("ldi r30,0x00\n");
124         __asm__ __volatile__ ("eijmp\n");
125
126         /* never returns */
127 }
128
129 void do_time_monitor(void *dummy)
130 {
131         uint16_t seconds;
132         seconds = eeprom_read_word(EEPROM_TIME_ADDRESS);
133         seconds ++;
134         eeprom_write_word(EEPROM_TIME_ADDRESS, seconds);
135 }
136
137 void do_led_blink(void *dummy)
138 {
139         static uint8_t a = 0;
140
141         if (mainboard.flags & DO_ERRBLOCKING) {
142                 if (a & 1)
143                         LED1_ON();
144                 else
145                         LED1_OFF();
146         }
147         else {
148                 if (a & 4)
149                         LED1_ON();
150                 else
151                         LED1_OFF();
152         }
153         a++;
154 }
155
156 static void main_timer_interrupt(void)
157 {
158         static uint8_t cpt = 0;
159         cpt++;
160         sei();
161         if ((cpt & 0x3) == 0)
162                 scheduler_interrupt();
163 }
164 #endif
165
166 int main(void)
167 {
168 #ifndef HOST_VERSION
169         /* brake */
170         BRAKE_DDR();
171         BRAKE_OFF();
172
173         /* CPLD reset on PG3 */
174         DDRG |= 1<<3;
175         PORTG &= ~(1<<3); /* implicit */
176
177         /* LEDS */
178         DDRJ |= 0x0c;
179         DDRL = 0xc0;
180         LED1_OFF();
181         LED2_OFF();
182         LED3_OFF();
183         LED4_OFF();
184 #endif
185
186         memset(&gen, 0, sizeof(gen));
187         memset(&mainboard, 0, sizeof(mainboard));
188         mainboard.flags = DO_ENCODERS | DO_CS | DO_RS |
189                 DO_POS | DO_POWER | DO_BD | DO_ERRBLOCKING;
190         ballboard.lcob = I2C_COB_NONE;
191         ballboard.rcob = I2C_COB_NONE;
192
193         /* UART */
194         uart_init();
195         uart_register_rx_event(CMDLINE_UART, emergency);
196 #ifndef HOST_VERSION
197 #if CMDLINE_UART == 3
198         fdevopen(uart3_dev_send, uart3_dev_recv);
199 #elif CMDLINE_UART == 1
200         fdevopen(uart1_dev_send, uart1_dev_recv);
201 #endif
202
203         /* check eeprom to avoid to run the bad program */
204         if (eeprom_read_byte(EEPROM_MAGIC_ADDRESS) !=
205             EEPROM_MAGIC_MAINBOARD) {
206                 int c;
207                 sei();
208                 printf_P(PSTR("Bad eeprom value ('f' to force)\r\n"));
209                 c = uart_recv(CMDLINE_UART);
210                 if (c == 'f')
211                         eeprom_write_byte(EEPROM_MAGIC_ADDRESS, EEPROM_MAGIC_MAINBOARD);
212                 wait_ms(100);
213                 bootloader();
214         }
215 #endif /* ! HOST_VERSION */
216
217         /* LOGS */
218         error_register_emerg(mylog);
219         error_register_error(mylog);
220         error_register_warning(mylog);
221         error_register_notice(mylog);
222         error_register_debug(mylog);
223
224 #ifndef HOST_VERSION
225         /* SPI + ENCODERS */
226         encoders_spi_init(); /* this will also init spi hardware */
227
228         /* I2C */
229         i2c_init(I2C_MODE_MASTER, I2C_MAINBOARD_ADDR);
230         i2c_protocol_init();
231         i2c_register_recv_event(i2c_recvevent);
232         i2c_register_send_event(i2c_sendevent);
233
234         /* TIMER */
235         timer_init();
236         timer0_register_OV_intr(main_timer_interrupt);
237
238         /* PWM */
239         PWM_NG_TIMER_16BITS_INIT(1, TIMER_16_MODE_PWM_10,
240                                  TIMER1_PRESCALER_DIV_1);
241         PWM_NG_TIMER_16BITS_INIT(4, TIMER_16_MODE_PWM_10,
242                                  TIMER4_PRESCALER_DIV_1);
243
244         PWM_NG_INIT16(&gen.pwm1_4A, 4, A, 10, PWM_NG_MODE_SIGNED,
245                       &PORTD, 4);
246         PWM_NG_INIT16(&gen.pwm2_4B, 4, B, 10, PWM_NG_MODE_SIGNED |
247                       PWM_NG_MODE_SIGN_INVERTED, &PORTD, 5);
248         PWM_NG_INIT16(&gen.pwm3_1A, 1, A, 10, PWM_NG_MODE_SIGNED,
249                       &PORTD, 6);
250         PWM_NG_INIT16(&gen.pwm4_1B, 1, B, 10, PWM_NG_MODE_SIGNED,
251                       &PORTD, 7);
252
253
254         /* servos */
255         PWM_NG_TIMER_16BITS_INIT(3, TIMER_16_MODE_PWM_10,
256                                  TIMER1_PRESCALER_DIV_256);
257         PWM_NG_INIT16(&gen.servo1, 3, C, 10, PWM_NG_MODE_NORMAL,
258                       NULL, 0);
259         PWM_NG_TIMER_16BITS_INIT(5, TIMER_16_MODE_PWM_10,
260                                  TIMER1_PRESCALER_DIV_256);
261         PWM_NG_INIT16(&gen.servo2, 5, A, 10, PWM_NG_MODE_NORMAL,
262                       NULL, 0);
263         PWM_NG_INIT16(&gen.servo3, 5, B, 10, PWM_NG_MODE_NORMAL,
264                       NULL, 0);
265         PWM_NG_INIT16(&gen.servo4, 5, C, 10, PWM_NG_MODE_NORMAL,
266                       NULL, 0);
267         support_balls_deploy(); /* init pwm for servos */
268 #endif /* !HOST_VERSION */
269
270         /* SCHEDULER */
271         scheduler_init();
272 #ifdef HOST_VERSION
273         hostsim_init();
274         robotsim_init();
275 #endif
276
277 #ifndef HOST_VERSION
278         scheduler_add_periodical_event_priority(do_led_blink, NULL,
279                                                 100000L / SCHEDULER_UNIT,
280                                                 LED_PRIO);
281 #endif /* !HOST_VERSION */
282
283         /* all cs management */
284         microb_cs_init();
285
286         /* TIME */
287         time_init(TIME_PRIO);
288
289 #ifndef HOST_VERSION
290         /* sensors, will also init hardware adc */
291         sensor_init();
292
293         /* start i2c slave polling */
294         scheduler_add_periodical_event_priority(i2c_poll_slaves, NULL,
295                                                 8000L / SCHEDULER_UNIT, I2C_POLL_PRIO);
296 #endif /* !HOST_VERSION */
297
298         /* strat */
299         gen.logs[0] = E_USER_STRAT;
300         gen.log_level = 5;
301         strat_db_init();
302         test_strat_avoid();
303
304         /* strat-related event */
305         scheduler_add_periodical_event_priority(strat_event, NULL,
306                                                 25000L / SCHEDULER_UNIT,
307                                                 STRAT_PRIO);
308
309 #ifndef HOST_VERSION
310         /* eeprom time monitor */
311         scheduler_add_periodical_event_priority(do_time_monitor, NULL,
312                                                 1000000L / SCHEDULER_UNIT,
313                                                 EEPROM_TIME_PRIO);
314 #endif /* !HOST_VERSION */
315
316         sei();
317
318         printf_P(PSTR("\r\n"));
319         printf_P(PSTR("Respect et robustesse.\r\n"));
320 #ifndef HOST_VERSION
321         {
322                 uint16_t seconds;
323                 seconds = eeprom_read_word(EEPROM_TIME_ADDRESS);
324                 printf_P(PSTR("Running since %d mn %d\r\n"), seconds/60, seconds%60);
325         }
326 #endif
327
328 #ifdef HOST_VERSION
329         strat_reset_pos(400, COLOR_Y(400), COLOR_A(-90));
330 #endif
331
332         cmdline_interact();
333
334         return 0;
335 }