Skip to content
Snippets Groups Projects
Commit 222f289a authored by Jake Read's avatar Jake Read
Browse files

bldc spins, accepts speed commands, now looking for acceleration

parent ad0bd0a9
No related branches found
No related tags found
No related merge requests found
...@@ -12,6 +12,7 @@ Power is bussed to the board with two M3 Screw Terminals. The board includes roo ...@@ -12,6 +12,7 @@ Power is bussed to the board with two M3 Screw Terminals. The board includes roo
- label lights - label lights
- wants one lo-side debug pin! - wants one lo-side debug pin!
- for resets etc, pull en to gnd on other side of switch!
- might have to go to DRV8320 - newer, available - might have to go to DRV8320 - newer, available
- CSD88548 is CSD88599 but more amps less volts, use these - CSD88548 is CSD88599 but more amps less volts, use these
......
...@@ -39,6 +39,7 @@ SUBDIRS := ...@@ -39,6 +39,7 @@ SUBDIRS :=
C_SRCS += \ C_SRCS += \
../atkhandler.c \ ../atkhandler.c \
../atkport.c \ ../atkport.c \
../bldc.c \
../fastmath.c \ ../fastmath.c \
../main.c \ ../main.c \
../pin.c \ ../pin.c \
...@@ -57,6 +58,7 @@ ASM_SRCS += ...@@ -57,6 +58,7 @@ ASM_SRCS +=
OBJS += \ OBJS += \
atkhandler.o \ atkhandler.o \
atkport.o \ atkport.o \
bldc.o \
fastmath.o \ fastmath.o \
main.o \ main.o \
pin.o \ pin.o \
...@@ -68,6 +70,7 @@ uartport.o ...@@ -68,6 +70,7 @@ uartport.o
OBJS_AS_ARGS += \ OBJS_AS_ARGS += \
atkhandler.o \ atkhandler.o \
atkport.o \ atkport.o \
bldc.o \
fastmath.o \ fastmath.o \
main.o \ main.o \
pin.o \ pin.o \
...@@ -79,6 +82,7 @@ uartport.o ...@@ -79,6 +82,7 @@ uartport.o
C_DEPS += \ C_DEPS += \
atkhandler.d \ atkhandler.d \
atkport.d \ atkport.d \
bldc.d \
fastmath.d \ fastmath.d \
main.d \ main.d \
pin.d \ pin.d \
...@@ -90,6 +94,7 @@ uartport.d ...@@ -90,6 +94,7 @@ uartport.d
C_DEPS_AS_ARGS += \ C_DEPS_AS_ARGS += \
atkhandler.d \ atkhandler.d \
atkport.d \ atkport.d \
bldc.d \
fastmath.d \ fastmath.d \
main.d \ main.d \
pin.d \ pin.d \
...@@ -130,6 +135,8 @@ LINKER_SCRIPT_DEP+= ...@@ -130,6 +135,8 @@ LINKER_SCRIPT_DEP+=
./%.o: .././%.c ./%.o: .././%.c
@echo Building file: $< @echo Building file: $<
......
...@@ -6,6 +6,8 @@ atkhandler.c ...@@ -6,6 +6,8 @@ atkhandler.c
atkport.c atkport.c
bldc.c
fastmath.c fastmath.c
main.c main.c
......
...@@ -148,6 +148,12 @@ ...@@ -148,6 +148,12 @@
<Compile Include="atkport.h"> <Compile Include="atkport.h">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
<Compile Include="bldc.c">
<SubType>compile</SubType>
</Compile>
<Compile Include="bldc.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="fastmath.c"> <Compile Include="fastmath.c">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
......
...@@ -8,7 +8,8 @@ ...@@ -8,7 +8,8 @@
#include "hardware.h" #include "hardware.h"
#include "atkhandler.h" #include "atkhandler.h"
int32_t steps; uint8_t testReply[4] = {127, 12, 24, 48};
uint8_t speedReply[4] = {141, 13, 25, 49};
void atk_handle_packet(uint8_t *packet, uint8_t length){ void atk_handle_packet(uint8_t *packet, uint8_t length){
// dirty debug reply // dirty debug reply
...@@ -16,7 +17,6 @@ void atk_handle_packet(uint8_t *packet, uint8_t length){ ...@@ -16,7 +17,6 @@ void atk_handle_packet(uint8_t *packet, uint8_t length){
// through packet // through packet
int i = 0; int i = 0;
int atk_handler_state = ATK_HANDLER_OUTSIDE; int atk_handler_state = ATK_HANDLER_OUTSIDE;
uint8_t testReply[4] = {127, 12, 24, 48};
while(i < length){ // prep for the messy double switch :| while(i < length){ // prep for the messy double switch :|
switch (atk_handler_state){ switch (atk_handler_state){
...@@ -50,7 +50,16 @@ void atk_handle_packet(uint8_t *packet, uint8_t length){ ...@@ -50,7 +50,16 @@ void atk_handle_packet(uint8_t *packet, uint8_t length){
break; break;
case DELIM_KEY_SPEED: case DELIM_KEY_SPEED:
// set timers // integer signed so that we can do dir with the same command
if(i + 9 > length){
i ++;
} else {
int32_t speed = ((int32_t)packet[i+1] << 24) | ((int32_t)packet[i+2] << 16) | ((int32_t)packet[i+3] << 8) | (int32_t)packet[i+4];
uint32_t duty = ((int32_t)packet[i+5] << 24) | ((int32_t)packet[i+6] << 16) | ((int32_t)packet[i+7] << 8) | (int32_t)packet[i+8];
i += 9;
bldc_newSpeed(&bldc, speed, duty);
atk_reply_packet(packet, speedReply, 4);
}
break; break;
case DELIM_KEY_TRAPEZOID: case DELIM_KEY_TRAPEZOID:
......
/*
* bldc.c
*
* Created: 7/12/2018 1:52:34 PM
* Author: Jake
*/
#include "bldc.h"
#include "hardware.h"
void bldc_init(bldc_t *bldc){
bldc->comState = 0;
bldc->comDir = 1;
bldc->comDuty = 0;
}
void bldc_shutdown(bldc_t *bldc){
pin_clear(&drvEnPin);
bldc->comState = 0;
bldc->comDir = 1;
bldc->comDuty = 0;
}
void bldc_setDuty(bldc_t *bldc, uint32_t duty){
// blind pwm duty 0-512
(duty > 512) ? duty = 512 : (0);
bldc->comDuty = (uint16_t) duty;
}
void bldc_setSpeed(bldc_t *bldc, int32_t speed){
// speed in eRPM
// assert max
uint32_t sAbs = abs(speed);
(sAbs > 20000) ? sAbs = 20000 : (0);
// check dir
if(speed == 0){
bldc_shutdown(bldc);
} else if (speed > 0){
bldc->comDir = 1;
} else {
bldc->comDir = 0;
}
// base time, and we want 6 steps / rev, and rpm-rps
bldc->comPeriod = COMTICKER_TICKS_SECOND / (sAbs / 10);
// set a new timer period
uint8_t ctPerBufL = (uint8_t) bldc->comPeriod;
uint8_t ctPerBufH = (uint8_t) (bldc->comPeriod >> 8);
TCD0.PERBUFL = ctPerBufL;
TCD0.PERBUFH = ctPerBufH;
}
void bldc_newSpeed(bldc_t *bldc, int32_t speed, uint32_t duty){
bldc_setDuty(bldc, duty);
bldc_setSpeed(bldc, speed);
}
\ No newline at end of file
/*
* bldc.h
*
* Created: 7/12/2018 1:52:26 PM
* Author: Jake
*/
#ifndef BLDC_H_
#define BLDC_H_
#include "avr/io.h"
typedef struct{
uint8_t comState;
uint8_t comDir;
uint16_t comDuty;
uint16_t comPeriod;
}bldc_t;
void bldc_init(bldc_t *bldc);
void bldc_shutdown(bldc_t *bldc);
void bldc_setDuty(bldc_t *bldc, uint32_t duty);
void bldc_setSpeed(bldc_t *bldc, int32_t speed);
void bldc_start(bldc_t *bldc, int32_t speed, uint32_t duty);
void bldc_newSpeed(bldc_t *bldc, int32_t speed, uint32_t duty);
#endif /* BLDC_H_ */
\ No newline at end of file
...@@ -16,12 +16,17 @@ ...@@ -16,12 +16,17 @@
#include "atkport.h" #include "atkport.h"
#include "atkhandler.h" #include "atkhandler.h"
#include "ams5047.h" #include "ams5047.h"
#include "bldc.h"
// results in 1MBaud // results in 1MBaud
#define SYSTEM_BAUDA 3 #define SYSTEM_BAUDA 3
#define SYSTEM_BAUDB 0 #define SYSTEM_BAUDB 0
#define SYSTEM_NUM_UPS 1 #define SYSTEM_NUM_UPS 1
// ticker bases
#define COMTICKER_TICKS_SECOND 750000
pin_t stlclk; pin_t stlclk;
pin_t stlerr; pin_t stlerr;
...@@ -70,6 +75,11 @@ pin_t hi3; ...@@ -70,6 +75,11 @@ pin_t hi3;
// controller functions // controller functions
bldc_t bldc;
uint8_t comState;
uint16_t comDuty;
// bldc_t bldc; // bldc_t bldc;
pin_t tstpin1; pin_t tstpin1;
......
...@@ -66,6 +66,15 @@ void pwm_periods(uint16_t peru, uint16_t perv, uint16_t perw){ ...@@ -66,6 +66,15 @@ void pwm_periods(uint16_t peru, uint16_t perv, uint16_t perw){
TCC0.CCCBUFH = (uint8_t) (peru >> 8); TCC0.CCCBUFH = (uint8_t) (peru >> 8);
} }
void pwm_by_offset(int16_t ofu, int16_t ofv, int16_t ofw){
// +ve offset to spend more time with hi-side off, signals are complimentary
uint16_t peru = 512 + ofu;
uint16_t perv = 512 + ofv;
uint16_t perw = 512 + ofw;
// now through business
pwm_periods(peru, perv, perw);
}
void pwm_init(void){ void pwm_init(void){
// setup awex etc // setup awex etc
...@@ -94,34 +103,62 @@ void pwm_init(void){ ...@@ -94,34 +103,62 @@ void pwm_init(void){
AWEXC.DTBOTHBUF = 4; // four counts of pwm clock for deadtime AWEXC.DTBOTHBUF = 4; // four counts of pwm clock for deadtime
AWEXC.OUTOVEN = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5); AWEXC.OUTOVEN = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5);
pwm_periods(256, 512, 768); pwm_periods(0, 0, 0);
} }
void drv_init(void){ void drv_init(void){
// mode pins
pin_init(&drvEnPin, &PORTD, PIN1_bm, 1, 1);
pin_init(&drvModePwm, &PORTB, PIN6_bm, 6, 1);
pin_init(&drvModeGain, &PORTC, PIN6_bm, 6, 1);
pin_init(&drvDcCal, &PORTB, PIN7_bm, 7, 1);
// status
pin_init(&drvFault, &PORTC, PIN7_bm, 7, 0);
pin_init(&drvOCTW, &PORTD, PIN0_bm, 0, 0);
// setup drv8302 mode
pin_clear(&drvModePwm); // low for 6-channel pwm, hi and lo sides from uc
pin_clear(&drvModeGain); // low for 10v/v, hi for 40v/v current sense gains
pin_clear(&drvDcCal); // turn DC cal off, we turn this high to set midpoint on amps
pin_clear(&drvEnPin); // disable the gate driver, to start. also broken by no/go hardware switch
}
void drv_enable(void){
pin_set(&drvEnPin);
}
void drv_disable(void){
pin_clear(&drvEnPin);
} }
void tickers_init(void){ void tickers_init(void){
// sets up two timers // sets up two timers
// compare and capture at value // compare and capture at value
uint16_t pera = 1200; // this is a low-speed-start friendly value, to start the commutation ticker with
uint16_t perStartSpeed = 2400;
// write low first, bc bussing / xmega 8-bit oddities cc datasheet @ 3.11 // write low first, bc bussing / xmega 8-bit oddities cc datasheet @ 3.11
uint8_t peral = (uint8_t) pera; uint8_t perSl = (uint8_t) perStartSpeed;
uint8_t perah = (uint8_t) (pera >> 8); uint8_t perSh = (uint8_t) (perStartSpeed >> 8);
// turn on TCC0 // turn on TCC0
TCD0.CTRLA = TC_CLKSEL_DIV256_gc; TCD0.CTRLA = TC_CLKSEL_DIV64_gc;
TCD0.PERBUFL = peral; TCD0.PERBUFL = perSl;
TCD0.PERBUFH = perah; TCD0.PERBUFH = perSh;
// set cca interrupt on // set cca interrupt on
TCD0.INTCTRLA = TC_OVFINTLVL_HI_gc; TCD0.INTCTRLA = TC_OVFINTLVL_HI_gc;
// and a reasonable speed for acceleration ticking
uint16_t perAccelRate = 2400;
uint8_t perAl = (uint8_t) perAccelRate;
uint8_t perAh = (uint8_t) (perAccelRate >> 8);
// another ticker to execute accel // another ticker to execute accel
TCD1.CTRLA = TC_CLKSEL_DIV256_gc; TCD1.CTRLA = TC_CLKSEL_DIV64_gc;
TCD1.PERBUFL = peral; TCD1.PERBUFL = perAl;
TCD1.PERBUFH = perah; TCD1.PERBUFH = perAh;
TCD1.INTCTRLA = TC_OVFINTLVL_HI_gc; TCD1.INTCTRLA = TC_OVFINTLVL_HI_gc;
} }
...@@ -144,9 +181,20 @@ int main(void) ...@@ -144,9 +181,20 @@ int main(void)
//pin_init(&tstpin1, &PORTC, PIN5_bm, 5, 1); //pin_init(&tstpin1, &PORTC, PIN5_bm, 5, 1);
//pin_init(&tstpin2, &PORTC, PIN3_bm, 3, 1); //pin_init(&tstpin2, &PORTC, PIN3_bm, 3, 1);
// start timers for commutation, accel tickers
tickers_init(); tickers_init();
// start pwm system
pwm_init(); pwm_init();
// initialize the bldc state structure
bldc_init(&bldc);
// on startup speed and duty
bldc_setSpeed(&bldc, 1000);
bldc_setDuty(&bldc, 10);
// startup the driver
drv_init();
// and enable the gate
drv_enable();
// now we should be spinning at 500 eRPM, so we can set an accel... later
// runtime globals // runtime globals
uint32_t tck = 0; uint32_t tck = 0;
...@@ -168,11 +216,30 @@ ISR(TCC0_OVF_vect){ ...@@ -168,11 +216,30 @@ ISR(TCC0_OVF_vect){
pin_toggle(&hi1); pin_toggle(&hi1);
} }
int8_t comTable[6][3] = {
{1,-1,0},
{1,0,-1},
{0,1,-1},
{-1,1,0},
{-1,0,1},
{0,-1,1}
};
// commutation timer
ISR(TCD0_OVF_vect){ ISR(TCD0_OVF_vect){
// commutate? // commutate?
//pin_toggle(&tstpin1); (bldc.comDir) ? (bldc.comState ++) : (bldc.comState --);
if(bldc.comState > 5){
bldc.comState = 0;
}
pwm_by_offset( comTable[bldc.comState][0] * bldc.comDuty,
comTable[bldc.comState][1] * bldc.comDuty,
comTable[bldc.comState][2] * bldc.comDuty
);
} }
// acceleration timer
ISR(TCD1_OVF_vect){ ISR(TCD1_OVF_vect){
//pin_toggle(&tstpin2); //pin_toggle(&tstpin2);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment