forked from esden/open-bldc-mk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bldc.c
317 lines (283 loc) · 7.95 KB
/
bldc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Open-BLDC Firmware, Firmware for BrushLess Drive Controllers
* Copyright (C) 2009 Piotr Esden-Tempski <piotr at esden.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <stdint.h>
#include "settings.h"
#include "timer.h"
#include "led.h"
#include "uart.h"
#include "bldc_utils.h"
#include "bldc.h"
volatile uint8_t bldc_phase = 0;
volatile uint16_t bldc_cnt_comm = 0;
uint8_t bldc_pwm = 0;
uint8_t bldc_pwm_max = PWM_MAX;
uint8_t bldc_running = 0;
uint8_t bldc_do_start = 0;
uint8_t bldc_old_phase = 0;
void bldc_set_pwm(uint8_t);
int8_t bldc_start();
void bldc_set_comm();
void bldc_init() {
DDRD |= 0x38;
PORTD = 0x00;
DDRB |= 0x0E;
PORTB = 0x31;
PWM_OFF();
/* enable analog comperator */
SFIOR = 0x08;
BEMF_SET_B();
/* PWM generator timer */
TCCR1B = (1 << CS10) | //
(0 << CS11) | //
(0 << CS12) | // clkI/O /1 (No prescaling)
(0 << WGM12) | //
(0 << WGM13) | //
(0 << ICES1) | //
(0 << ICNC1);
}
void bldc_set_pwm(uint8_t val) {
unsigned char pwm_tmp;
pwm_tmp = bldc_pwm = val;
if (pwm_tmp > bldc_pwm_max) {
pwm_tmp = bldc_pwm_max;
LED_RED_ON();
}
PWM_SET(pwm_tmp);
}
/**
* Generates a manual field rotation to give an initial impulse to the
* motor.
*/
int8_t bldc_start() {
unsigned long timer = START_MAX, i;
unsigned char maintain = START_MAINTAIN;
bldc_phase = 0;
DISABLE_BEMF_INT();
bldc_set_pwm(PWM_START);
bldc_set_comm();
while (1) {
for (i = 0; i < timer; i++) {
timer_wait(100);
}
timer -= START_DEC(timer);
if (timer < START_MIN) {
if (maintain) {
timer = START_MIN;
maintain--;
} else {
return 1;
}
}
bldc_set_comm();
bldc_phase++;
bldc_phase %= 6;
if (BEMF) {
LED_GREEN_TOGGLE();
}
}
return 0;
}
/**
* Monitor the motor and control it's speed.
*/
void bldc_run() {
//wdt_enable(WDTO_2S);
uint16_t bldc_stop_detect_timer = timer_new_sw_ms(64);
uint16_t speed_report = timer_new_sw_ms(50);
while (1) {
wdt_reset();
static uint8_t uart_cmd = 0;
uint8_t ch = 0;
if (getch((char*) &ch) == UART_SUCCESS) {
if (ch <= '9' && ch >= '1') {
ch = ch - '0';
ch = ((uint16_t) ch) * 255 / 9;
bldc_set_pwm(uart_cmd = ch);
print_str("cmd:");
print_number(ch, 10);
print('\n');
} else if (ch == ',') {
print_str("=");
bldc_phase = 0;
bldc_old_phase = bldc_phase;
DISABLE_BEMF_INT();
bldc_set_pwm(5);
bldc_pwm = 0;
bldc_running = 0;
bldc_set_comm();
bldc_cnt_comm = 0;
} else if (ch == '.') {
do {
bldc_phase++;
bldc_phase %= 6;
bldc_set_comm();
bldc_cnt_comm++;
if (BEMF) {
LED_GREEN_TOGGLE();
}
for (int ee = 0; ee < 20 * 10; ee++)
timer_wait(100);
} while (bldc_phase != 0);
bldc_running = 0;
bldc_old_phase = bldc_phase;
print_str("stop.\n");
print_number(bldc_cnt_comm, 10);
} else {
// echo
while (!(UCSRA & (1 << UDRE)))
;
UDR = ch;
print_str("stop.\n");
bldc_set_pwm(0);
LED_RED_ON();
DISABLE_BEMF_INT();
bldc_running = 0;
SET_ALL_OFF();
uart_cmd = 0;
}
}
if (bldc_old_phase != bldc_phase) {
bldc_stop_detect_timer = timer_new_sw_ms(125);
bldc_running = 1;
bldc_old_phase = bldc_phase;
}
if (bldc_running && timer_sw_check(bldc_stop_detect_timer)) {
print_str("stalled\n");
LED_RED_ON();
DISABLE_BEMF_INT();
bldc_running = 0;
SET_ALL_OFF();
}
if (!bldc_running) {
LED_GREEN_OFF();
bldc_do_start = (bldc_pwm != 0);
}
if (bldc_running && timer_sw_check(speed_report)) {
const uint16_t dt = 20; //ms
speed_report = timer_new_sw_ms(dt);
static uint16_t previous_cnt = 0;
uint32_t rotations = (bldc_cnt_comm - previous_cnt);
uint32_t rps = rotations * (1000L / dt) / 66;
bldc_set_pwm(uart_cmd);
print_number(rps, 10);
print('\n');
previous_cnt = bldc_cnt_comm;
}
if (bldc_do_start) {
LED_RED_OFF();
bldc_do_start = 0;
print_str("starting...");
if (bldc_start()) {
print_str("done.\n");
LED_GREEN_ON();
bldc_running = 1;
BEMF_TOGGLE_INT();
ENABLE_BEMF_INT();
bldc_set_pwm(PWM_MIN);
bldc_stop_detect_timer = timer_new_sw_ms(75);
while (!timer_sw_check(bldc_stop_detect_timer)) {
asm("nop");
}
bldc_old_phase = 7;
} else
bldc_do_start = 1;
}
}
}
/**
* Enforce commutation setting.
*/
void bldc_set_comm() {
switch (bldc_phase) {
case 0:
SET_A_H();
SET_B_L();
BEMF_SET_C();
BEMF_RISING_INT();
break;
case 1:
SET_A_H();
SET_C_L();
BEMF_SET_B();
BEMF_FALLING_INT();
break;
case 2:
SET_B_H();
SET_C_L();
BEMF_SET_A();
BEMF_RISING_INT();
break;
case 3:
SET_B_H();
SET_A_L();
BEMF_SET_C();
BEMF_FALLING_INT();
break;
case 4:
SET_C_H();
SET_A_L();
BEMF_SET_B();
BEMF_RISING_INT();
break;
case 5:
SET_C_H();
SET_B_L();
BEMF_SET_A();
BEMF_FALLING_INT();
break;
}
}
/*
* Handle commutation.
*
* The analog comparator compares the positive pin AIN0 and negative
* pin AIN1. When the voltage on the positive pin AIN0 is higher then
* the voltage on the negative pin AIN1, the Analog Comparator Output,
* ACO, is set. The following interrupt gets called on ACO raise, fall
* or toggle depending on the call of BEMF_RISING_INT(),
* BEMF_FALLING_INT() or BEMF_TOGGLE_INT() macros.
*/
ISR(ANA_COMP_vect)
{
unsigned char i;
uint8_t cnt = 0;
/* debounce the bemf signal */
for (i = 0; i < BEMF_DEBOUNCE_COUNT && cnt != 50; i++) {
if (bldc_phase & 1) {
if (BEMF_L) {
i -= BEMF_DEBOUNCE_DEC;
//print_str("o");
cnt++;
}
} else {
if (BEMF_H) {
i -= BEMF_DEBOUNCE_DEC;
//print_str("O");
cnt++;
}
}
}
bldc_set_comm();
bldc_phase++;
bldc_phase %= 6;
bldc_cnt_comm++;
//print_str("_");
}