2018-02-09 07:53:25 +00:00
|
|
|
|
|
|
|
#include "stm32f1xx_hal.h"
|
|
|
|
#include "defines.h"
|
|
|
|
#include "setup.h"
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
|
|
|
|
volatile int posl = 0;
|
|
|
|
volatile int posr = 0;
|
|
|
|
volatile int pwml = 0;
|
|
|
|
volatile int pwmr = 0;
|
2018-04-07 22:03:35 +00:00
|
|
|
volatile int weakl = 0;
|
|
|
|
volatile int weakr = 0;
|
|
|
|
|
2018-04-09 19:43:59 +00:00
|
|
|
extern volatile int speed;
|
2018-02-09 07:53:25 +00:00
|
|
|
|
|
|
|
extern volatile adc_buf_t adc_buffer;
|
|
|
|
|
2018-02-10 00:37:36 +00:00
|
|
|
extern volatile uint32_t timeout;
|
|
|
|
|
|
|
|
uint8_t buzzerFreq = 0;
|
|
|
|
uint8_t buzzerPattern = 0;
|
|
|
|
|
|
|
|
uint8_t enable = 0;
|
|
|
|
|
2018-02-09 07:53:25 +00:00
|
|
|
const int pwm_res = 64000000 / 2 / PWM_FREQ; // = 2000
|
|
|
|
|
|
|
|
const uint8_t hall_to_pos[8] = {
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
2,
|
|
|
|
1,
|
|
|
|
4,
|
|
|
|
5,
|
|
|
|
3,
|
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
|
|
|
inline void blockPWM(int pwm, int pos, int *u, int *v, int *w) {
|
|
|
|
switch(pos) {
|
|
|
|
case 0:
|
|
|
|
*u = 0;
|
|
|
|
*v = pwm;
|
|
|
|
*w = -pwm;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
*u = -pwm;
|
|
|
|
*v = pwm;
|
|
|
|
*w = 0;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
*u = -pwm;
|
|
|
|
*v = 0;
|
|
|
|
*w = pwm;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
*u = 0;
|
|
|
|
*v = -pwm;
|
|
|
|
*w = pwm;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
*u = pwm;
|
|
|
|
*v = -pwm;
|
|
|
|
*w = 0;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
*u = pwm;
|
|
|
|
*v = 0;
|
|
|
|
*w = -pwm;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*u = 0;
|
|
|
|
*v = 0;
|
|
|
|
*w = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void blockPhaseCurrent(int pos, int u, int v, int *q) {
|
|
|
|
switch(pos) {
|
|
|
|
case 0:
|
|
|
|
*q = u - v;
|
|
|
|
// *u = 0;
|
|
|
|
// *v = pwm;
|
|
|
|
// *w = -pwm;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
*q = u;
|
|
|
|
// *u = -pwm;
|
|
|
|
// *v = pwm;
|
|
|
|
// *w = 0;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
*q = u;
|
|
|
|
// *u = -pwm;
|
|
|
|
// *v = 0;
|
|
|
|
// *w = pwm;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
*q = v;
|
|
|
|
// *u = 0;
|
|
|
|
// *v = -pwm;
|
|
|
|
// *w = pwm;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
*q = v;
|
|
|
|
// *u = pwm;
|
|
|
|
// *v = -pwm;
|
|
|
|
// *w = 0;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
*q = -(u - v);
|
|
|
|
// *u = pwm;
|
|
|
|
// *v = 0;
|
|
|
|
// *w = -pwm;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*q = 0;
|
|
|
|
// *u = 0;
|
|
|
|
// *v = 0;
|
|
|
|
// *w = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-10 00:37:36 +00:00
|
|
|
uint16_t buzzerTimer = 0;
|
2018-02-09 07:53:25 +00:00
|
|
|
|
|
|
|
int offsetcount = 0;
|
|
|
|
int offsetrl1 = 2000;
|
|
|
|
int offsetrl2 = 2000;
|
|
|
|
int offsetrr1 = 2000;
|
|
|
|
int offsetrr2 = 2000;
|
|
|
|
int offsetdcl = 2000;
|
|
|
|
int offsetdcr = 2000;
|
|
|
|
|
2018-07-22 18:34:04 +00:00
|
|
|
float batteryVoltage = BAT_NUMBER_OF_CELLS * 4.0;
|
2018-02-10 00:37:36 +00:00
|
|
|
|
2018-02-09 07:53:25 +00:00
|
|
|
int curl = 0;
|
|
|
|
// int errorl = 0;
|
|
|
|
// int kp = 5;
|
|
|
|
// volatile int cmdl = 0;
|
|
|
|
|
2018-02-18 12:37:50 +00:00
|
|
|
int last_pos = 0;
|
|
|
|
int timer = 0;
|
|
|
|
const int max_time = PWM_FREQ / 10;
|
|
|
|
volatile int vel = 0;
|
|
|
|
|
2018-05-08 10:51:01 +00:00
|
|
|
//scan 8 channels with 2ADCs @ 20 clk cycles per sample
|
|
|
|
//meaning ~80 ADC clock cycles @ 8MHz until new DMA interrupt =~ 100KHz
|
|
|
|
//=640 cpu cycles
|
2018-02-09 07:53:25 +00:00
|
|
|
void DMA1_Channel1_IRQHandler() {
|
|
|
|
DMA1->IFCR = DMA_IFCR_CTCIF1;
|
|
|
|
// HAL_GPIO_WritePin(LED_PORT, LED_PIN, 1);
|
|
|
|
|
|
|
|
if(offsetcount < 1000) { // calibrate ADC offsets
|
|
|
|
offsetcount++;
|
|
|
|
offsetrl1 = (adc_buffer.rl1 + offsetrl1) / 2;
|
|
|
|
offsetrl2 = (adc_buffer.rl2 + offsetrl2) / 2;
|
|
|
|
offsetrr1 = (adc_buffer.rr1 + offsetrr1) / 2;
|
|
|
|
offsetrr2 = (adc_buffer.rr2 + offsetrr2) / 2;
|
|
|
|
offsetdcl = (adc_buffer.dcl + offsetdcl) / 2;
|
|
|
|
offsetdcr = (adc_buffer.dcr + offsetdcr) / 2;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-21 11:56:23 +00:00
|
|
|
if (buzzerTimer % 100 == 0) { // because you get float rounding errors if it would run every time
|
|
|
|
batteryVoltage = batteryVoltage * 0.999 + ((float)adc_buffer.batt1 * ((float)BAT_CALIB_REAL_VOLTAGE / (float)BAT_CALIB_ADC)) * 0.001;
|
2018-02-18 12:37:50 +00:00
|
|
|
}
|
2018-02-18 13:02:05 +00:00
|
|
|
|
2018-05-08 10:51:01 +00:00
|
|
|
//disable PWM when current limit is reached (current chopping)
|
2018-05-06 18:11:53 +00:00
|
|
|
if(ABS((adc_buffer.dcl - offsetdcl) * MOTOR_AMP_CONV_DC_AMP) > DC_CUR_LIMIT || timeout > TIMEOUT || enable == 0) {
|
2018-02-09 07:53:25 +00:00
|
|
|
LEFT_TIM->BDTR &= ~TIM_BDTR_MOE;
|
|
|
|
//HAL_GPIO_WritePin(LED_PORT, LED_PIN, 1);
|
|
|
|
} else {
|
|
|
|
LEFT_TIM->BDTR |= TIM_BDTR_MOE;
|
|
|
|
//HAL_GPIO_WritePin(LED_PORT, LED_PIN, 0);
|
|
|
|
}
|
|
|
|
|
2018-05-06 18:11:53 +00:00
|
|
|
if(ABS((adc_buffer.dcr - offsetdcr) * MOTOR_AMP_CONV_DC_AMP) > DC_CUR_LIMIT || timeout > TIMEOUT || enable == 0) {
|
2018-02-09 07:53:25 +00:00
|
|
|
RIGHT_TIM->BDTR &= ~TIM_BDTR_MOE;
|
|
|
|
} else {
|
|
|
|
RIGHT_TIM->BDTR |= TIM_BDTR_MOE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ul, vl, wl;
|
|
|
|
int ur, vr, wr;
|
|
|
|
|
2018-05-08 10:51:01 +00:00
|
|
|
//determine next position based on hall sensors
|
2018-02-09 07:53:25 +00:00
|
|
|
uint8_t hall_ul = !(LEFT_HALL_U_PORT->IDR & LEFT_HALL_U_PIN);
|
|
|
|
uint8_t hall_vl = !(LEFT_HALL_V_PORT->IDR & LEFT_HALL_V_PIN);
|
|
|
|
uint8_t hall_wl = !(LEFT_HALL_W_PORT->IDR & LEFT_HALL_W_PIN);
|
|
|
|
|
|
|
|
uint8_t hall_ur = !(RIGHT_HALL_U_PORT->IDR & RIGHT_HALL_U_PIN);
|
|
|
|
uint8_t hall_vr = !(RIGHT_HALL_V_PORT->IDR & RIGHT_HALL_V_PIN);
|
|
|
|
uint8_t hall_wr = !(RIGHT_HALL_W_PORT->IDR & RIGHT_HALL_W_PIN);
|
|
|
|
|
|
|
|
uint8_t halll = hall_ul * 1 + hall_vl * 2 + hall_wl * 4;
|
|
|
|
posl = hall_to_pos[halll];
|
|
|
|
posl += 2;
|
|
|
|
posl %= 6;
|
|
|
|
|
|
|
|
uint8_t hallr = hall_ur * 1 + hall_vr * 2 + hall_wr * 4;
|
|
|
|
posr = hall_to_pos[hallr];
|
|
|
|
posr += 2;
|
|
|
|
posr %= 6;
|
|
|
|
|
|
|
|
blockPhaseCurrent(posl, adc_buffer.rl1 - offsetrl1, adc_buffer.rl2 - offsetrl2, &curl);
|
|
|
|
|
2018-04-09 19:43:59 +00:00
|
|
|
//setScopeChannel(2, (adc_buffer.rl1 - offsetrl1) / 8);
|
|
|
|
//setScopeChannel(3, (adc_buffer.rl2 - offsetrl2) / 8);
|
2018-02-09 07:53:25 +00:00
|
|
|
|
2018-05-08 10:51:01 +00:00
|
|
|
//create square wave for buzzer
|
2018-02-10 00:37:36 +00:00
|
|
|
buzzerTimer++;
|
2018-02-26 21:00:09 +00:00
|
|
|
if (buzzerFreq != 0 && (buzzerTimer / 5000) % (buzzerPattern + 1) == 0) {
|
2018-02-10 00:37:36 +00:00
|
|
|
if (buzzerTimer % buzzerFreq == 0) {
|
|
|
|
HAL_GPIO_TogglePin(BUZZER_PORT, BUZZER_PIN);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
HAL_GPIO_WritePin(BUZZER_PORT, BUZZER_PIN, 0);
|
|
|
|
}
|
|
|
|
|
2018-05-08 10:51:01 +00:00
|
|
|
//update PWM channels based on position
|
2018-02-09 07:53:25 +00:00
|
|
|
blockPWM(pwml, posl, &ul, &vl, &wl);
|
|
|
|
blockPWM(pwmr, posr, &ur, &vr, &wr);
|
|
|
|
|
2018-04-07 22:03:35 +00:00
|
|
|
int weakul, weakvl, weakwl;
|
|
|
|
if (pwml > 0) {
|
|
|
|
blockPWM(weakl, (posl+5) % 6, &weakul, &weakvl, &weakwl);
|
|
|
|
} else {
|
|
|
|
blockPWM(-weakl, (posl+1) % 6, &weakul, &weakvl, &weakwl);
|
|
|
|
}
|
|
|
|
ul += weakul;
|
|
|
|
vl += weakvl;
|
|
|
|
wl += weakwl;
|
|
|
|
|
|
|
|
int weakur, weakvr, weakwr;
|
|
|
|
if (pwmr > 0) {
|
|
|
|
blockPWM(weakr, (posr+5) % 6, &weakur, &weakvr, &weakwr);
|
|
|
|
} else {
|
|
|
|
blockPWM(-weakr, (posr+1) % 6, &weakur, &weakvr, &weakwr);
|
|
|
|
}
|
|
|
|
ur += weakur;
|
|
|
|
vr += weakvr;
|
|
|
|
wr += weakwr;
|
|
|
|
|
2018-02-09 07:53:25 +00:00
|
|
|
LEFT_TIM->LEFT_TIM_U = CLAMP(ul + pwm_res / 2, 10, pwm_res-10);
|
|
|
|
LEFT_TIM->LEFT_TIM_V = CLAMP(vl + pwm_res / 2, 10, pwm_res-10);
|
|
|
|
LEFT_TIM->LEFT_TIM_W = CLAMP(wl + pwm_res / 2, 10, pwm_res-10);
|
|
|
|
|
|
|
|
RIGHT_TIM->RIGHT_TIM_U = CLAMP(ur + pwm_res / 2, 10, pwm_res-10);
|
|
|
|
RIGHT_TIM->RIGHT_TIM_V = CLAMP(vr + pwm_res / 2, 10, pwm_res-10);
|
|
|
|
RIGHT_TIM->RIGHT_TIM_W = CLAMP(wr + pwm_res / 2, 10, pwm_res-10);
|
|
|
|
}
|