]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/eb_ebb/engine.c
Odometry autocalibration
[eurobot/public.git] / src / eb_ebb / engine.c
1
2
3
4
5 #include <lpc21xx.h>
6 #include <deb_led.h>
7 #include <system_def.h>
8 #include "engine.h"
9
10 #define ENGINE_PIN_A    7       ///< PWM output for engine A, pin P0.7
11 #define ENGINE_PIN_B    8       ///< PWM output for engine B, pin P0.8
12
13 #define ENGINE_ENA      28      ///< Enable output for engine A, pin P1.28
14 #define ENGINE_ENB      30      ///< Enable output for engine B, pin P1.30
15
16 #define ENGINE_IN2A     27      ///< Direction output for engine A , pin P1.27
17 #define ENGINE_IN2B     29      ///< Direction output for engine A , pin P1.29
18
19 #define PWM_FREQ        20000   ///< PWM frequency
20 #define PWM_RES         100     ///< PWM resolution
21
22 #define PWM_STEP ((CPU_APB_HZ / PWM_FREQ)/PWM_RES + 1)  ///< Calculates smallest step PWM for resolution 0~100%
23
24
25
26 unsigned int pwm_A = 0;         ///< Actual value of PWM A
27 unsigned int pwm_B = 0;         ///< Actual value of PWM B
28
29
30 /**
31  *  Enables or disables the ouput on engine A. Causes smooth stop, without break.
32  *
33  * @param status        status parameters are defined in engine.h
34  *
35  */ 
36 void engine_A_en(unsigned status)
37 {
38         if (status == ENGINE_EN_ON) 
39                 IO1SET |= (1<<ENGINE_ENA);
40         else
41                 IO1CLR |= (1<<ENGINE_ENA);
42 }
43
44 /**
45  *  Enables or disables the ouput on engine B. Causes smooth stop, without break.
46  *
47  * @param status        status parameters are defined in engine.h
48  *
49  */ 
50 void engine_B_en(unsigned status)
51 {
52         if (status == ENGINE_EN_ON) 
53                 IO1SET |= (1<<ENGINE_ENB);
54         else
55                 IO1CLR |= (1<<ENGINE_ENB);
56 }
57
58 /**
59  *  Changes motor A direction
60  *
61  * @param dir   direction parameters are defined in engine.h
62  *
63  */ 
64 void engine_A_dir(unsigned dir)
65 {
66         if (dir == ENGINE_DIR_BW) 
67                 IO1SET |= (1<<ENGINE_IN2A);
68         else
69                 IO1CLR |= (1<<ENGINE_IN2A);
70
71         engine_A_pwm(pwm_A);
72         
73 }
74
75 /**
76  *  Changes motor B direction
77  *
78  * @param dir   direction parameters are defined in engine.h
79  *
80  */ 
81 void engine_B_dir(unsigned dir)
82 {
83         if (dir == ENGINE_DIR_BW) 
84                 IO1SET |= (1<<ENGINE_IN2B);
85         else
86                 IO1CLR |= (1<<ENGINE_IN2B);
87
88         engine_B_pwm(pwm_B);
89 }
90
91 /**
92  *  Sets motor A PWM value
93  *
94  * @param pwm   Unsigned int, 0~100%
95  *
96  * @note 0 causes fast stop
97  */ 
98 void engine_A_pwm(unsigned pwm)
99 {
100         if (pwm>100) pwm = 100;
101
102         pwm_A = pwm;
103         if (IO1PIN & (1<<ENGINE_IN2A))  PWMMR2 = PWM_STEP  * (100 - pwm);
104         else PWMMR2 = PWM_STEP  * (pwm);
105         PWMLER |= PWMLER_LA2_m;
106
107 }
108
109 /**
110  *  Sets motor B PWM value
111  *
112  * @param pwm   Unsigned int, 0~100%
113  *
114  * @note 0 causes fast stop
115  */ 
116 void engine_B_pwm(unsigned pwm)
117 {
118         if (pwm>100) pwm = 100;
119         pwm_B = pwm;
120         if (IO1PIN & (1<<ENGINE_IN2B))  PWMMR4 = PWM_STEP  * (100 - pwm);
121         else PWMMR4 = PWM_STEP  * (pwm);        
122         PWMLER |= PWMLER_LA4_m;
123
124 }
125
126
127 /**
128  *  Inicializes Engine A - enables PWM outputs and sets IOs. Uses PWM channel - PWMMR2.
129  *  If is somthing other set on PWM channels, it will affect the main PWM frequency     
130  *  If the engine B is set it will afect its output for one cycle
131  *
132  * 
133  */ 
134 void init_engine_A()
135 {
136         PINSEL0 &= ~((PINSEL_3 << 14) );
137         PINSEL0 |= (PINSEL_2 << 14) ;
138
139         IO0DIR |= (1<<ENGINE_PIN_A);
140         IO1DIR |= (1<<ENGINE_ENA) | (1<<ENGINE_IN2A);
141         IO1SET |= (1<<ENGINE_ENA) | (1<<ENGINE_IN2A);
142         
143         PWMPR = 0;
144         PWMMR0 = CPU_APB_HZ / 20000;
145
146         PWMMR2 =0;      
147         
148         PWMPCR |= PWMPCR_PWMENA2_m;
149         PWMLER |= PWMLER_LA0_m | PWMLER_LA2_m;
150         PWMMCR |= PWMMCR_PWMMR0R_m;
151         PWMTCR |= PWMTCR_CE_m | PWMTCR_EN_m;    
152 }
153
154 /**
155  *  Inicializes Engine B - enables PWM outputs and sets IOs. Uses PWM channel - PWMMR2.
156  *  If is somthing other set on PWM channels, it will affect the main PWM frequency     
157  *  If the engine A is set it will afect its output for one cycle
158  *
159  * 
160  */ 
161 void init_engine_B()
162 {
163         PINSEL0 &= ~((PINSEL_3 << 16));
164         PINSEL0 |= (PINSEL_2 << 16);
165
166         IO0DIR |= (1<<ENGINE_PIN_B);
167         IO1DIR |= (1<<ENGINE_ENB) | (1<<ENGINE_IN2B);
168
169         IO1SET |= (1<<ENGINE_ENB) | (1<<ENGINE_IN2B);
170
171         
172         PWMPR = 0;
173         PWMMR0 = CPU_APB_HZ / 20000;
174
175         PWMMR4 = 0;
176         
177         PWMPCR |= PWMPCR_PWMENA4_m;
178         PWMLER |= PWMLER_LA0_m | PWMLER_LA4_m;
179         PWMMCR |= PWMMCR_PWMMR0R_m;
180         PWMTCR |= PWMTCR_CE_m | PWMTCR_EN_m;    
181 }