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