]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/eb_ebb/main.c
Odometry autocalibration
[eurobot/public.git] / src / eb_ebb / main.c
1 // $Id$
2 // $Name$
3 // $ProjectName$
4
5 /**
6  * \mainpage EB-ebb code sample
7  *
8  * \section Introduction
9  * This codes are designed for use with ebBoard developed in DCE CVUT.
10  * There are two purpose of this program: 
11  *
12  *      1) library named ebb, it contains function for handling engines,
13  *         servos, ADC (median or avery filtered) and power switch.
14  *
15  *      2) in main.c file is simply HOW-TO use this libraries
16  *
17  * Short description of ebb library:
18  * 
19  *      ADC \96 This library use 4 ADC channels which parameters I defined 
20  *      in adc.h or adc_filtr.h.  There is two implementation of ADC output
21  *      filter. First is simply averaging ((last value + actual value)/2),
22  *      for this filter you have to include adc.h. The Second filter is 
23  *      median filter in adc_filtr.h . The size of this filter is defined
24  *      in adc_filtr.h. 
25  *
26  *      PowerSwitch \96 ebboard have one 6A power switch which can be used for
27  *       switching small loads (capacity or resistant recommended). In powswitch.h is defined initialization, on and off function.
28  *
29  *      Servos \96 ebboard have three servo connectors. In servo.h is defined 
30  *      functions for setting them. The servo range is divided into 256 (0~255)
31  *       steps.
32  *
33  *      Engines \96 this part can controls up two DC motors (5A each) or in 
34  *      special cases one motor (up to 10A). In engines.h is defined several 
35  *      controls function. Read carefully description in this section. Is 
36  *      important to understanding the way how to stop engines. 
37  *
38  */
39
40 /**
41  * @file   main.c
42  * 
43 * @brief  Demo application demonstrating use of eb_ebb library.
44  *      This file provides simply how-to use eb_ebb library.
45  *      From main function is called init_perip function 
46  *      where is initialized servos, engines, power switch,
47  *      CAN,ADC and serial port. After this initialization is shown 
48  *  how to control each devices. This sample also include simply 
49  *  sample of sending and receiving CAN message.
50  * 
51  */
52
53
54 #include <lpc21xx.h>                            /* LPC21xx definitions */
55 #include <errno.h>
56 #include <periph/can.h>
57 #include <string.h>
58 #include <deb_led.h>
59 #include "uart.h"
60 #include "servo.h"
61 #include "powswitch.h"
62 #include "engine.h"
63 //#include "adc.h"              // header ADC with averaging filter
64 #include "adc_filtr.h"          // header ADC with mean filter  
65
66
67
68
69 #define CAN_SPEED       1000000         ///< CAN speed
70
71 #define CAN_SERVO       0x32            ///< CAN ID for servo message
72
73 /**
74  * Variables ISR values
75  */
76 /*@{*/
77 #define CAN_ISR         0
78 #define ENG_ISR         1
79 #define ADC_ISR         3
80 #define SERVO_ISR       5
81 /*@}*/
82
83 #define SPEED           30      ///<  Motor speed
84
85
86 /**
87  *  Dummy wait (busy wait)
88  *  This function shoul be removed in future
89  */ 
90 void dummy_wait(void)
91 {
92     int i = 1000000;
93     
94     while(--i);
95 }
96
97 /**
98  *  This function is called when we recieve CAN message.
99  *  Its called from CAN ISR
100  *
101  * @param *msg  structure of can message (can_msg_t)
102  *
103  */ 
104 void can_rx(can_msg_t *msg) {
105         can_msg_t rx_msg;
106         
107         memcpy(&rx_msg, msg, sizeof(can_msg_t));
108
109         switch (rx_msg.id) {            // demo sample parsing recieved message by ID
110                 case CAN_SERVO:
111                         
112                         set_servo(0, rx_msg.data[0]);
113                         set_servo(1, rx_msg.data[1]);
114                         set_servo(2, rx_msg.data[2]);
115                         break;
116
117                 default:
118                         break;
119         }
120 }
121
122
123 /**
124  *  Inicializes pepherials used in ebb library - sample use
125  *
126  */ 
127 void init_perip(void)      
128 {
129         init_servo(SERVO_ISR);
130         init_pow_switch();
131
132         init_engine_A();
133         init_engine_B();
134
135         init_uart0((int)9600 ,UART_BITS_8, UART_STOP_BIT_1, UART_PARIT_OFF, 0 );
136         //init_adc(ADC_ISR);                    // inicialization ADC with averaging filter
137         init_adc_filter(ADC_ISR);               // inicialization ADC with mean filter
138         can_init_baudrate(CAN_SPEED, CAN_ISR, can_rx);
139 }
140
141
142 /**
143  *  Main function contains a small sample how to use ebb library.
144  *
145  */
146 int main (void)  {
147
148         init_perip();                   // sys init MCU
149
150         set_servo(0, 0x80);             // set servo 1 to center position 
151         set_servo(1, 0x80);             // set servo 2 to center position 
152         set_servo(2, 0x80);             // set servo 3 to center position 
153
154         engine_A_dir(ENGINE_DIR_FW);            // set engine A to direction forward
155         engine_B_dir(ENGINE_DIR_BW);            // set engine B to direction backward
156         engine_A_en(ENGINE_EN_ON);              // enables engine A
157         engine_B_en(ENGINE_EN_ON);              // enables engine B
158         engine_A_pwm(SPEED);                    // sets speed on Engine A
159         engine_B_pwm(SPEED);                    // sets speed on Engine B
160
161         
162         can_msg_t msg;                  // This part is sending CAN messge 
163         msg.id = 0xAA;                  // CAN message ID
164         msg.flags = 0;                  
165         msg.dlc = 1;                    // number of transmited data bytes
166         msg.data[0] = 0x55;             // data byte
167         while(can_tx_msg(&msg));        // sending function
168         
169
170         while(1)                // this will blink for ever
171         {
172                 __deb_led_on(LEDG);
173                 dummy_wait();
174                 __deb_led_off(LEDG);
175                 dummy_wait();
176         } 
177 }
178
179
180