microb 2010
[aversive.git] / projects / microb2010 / tests / test_board2008 / 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.8 2009-05-02 10:08:09 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 <avr/eeprom.h>
31
32 #include <uart.h>
33 #include <pwm_ng.h>
34 #include <timer.h>
35 #include <scheduler.h>
36 #include <time.h>
37 #include <adc.h>
38
39 #include <pid.h>
40 #include <quadramp.h>
41 #include <control_system_manager.h>
42 #include <trajectory_manager.h>
43 #include <blocking_detection_manager.h>
44 #include <robot_system.h>
45 #include <position_manager.h>
46 #include <encoders_microb.h>
47
48 #include <parse.h>
49 #include <rdline.h>
50
51 #include "main.h"
52 #include "cmdline.h"
53 #include "sensor.h"
54 #include "actuator.h"
55 #include "cs.h"
56
57 #if __AVR_LIBC_VERSION__ == 10602UL
58 //#error "won't work with this version"
59 #endif
60
61 /* 0 means "programmed"
62  * ---- with 16 Mhz quartz
63  * CKSEL 3-0 : 0111
64  * SUT 1-0 : 10 
65  * CKDIV8 : 1
66  * ---- bootloader
67  * BOOTZ 1-0 : 01 (4K bootloader)
68  * BOOTRST : 0 (reset on bootloader)
69  * ---- jtag
70  * jtagen : 0
71  */
72
73 struct genboard gen;
74 struct mainboard mainboard;
75
76 void do_led_blink(void *dummy)
77 {
78 #if 1 /* simple blink */
79         LED1_TOGGLE();
80 #endif
81 }
82
83 static void main_timer_interrupt(void)
84 {
85         static uint8_t cpt = 0;
86         static uint8_t encoder_running = 0;
87
88         cpt++;
89
90         /* BAAAAd */
91         if (encoder_running)
92                 return;
93
94         encoder_running = 1;
95         sei();
96
97         encoders_microb_manage(NULL);
98         encoder_running = 0;
99
100         if ((cpt & 0x3) == 0)
101                 scheduler_interrupt();
102 }
103
104 int main(void)
105 {
106         /* LEDs */
107         DDRG = 0x18;
108         DDRB = 0x10;
109
110         /* brake */
111         BRAKE_DDR();
112         BRAKE_OFF();
113
114         LED1_ON();
115         LED2_ON();
116         LED3_ON();
117
118         memset(&gen, 0, sizeof(gen));
119         memset(&mainboard, 0, sizeof(mainboard));
120         mainboard.flags = DO_ENCODERS | DO_RS |
121                 DO_POS | DO_POWER | DO_BD;
122
123         /* UART */
124         uart_init();
125 #if CMDLINE_UART == 0
126         fdevopen(uart0_dev_send, uart0_dev_recv);
127         uart_register_rx_event(0, emergency);
128 #elif CMDLINE_UART == 1
129         fdevopen(uart1_dev_send, uart1_dev_recv);
130         uart_register_rx_event(1, emergency);
131 #else
132 #  error not supported
133 #endif
134
135         /* LOGS */
136         error_register_emerg(mylog);
137         error_register_error(mylog);
138         error_register_warning(mylog);
139         error_register_notice(mylog);
140         error_register_debug(mylog);
141
142         encoders_microb_init(); 
143
144         /* TIMER */
145         timer_init();
146         timer0_register_OV_intr(main_timer_interrupt);
147
148         /* PWM */
149         PWM_NG_TIMER_16BITS_INIT(1, TIMER_16_MODE_PWM_10, 
150                                  TIMER1_PRESCALER_DIV_1);
151         PWM_NG_INIT16(&gen.pwm1_1A, 1, A, 10, PWM_NG_MODE_SIGNED,
152                       &PORTG, 0);
153         PWM_NG_INIT16(&gen.pwm2_1B, 1, B, 10, PWM_NG_MODE_SIGNED,
154                       &PORTG, 1);
155
156
157         /* servos */
158         PWM_NG_TIMER_16BITS_INIT(3, TIMER_16_MODE_PWM_10, 
159                                  TIMER1_PRESCALER_DIV_1);
160         PWM_NG_INIT16(&gen.pwm3_3A, 3, A, 10, PWM_NG_MODE_SIGNED,
161                       &PORTG, 2);
162         PWM_NG_INIT16(&gen.pwm4_3B, 3, B, 10, PWM_NG_MODE_SIGNED | PWM_NG_MODE_SIGN_INVERTED,
163                       &PORTD, 7);
164
165         /* SCHEDULER */
166         scheduler_init();
167
168         scheduler_add_periodical_event_priority(do_led_blink, NULL, 
169                                                 100000L / SCHEDULER_UNIT, 
170                                                 LED_PRIO);
171         /* all cs management */
172         microb_cs_init();
173
174         /* sensors, will also init hardware adc */
175         sensor_init();
176         fessor_init();
177         elevator_init();
178
179         /* TIME */
180         time_init(TIME_PRIO);
181
182         /* strat */
183         gen.logs[0] = E_USER_STRAT;
184         gen.log_level = 5;
185
186         sei();
187
188         printf_P(PSTR("\r\n"));
189         printf_P(PSTR("Respekt Robustheit!\r\n"));
190         cmdline_interact();
191
192         return 0;
193 }