got speed calc working. Still goes in wrong direction

This commit is contained in:
Nicolas Trimborn
2021-08-11 20:41:58 +02:00
parent afe33303ad
commit e367b994f2
25 changed files with 1132 additions and 171 deletions

View File

@@ -112,11 +112,11 @@ drivers:
adc_arch_winmode: No window mode
adc_arch_winmoneo: false
adc_arch_winut: 0
adc_differential_mode: false
adc_differential_mode: true
adc_freerunning_mode: false
adc_pinmux_negative: Internal ground
adc_pinmux_negative: ADC AIN0 pin
adc_pinmux_positive: ADC AIN6 pin
adc_prescaler: Peripheral clock divided by 16
adc_prescaler: Peripheral clock divided by 8
adc_reference: External reference A
adc_resolution: 12-bit
optional_signals:
@@ -206,7 +206,7 @@ drivers:
ccl_arch_lutei_3: false
ccl_arch_luteo_0: true
ccl_arch_luteo_1: false
ccl_arch_luteo_2: false
ccl_arch_luteo_2: true
ccl_arch_luteo_3: false
ccl_arch_runstdby: false
ccl_arch_seqsel_0: Sequential logic is disabled
@@ -1014,9 +1014,9 @@ drivers:
evsys_channel_43: No channel output selected
evsys_channel_44: No channel output selected
evsys_channel_45: No channel output selected
evsys_channel_46: No channel output selected
evsys_channel_46: Channel 1
evsys_channel_47: No channel output selected
evsys_channel_48: No channel output selected
evsys_channel_48: Channel 2
evsys_channel_49: No channel output selected
evsys_channel_5: No channel output selected
evsys_channel_50: No channel output selected
@@ -1041,7 +1041,7 @@ drivers:
evsys_channel_8: No channel output selected
evsys_channel_9: No channel output selected
evsys_channel_setting_0: true
evsys_channel_setting_1: false
evsys_channel_setting_1: true
evsys_channel_setting_10: false
evsys_channel_setting_11: false
evsys_channel_setting_12: false
@@ -1052,7 +1052,7 @@ drivers:
evsys_channel_setting_17: false
evsys_channel_setting_18: false
evsys_channel_setting_19: false
evsys_channel_setting_2: false
evsys_channel_setting_2: true
evsys_channel_setting_20: false
evsys_channel_setting_21: false
evsys_channel_setting_22: false
@@ -1169,7 +1169,7 @@ drivers:
evsys_evd_8: false
evsys_evd_9: false
evsys_evgen_0: TCC0 overflow
evsys_evgen_1: No event generator
evsys_evgen_1: CCL LUT output 0
evsys_evgen_10: No event generator
evsys_evgen_11: No event generator
evsys_evgen_12: No event generator
@@ -1180,7 +1180,7 @@ drivers:
evsys_evgen_17: No event generator
evsys_evgen_18: No event generator
evsys_evgen_19: No event generator
evsys_evgen_2: No event generator
evsys_evgen_2: CCL LUT output 2
evsys_evgen_20: No event generator
evsys_evgen_21: No event generator
evsys_evgen_22: No event generator
@@ -1265,7 +1265,7 @@ drivers:
evsys_ovr_8: false
evsys_ovr_9: false
evsys_path_0: Asynchronous path
evsys_path_1: Synchronous path
evsys_path_1: Asynchronous path
evsys_path_10: Synchronous path
evsys_path_11: Synchronous path
evsys_path_12: Synchronous path
@@ -1276,7 +1276,7 @@ drivers:
evsys_path_17: Synchronous path
evsys_path_18: Synchronous path
evsys_path_19: Synchronous path
evsys_path_2: Synchronous path
evsys_path_2: Asynchronous path
evsys_path_20: Synchronous path
evsys_path_21: Synchronous path
evsys_path_22: Synchronous path

View File

@@ -339,7 +339,7 @@
// <i> These bits define the ADC clock relative to the peripheral clock (PRESCALER)
// <id> adc_prescaler
#ifndef CONF_ADC_1_PRESCALER
#define CONF_ADC_1_PRESCALER 0x3
#define CONF_ADC_1_PRESCALER 0x2
#endif
// <q> Free Running Mode
@@ -353,7 +353,7 @@
// <i> In differential mode, the voltage difference between the MUXPOS and MUXNEG inputs will be converted by the ADC. (DIFFMODE)
// <id> adc_differential_mode
#ifndef CONF_ADC_1_DIFFMODE
#define CONF_ADC_1_DIFFMODE 0
#define CONF_ADC_1_DIFFMODE 1
#endif
// <o> Positive Mux Input Selection
@@ -399,7 +399,7 @@
// <i> These bits define the Mux selection for the negative ADC input. (MUXNEG)
// <id> adc_pinmux_negative
#ifndef CONF_ADC_1_MUXNEG
#define CONF_ADC_1_MUXNEG 0x18
#define CONF_ADC_1_MUXNEG 0x0
#endif
// </h>

View File

@@ -336,7 +336,7 @@
// <q> Event output enable
// <id> ccl_arch_luteo_2
#ifndef CONF_CCL_LUTEO_2
#define CONF_CCL_LUTEO_2 0
#define CONF_CCL_LUTEO_2 1
#endif
// <q> Event input enable

View File

@@ -188,7 +188,7 @@
// <e> Channel 1 settings
// <id> evsys_channel_setting_1
#ifndef CONF_EVSYS_CHANNEL_SETTINGS_1
#define CONF_EVSYS_CHANNEL_SETTINGS_1 0
#define CONF_EVSYS_CHANNEL_SETTINGS_1 1
#endif
// <y> Edge detection
@@ -209,7 +209,7 @@
// <EVSYS_CHANNEL_PATH_ASYNCHRONOUS_Val"> Asynchronous path
// <id> evsys_path_1
#ifndef CONF_PATH_1
#define CONF_PATH_1 EVSYS_CHANNEL_PATH_SYNCHRONOUS_Val
#define CONF_PATH_1 EVSYS_CHANNEL_PATH_ASYNCHRONOUS_Val
#endif
// <o> Event generator
@@ -333,7 +333,7 @@
// <0x77=>CCL LUT output 3
// <id> evsys_evgen_1
#ifndef CONF_EVGEN_1
#define CONF_EVGEN_1 0
#define CONF_EVGEN_1 116
#endif
// <q> Overrun channel interrupt
@@ -369,7 +369,7 @@
// <e> Channel 2 settings
// <id> evsys_channel_setting_2
#ifndef CONF_EVSYS_CHANNEL_SETTINGS_2
#define CONF_EVSYS_CHANNEL_SETTINGS_2 0
#define CONF_EVSYS_CHANNEL_SETTINGS_2 1
#endif
// <y> Edge detection
@@ -390,7 +390,7 @@
// <EVSYS_CHANNEL_PATH_ASYNCHRONOUS_Val"> Asynchronous path
// <id> evsys_path_2
#ifndef CONF_PATH_2
#define CONF_PATH_2 EVSYS_CHANNEL_PATH_SYNCHRONOUS_Val
#define CONF_PATH_2 EVSYS_CHANNEL_PATH_ASYNCHRONOUS_Val
#endif
// <o> Event generator
@@ -514,7 +514,7 @@
// <0x77=>CCL LUT output 3
// <id> evsys_evgen_2
#ifndef CONF_EVGEN_2
#define CONF_EVGEN_2 0
#define CONF_EVGEN_2 118
#endif
// <q> Overrun channel interrupt
@@ -7544,7 +7544,7 @@
// <id> evsys_channel_46
// <i> Indicates which channel is chosen for user
#ifndef CONF_CHANNEL_46
#define CONF_CHANNEL_46 0
#define CONF_CHANNEL_46 2
#endif
// <o> Channel selection for TC3 event
@@ -7624,7 +7624,7 @@
// <id> evsys_channel_48
// <i> Indicates which channel is chosen for user
#ifndef CONF_CHANNEL_48
#define CONF_CHANNEL_48 0
#define CONF_CHANNEL_48 3
#endif
// <o> Channel selection for TC5 event

View File

@@ -148,7 +148,8 @@ static void update_telemetry(void)
*M1_Motor_currentPhC = convert_to_mA(Motor1.Iphase_pu.C);
*M1_Motor__hallState = Motor1.motor_status.currentHallPattern;
*M1_Motor_dutyCycle = Motor1.motor_status.duty_cycle;
//
*M1_Motor_speed = (int16_t)Motor1.motor_status.calc_rpm;
*M1_Joint_abs_position = Motor1.motor_status.actualDirection;
/* Motor 2 */
*M2_Status = Motor2.motor_state.currentstate;
*M2_Joint_rel_position = Motor2.motor_status.Num_Steps;
@@ -160,7 +161,8 @@ static void update_telemetry(void)
*M2_Motor_currentPhC = convert_to_mA(Motor2.Iphase_pu.C);
*M2_Motor__hallState = Motor2.motor_status.currentHallPattern;
*M2_Motor_dutyCycle = Motor2.motor_status.duty_cycle;
*M2_Motor_speed = (int16_t)Motor2.motor_status.calc_rpm;
*M2_Joint_abs_position = Motor2.motor_status.actualDirection;
}
static void update_setpoints(void)

View File

@@ -148,7 +148,8 @@ static void update_telemetry(void)
*M1_Motor_currentPhC = convert_to_mA(Motor1.Iphase_pu.C);
*M1_Motor__hallState = Motor1.motor_status.currentHallPattern;
*M1_Motor_dutyCycle = Motor1.motor_status.duty_cycle;
//
*M1_Motor_speed = (int16_t)Motor1.motor_status.calc_rpm;
*M1_Joint_abs_position = Motor1.motor_status.actualDirection;
/* Motor 2 */
*M2_Status = Motor2.motor_state.currentstate;
*M2_Joint_rel_position = Motor2.motor_status.Num_Steps;
@@ -160,7 +161,8 @@ static void update_telemetry(void)
*M2_Motor_currentPhC = convert_to_mA(Motor2.Iphase_pu.C);
*M2_Motor__hallState = Motor2.motor_status.currentHallPattern;
*M2_Motor_dutyCycle = Motor2.motor_status.duty_cycle;
*M2_Motor_speed = (int16_t)Motor2.motor_status.calc_rpm;
*M2_Joint_abs_position = Motor2.motor_status.actualDirection;
}
static void update_setpoints(void)

View File

@@ -150,7 +150,7 @@
<AcmeProjectActionInfo Action="File" Source="hri/hri_usb_e51.h" IsConfig="false" Hash="x6M7vYgNCS2oECqykr5+yw" />
<AcmeProjectActionInfo Action="File" Source="hri/hri_wdt_e51.h" IsConfig="false" Hash="o9Rg/hyuMzwOCphVc7uG1w" />
<AcmeProjectActionInfo Action="File" Source="main.c" IsConfig="false" Hash="k0AH7j+BrmdFhBPzCCMptA" />
<AcmeProjectActionInfo Action="File" Source="driver_init.c" IsConfig="false" Hash="rYZS19ftiPajVCnE2RLIkA" />
<AcmeProjectActionInfo Action="File" Source="driver_init.c" IsConfig="false" Hash="Yq1q1Sa7cd5/H8qv36yeFQ" />
<AcmeProjectActionInfo Action="File" Source="driver_init.h" IsConfig="false" Hash="Djt0mG2wi689XcXrLHKt0g" />
<AcmeProjectActionInfo Action="File" Source="atmel_start_pins.h" IsConfig="false" Hash="ByCGTBpkOpAk+zk9txhJSA" />
<AcmeProjectActionInfo Action="File" Source="examples/driver_examples.h" IsConfig="false" Hash="kvd7eb1e9guBnXkzUfLueg" />
@@ -207,12 +207,12 @@
<AcmeProjectActionInfo Action="File" Source="hpl/tcc/hpl_tcc.h" IsConfig="false" Hash="rdWkAK12qkOYV59tWwzy6A" />
<AcmeProjectActionInfo Action="File" Source="atmel_start.h" IsConfig="false" Hash="9RBG5E2ViQiSP2Gn4/FGpA" />
<AcmeProjectActionInfo Action="File" Source="atmel_start.c" IsConfig="false" Hash="1RHIE7zTtYK4DURNPUqF9w" />
<AcmeProjectActionInfo Action="File" Source="config/hpl_adc_config.h" IsConfig="true" Hash="49h3xITKddAgoXK8+KV9zQ" />
<AcmeProjectActionInfo Action="File" Source="config/hpl_ccl_config.h" IsConfig="true" Hash="qVCPwh4wAVouqj+bKLxfMA" />
<AcmeProjectActionInfo Action="File" Source="config/hpl_adc_config.h" IsConfig="true" Hash="XAOTvk5xMalucgzL/ILTWw" />
<AcmeProjectActionInfo Action="File" Source="config/hpl_ccl_config.h" IsConfig="true" Hash="Q1yijLwNXjFOsGrwEEma+g" />
<AcmeProjectActionInfo Action="File" Source="config/hpl_cmcc_config.h" IsConfig="true" Hash="bmtxQ8rLloaRtAo2HeXZRQ" />
<AcmeProjectActionInfo Action="File" Source="config/hpl_dmac_config.h" IsConfig="true" Hash="xCXhbdBwXKUe304TPFonTw" />
<AcmeProjectActionInfo Action="File" Source="config/hpl_eic_config.h" IsConfig="true" Hash="TppQbvFQZJVVi94ogHlIog" />
<AcmeProjectActionInfo Action="File" Source="config/hpl_evsys_config.h" IsConfig="true" Hash="nOzj3aNXGFe1uh39MSx0eA" />
<AcmeProjectActionInfo Action="File" Source="config/hpl_evsys_config.h" IsConfig="true" Hash="/3bNiu/UgpvPbmvfRA+w3g" />
<AcmeProjectActionInfo Action="File" Source="config/hpl_gclk_config.h" IsConfig="true" Hash="fvc5nhPTGTNHCTNlzs6nhA" />
<AcmeProjectActionInfo Action="File" Source="config/hpl_mclk_config.h" IsConfig="true" Hash="pxBzoQXTG66x4dbzVzxteg" />
<AcmeProjectActionInfo Action="File" Source="config/hpl_osc32kctrl_config.h" IsConfig="true" Hash="HgvzEqDUH4jq/syjj/+G+Q" />
@@ -228,7 +228,7 @@
</AcmeProjectConfig>
</AcmeProjectConfig>
<avrtool>com.atmel.avrdbg.tool.atmelice</avrtool>
<avrtoolserialnumber>J42700013870</avrtoolserialnumber>
<avrtoolserialnumber>J42700013287</avrtoolserialnumber>
<avrdeviceexpectedsignature>0x61810302</avrdeviceexpectedsignature>
<avrtoolinterface>SWD</avrtoolinterface>
<com_atmel_avrdbg_tool_atmelice>
@@ -239,7 +239,7 @@
<InterfaceName>SWD</InterfaceName>
</ToolOptions>
<ToolType>com.atmel.avrdbg.tool.atmelice</ToolType>
<ToolNumber>J42700013870</ToolNumber>
<ToolNumber>J42700013287</ToolNumber>
<ToolName>Atmel-ICE</ToolName>
</com_atmel_avrdbg_tool_atmelice>
<avrtoolinterfaceclock>2000000</avrtoolinterfaceclock>
@@ -387,6 +387,7 @@
<armgcc.compiler.directories.IncludePaths>
<ListValues>
<Value>%24(PackRepoDir)\arm\CMSIS\5.4.0\CMSIS\Core\Include\</Value>
<Value>%24(PackRepoDir)\atmel\SAME51_DFP\1.1.139\include</Value>
<Value>../Config</Value>
<Value>../</Value>
<Value>../examples</Value>
@@ -411,10 +412,8 @@
<Value>../hpl/tc</Value>
<Value>../hpl/tcc</Value>
<Value>../hri</Value>
<Value>%24(PackRepoDir)\atmel\SAME51_DFP\1.1.139\include</Value>
</ListValues>
</armgcc.compiler.directories.IncludePaths>
<armgcc.compiler.optimization.level>Optimize more (-O2)</armgcc.compiler.optimization.level>
<armgcc.compiler.optimization.PrepareFunctionsForGarbageCollection>True</armgcc.compiler.optimization.PrepareFunctionsForGarbageCollection>
<armgcc.compiler.optimization.DebugLevel>Maximum (-g3)</armgcc.compiler.optimization.DebugLevel>
<armgcc.compiler.warnings.AllWarnings>True</armgcc.compiler.warnings.AllWarnings>
@@ -439,6 +438,7 @@
<armgcc.assembler.general.IncludePaths>
<ListValues>
<Value>%24(PackRepoDir)\arm\CMSIS\5.4.0\CMSIS\Core\Include\</Value>
<Value>%24(PackRepoDir)\atmel\SAME51_DFP\1.1.139\include</Value>
<Value>../Config</Value>
<Value>../</Value>
<Value>../examples</Value>
@@ -463,13 +463,13 @@
<Value>../hpl/tc</Value>
<Value>../hpl/tcc</Value>
<Value>../hri</Value>
<Value>%24(PackRepoDir)\atmel\SAME51_DFP\1.1.139\include</Value>
</ListValues>
</armgcc.assembler.general.IncludePaths>
<armgcc.assembler.debugging.DebugLevel>Default (-g)</armgcc.assembler.debugging.DebugLevel>
<armgcc.preprocessingassembler.general.IncludePaths>
<ListValues>
<Value>%24(PackRepoDir)\arm\CMSIS\5.4.0\CMSIS\Core\Include\</Value>
<Value>%24(PackRepoDir)\atmel\SAME51_DFP\1.1.139\include</Value>
<Value>../Config</Value>
<Value>../</Value>
<Value>../examples</Value>
@@ -494,7 +494,6 @@
<Value>../hpl/tc</Value>
<Value>../hpl/tcc</Value>
<Value>../hri</Value>
<Value>%24(PackRepoDir)\atmel\SAME51_DFP\1.1.139\include</Value>
</ListValues>
</armgcc.preprocessingassembler.general.IncludePaths>
<armgcc.preprocessingassembler.debugging.DebugLevel>Default (-Wa,-g)</armgcc.preprocessingassembler.debugging.DebugLevel>

View File

@@ -11,8 +11,7 @@
#include "utilities.h"
#include "Ethercat_SlaveDef.h"
void motor_StateMachine(BLDCMotor_t *motor)
void motor_StateMachine(BLDCMotor_t* const motor)
{
if (motor->motor_state.fault)
@@ -34,11 +33,14 @@ void motor_StateMachine(BLDCMotor_t *motor)
motor->motor_state.currentstate = MOTOR_IDLE;
break;
case MOTOR_IDLE:
hri_tcc_write_PATTBUF_reg(motor->motor_param->pwm_desc->device.hw, DISABLE_PATTERN);
motor->motor_state.previousstate = motor->motor_state.currentstate;
motor->motor_state.currentstate = MOTOR_OPEN_LOOP_STATE;
break;
case MOTOR_OPEN_LOOP_STATE:
BLDC_runOpenLoop(motor, *M1_Desired_dc);
BLDC_runOpenLoop(motor, *M1_Desired_dc);
calculate_motor_speed(motor);
motor->motor_state.previousstate = motor->motor_state.currentstate;
break;
case MOTOR_PVI_CTRL_STATE:
switch (Motor1.regulation_loop_count) {
@@ -49,7 +51,7 @@ void motor_StateMachine(BLDCMotor_t *motor)
case 5: case 10: case 15: case 20:/* PWM FREQ / 5 - 5kHz */
//calculate_motor_speed();
//calculate_motor_speed(motor);
//calculate_motor_speed();
BLDC_runSpeedCntl(&Motor1, Motor1.motor_status.calc_rpm, Motor1.controllers.Pid_Speed.Ref_pu);
@@ -75,7 +77,7 @@ void motor_StateMachine(BLDCMotor_t *motor)
}
void BldcInitStruct(BLDCMotor_t *motor, BLDCMotor_param_t *motor_param)
void BldcInitStruct(BLDCMotor_t* const motor, BLDCMotor_param_t * const motor_param)
{
// ----------------------------------------------------------------------
// Assign Motor Parameters:
@@ -97,7 +99,7 @@ void BldcInitStruct(BLDCMotor_t *motor, BLDCMotor_param_t *motor_param)
motor->motor_status.Num_Steps = 0;
motor->motor_status.cur_comm_step = 0;
motor->motor_status.currentHallPattern = 1;
motor->motor_status.nextHallPattern = 3;
//motor->motor_status.nextHallPattern = 3;
motor->motor_setpoints.directionOffset = 0;
// ----------------------------------------------------------------------
@@ -179,7 +181,7 @@ void BldcInitStruct(BLDCMotor_t *motor, BLDCMotor_param_t *motor_param)
motor->controllers.Pi_Pos.OutMin_pu = -(motor_param->motor_Max_Spd_RPM);
}
void exec_commutation(BLDCMotor_t *motor)
void exec_commutation(BLDCMotor_t* const motor)
{
// ----------------------------------------------------------------------
@@ -190,45 +192,41 @@ void exec_commutation(BLDCMotor_t *motor)
volatile uint8_t currentHall = 1;
currentHall = motor->readHall();
motor->motor_status.currentHallPattern = currentHall;
volatile uint8_t currentHallv2 = readHallSensorM1v2();
// ----------------------------------------------------------------------
// Set Pattern Buffers
// ----------------------------------------------------------------------
volatile uint16_t temp_M1 = COMMUTATION_PATTERN[motor->motor_status.currentHallPattern +
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);
// ----------------------------------------------------------------------
// Set Calculated Duty Cycles
// Set Calculated Duty Cycle
// ----------------------------------------------------------------------
hri_tcc_write_CCBUF_CCBUF_bf(motor->motor_param->pwm_desc->device.hw, 0, motor->motor_status.duty_cycle);
motor->motor_status.cur_comm_step = MOTOR_COMMUTATION_STEPS[motor->motor_status.currentHallPattern];
volatile int8_t step_change1 = motor->motor_status.cur_comm_step - motor->motor_status.prev_comm_step;
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);
switch(step_change1)
switch(step_change)
{
case -1: case 5:
motor->motor_status.Num_Steps = motor->motor_status.Num_Steps+1;
motor->motor_status.actualDirection = CW;
case 1: case -5:
motor->motor_status.Num_Steps += 1;
motor->motor_status.actualDirection = CW;
//Motor1.motor_setpoints.directionOffset = DIRECTION_CW_OFFSET;
break;
case 1: case -5:
motor->motor_status.Num_Steps = motor->motor_status.Num_Steps-1;
motor->motor_status.actualDirection = CCW;
case -1: case 5:
motor->motor_status.Num_Steps -= 1;
motor->motor_status.actualDirection = CCW;
//Motor1.motor_setpoints.directionOffset = DIRECTION_CCW_OFFSET;
break;
default:
// do nothing
break;
}
motor->motor_status.prev_comm_step = motor->motor_status.cur_comm_step;
motor->motor_status.prev_comm_step = cur_comm_step;
//toc_port(DEBUG_2_PORT);
}
@@ -238,7 +236,7 @@ void exec_commutation(BLDCMotor_t *motor)
// CW -> Always positive current
// CCW -> Always negative current
//// ------------------------------------------------------------------------------
void select_active_phase(BLDCMotor_t *Motor)
void select_active_phase(BLDCMotor_t* const Motor)
{
uint8_t hall_state = Motor->motor_status.currentHallPattern;
volatile float32_t phase_current = 0;
@@ -263,24 +261,27 @@ void select_active_phase(BLDCMotor_t *Motor)
Motor->Iphase_pu.Bus = phase_current;
}
void calculate_motor_speed(BLDCMotor_t *motor)
void calculate_motor_speed(BLDCMotor_t* const motor)
{
//tic_port(DEBUG_2_PORT);
volatile uint32_t temp_rpm = 0;
hri_tccount32_read_CC_reg(TC0, 0); /* Read CC0 but throw away)*/
volatile uint32_t period_after_capture = hri_tccount32_read_CC_reg(TC0, 1);
if((period_after_capture > UINT32_MAX)|| (period_after_capture > 10712046)) {
Motor1.motor_status.calc_rpm = 0;
hri_tccount32_read_CC_reg(TC2, 0); /* Read CC0 but throw away)*/
volatile uint32_t period_after_capture = hri_tccount32_read_CC_reg(TC2, 1);
if((period_after_capture > UINT32_MAX)||(period_after_capture == 0)) {
motor->motor_status.calc_rpm = 0;
} else {
//uint32_t test = (SPEEDFACTOR, period_after_capture);
//temp_rpm = SPEEDFACTOR / period_after_capture;
//tic_port(DEBUG_3_PORT);
temp_rpm = HZ_TO_RPM / (period_after_capture*motor->motor_param->motor_commutationStates);
////uint32_t test = (SPEEDFACTOR, period_after_capture);
////temp_rpm = SPEEDFACTOR / period_after_capture;
////tic_port(DEBUG_3_PORT);
temp_rpm = HZ_TO_RPM / (period_after_capture * 42);
//temp_rpm = HZ_TO_RPM * period_after_capture;
//toc_port(DEBUG_3_PORT);
////toc_port(DEBUG_3_PORT);
//uint32_t muti = DEVICE_SPEEDTC_FREQUENCY_Hz*60;
//temp_rpm = (muti) / (period_after_capture*42);
}
if(Motor1.motor_status.actualDirection == CCW) /* Changed from CCW */
if(motor->motor_status.actualDirection == CCW) /* Changed from CCW */
{
//*motor_speed = -1* Motor1.calc_rpm;
temp_rpm = -1 * temp_rpm;
@@ -289,33 +290,37 @@ void calculate_motor_speed(BLDCMotor_t *motor)
temp_rpm = temp_rpm;
}
Motor1.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
//if(temp_rpm==0)
//#define n_SAMPLE 8
//static uint32_t speed_average = 0;
//static uint8_t count = 1;
//// To avoid noise an average is realized on 8 samples
//speed_average += temp_rpm;
//if(count >= n_SAMPLE)
//{
//count = 1;
//Motor1.motor_status.calc_rpm = (speed_average >> 3); // divide by 32
////Motor1.motor_status.calc_rpm = (speed_average); // divide by 32
//motor->motor_status.calc_rpm = (speed_average >> 3); // divide by 8
////Motor1.motor_status.calc_rpm = (speed_average); // divide by 8
//speed_average = 0;
////*Spare_byte1 = motorState.actualDirection;
//if(Motor1.motor_status.actualDirection == CCW) /* Changed from CCW */
//if(motor->motor_status.actualDirection == CCW) /* Changed from CCW */
//{
////*motor_speed = -1* Motor1.calc_rpm;
//Motor1.motor_status.calc_rpm = -1* Motor1.motor_status.calc_rpm;
//motor->motor_status.calc_rpm = -1* motor->motor_status.calc_rpm;
//} else {
////*motor_speed = Motor1.calc_rpm;
//Motor1.motor_status.calc_rpm = Motor1.motor_status.calc_rpm;
//motor->motor_status.calc_rpm = motor->motor_status.calc_rpm;
//}
//return;
//}
//else count++;
//#else
//Motor1.motor_status.calc_rpm = (int16_t)temp_rpm;
//#endif
motor->motor_status.calc_rpm = (int16_t)temp_rpm;
//toc_port(DEBUG_2_PORT);
}
@@ -418,36 +423,18 @@ volatile uint8_t readHallSensorM1(void)
{
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));
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));
return motor_read;
}
volatile uint8_t readHallSensorM1v2(void)
{
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;
//(b << 1) |
//(c << 0));
}
volatile uint8_t readHallSensorM2(void)
@@ -458,14 +445,12 @@ 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_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;
}
//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);
@@ -493,7 +478,10 @@ void read_zero_current_offset_value(BLDCMotor_t *motor1, BLDCMotor_t *motor2)
adc_sync_enable_channel(&ADC_1, 9);
//adc_sync_enable_channel(&ADC_1, 0);
ADC1->INPUTCTRL.reg = 0x1809;
/* Single ended */
//ADC1->INPUTCTRL.reg = 0x1809;
/* Differential */
ADC1->INPUTCTRL.reg = 0x0089;
while (ADC1->STATUS.bit.ADCBUSY) {}; /* Wait for bus synchronization. */
for (i=0; i<samples; i++)
@@ -512,7 +500,10 @@ void read_zero_current_offset_value(BLDCMotor_t *motor1, BLDCMotor_t *motor2)
/* Set Motor Variables */
motor1->Voffset_lsb.A = phase_A_zero_current_offset_temp/samples;
ADC1->INPUTCTRL.reg = 0x1808;
/* Single ended */
//ADC1->INPUTCTRL.reg = 0x1808;
/* Differential */
ADC1->INPUTCTRL.reg = 0x0088;
while (ADC1->STATUS.bit.ADCBUSY) {}; /* Wait for bus synchronization. */
for (i=0; i<samples; i++)
@@ -537,7 +528,11 @@ void read_zero_current_offset_value(BLDCMotor_t *motor1, BLDCMotor_t *motor2)
phase_A_zero_current_offset_temp = 0;
phase_B_zero_current_offset_temp = 0;
ADC1->INPUTCTRL.reg = 0x1807;
/* Single ended */
//ADC1->INPUTCTRL.reg = 0x1807;
/* Differential */
ADC1->INPUTCTRL.reg = 0x0087;
while (ADC1->STATUS.bit.ADCBUSY) {}; /* Wait for bus synchronization. */
for (i=0; i<samples; i++)
@@ -556,7 +551,10 @@ void read_zero_current_offset_value(BLDCMotor_t *motor1, BLDCMotor_t *motor2)
/* Set Motor Variables */
motor2->Voffset_lsb.A = phase_A_zero_current_offset_temp/samples;
ADC1->INPUTCTRL.reg = 0x1806;
/* Single ended */
//ADC1->INPUTCTRL.reg = 0x1806;
/* Differential */
ADC1->INPUTCTRL.reg = 0x0086;
while (ADC1->STATUS.bit.ADCBUSY) {}; /* Wait for bus synchronization. */
for (i=0; i<samples; i++)

View File

@@ -29,6 +29,8 @@
#define INVALID_HALL_0 (0)
#define INVALID_HALL_7 (7)
#define AVERAGE_SPEED_MEASURE 1
// ----------------------------------------------------------------------
// ADC Parameters
// ----------------------------------------------------------------------
@@ -63,7 +65,7 @@
// global variables
// ----------------------------------------------------------------------
//static const uint8_t HALL_PATTERN_ARRAY[16] = {0, 5, 3, 1, 6, 4, 2, 0, 0, 3, 6, 2, 5, 1, 4, 0 };
static const uint8_t HALL_PATTERN_ARRAY[16] = {0, 5, 3, 1, 6, 4, 2, 0, 0, 3, 6, 2, 5, 1, 4, 0 };
static const uint8_t MOTOR_COMMUTATION_STEPS[8] = {9, 1, 3, 2, 5, 6, 4, 9};
volatile BLDCMotor_t Motor1;
@@ -75,15 +77,14 @@ volatile MOTOR_STATE_t Motor2_Status;
// ----------------------------------------------------------------------
// functions
// ----------------------------------------------------------------------
void motor_StateMachine(BLDCMotor_t *motor);
void BldcInitStruct(BLDCMotor_t *motor, BLDCMotor_param_t *motor_param);
void exec_commutation(BLDCMotor_t *motor);
void select_active_phase(BLDCMotor_t *Motor);
void motor_StateMachine(BLDCMotor_t* const motor);
void BldcInitStruct(BLDCMotor_t* const motor, BLDCMotor_param_t* constmotor_param);
void exec_commutation(BLDCMotor_t* const motor);
void select_active_phase(BLDCMotor_t* const Motor);
void calculate_motor_speed(BLDCMotor_t* const motor);
// ----------------------------------------------------------------------
// Static Functions
// ----------------------------------------------------------------------
static void calculate_motor_speed(BLDCMotor_t *motor);
static void BLDC_runSpeedCntl(BLDCMotor_t *motor, const float32_t speedfbk, const float32_t speedRef);
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);

View File

@@ -68,12 +68,12 @@ volatile typedef struct
{
volatile uint8_t actualDirection; //! The actual direction of rotation.
volatile uint16_t duty_cycle;
volatile float32_t calc_rpm;
volatile int32_t Num_Steps;
volatile int16_t calc_rpm;
volatile int16_t Num_Steps;
/* Hall States */
volatile uint8_t prevHallPattern;
volatile uint8_t currentHallPattern;
volatile uint8_t nextHallPattern;
//volatile uint8_t prevHallPattern;
volatile uint8_t currentHallPattern;
//volatile uint8_t nextHallPattern;
/* Commutation State */
volatile uint8_t cur_comm_step;
volatile uint8_t prev_comm_step;
@@ -85,10 +85,10 @@ volatile typedef struct
volatile typedef struct BLDCmotor
{
/* Hardware */
BLDCMotor_param_t *motor_param;
MOTOR_STATE_t motor_state;
volatile BLDCMotor_param_t *motor_param;
volatile MOTOR_STATE_t motor_state;
/* Status */
MOTOR_Status motor_status;
volatile MOTOR_Status motor_status;
/* Measured Values */
volatile MOTOR_PHASES_t Iphase_pu;
volatile MOTOR_phase_offset_t Voffset_lsb;
@@ -101,7 +101,7 @@ volatile typedef struct BLDCmotor
/* Setpoints */
volatile MOTOR_Setpoints motor_setpoints;
/* Function Pointers*/
readHallSensor readHall;
volatile readHallSensor readHall;
} BLDCMotor_t;

View File

@@ -19,22 +19,27 @@
#define DMAC_CHANNEL_ADC_SEQ 2U
#define DMAC_CHANNEL_ADC_SRAM 3U
const uint32_t adc_seq_regs[4] = {0x1806, 0x1807, 0x1808, 0x1809};
//const uint32_t adc_seq_regs[5] = {0x0086, 0x0087, 0x0088, 0x0089};
//uint32_t adc_seq_regs[4] = {0x0006, 0x0007, 0x0008, 0x0009};
//const uint32_t adc_seq_regs[5] = {0x1800, 0x1806, 0x1807, 0x1808, 0x1809};
// ----------------------------------------------------------------------
// M1_IA=ADC1_AIN[9], M1_IB=ADC1_AIN[8], M2_IA=ADC1_AIN[7], M2_IB=ADC1_AIN[6]
// ----------------------------------------------------------------------
/* Single Ended */
//const uint32_t adc_seq_regs[4] = {0x1807, 0x1806, 0x1809, 0x1808};
/* Differential */
const uint32_t adc_seq_regs[4] = {0x0089, 0x0088, 0x0087, 0x0086};
volatile int16_t adc_res[4] = {0};
struct _dma_resource *adc_sram_dma_resource;
struct _dma_resource *adc_dmac_sequence_resource;
inline static void configure_tcc_pwm(void)
{
gpio_set_pin_pull_mode(M1_HALLA, GPIO_PULL_UP);
gpio_set_pin_pull_mode(M1_HALLB, GPIO_PULL_UP);
gpio_set_pin_pull_mode(M1_HALLC, GPIO_PULL_UP);
gpio_set_pin_pull_mode(M2_HALLA, GPIO_PULL_UP);
gpio_set_pin_pull_mode(M2_HALLB, GPIO_PULL_UP);
gpio_set_pin_pull_mode(M2_HALLC, GPIO_PULL_UP);
//gpio_set_pin_pull_mode(M1_HALLA, GPIO_PULL_UP);
//gpio_set_pin_pull_mode(M1_HALLB, GPIO_PULL_UP);
//gpio_set_pin_pull_mode(M1_HALLC, GPIO_PULL_UP);
//gpio_set_pin_pull_mode(M2_HALLA, GPIO_PULL_UP);
//gpio_set_pin_pull_mode(M2_HALLB, GPIO_PULL_UP);
//gpio_set_pin_pull_mode(M2_HALLC, GPIO_PULL_UP);
/* TCC0 */
hri_tcc_set_WEXCTRL_OTMX_bf(TCC0, 0x02);

View File

@@ -83,7 +83,7 @@ typedef volatile struct
//}
inline void PI_objectInit(volatile PI_t *pPi_obj)
static inline void PI_objectInit(volatile PI_t *pPi_obj)
{
PI_t *obj = (PI_t *)pPi_obj;
@@ -101,7 +101,7 @@ inline void PI_objectInit(volatile PI_t *pPi_obj)
obj->SatFlag = false;
}
inline void PID_objectInit(volatile PID_t *pPiD_obj)
static inline void PID_objectInit(volatile PID_t *pPiD_obj)
{
PID_t *obj = (PID_t *)pPiD_obj;

View File

@@ -178,6 +178,8 @@ void EXTERNAL_IRQ_0_init(void)
void EVENT_SYSTEM_0_init(void)
{
hri_gclk_write_PCHCTRL_reg(GCLK, EVSYS_GCLK_ID_0, CONF_GCLK_EVSYS_CHANNEL_0_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
hri_gclk_write_PCHCTRL_reg(GCLK, EVSYS_GCLK_ID_1, CONF_GCLK_EVSYS_CHANNEL_1_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
hri_gclk_write_PCHCTRL_reg(GCLK, EVSYS_GCLK_ID_2, CONF_GCLK_EVSYS_CHANNEL_2_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
hri_mclk_set_APBBMASK_EVSYS_bit(MCLK);

View File

@@ -21,8 +21,8 @@ void process_currents()
volatile int16_t phase_A_current_raw, phase_B_current_raw;
/* Motor 1 */
phase_A_current_raw = (adc_res[3] - Motor1.Voffset_lsb.A);
phase_B_current_raw = (adc_res[2] - Motor1.Voffset_lsb.B)*-1;
phase_A_current_raw = (adc_res[0] - Motor1.Voffset_lsb.A);
phase_B_current_raw = (adc_res[1] - Motor1.Voffset_lsb.B)*-1;
// Covert from LSB to PU (A) and filter out small readings
Motor1.Iphase_pu.A = phase_A_current_raw * LSB_TO_PU;
Motor1.Iphase_pu.B = phase_B_current_raw * LSB_TO_PU;
@@ -30,8 +30,8 @@ void process_currents()
Motor1.Iphase_pu.C = -Motor1.Iphase_pu.A - Motor1.Iphase_pu.B;
/* Motor 2 negative is A instead of B*/
phase_A_current_raw = (adc_res[1] - Motor2.Voffset_lsb.A);
phase_B_current_raw = (adc_res[0] - Motor2.Voffset_lsb.B)*-1;
phase_A_current_raw = (adc_res[2] - Motor2.Voffset_lsb.A);
phase_B_current_raw = (adc_res[3] - Motor2.Voffset_lsb.B)*-1;
// Covert from LSB to PU (A) and filter out small readings
Motor2.Iphase_pu.A = phase_A_current_raw * LSB_TO_PU;
Motor2.Iphase_pu.B = phase_B_current_raw * LSB_TO_PU;
@@ -69,6 +69,14 @@ void One_ms_timer_init(void)
timer_start(&TIMER_0);
}
//void speed_timer_init(void)
//{
//hri_tc_write_WAVE_reg(TC_SPEED_M1.device.hw, 0x00);
//hri_tc_set_CTRLA_CAPTEN0_bit(TC_SPEED_M1.device.hw);
//hri_tc_set_CTRLA_CAPTEN1_bit(TC_SPEED_M1.device.hw);
//timer_start(&TC_SPEED_M1);
//}
void enable_NVIC_IRQ(void)
{
//NVIC_EnableIRQ(TC7_IRQn); // TC7: TC_ECAT
@@ -79,7 +87,7 @@ void enable_NVIC_IRQ(void)
//NVIC_EnableIRQ(EIC_5_IRQn);
}
inline void APPLICATION_StateMachine(void)
void APPLICATION_StateMachine(void)
{
Motor1.timerflags.current_loop_tic = false;
@@ -110,8 +118,6 @@ inline void APPLICATION_StateMachine(void)
//applicationStatus.currentstate ;
motor_StateMachine(&Motor1);
motor_StateMachine(&Motor2);
exec_commutation(&Motor1);
exec_commutation(&Motor2);
break;
case APP_FAULT:
//DisableGateDrivers(&Motor1);
@@ -141,19 +147,37 @@ int main(void)
/* Initializes MCU, drivers and middleware */
atmel_start_init();
//initMotorParameters();
//configure_tcc_pwm();
//BldcInitStruct(&Motor1, &FH_22mm24BXTR);
//BldcInitStruct(&Motor2, &FH_32mm24BXTR);
//Motor1.readHall = &readHallSensorM1;
//Motor2.readHall = &readHallSensorM2;
//read_zero_current_offset_value(&Motor1, &Motor2);
//adc_sync_enable_channel(&ADC_1, 6);
//adc_init_dma();
//config_qspi();
//custom_logic_enable();
//enable_NVIC_IRQ();
//ECAT_STATE_MACHINE();
//One_ms_timer_init();
BldcInitStruct(&Motor1, &FH_22mm24BXTR);
BldcInitStruct(&Motor2, &FH_32mm24BXTR);
Motor1.readHall = &readHallSensorM1;
Motor2.readHall = &readHallSensorM2;
read_zero_current_offset_value(&Motor1, &Motor2);
config_qspi();
One_ms_timer_init();
configure_tcc_pwm();
adc_sync_enable_channel(&ADC_1, 6);
adc_init_dma();
ECAT_STATE_MACHINE();
One_ms_timer_init();
custom_logic_enable();
//speed_timer_init();
enable_NVIC_IRQ();
/* Replace with your application code */
while (1) {
//delay_ms(100);
@@ -164,8 +188,9 @@ int main(void)
//update_setpoints();
if (Motor1.timerflags.adc_readings_ready_tic) {process_currents();}
if (Motor1.timerflags.current_loop_tic) {APPLICATION_StateMachine();}
if (run_ECAT) {ECAT_STATE_MACHINE();}
if (run_ECAT) {ECAT_STATE_MACHINE();}
exec_commutation(&Motor1);
exec_commutation(&Motor2);
}
}

View File

@@ -131,24 +131,27 @@
//0x003F // (15) invalid state
//};
//
#define DISABLE_PATTERN 0x003F
////* 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)
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
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 */
@@ -177,6 +180,7 @@ static const uint16_t COMMUTATION_PATTERN[16] = {
typedef struct
{
struct pwm_descriptor const *pwm_desc;
void *const speedtimer_hw;
const uint16_t motor_Poles;
const uint16_t motor_polePairs;
const uint16_t motor_commutationStates;
@@ -196,6 +200,7 @@ typedef struct
/* Small Motor - 2214S024BXTR*/
const static BLDCMotor_param_t FH_22mm24BXTR = {
.pwm_desc = &PWM_0,
.speedtimer_hw = TC2,
.motor_Poles = 14,
.motor_polePairs = 7,
.motor_commutationStates = 42, //polePairs * 6
@@ -212,6 +217,7 @@ const static BLDCMotor_param_t FH_22mm24BXTR = {
/* Big Motor - 3216W024BXTR */
const static BLDCMotor_param_t FH_32mm24BXTR = {
.pwm_desc = &PWM_1,
.speedtimer_hw = TC4,
.motor_Poles = 14,
.motor_polePairs = 7,
.motor_commutationStates = 42, //polePairs * 6