53 #pragma CODE_SECTION(mainISR,"ramfuncs");
101 #pragma DATA_SECTION(halHandle,"rom_accessed_data");
120 #pragma DATA_SECTION(gUserParams,"rom_accessed_data");
128 extern uint16_t *RamfuncsLoadStart, *RamfuncsLoadEnd, *RamfuncsRunStart;
131 extern uint16_t *econst_start, *econst_end, *econst_ram_load;
132 extern uint16_t *switch_start, *switch_end, *switch_ram_load;
193 memCopy((uint16_t *)&RamfuncsLoadStart,(uint16_t *)&RamfuncsLoadEnd,(uint16_t *)&RamfuncsRunStart);
197 if(*econst_end - *econst_start)
199 memCopy((uint16_t *)&econst_start,(uint16_t *)&econst_end,(uint16_t *)&econst_ram_load);
203 if(*switch_end - *switch_start)
205 memCopy((uint16_t *)&switch_start,(uint16_t *)&switch_end,(uint16_t *)&switch_ram_load);
232 clarkeHandle_I =
CLARKE_init(&clarke_I,
sizeof(clarke_I));
233 clarkeHandle_V =
CLARKE_init(&clarke_V,
sizeof(clarke_V));
307 _iq Kp_Id =
_IQ((0.25*Ls_d*fullScaleCurrent)/(IsrPeriod_sec*fullScaleVoltage));
308 _iq Ki_Id =
_IQ(RoverLs_d*IsrPeriod_sec);
309 _iq Kp_Iq =
_IQ((0.25*Ls_q*fullScaleCurrent)/(IsrPeriod_sec*fullScaleVoltage));
310 _iq Ki_Iq =
_IQ(RoverLs_q*IsrPeriod_sec);
312 pidHandle[0] =
PID_init(&pid[0],
sizeof(pid[0]));
313 pidHandle[1] =
PID_init(&pid[1],
sizeof(pid[1]));
314 pidHandle[2] =
PID_init(&pid[2],
sizeof(pid[2]));
334 iparkHandle =
IPARK_init(&ipark,
sizeof(ipark));
343 for(cnt=0;cnt<6;cnt++)
345 filterHandle[cnt] =
FILTER_FO_init(&filter[cnt],
sizeof(filter[0]));
355 svgenHandle =
SVGEN_init(&svgen,
sizeof(svgen));
362 float_t minWidth_microseconds = 2.0;
365 SVGENCURRENT_setMinWidth(svgencurrentHandle, minWidth_counts);
366 SVGENCURRENT_setIgnoreShunt(svgencurrentHandle, use_all);
370 trajHandle_spd =
TRAJ_init(&traj_spd,
sizeof(traj_spd));
380 trajHandle_Id =
TRAJ_init(&traj_Id,
sizeof(traj_Id));
392 fwHandle =
FW_init(&fw,
sizeof(fw));
574 gFlag_enableRsOnLine =
false;
575 gFlag_updateRs =
false;
717 fbackValue = gIdq_pu.
value[0];
720 PID_run(pidHandle[1],refValue,fbackValue,&(gVdq_out_pu.
value[0]));
726 refValue = gIdq_ref_pu.
value[1];
729 fbackValue = gIdq_pu.
value[1];
734 PID_run(pidHandle[2],refValue,fbackValue,&(gVdq_out_pu.
value[1]));
747 IPARK_run(iparkHandle,&gVdq_out_pu,&Vab_pu);
797 _iq angleDeltaComp_pu =
_IQmpy(angleDelta_pu, angleCompFactor);
798 uint32_t angleMask = ((uint32_t)0xFFFFFFFF >> (32 -
GLOBAL_Q));
803 angleTmp_pu = angleUncomp_pu + angleDeltaComp_pu;
807 angleComp_pu =
_IQabs(angleTmp_pu) & angleMask;
810 if(angleTmp_pu <
_IQ(0.0))
812 angleComp_pu = -angleComp_pu;
815 return(angleComp_pu);
829 SVGENCURRENT_RunIgnoreShunt(svgencurrentHandle,cmp1,cmp2,cmp3,cmpM1,cmpM2,cmpM3);
838 SVGENCURRENT_RunRegenCurrent(svgencurrentHandle, (
MATH_vec3 *)(gAdcData.
I.
value));
844 if(ignoreShuntThisCycle == ignore_ab)
849 else if(ignoreShuntThisCycle == ignore_ac)
854 else if(ignoreShuntThisCycle == ignore_bc)
877 refValue = gMotorVars.
VsRef;
879 fbackValue = gMotorVars.
Vs;
881 FW_run(fwHandle, refValue, fbackValue, &(gIdq_ref_pu.
value[0]));
902 for(cnt=0;cnt<3;cnt++)
908 gOffsets_I_pu.
value[cnt] =
_IQ(0.0);
909 gOffsets_V_pu.
value[cnt] =
_IQ(0.0);
916 if(gOffsetCalcCount++ >= gUserParams.
ctrlWaitTime[CTRL_State_OffLine])
919 gOffsetCalcCount = 0;
921 for(cnt=0;cnt<3;cnt++)
941 int_least8_t lShift = ceil(log(
USER_MOTOR_Ls_d/(Ls_coarse_max*fullScaleInductance))/log(2.0));
942 uint_least8_t Ls_qFmt = 30 - lShift;
943 float_t L_max = fullScaleInductance * pow(2.0,lShift);
959 int16_t minwidth = SVGENCURRENT_getMinWidth(svgencurrentHandle);
974 _iq alpha_sf,beta_sf;
977 if(numCurrentSensors == 3)
982 else if(numCurrentSensors == 2)
1006 _iq alpha_sf,beta_sf;
1009 if(numVoltageSensors == 3)
1016 alpha_sf =
_IQ(0.0);
1041 _iq Torque_Flux_Iq_Nm =
_IQmpy(
_IQmpy(Flux_pu,Iq_pu),gTorque_Flux_Iq_pu_to_Nm_sf);
1042 _iq Torque_Ls_Id_Iq_Nm =
_IQmpy(
_IQmpy(
_IQmpy(Ld_minus_Lq_pu,Id_pu),Iq_pu),gTorque_Ls_Id_Iq_pu_to_Nm_sf);
1043 _iq Torque_Nm = Torque_Flux_Iq_Nm + Torque_Ls_Id_Iq_Nm;
1079 gMotorVars.
Vd = gVdq_out_pu.
value[0];
1080 gMotorVars.
Vq = gVdq_out_pu.
value[1];
1105 gCpuUsagePercentageMin = (
float_t)minDeltaCntObserved / cpu_usage_den * 100.0;
1108 gCpuUsagePercentageAvg = (
float_t)avgDeltaCntObserved / cpu_usage_den * 100.0;
1111 gCpuUsagePercentageMax = (
float_t)maxDeltaCntObserved / cpu_usage_den * 100.0;
float_t EST_getFlux_VpHz(EST_Handle handle)
#define USER_SYSTEM_FREQ_MHz
CLOCKS & TIMERS.
float_t EST_getRsOnLine_Ohm(EST_Handle handle)
#define USER_NUM_CURRENT_SENSORS
Defines the number of current sensors used.
void EST_setFlag_enableForceAngle(EST_Handle handle, const bool state)
IPARK_Handle IPARK_init(void *pMemory, const size_t numBytes)
_iq EST_getLs_coarse_max_pu(EST_Handle handle)
HAL_AdcData_t gAdcData
contains three current values, three voltage values and one DC buss value
static void TRAJ_setTargetValue(TRAJ_Handle handle, const _iq targetValue)
void HAL_enableGlobalInts(HAL_Handle handle)
void HAL_enableAdcInts(HAL_Handle handle)
static _iq PID_getRefValue(PID_Handle handle)
USER_ErrorCode_e UserErrorCode
#define USER_MAX_NEGATIVE_ID_REF_CURRENT_A
LIMITS.
float_t gCpuUsagePercentageMin
static void HAL_readAdcDataWithOffsets(HAL_Handle handle, HAL_AdcData_t *pAdcData)
HAL_Handle halHandle
the handle for the hardware abstraction layer (HAL)
_iq USER_computeFlux_pu_to_VpHz_sf(void)
Computes the scale factor needed to convert from per unit to V/Hz.
_iq gTorque_Ls_Id_Iq_pu_to_Nm_sf
MATH_vec3 gOffsets_V_pu
contains the offsets for the voltage feedback
TRAJ_Handle trajHandle_Id
the handle for the id reference trajectory
static void TRAJ_setMaxDelta(TRAJ_Handle handle, const _iq maxDelta)
CLARKE_Obj clarke_I
the current Clarke transform object
static void PID_setUi(PID_Handle handle, const _iq Ui)
_iq EST_getSpeed_krpm(EST_Handle handle)
void EST_setRsOnLineAngleDelta_pu(EST_Handle handle, const _iq angleDelta_pu)
volatile _iq gRsOnLineId_mag_A
static uint16_t HAL_readPwmCmpAM(HAL_Handle handle, const PWM_Number_e pwmNumber)
void EST_setRsOnLineId_mag_pu(EST_Handle handle, const _iq Id_mag_pu)
FW_Handle FW_init(void *pMemory, const size_t numBytes)
static _iq TRAJ_getIntValue(TRAJ_Handle handle)
void EST_setupEstIdleState(EST_Handle handle)
float_t gCpuUsagePercentageMax
static bool FW_getFlag_enableFw(FW_Handle fwHandle)
static uint32_t FW_getNumIsrTicksPerFwTick(FW_Handle fwHandle)
void softwareUpdate1p6(EST_Handle handle)
Updates version 1p6 of library.
bool Flag_enablePowerWarp
static void HAL_writePwmData(HAL_Handle handle, HAL_PwmData_t *pPwmData)
CLARKE_Obj clarke_V
the voltage Clarke transform object
static void FW_setOutput(FW_Handle fwHandle, const _iq output)
struct _EST_Obj_ * EST_Handle
void EST_setIdle(EST_Handle handle)
void HAL_writeDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
IPARK_Handle iparkHandle
the handle for the inverse Park transform
TRAJ_Obj traj_Id
the id reference trajectory object
#define USER_VD_SF
Defines the direct voltage (Vd) scale factor.
static void HAL_acqAdcInt(HAL_Handle handle, const ADC_IntNumber_e intNumber)
uint_least32_t ctrlWaitTime[CTRL_numStates]
#define USER_IQ_FULL_SCALE_VOLTAGE_V
Defines full scale value for the IQ30 variable of Voltage inside the system.
MATH_vec2 gIdq_ref_pu
contains the Id and Iq references
#define USER_MOTOR_NUM_POLE_PAIRS
volatile bool gFlag_enableRsOnLine
CLARKE_Handle clarkeHandle_I
the handle for the current Clarke transform
static void FW_run(FW_Handle fwHandle, const _iq refValue, const _iq fbackValue, _iq *pOutValue)
void memCopy(uint16_t *srcStartAddr, uint16_t *srcEndAddr, uint16_t *dstAddr)
float_t gCpuUsagePercentageAvg
bool Flag_enableForceAngle
#define USER_PWM_FREQ_kHz
Defines the Pulse Width Modulation (PWM) frequency, kHz.
static void PID_setMinMax(PID_Handle handle, const _iq outMin, const _iq outMax)
static void SVGEN_run(SVGEN_Handle handle, const MATH_vec2 *pVab, MATH_vec3 *pT)
static uint32_t CPU_USAGE_getAvgDeltaCntObserved(CPU_USAGE_Handle handle)
static _iq FILTER_FO_run(FILTER_FO_Handle handle, const _iq inputValue)
static _iq PID_getFbackValue(PID_Handle handle)
CPU_USAGE_Handle cpu_usageHandle
void updateCPUusage(void)
Updates CPU usage.
#define MATH_ONE_OVER_THREE
void USER_setParams(USER_Params *pUserParams)
Sets the user parameter values.
void EST_setLs_qFmt(EST_Handle handle, const uint_least8_t Ls_qFmt)
#define USER_MAX_ACCEL_Hzps
Defines the starting maximum acceleration AND deceleration for the speed profiles, Hz/s.
void EST_setFlag_updateRs(EST_Handle handle, const bool state)
static _iq FILTER_FO_get_y1(FILTER_FO_Handle handle)
volatile MOTOR_Vars_t gMotorVars
the global motor variables that are defined in main.h and used for display in the debugger's watch wi...
FILTER_FO_Handle FILTER_FO_init(void *pMemory, const size_t numBytes)
#define V_A_offset
ADC voltage offsets for A, B, and C phases.
_iq EST_getLs_d_pu(EST_Handle handle)
#define I_A_offset
ADC current offsets for A, B, and C phases.
static void CLARKE_setScaleFactors(CLARKE_Handle handle, const _iq alpha_sf, const _iq beta_sf)
Defines the structures, global initialization, and functions used in MAIN.
void runCurrentReconstruction(void)
TRAJ_Handle TRAJ_init(void *pMemory, const size_t numBytes)
CTRL_Handle CTRL_init(void *pMemory, const size_t numBytes)
void HAL_setupFaults(HAL_Handle handle)
EST_Handle EST_init(void *pMemory, const size_t numBytes)
float_t EST_getLs_d_H(EST_Handle handle)
void EST_setFlag_enableRsRecalc(EST_Handle handle, const bool state)
static void TRAJ_setIntValue(TRAJ_Handle handle, const _iq intValue)
static void PID_setGains(PID_Handle handle, const _iq Kp, const _iq Ki, const _iq Kd)
static void HAL_enablePwm(HAL_Handle handle)
#define USER_MAX_VS_MAG_PU
Defines the maximum Voltage vector (Vs) magnitude allowed. This value sets the maximum magnitude for ...
void CTRL_setParams(CTRL_Handle handle, USER_Params *pUserParams)
SVGEN_Handle svgenHandle
the handle for the space vector generator
SVGENCURRENT_Obj svgencurrent
USER_ErrorCode_e USER_getErrorCode(USER_Params *pUserParams)
Gets the error code in the user parameters.
void HAL_enableDrv(HAL_Handle handle)
void FILTER_FO_setNumCoeffs(FILTER_FO_Handle handle, const _iq b0, const _iq b1)
void updateGlobalVariables(EST_Handle handle)
Update the global variables (gMotorVars).
_iq EST_getOneOverDcBus_pu(EST_Handle handle)
MATH_vec3 gOffsets_I_pu
contains the offsets for the current feedback
_iq EST_getFlux_pu(EST_Handle handle)
void HAL_setupDrvSpi(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
_iq gTorque_Flux_Iq_pu_to_Nm_sf
float_t EST_getLs_q_H(EST_Handle handle)
#define USER_EST_HANDLE_ADDRESS
Defines the address of estimator handle.
void HAL_enableDebugInt(HAL_Handle handle)
void CTRL_setupEstIdleState(CTRL_Handle handle)
#define USER_NUM_CTRL_TICKS_PER_SPEED_TICK
Defines the number of controller clock ticks per speed controller clock tick.
CLARKE_Handle CLARKE_init(void *pMemory, const size_t numBytes)
static void FW_incCounter(FW_Handle fwHandle)
void CTRL_setUserMotorParams(CTRL_Handle handle)
SVGENCURRENT_IgnoreShunt_e
static void IPARK_run(IPARK_Handle handle, const MATH_vec2 *pInVec, MATH_vec2 *pOutVec)
PID_Obj pid[3]
three handles for PID controllers 0 - Speed, 1 - Id, 2 - Iq
#define FW_NUM_ISR_TICKS_PER_CTRL_TICK
EST_State_e EST_getState(EST_Handle handle)
bool Flag_enableOffsetcalc
_iq EST_getFm_pu(EST_Handle handle)
TRAJ_Handle trajHandle_spd
the handle for the speed reference trajectory
PID_Handle pidHandle[3]
three objects for PID controllers 0 - Speed, 1 - Id, 2 - Iq
#define MOTOR_Vars_INIT
Initialization values of global variables.
volatile _iq gRsOnLineFreq_Hz
void EST_setFlag_enableRsOnLine(EST_Handle handle, const bool state)
CLARKE_Handle clarkeHandle_V
the handle for the voltage Clarke transform
_iq USER_computeTorque_Flux_Iq_pu_to_Nm_sf(void)
Computes the scale factor needed to convert from torque created by flux and Iq, from per unit to Nm...
float_t EST_getIdRated(EST_Handle handle)
#define USER_MOTOR_RES_EST_CURRENT
uint32_t gOffsetCalcCount
static uint32_t HAL_readTimerCnt(HAL_Handle handle, const uint_least8_t timerNumber)
_iq EST_getLs_q_pu(EST_Handle handle)
static void CLARKE_setNumSensors(CLARKE_Handle handle, const uint_least8_t numSensors)
float_t EST_getRs_Ohm(EST_Handle handle)
FILTER_FO_Handle filterHandle[6]
the handles for the 3-current and 3-voltage filters for offset calculation
#define USER_MOTOR_MAGNETIZING_CURRENT
#define USER_IQ_FULL_SCALE_FREQ_Hz
CURRENTS AND VOLTAGES.
IPARK_Obj ipark
the inverse Park transform object
void CPU_USAGE_setParams(CPU_USAGE_Handle handle, const uint32_t timerPeriod_cnts, const uint32_t numDeltaCntsAvg)
USER_Params gUserParams
The user parameters.
SVGEN_Obj svgen
the space vector generator object
static void FW_setFlag_enableFw(FW_Handle fwHandle, const bool state)
#define USER_ISR_FREQ_Hz
Defines the Interrupt Service Routine (ISR) frequency, Hz.
static void IPARK_setPhasor(IPARK_Handle handle, const MATH_vec2 *pPhasor)
interrupt void mainISR(void)
The main ISR that implements the motor control.
static void FW_clearCounter(FW_Handle fwHandle)
CPU_USAGE_Handle CPU_USAGE_init(void *pMemory, const size_t numBytes)
void setupClarke_I(CLARKE_Handle handle, const uint_least8_t numCurrentSensors)
Setup the Clarke transform for either 2 or 3 sensors.
_iq EST_getAngle_pu(EST_Handle handle)
static void HAL_disablePwm(HAL_Handle handle)
float_t EST_runPowerWarp(EST_Handle handle, const float_t Id_int_A, const float_t Iq_A)
void FILTER_FO_setInitialConditions(FILTER_FO_Handle handle, const _iq x1, const _iq y1)
static void TRAJ_setMaxValue(TRAJ_Handle handle, const _iq maxValue)
static void FW_setDeltas(FW_Handle fwHandle, const _iq delta_inc, const _iq delta_dec)
PID_Handle PID_init(void *pMemory, const size_t numBytes)
HAL_Handle HAL_init(void *pMemory, const size_t numBytes)
static void CLARKE_run(CLARKE_Handle handle, const MATH_vec3 *pInVec, MATH_vec2 *pOutVec)
void EST_setAngle_pu(EST_Handle handle, const _iq angle_pu)
static uint32_t CPU_USAGE_getMaxDeltaCntObserved(CPU_USAGE_Handle handle)
void runCurrentIgnore(void)
static uint16_t HAL_readPwmCmpA(HAL_Handle handle, const PWM_Number_e pwmNumber)
#define USER_VOLTAGE_FILTER_POLE_rps
Defines the analog voltage filter pole location, rad/s.
#define USER_NUM_VOLTAGE_SENSORS
Defines the number of voltage (phase) sensors.
void EST_setLs_q_pu(EST_Handle handle, const _iq Ls_q_pu)
EST_Handle estHandle
the handle for the estimator
uint_least32_t ctrlFreq_Hz
#define USER_IQ_FULL_SCALE_CURRENT_A
Defines the full scale current for the IQ variables, A.
static void HAL_setTrigger(HAL_Handle handle, const SVGENCURRENT_IgnoreShunt_e ignoreShunt, const int16_t minwidth, const int16_t cmpOffset)
static void PID_run_spd(PID_Handle handle, const _iq refValue, const _iq fbackValue, _iq *pOutValue)
CTRL_Handle ctrlHandle
The controller handle.
volatile _iq gRsOnLinePole_Hz
_iq EST_getRsOnLineId_pu(EST_Handle handle)
static void CPU_USAGE_updateCnts(CPU_USAGE_Handle handle, const uint32_t cnt)
static uint32_t CPU_USAGE_getMinDeltaCntObserved(CPU_USAGE_Handle handle)
#define MATH_ONE_OVER_SQRT_THREE
void setupClarke_V(CLARKE_Handle handle, const uint_least8_t numVoltageSensors)
Setup the Clarke transform for either 2 or 3 sensors.
static uint32_t FW_getCounter(FW_Handle fwHandle)
static void HAL_initIntVectorTable(HAL_Handle handle)
uint16_t pidCntSpeed
count variable to decimate the execution of the speed PID controller
void FILTER_FO_setDenCoeffs(FILTER_FO_Handle handle, const _iq a1)
SVGENCURRENT_Handle SVGENCURRENT_init(void *pMemory, const size_t numBytes)
static void FW_setNumIsrTicksPerFwTick(FW_Handle fwHandle, const uint32_t numIsrTicksPerFwTick)
static uint16_t HAL_readPwmPeriod(HAL_Handle handle, const PWM_Number_e pwmNumber)
bool EST_updateState(EST_Handle handle, const _iq Id_target_pu)
TRAJ_Obj traj_spd
the speed reference trajectory object
float_t EST_getRr_Ohm(EST_Handle handle)
volatile bool gFlag_updateRs
void HAL_setParams(HAL_Handle handle, const USER_Params *pUserParams)
static void CPU_USAGE_run(CPU_USAGE_Handle handle)
void EST_setRsOnLineId_pu(EST_Handle handle, const _iq Id_pu)
FILTER_FO_Obj filter[6]
the 3-current and 3-voltage filters for offset calculation
void HAL_readDrvData(HAL_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars)
void EST_setRsOnLineFilterParams(EST_Handle handle, const EST_RsOnLineFilterType_e filterType, const _iq filter_0_b0, const _iq filter_0_a1, const _iq filter_0_y1, const _iq filter_1_b0, const _iq filter_1_a1, const _iq filter_1_y1)
MATH_vec2 gVdq_out_pu
contains the output Vd and Vq from the current controllers
bool Flag_enableFieldWeakening
static void TRAJ_run(TRAJ_Handle handle)
HAL_PwmData_t gPwmData
contains the three pwm values -1.0 - 0%, 1.0 - 100%
#define USER_MOTOR_MAX_CURRENT
static void FW_setMinMax(FW_Handle fwHandle, const _iq outMin, const _iq outMax)
_iq angleDelayComp(const _iq fm_pu, const _iq angleUncomp_pu)
The angleDelayComp function compensates for the delay introduced.
void EST_getIdq_pu(EST_Handle handle, MATH_vec2 *pIdq_pu)
void runFieldWeakening(void)
void USER_checkForErrors(USER_Params *pUserParams)
Checks for errors in the user parameter values.
MATH_vec2 gIdq_pu
contains the Id and Iq measured values
SVGEN_Handle SVGEN_init(void *pMemory, const size_t numBytes)
void EST_setEstParams(EST_Handle handle, USER_Params *pUserParams)
_iq USER_computeTorque_Ls_Id_Iq_pu_to_Nm_sf(void)
Computes the scale factor needed to convert from torque created by Ld, Lq, Id and Iq...
_iq USER_computeFlux_pu_to_Wb_sf(void)
Computes the scale factor needed to convert from per unit to Wb.
SVGENCURRENT_Handle svgencurrentHandle
void EST_setLs_d_pu(EST_Handle handle, const _iq Ls_d_pu)
void runOffsetsCalculation(void)
static void PID_run(PID_Handle handle, const _iq refValue, const _iq fbackValue, _iq *pOutValue)
static void TRAJ_setMinValue(TRAJ_Handle handle, const _iq minValue)
void EST_run(EST_Handle handle, const MATH_vec2 *pIab_pu, const MATH_vec2 *pVab_pu, const _iq dcBus_pu, const _iq speed_ref_pu)
#define USER_NUM_PWM_TICKS_PER_ISR_TICK
DECIMATION.