Commit a8e5d8c8 authored by Jake Read's avatar Jake Read

does speed acceleration to slew between open loop speeds

parent 222f289a
......@@ -12,6 +12,9 @@ void bldc_init(bldc_t *bldc){
bldc->comState = 0;
bldc->comDir = 1;
bldc->comDuty = 0;
bldc->targetSpeed = 0;
bldc->currentSpeed = 0;
}
void bldc_shutdown(bldc_t *bldc){
......@@ -23,10 +26,22 @@ void bldc_shutdown(bldc_t *bldc){
void bldc_setDuty(bldc_t *bldc, uint32_t duty){
// blind pwm duty 0-512
(duty > 512) ? duty = 512 : (0);
// hard stop at 256 to avoid breaking things
(duty > 256) ? duty = 256 : (0);
bldc->comDuty = (uint16_t) duty;
}
void bldc_setTargetSpeed(bldc_t *bldc, int32_t speed){
// assert limits
if(speed > 20000){
speed = 20000;
} else if (speed < -20000){
speed = -20000;
}
bldc->targetSpeed = speed;
}
void bldc_setSpeed(bldc_t *bldc, int32_t speed){
// speed in eRPM
// assert max
......@@ -53,5 +68,5 @@ void bldc_setSpeed(bldc_t *bldc, int32_t speed){
void bldc_newSpeed(bldc_t *bldc, int32_t speed, uint32_t duty){
bldc_setDuty(bldc, duty);
bldc_setSpeed(bldc, speed);
bldc_setTargetSpeed(bldc, speed);
}
\ No newline at end of file
......@@ -16,6 +16,8 @@ typedef struct{
uint8_t comDir;
uint16_t comDuty;
int32_t currentSpeed;
int32_t targetSpeed;
uint16_t comPeriod;
}bldc_t;
......@@ -25,6 +27,8 @@ void bldc_shutdown(bldc_t *bldc);
void bldc_setDuty(bldc_t *bldc, uint32_t duty);
void bldc_setTargetSpeed(bldc_t *bldc, int32_t speed);
void bldc_setSpeed(bldc_t *bldc, int32_t speed);
void bldc_start(bldc_t *bldc, int32_t speed, uint32_t duty);
......
......@@ -151,7 +151,7 @@ void tickers_init(void){
// and a reasonable speed for acceleration ticking
uint16_t perAccelRate = 2400;
uint16_t perAccelRate = 800;
uint8_t perAl = (uint8_t) perAccelRate;
uint8_t perAh = (uint8_t) (perAccelRate >> 8);
......@@ -188,7 +188,7 @@ int main(void)
// initialize the bldc state structure
bldc_init(&bldc);
// on startup speed and duty
bldc_setSpeed(&bldc, 1000);
bldc_setTargetSpeed(&bldc, 1000);
bldc_setDuty(&bldc, 10);
// startup the driver
drv_init();
......@@ -241,7 +241,15 @@ ISR(TCD0_OVF_vect){
// acceleration timer
ISR(TCD1_OVF_vect){
//pin_toggle(&tstpin2);
if(bldc.currentSpeed != bldc.targetSpeed){
if(bldc.currentSpeed < bldc.targetSpeed){
bldc.currentSpeed ++;
bldc_setSpeed(&bldc, bldc.currentSpeed);
} else {
bldc.currentSpeed --;
bldc_setSpeed(&bldc, bldc.currentSpeed);
}
}
}
ISR(USARTE1_RXC_vect){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment