diff --git a/2_Motor_Master/Motor_Master/Motor_Master/bldc.c b/2_Motor_Master/Motor_Master/Motor_Master/bldc.c index 12c1587..30a04f5 100644 --- a/2_Motor_Master/Motor_Master/Motor_Master/bldc.c +++ b/2_Motor_Master/Motor_Master/Motor_Master/bldc.c @@ -23,7 +23,13 @@ void motor_StateMachine(BLDCMotor_t *motor) switch (motor->motor_state.currentstate) { - case MOTOR_INIT: + case MOTOR_INIT: ; + volatile uint8_t currentHall = motor->readHall(); + if((currentHall > 6) || (currentHall < 1) ) { + motor->motor_state.fault = MOTOR_HALLSENSORINVALID; + motor->motor_state.currentstate = MOTOR_FAULT; + break; + } motor->motor_state.previousstate = motor->motor_state.currentstate; motor->motor_state.currentstate = MOTOR_IDLE; break; @@ -175,19 +181,21 @@ void BldcInitStruct(BLDCMotor_t *motor, BLDCMotor_param_t *motor_param) void exec_commutation(BLDCMotor_t *motor) { + // ---------------------------------------------------------------------- // Read Motor Hall Sensors // ---------------------------------------------------------------------- //tic_port(DEBUG_2_PORT); //motor->motor_status.currentHallPattern = readM1Hall(); - volatile uint8_t currentHall = motor->readHall(); + volatile uint8_t currentHall = 1; + currentHall = motor->readHall(); motor->motor_status.currentHallPattern = currentHall; - if(((currentHall == INVALID_HALL_0) || (currentHall == INVALID_HALL_7))) { - //motor->motor_state.fault = MOTOR_HALLSENSORINVALID; - //motor->motor_state.currentstate = MOTOR_FAULT; - //applicationStatus.currentstate = APP_FAULT; - } - + + + volatile uint8_t currentHallv2 = readHallSensorM1v2(); + + + // ---------------------------------------------------------------------- // Set Pattern Buffers // ---------------------------------------------------------------------- @@ -406,27 +414,66 @@ void BLDC_runOpenLoop(BLDCMotor_t *motor, int16_t duty) motor->motor_status.duty_cycle = u_clamp(abs(duty), 0, MAX_PWM); } -uint8_t readHallSensorM1(void) +volatile uint8_t readHallSensorM1(void) { - volatile uint8_t a = gpio_get_pin_level(M1_HALL_A_PIN); - volatile uint8_t b = gpio_get_pin_level(M1_HALL_B_PIN); - volatile uint8_t c = gpio_get_pin_level(M1_HALL_C_PIN); - - return ((a << 2) | - (b << 1) | - (c << 0)); + volatile uint8_t motor_read = 0; + motor_read = (motor_read & M1_HALL_A_MASK) | (uint8_t)((PORT->Group[M1_HALL_A_GROUP].IN.reg & M1_HALL_A_PORT)>>(M1_HALL_A_LSR)); + motor_read = (motor_read & M1_HALL_B_MASK) | (uint8_t)((PORT->Group[M1_HALL_B_GROUP].IN.reg & M1_HALL_B_PORT)>>(M1_HALL_B_LSR)); + motor_read = (motor_read & M1_HALL_C_MASK) | (uint8_t)((PORT->Group[M1_HALL_C_GROUP].IN.reg & M1_HALL_C_PORT)>>(M1_HALL_C_LSR)); + + //volatile uint8_t a = gpio_get_pin_level(M1_HALL_A_PIN); + //volatile uint8_t b = gpio_get_pin_level(M1_HALL_B_PIN); + //volatile uint8_t c = gpio_get_pin_level(M1_HALL_C_PIN); + //volatile uint8_t motor_read2 = ((a << 2)|(b << 1)|(c << 0)); + //return ((a << 2) | + //(b << 1) | + //(c << 0)); + return motor_read; } -uint8_t readHallSensorM2(void) +volatile uint8_t readHallSensorM1v2(void) { - volatile uint8_t a = gpio_get_pin_level(M2_HALL_A_PIN); - volatile uint8_t b = gpio_get_pin_level(M2_HALL_B_PIN); - volatile uint8_t c = gpio_get_pin_level(M2_HALL_C_PIN); - return ((a << 2) | - (b << 1) | - (c << 0)); + volatile uint8_t motor_read = 0; + motor_read = (motor_read & M1_HALL_A_MASK) | (uint8_t)((PORT->Group[M1_HALL_A_GROUP].IN.reg & M1_HALL_A_PORT)>>(M1_HALL_A_LSR)); + motor_read = (motor_read & M1_HALL_B_MASK) | (uint8_t)((PORT->Group[M1_HALL_B_GROUP].IN.reg & M1_HALL_B_PORT)>>(M1_HALL_B_LSR)); + motor_read = (motor_read & M1_HALL_C_MASK) | (uint8_t)((PORT->Group[M1_HALL_C_GROUP].IN.reg & M1_HALL_C_PORT)>>(M1_HALL_C_LSR)); + + //volatile uint8_t a = gpio_get_pin_level(M1_HALL_A_PIN); + //volatile uint8_t b = gpio_get_pin_level(M1_HALL_B_PIN); + //volatile uint8_t c = gpio_get_pin_level(M1_HALL_C_PIN); + //volatile uint8_t motor_read2 = ((a << 2)|(b << 1)|(c << 0)); + //return ((a << 2) | + //(b << 1) | + //(c << 0)); + return motor_read; +} + +volatile uint8_t readHallSensorM2(void) +{ + + volatile uint8_t motor_read = 0; + motor_read = (motor_read & M2_HALL_A_MASK) | (uint8_t)((PORT->Group[M2_HALL_A_GROUP].IN.reg & M2_HALL_A_PORT)>>(M2_HALL_A_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)); + + if(((motor_read == INVALID_HALL_0) || (motor_read == INVALID_HALL_7))) { + Motor2.motor_state.fault = MOTOR_HALLSENSORINVALID; + Motor2.motor_state.currentstate = MOTOR_FAULT; + //applicationStatus.currentstate = APP_FAULT; + } + + + + //volatile uint8_t a = gpio_get_pin_level(M2_HALL_A_PIN); + //volatile uint8_t b = gpio_get_pin_level(M2_HALL_B_PIN); + //volatile uint8_t c = gpio_get_pin_level(M2_HALL_C_PIN); + //volatile uint8_t motor_read2 = ((a << 2)|(b << 1)|(c << 0)); + //return ((a << 2) | + //(b << 1) | + //(c << 0)); + return motor_read; } // ---------------------------------------------------------------------- diff --git a/2_Motor_Master/Motor_Master/Motor_Master/bldc.h b/2_Motor_Master/Motor_Master/Motor_Master/bldc.h index 4796651..c3558ea 100644 --- a/2_Motor_Master/Motor_Master/Motor_Master/bldc.h +++ b/2_Motor_Master/Motor_Master/Motor_Master/bldc.h @@ -26,8 +26,8 @@ #define DIRECTION_CCW_OFFSET (8) //CBA /* if the Hall sensor reads 0x0 or 0x7 that means either one of the hall sensor is damaged or disconnected*/ -#define INVALID_HALL_0 (0U) -#define INVALID_HALL_7 (7U) +#define INVALID_HALL_0 (0) +#define INVALID_HALL_7 (7) // ---------------------------------------------------------------------- // ADC Parameters @@ -88,7 +88,8 @@ static void BLDC_runSpeedCntl(BLDCMotor_t *motor, const float32_t speedfbk, cons static void BLDC_runCurrentCntl(BLDCMotor_t *motor, const float32_t curfbk, const float32_t curRef); static void BLDC_runPosCntl(BLDCMotor_t *motor, int16_t posfbk, int16_t posRef); static void BLDC_runOpenLoop(BLDCMotor_t *motor, int16_t duty); -uint8_t readHallSensorM1(void); -uint8_t readHallSensorM2(void); +volatile uint8_t readHallSensorM1(void); +volatile uint8_t readHallSensorM2(void); +volatile uint8_t readHallSensorM1v2(void); #endif /* BLDC_H_ */ \ No newline at end of file diff --git a/2_Motor_Master/Motor_Master/Motor_Master/configuration.h b/2_Motor_Master/Motor_Master/Motor_Master/configuration.h index 30adfb2..04a3aa1 100644 --- a/2_Motor_Master/Motor_Master/Motor_Master/configuration.h +++ b/2_Motor_Master/Motor_Master/Motor_Master/configuration.h @@ -45,24 +45,24 @@ inline static void configure_tcc_pwm(void) hri_tcc_set_WAVE_POL3_bit(TCC0); hri_tcc_set_WAVE_POL4_bit(TCC0); hri_tcc_set_WAVE_POL5_bit(TCC0); - hri_tcc_write_CC_CC_bf(TCC0, 0, 150); - hri_tcc_write_CC_CC_bf(TCC0, 1, 150); - hri_tcc_write_CC_CC_bf(TCC0, 2, 150); - hri_tcc_write_CC_CC_bf(TCC0, 3, 150); - hri_tcc_write_CC_CC_bf(TCC0, 4, 150); - hri_tcc_write_CC_CC_bf(TCC0, 5, 150); + hri_tcc_write_CC_CC_bf(TCC0, 0, 0); + hri_tcc_write_CC_CC_bf(TCC0, 1, 0); + hri_tcc_write_CC_CC_bf(TCC0, 2, 0); + hri_tcc_write_CC_CC_bf(TCC0, 3, 0); + hri_tcc_write_CC_CC_bf(TCC0, 4, 0); + hri_tcc_write_CC_CC_bf(TCC0, 5, 0); hri_tcc_write_PER_reg(TCC0,1200); //hri_tcc_write_CCBUF_CCBUF_bf(TCC0, 0,250); hri_tcc_write_CTRLA_ENABLE_bit(TCC0, 1 << TCC_CTRLA_ENABLE_Pos); /* TCC1 */ hri_tcc_set_WEXCTRL_OTMX_bf(TCC1, 0x02); - hri_tcc_write_CC_CC_bf(TCC1, 0, 150); - hri_tcc_write_CC_CC_bf(TCC1, 1, 150); - hri_tcc_write_CC_CC_bf(TCC1, 2, 150); - hri_tcc_write_CC_CC_bf(TCC1, 3, 150); - hri_tcc_write_CC_CC_bf(TCC0, 4, 150); - hri_tcc_write_CC_CC_bf(TCC0, 5, 150); + hri_tcc_write_CC_CC_bf(TCC1, 0, 0); + hri_tcc_write_CC_CC_bf(TCC1, 1, 0); + hri_tcc_write_CC_CC_bf(TCC1, 2, 0); + hri_tcc_write_CC_CC_bf(TCC1, 3, 0); + hri_tcc_write_CC_CC_bf(TCC0, 4, 0); + hri_tcc_write_CC_CC_bf(TCC0, 5, 0); // ////pwm_set_parameters(&TCC_PWM, 1000, 250); hri_tcc_set_WAVE_POL0_bit(TCC1); diff --git a/2_Motor_Master/Motor_Master/Motor_Master/motorparameters.h b/2_Motor_Master/Motor_Master/Motor_Master/motorparameters.h index 458dc70..fe9aaf3 100644 --- a/2_Motor_Master/Motor_Master/Motor_Master/motorparameters.h +++ b/2_Motor_Master/Motor_Master/Motor_Master/motorparameters.h @@ -16,8 +16,8 @@ #define M1_HALL_A_PIN GPIO(GPIO_PORTA, 4) #define M1_HALL_A_PORT PORT_PA04 -#define M1_HALL_A_MASK ~(1<<0) -#define M1_HALL_A_LSR M1_HALL_A_PIN - M1_HALL_A_GROUP*32 -0 +#define M1_HALL_A_MASK ~(1<<2) +#define M1_HALL_A_LSR M1_HALL_A_PIN - M1_HALL_A_GROUP*32 -2 #define M1_HALL_A_GROUP M1_HALL_A_PIN/32 #define M1_HALL_B_PIN GPIO(GPIO_PORTA, 5) @@ -28,8 +28,8 @@ #define M1_HALL_C_PIN GPIO(GPIO_PORTA, 6) #define M1_HALL_C_PORT PORT_PA06 -#define M1_HALL_C_MASK ~(1<<2) -#define M1_HALL_C_LSR M1_HALL_C_PIN - M1_HALL_C_GROUP*32 -2 +#define M1_HALL_C_MASK ~(1<<0) +#define M1_HALL_C_LSR M1_HALL_C_PIN - M1_HALL_C_GROUP*32 -0 #define M1_HALL_C_GROUP M1_HALL_C_PIN/32 // ---------------------------------------------------------------------- @@ -38,8 +38,8 @@ #define M2_HALL_A_PIN GPIO(GPIO_PORTA, 22) #define M2_HALL_A_PORT PORT_PA22 -#define M2_HALL_A_MASK ~(1<<0) -#define M2_HALL_A_LSR M2_HALL_A_PIN - M2_HALL_A_GROUP*32 -0 +#define M2_HALL_A_MASK ~(1<<2) +#define M2_HALL_A_LSR M2_HALL_A_PIN - M2_HALL_A_GROUP*32 -2 #define M2_HALL_A_GROUP M2_HALL_A_PIN/32 #define M2_HALL_B_PIN GPIO(GPIO_PORTA, 23) @@ -50,8 +50,8 @@ #define M2_HALL_C_PIN GPIO(GPIO_PORTA, 24) #define M2_HALL_C_PORT PORT_PA24 -#define M2_HALL_C_MASK ~(1<<2) -#define M2_HALL_C_LSR M2_HALL_C_PIN - M2_HALL_C_GROUP*32 -2 +#define M2_HALL_C_MASK ~(1<<0) +#define M2_HALL_C_LSR M2_HALL_C_PIN - M2_HALL_C_GROUP*32 -0 #define M2_HALL_C_GROUP M2_HALL_C_PIN/32