Got AS series of angle sensor working

This commit is contained in:
Nicolas Trimborn
2021-08-22 14:03:02 +02:00
parent 9d2f2a2598
commit 4dbced4c9d
14 changed files with 87 additions and 59 deletions

View File

@@ -102,7 +102,7 @@ static void update_telemetry(void)
*M3_Motor__hallState = Motor1.motor_status.currentHallPattern;
*M3_Motor_dutyCycle = Motor1.motor_status.duty_cycle;
*M3_Motor_speed = (int16_t)Motor1.motor_status.calc_rpm;
*M3_Joint_abs_position = Motor1.motor_status.actualDirection;
//*M3_Joint_abs_position = ;
/* Motor 2 */
*M4_Status = Motor2.motor_state.currentstate;
*M4_Joint_rel_position = Motor2.motor_status.Num_Steps;
@@ -115,7 +115,7 @@ static void update_telemetry(void)
*M4_Motor__hallState = Motor2.motor_status.currentHallPattern;
*M4_Motor_dutyCycle = Motor2.motor_status.duty_cycle;
*M4_Motor_speed = (int16_t)Motor2.motor_status.calc_rpm;
*M4_Joint_abs_position = Motor2.motor_status.actualDirection;
//*M4_Joint_abs_position = Motor2.motor_status.actualDirection;
}
static void update_setpoints(void)

View File

@@ -29,20 +29,19 @@ void angle_sensor_init()
gpio_set_pin_level(ANGLESENSOR.SS_pin, true);
}
void read(ASCommand command)
int16_t* read(ASCommand command)
{
_read(command); // Send command to device(s)
_read(AS_CMD_NOP); // Read-out device(s)
return _read(AS_CMD_NOP); // Read-out device(s)
}
int16_t* read_angle()
{
_read(AS_CMD_ANGLE); // Send command to device(s)
_read(AS_CMD_ANGLE); // Read-out device(s)
return &ANGLESENSOR._readBuffer;
return _read(AS_CMD_ANGLE); // Read-out device(s)
}
void _read(ASCommand command)
int16_t* _read(ASCommand command)
{
if(ANGLESENSOR.n_dev == 1)
@@ -52,19 +51,20 @@ void _read(ASCommand command)
uint16_t temp = _spi_transfer16(&ANGLESENSOR, command);
ANGLESENSOR._readBuffer[0] = (temp & ~0xC000);
gpio_set_pin_level(ANGLESENSOR.SS_pin, true);
//delay_us(1); // Wait at least 350ns after chip select
delay_us(1); // Wait at least 350ns after chip select
} else {
// Enable the sensor on the chain
gpio_set_pin_level(ANGLESENSOR.SS_pin, false);
//delay_us(1); // Wait at least 350ns after chip select
delay_us(1); // Wait at least 350ns after chip select
for(int i = 0; i < ANGLESENSOR.n_dev; ++i)
{
uint16_t temp = _spi_transfer16(&ANGLESENSOR, command);
ANGLESENSOR._readBuffer[i] = (temp & ~0xC000);
}
gpio_set_pin_level(ANGLESENSOR.SS_pin, true);
//delay_us(1); // Wait at least 350ns after chip select
delay_us(1); // Wait at least 350ns after chip select
}
return &ANGLESENSOR._readBuffer;
}

View File

@@ -83,12 +83,12 @@ static struct SPI_ANGLE_SENSOR ANGLESENSOR = {
.SPI_descr = &SPI_3,
.flags = 0,
.SS_pin = SPI3_SS,
.n_dev = 1,
.n_dev = 2,
._readBuffer = {0} // Must Equal n_dev
};
void angle_sensor_init();
void read(ASCommand command);
int16_t* read(ASCommand command);
// Performs a single angle measurement on all sensors
// @return Array of raw angle data. To get the 14-bit value representing
// the angle, apply the mask() to the result.
@@ -103,7 +103,7 @@ static bool parity_check(int16_t sensor_result);
int16_t degrees(int16_t sensor_result);
// Intended to be used Internally
void _read(ASCommand command);
int16_t* _read(ASCommand command);
uint16_t _spi_transfer16(struct spi_m_sync_descriptor *spi, uint16_t value);

View File

@@ -227,13 +227,18 @@ void exec_commutation(BLDCMotor_t* const motor)
volatile uint16_t temp_M1 = COMMUTATION_PATTERN[currentHall +
motor->motor_setpoints.directionOffset];
hri_tcc_write_PATTBUF_reg(motor->motor_param->pwm_desc->device.hw, temp_M1);
//TCC0->PATTBUF.reg = temp_M1;
Tcc * tmp = (Tcc *)motor->motor_param->pwm_desc->device.hw;
tmp->PATTBUF.reg = (uint16_t)temp_M1;
//motor->motor_param->pwm_desc->device.hw->PATTBUF.reg = temp_M1;
//hri_tcc_write_PATTBUF_reg(motor->motor_param->pwm_desc->device.hw, temp_M1);
// ----------------------------------------------------------------------
// Set Calculated Duty Cycle
// ----------------------------------------------------------------------
hri_tcc_write_CCBUF_CCBUF_bf(motor->motor_param->pwm_desc->device.hw, 0, motor->motor_status.duty_cycle);
//hri_tcc_write_CCBUF_CCBUF_bf(motor->motor_param->pwm_desc->device.hw, 0, motor->motor_status.duty_cycle);
tmp->CCBUF[0].reg = (uint32_t)motor->motor_status.duty_cycle;
volatile uint8_t cur_comm_step = MOTOR_COMMUTATION_STEPS[currentHall];
motor->motor_status.cur_comm_step = cur_comm_step;
volatile int8_t step_change = (cur_comm_step - motor->motor_status.prev_comm_step);

View File

@@ -70,7 +70,7 @@ static struct timer_task Onems_task;
void One_ms_timer_init(void)
{
Onems_task.interval = 10;
Onems_task.interval = 1;
Onems_task.cb = One_ms_cycle_callback;
Onems_task.mode = TIMER_TASK_REPEAT;
timer_add_task(&TIMER_0, &Onems_task);
@@ -86,6 +86,10 @@ void SERCOM1_1_Handler()
//SERCOM1->SPI.INTFLAG.bit.TXC = 0x01;
//SPI_tx_buffer[0] += 1;
//tx_buffer[31] += 1;
//DMAC->Channel[0].CHCTRLA.reg |= DMAC_CHCTRLA_ENABLE;
//DMAC->Channel[1].CHCTRLA.reg |= DMAC_CHCTRLA_ENABLE;
_dma_enable_transaction(CONF_SERCOM_1_RECEIVE_DMA_CHANNEL, false);
_dma_enable_transaction(CONF_SERCOM_1_TRANSMIT_DMA_CHANNEL, false);
@@ -103,6 +107,7 @@ void SERCOM1_3_Handler()
//SERCOM1->SPI.INTFLAG.bit.TXC = 0x01;
//tx_buffer[0] += 1;
//tx_buffer[31] += 1;
//_dma_enable_transaction(CONF_SERCOM_1_RECEIVE_DMA_CHANNEL, false);
//_dma_enable_transaction(CONF_SERCOM_1_TRANSMIT_DMA_CHANNEL, false);
@@ -129,13 +134,15 @@ void enable_NVIC_IRQ(void)
NVIC_SetPriority(DMAC_0_IRQn, 2);
NVIC_SetPriority(ADC1_0_IRQn, 3);
NVIC_EnableIRQ(TCC0_0_IRQn);
NVIC_SetPriority(TCC0_0_IRQn, 1);
NVIC_EnableIRQ(TCC1_0_IRQn);
NVIC_SetPriority(TCC1_0_IRQn, 1);
//NVIC_EnableIRQ(SERCOM5_0_IRQn);
//NVIC_EnableIRQ(SERCOM1_1_IRQn);
//NVIC_SetPriority(SERCOM1_1_IRQn, 1);
//NVIC_EnableIRQ(SERCOM1_3_IRQn);
//NVIC_SetPriority(SERCOM1_3_IRQn, 1);
NVIC_EnableIRQ(SERCOM1_1_IRQn);
NVIC_SetPriority(SERCOM1_1_IRQn, 0);
NVIC_EnableIRQ(SERCOM1_3_IRQn);
NVIC_SetPriority(SERCOM1_3_IRQn, 0);
//NVIC_EnableIRQ(SERCOM1_3_IRQn);
//NVIC_EnableIRQ(EIC_5_IRQn);
}
@@ -171,7 +178,7 @@ void APPLICATION_StateMachine(void)
//applicationStatus.currentstate ;
//comms_check();
motor_StateMachine(&Motor1);
motor_StateMachine(&Motor2);
//motor_StateMachine(&Motor2);
break;
case APP_FAULT:
//DisableGateDrivers(&Motor1);
@@ -239,10 +246,10 @@ int main(void)
/* Replace with your application code */
while (1) {
//if (Motor1.timerflags.adc_readings_ready_tic) {process_currents();}
if (Motor1.timerflags.adc_readings_ready_tic) {process_currents();}
if (Motor1.timerflags.current_loop_tic) {
//APPLICATION_StateMachine();
//exec_commutation(&Motor1);
APPLICATION_StateMachine();
exec_commutation(&Motor1);
//exec_commutation(&Motor2);
}
@@ -263,7 +270,7 @@ int main(void)
int16_t* angles;
angles = read_angle();
*M3_Joint_abs_position = degrees(angles[0]);
//*M4_Joint_abs_position = angles[1];
*M4_Joint_abs_position = degrees(angles[1]);
volatile int y = 0;