fixed speed time issues

This commit is contained in:
Nicolas Trimborn 2021-08-12 10:18:07 +02:00
parent e367b994f2
commit 1b6dab7143
7 changed files with 91 additions and 123 deletions

View File

@ -14,11 +14,11 @@
void motor_StateMachine(BLDCMotor_t* const motor) void motor_StateMachine(BLDCMotor_t* const motor)
{ {
if (motor->motor_state.fault) //if (motor->motor_state.fault)
{ //{
motor->motor_state.previousstate = motor->motor_state.currentstate; //motor->motor_state.previousstate = motor->motor_state.currentstate;
motor->motor_state.currentstate = MOTOR_FAULT; //motor->motor_state.currentstate = MOTOR_FAULT;
} //}
switch (motor->motor_state.currentstate) switch (motor->motor_state.currentstate)
{ {
@ -189,9 +189,16 @@ void exec_commutation(BLDCMotor_t* const motor)
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
//tic_port(DEBUG_2_PORT); //tic_port(DEBUG_2_PORT);
//motor->motor_status.currentHallPattern = readM1Hall(); //motor->motor_status.currentHallPattern = readM1Hall();
volatile uint8_t currentHall = 1; volatile uint8_t currentHall = motor->readHall();
currentHall = motor->readHall();
motor->motor_status.currentHallPattern = currentHall; motor->motor_status.currentHallPattern = currentHall;
if ((currentHall == INVALID_HALL_0)||(currentHall == INVALID_HALL_7))
{
hri_tcc_write_PATTBUF_reg(motor->motor_param->pwm_desc->device.hw, DISABLE_PATTERN);
motor->motor_state.currentstate == MOTOR_FAULT;
motor->motor_state.fault == MOTOR_HALLSENSORINVALID;
return;
}
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// Set Pattern Buffers // Set Pattern Buffers
@ -265,9 +272,9 @@ void calculate_motor_speed(BLDCMotor_t* const motor)
{ {
//tic_port(DEBUG_2_PORT); //tic_port(DEBUG_2_PORT);
volatile uint32_t temp_rpm = 0; volatile uint32_t temp_rpm = 0;
hri_tccount32_read_CC_reg(TC2, 0); /* Read CC0 but throw away)*/ hri_tccount32_read_CC_reg(motor->motor_param->speedtimer_hw, 0); /* Read CC0 but throw away)*/
volatile uint32_t period_after_capture = hri_tccount32_read_CC_reg(TC2, 1); volatile uint32_t period_after_capture = hri_tccount32_read_CC_reg(motor->motor_param->speedtimer_hw, 1);
if((period_after_capture > UINT32_MAX)||(period_after_capture == 0)) { if((period_after_capture >= UINT32_MAX)||(period_after_capture == 0)||(period_after_capture > 600000)) {
motor->motor_status.calc_rpm = 0; motor->motor_status.calc_rpm = 0;
} else { } else {
////uint32_t test = (SPEEDFACTOR, period_after_capture); ////uint32_t test = (SPEEDFACTOR, period_after_capture);
@ -290,14 +297,12 @@ void calculate_motor_speed(BLDCMotor_t* const motor)
temp_rpm = temp_rpm; temp_rpm = temp_rpm;
} }
//motor->motor_status.calc_rpm = (int16_t)temp_rpm; motor->motor_status.calc_rpm = (int16_t)temp_rpm;
//toc_port(DEBUG_2_PORT);
//#ifdef AVERAGE_SPEED_MEASURE #ifdef AVERAGE_SPEED_MEASURE
//if(temp_rpm==0)
//#define n_SAMPLE 8 //volatile uint32_t speed_average = 0;
//static uint32_t speed_average = 0; //volatile uint8_t count = 1;
//static uint8_t count = 1;
//// To avoid noise an average is realized on 8 samples //// To avoid noise an average is realized on 8 samples
//speed_average += temp_rpm; //speed_average += temp_rpm;
//if(count >= n_SAMPLE) //if(count >= n_SAMPLE)
@ -318,7 +323,7 @@ void calculate_motor_speed(BLDCMotor_t* const motor)
//return; //return;
//} //}
//else count++; //else count++;
//#endif #endif
motor->motor_status.calc_rpm = (int16_t)temp_rpm; motor->motor_status.calc_rpm = (int16_t)temp_rpm;
//toc_port(DEBUG_2_PORT); //toc_port(DEBUG_2_PORT);
@ -445,6 +450,7 @@ volatile uint8_t readHallSensorM2(void)
motor_read = (motor_read & M2_HALL_B_MASK) | (uint8_t)((PORT->Group[M2_HALL_B_GROUP].IN.reg & M2_HALL_B_PORT)>>(M2_HALL_B_LSR)); motor_read = (motor_read & M2_HALL_B_MASK) | (uint8_t)((PORT->Group[M2_HALL_B_GROUP].IN.reg & M2_HALL_B_PORT)>>(M2_HALL_B_LSR));
motor_read = (motor_read & M2_HALL_C_MASK) | (uint8_t)((PORT->Group[M2_HALL_C_GROUP].IN.reg & M2_HALL_C_PORT)>>(M2_HALL_C_LSR)); motor_read = (motor_read & M2_HALL_C_MASK) | (uint8_t)((PORT->Group[M2_HALL_C_GROUP].IN.reg & M2_HALL_C_PORT)>>(M2_HALL_C_LSR));
return motor_read;
//if(((motor_read == INVALID_HALL_0) || (motor_read == INVALID_HALL_7))) { //if(((motor_read == INVALID_HALL_0) || (motor_read == INVALID_HALL_7))) {
//Motor2.motor_state.fault = MOTOR_HALLSENSORINVALID; //Motor2.motor_state.fault = MOTOR_HALLSENSORINVALID;
//Motor2.motor_state.currentstate = MOTOR_FAULT; //Motor2.motor_state.currentstate = MOTOR_FAULT;
@ -458,7 +464,7 @@ volatile uint8_t readHallSensorM2(void)
//return ((a << 2) | //return ((a << 2) |
//(b << 1) | //(b << 1) |
//(c << 0)); //(c << 0));
return motor_read;
} }
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------

View File

@ -29,7 +29,8 @@
#define INVALID_HALL_0 (0) #define INVALID_HALL_0 (0)
#define INVALID_HALL_7 (7) #define INVALID_HALL_7 (7)
#define AVERAGE_SPEED_MEASURE 1 //#define AVERAGE_SPEED_MEASURE
#define n_SAMPLE 8
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// ADC Parameters // ADC Parameters

View File

@ -24,6 +24,29 @@ void adc_sram_dma_callback(struct _dma_resource *adc_dma_res)
Motor2.timerflags.adc_readings_ready_tic = true; Motor2.timerflags.adc_readings_ready_tic = true;
} }
// ----------------------------------------------------------------------
// EtherCAT Cycle Timer - 1kHz
// ----------------------------------------------------------------------
void TC2_Handler(void)
{
if (TC2->COUNT32.INTFLAG.bit.OVF == 0x01) {
TC2->COUNT32.INTFLAG.bit.OVF = 0x01;
Motor1.motor_status.calc_rpm = 0;
}
}
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
void TC4_Handler(void)
{
if (TC4->COUNT32.INTFLAG.bit.OVF == 0x01) {
TC4->COUNT32.INTFLAG.bit.OVF = 0x01;
Motor2.motor_status.calc_rpm = 0;
}
}
#endif /* INTERRUPTS_H_ */ #endif /* INTERRUPTS_H_ */

View File

@ -79,8 +79,10 @@ void One_ms_timer_init(void)
void enable_NVIC_IRQ(void) void enable_NVIC_IRQ(void)
{ {
//NVIC_EnableIRQ(TC7_IRQn); // TC7: TC_ECAT //hri_tc_clear_INTEN_OVF_bit(TC2);
//NVIC_EnableIRQ(TC0_IRQn); // TC0: TC_SPEED //hri_tc_clear_INTEN_OVF_bit(TC4);
NVIC_EnableIRQ(TC2_IRQn); // TC2: M1_Speed_Timer
NVIC_EnableIRQ(TC4_IRQn); // TC4: M2_Speed_Timer
NVIC_EnableIRQ(DMAC_0_IRQn); NVIC_EnableIRQ(DMAC_0_IRQn);
NVIC_EnableIRQ(TCC1_0_IRQn); NVIC_EnableIRQ(TCC1_0_IRQn);
NVIC_EnableIRQ(TCC1_0_IRQn); NVIC_EnableIRQ(TCC1_0_IRQn);
@ -175,9 +177,6 @@ int main(void)
//speed_timer_init(); //speed_timer_init();
enable_NVIC_IRQ(); enable_NVIC_IRQ();
/* Replace with your application code */ /* Replace with your application code */
while (1) { while (1) {
//delay_ms(100); //delay_ms(100);

View File

@ -71,109 +71,48 @@
** W7 | M1_0 | CC0 || M2_0 | CC0 | ** W7 | M1_0 | CC0 || M2_0 | CC0 |
*/ */
/* CCW from Back - works best in forward diraction*/
//static const uint16_t COMMUTATION_PATTERN[] = {
//0x003F, // (0) invalid state
//0x053D, // (1)
//0x1437, // (2)
//0x113D, // (3)
//0x111F, // (4)
//0x141F, // (5)
//0x0537, // (6)
//0x003F, // (7) invalid state
//0x003F, // (8) invalid state
//0x0537, // (9)
//0x141F, // (10)
//0x111F, // (11)
//0x113D, // (12)
//0x1437, // (13)
//0x053D, // (14)
//0x003F // (15) invalid state
//};
////* CW From Back */
//static const uint16_t COMMUTATION_PATTERN[16] = {
//0x003F, // (0) invalid state
//0x0537, // (1)
//0x141F, // (2)
//0x111F, // (3)
//0x113D, // (4)
//0x1437, // (5)
//0x053D, // (6)
//0x003F, // (7) invalid state
//0x003F, // (8) invalid state
//0x053D, // (9)
//0x1437, // (10)
//0x113D, // (11)
//0x111F, // (12)
//0x141F, // (13)
//0x0537, // (14)
//0x003F // (15) invalid state
//};
////* CW From Back */
//static const uint16_t COMMUTATION_PATTERN[16] = {
//0x003F, // (0) invalid state
//0x0537, // (1)
//0x141F, // (2)
//0x111F, // (3)
//0x113D, // (4)
//0x1437, // (5)
//0x053D, // (6)
//0x003F, // (7) invalid state
//0x003F, // (8) invalid state
//0x0537, // (9)
//0x053D, // (10)
//0x1437, // (11)
//0x113D, // (12)
//0x111F, // (13)
//0x141F, // (14)
//0x003F // (15) invalid state
//};
//
#define DISABLE_PATTERN 0x003F #define DISABLE_PATTERN 0x003F
////* A&C phase swapped */ //////* A&C phase swapped */
static const uint16_t COMMUTATION_PATTERN[16] = {
0x003F, // (0) invalid state
0x1437, // (1)
0x053D, // (2)
0x113D, // (3)
0x111F, // (4)
0x0537, // (5)
0x141F, // (6)
0x003F, // (7) invalid state
0x003F, // (8) invalid state
0x141F, // (9)
0x0537, // (10)
0x111F, // (11)
0x113D, // (12)
0x053D, // (13)
0x1437, // (14)
0x003F, // (15) invalid state
};
//////* A&C phase swapped and modified to match currents */
//static const uint16_t COMMUTATION_PATTERN[16] = { //static const uint16_t COMMUTATION_PATTERN[16] = {
//0x003F, // (0) invalid state //0x003F, // (0) invalid state
//0x141F, // (1) //0x1437, // (1)
//0x0537, // (2) //0x053D, // (2)
//0x111F, // (3) //0x113D, // (3)
//0x113D, // (4) //0x111F, // (4)
//0x053D, // (5) //0x0537, // (5)
//0x1437, // (6) //0x141F, // (6)
//0x003F, // (7) invalid state //0x003F, // (7) invalid state
//0x003F, // (8) invalid state //0x003F, // (8) invalid state
//0x1437, // (9) //0x141F, // (9)
//0x053D, // (10) //0x0537, // (10)
//0x113D, // (11) //0x111F, // (11)
//0x111F, // (12) //0x113D, // (12)
//0x0537, // (13) //0x053D, // (13)
//0x141F, // (14) //0x1437, // (14)
//0x003F // (15) invalid state //0x003F, // (15) invalid state
//}; //};
////* A&C phase swapped and modified to match currents */
static const uint16_t COMMUTATION_PATTERN[16] = {
0x003F, // (0) invalid state
0x141F, // (1)
0x0537, // (2)
0x111F, // (3)
0x113D, // (4)
0x053D, // (5)
0x1437, // (6)
0x003F, // (7) invalid state
0x003F, // (8) invalid state
0x1437, // (9)
0x053D, // (10)
0x113D, // (11)
0x111F, // (12)
0x0537, // (13)
0x141F, // (14)
0x003F // (15) invalid state
};
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// Motor Physical Parameters // Motor Physical Parameters
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------

View File

@ -59,9 +59,9 @@ typedef enum
typedef enum typedef enum
{ {
MOTOR_NOFAULT = 0, MOTOR_NOFAULT = 0xE1,
MOTOR_HALLSENSORINVALID = 1, MOTOR_HALLSENSORINVALID = 0xE2,
MOTOR_DRIVER_OVER_CURRENT = 2, MOTOR_DRIVER_OVER_CURRENT = 0xE3,
} MOTOR_FAULTS_t; } MOTOR_FAULTS_t;
typedef struct MOTOR_STATE typedef struct MOTOR_STATE