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