|
MasterFOC 0.0.1
|
#include "stm32f4xx_hal.h"#include "arm_math.h"#include "tim.h"#include "stdio.h"Go to the source code of this file.
Data Structures | |
| struct | Components |
| Coordinates components structure. More... | |
| struct | PID_para |
| PID parameter structure. More... | |
| struct | Motor |
| Motor parameter structure. More... | |
Macros | |
| #define | pi 3.14159265359f |
| #define | SQRT_3 1.7320508f |
| #define | UnitPulse 4096 |
Typedefs | |
| typedef struct Components | Coordinates |
| Coordinates components structure. More... | |
| typedef struct PID_para | PID_para |
| PID parameter structure. More... | |
| typedef enum ControlMode | ControlMode |
| ControlMode enumerator. More... | |
| typedef enum UserAPP | UserAPP |
| UserAPP enumerator. More... | |
| typedef enum DRV_GAIN | DRV_GAIN |
| DRV8302 amplification gain enumerator. More... | |
Enumerations | |
| enum | ControlMode { Idle = 0 , Speed , Position , Torque } |
| ControlMode enumerator. More... | |
| enum | UserAPP { Drag = 1 , ForceFeedback , Switch , SpaceWalk } |
| UserAPP enumerator. More... | |
| enum | DRV_GAIN { DRV_GAIN_10 = 0 , DRV_GAIN_40 } |
| DRV8302 amplification gain enumerator. More... | |
Functions | |
| void | MotorInit (Motor *mHandle, ADC_HandleTypeDef hadc, TIM_HandleTypeDef hDriver, TIM_HandleTypeDef hEncoder, I2C_HandleTypeDef hIICEncoder, ControlMode mode) |
| : Motor initialization function More... | |
| void | ModeSelection (Motor *mHandle, ControlMode mode) |
| : Mode selection function More... | |
| void | UserAPPSelection (Motor *mHandle, UserAPP app) |
| : user application selection function More... | |
| void | EnableGate (void) |
| : Pull up the ENGATE pin of the DRV8302 to activate the gate More... | |
| void | GainSelection (DRV_GAIN gain) |
| : Select the gain of the buit-in amplifier of DRV8302 If GAIN = LOW, the internal current shunt amplifiers have a gain of 10V/V. If GAIN = HIGH, the current shunt amplifiers have a gain of 40V/V More... | |
| void | DC_Calibration (Motor *mHandle) |
| : Calibrate the DC offset of the current shunt amplifier should be called after EnableGate() More... | |
| void | SetMotorParam (Motor *mHandle, uint8_t DC_supply, uint8_t phase, uint8_t polePairs, float MaxCurrent, float MaxSpeed) |
| : Set motor parameters More... | |
| int | calcAngle (Motor *mHandle) |
| : [Auxiliary Debug Function] calculate electric angle from Valpha and Vbeta More... | |
| void | MotorCalibration (Motor *mHandle) |
| : Calibrate motor when power on Measure the angle offset between encoder 0 deg and motor electrical 0 deg(phase a 100%) Angle_E = (Angle_M - Offset)*nPolePairs More... | |
| void | MotorOpenLoop (Motor *mHandle) |
| : openloop drag control More... | |
| void | ADCSample (Motor *mHandle) |
| : Current sampling function, once called, retrieve one set of sampled data from DMA Data stored in mHandle->Current called in timer callback function every control period More... | |
| void | ADCFilter (Motor *mHandle) |
| : Filter the sampled current More... | |
| void | CurrentReconstruction (Motor *mHandle) |
| : Reconstruct the sampled current from 2 phases to three phases More... | |
| void | getEangle (Motor *mHandle) |
| : Calculate electric angle from mechanical angle, store in mHandle->E_angle More... | |
| void | getSpeed (Motor *mHandle) |
| : Calculate the motor speed in rpm, store in mHandle->speed More... | |
| void | ClarkeTransform (Coordinates *pComponents) |
| : Clarke Transformation function, convert abc to alpha beta More... | |
| void | ParkTransform (Coordinates *pComponents, int theta) |
| : Park Transformation function, convert alpha, beta to d, q Original park: q leads d axis by 90 deg Modified park: d leads q axis by 90 deg More... | |
| void | InvPark (Coordinates *pComponents, int theta) |
| : Inverse Park Transformation function, convert d, q to alpha, beta Original park: q leads d axis by 90 deg Modified park: d leads q axis by 90 deg More... | |
| void | GetSector (Motor *mHandle) |
| : Calculate the sector of the target space voltage vector More... | |
| void | ModulationLimit (Motor *mHandle) |
| : Overmodulation limiting function, prevent overmodulation More... | |
| void | SetSVPWM (Motor *mHandle) |
| : MOSFET dwelling time calculation function, to generate SVPWM More... | |
| void | dq2SVPWM (Motor *mHandle) |
| : Generate SVPWM directly from dq voltage More... | |
| void | SetCurrentPID (Motor *mHandle, float kp, float ki, float kd) |
| : Set PID parameters of both d,q current loop More... | |
| void | SetSpeedPID (Motor *mHandle, float kp, float ki, float kd, float outMax) |
| : Set speed loop PID parameters and the speed limit More... | |
| void | SetCurrentTarget (Motor *mHandle, float idTarget, float iqTarget) |
| : Set target id, iq More... | |
| void | SetSpeedTarget (Motor *mHandle, float Target) |
| : Set target motor speed, with over speed limit More... | |
| void | IdPID (Motor *mHandle) |
| : Current loop Id PID calculation, calculate target Vd from Id with current kalman filter, integral limiter, and over voltage limiter More... | |
| void | IqPID (Motor *mHandle) |
| : Current loop Iq PID calculation, calculate target Vq from Iq with current kalman filter, integral limiter, and over voltage limiter More... | |
| void | SpeedPID (Motor *mHandle) |
| : Speed loop PID calculation, set target Id = 0, calculate target Iq from speed with integral limiter, and over current limiter More... | |
| void | AnglePID (Motor *mHandle) |
| : Angle loop PID calculation, calculate target speed with integral limiter More... | |
| void | SetAnglePID (Motor *mHandle, float kp, float ki, float kd) |
| : Set angle loop PID parameters More... | |
| void | SetAngleTarget (Motor *mHandle, float Target) |
| : Set absolute target angle, only single-turn (0-360 deg) More... | |
| void | SetIncrementAngle (Motor *mHandle, float Target) |
| : Set increment target angle, the incremental angle is also restricted in (0-360 deg) More... | |
| void | FOC (Motor *mHandle) |
| : FOC loop funtion, including electric angle conversion, phase current filtering, Clarke/Park transformation, PID calculation, and SVPWM generation Called in ADC_DMA callback every control period More... | |
| void | FOCcheck (Motor *mHandle) |
| : [Auxiliary Debug Function] call to print FOC PID parameters More... | |
| void | GetSVPWM (Motor *mHandle) |
| : Original SVPWM calculation function using complex numbers, slow More... | |
| typedef enum ControlMode ControlMode |
ControlMode enumerator.
to set the operation mode of the FOC driver
| typedef struct Components Coordinates |
Coordinates components structure.
abc, alpha/beta, d/q
PID parameter structure.
pid parameters and target, error, previous error, and sum of error
UserAPP enumerator.
to set different user applications of the FOC driver
| enum ControlMode |
| enum DRV_GAIN |
| enum UserAPP |
| void ADCFilter | ( | Motor * | mHandle | ) |
: Filter the sampled current
| [in] | mHandle motor instance handler | |
| [out] |
| void ADCSample | ( | Motor * | mHandle | ) |
: Current sampling function, once called, retrieve one set of sampled data from DMA Data stored in mHandle->Current called in timer callback function every control period
| [in] | mHandle motor instance handler | |
| [out] |
| void AnglePID | ( | Motor * | mHandle | ) |
: Angle loop PID calculation, calculate target speed with integral limiter
| [in] | mHandle motor instance handler | |
| [out] | SetSpeedTarget(mHandle, target) |
| int calcAngle | ( | Motor * | mHandle | ) |
: [Auxiliary Debug Function] calculate electric angle from Valpha and Vbeta
| [in] | mHandle motor instance handler | |
| [out] |
| void ClarkeTransform | ( | Coordinates * | pComponents | ) |
: Clarke Transformation function, convert abc to alpha beta
| [in] | pComponents coordinates pointer (pComponents->a, pComponents->b, pComponents->c) | |
| [out] | pComponents coordinates pointer (pComponents->alpha, pComponents->beta) |
| void CurrentReconstruction | ( | Motor * | mHandle | ) |
: Reconstruct the sampled current from 2 phases to three phases
| [in] | mHandle motor instance handler | |
| [out] |
| void DC_Calibration | ( | Motor * | mHandle | ) |
: Calibrate the DC offset of the current shunt amplifier should be called after EnableGate()
| [in] | mHandle motor instance handler | |
| [out] |
| void dq2SVPWM | ( | Motor * | mHandle | ) |
: Generate SVPWM directly from dq voltage
| [in] | mHandle motor instance handler | |
| [out] |
| void EnableGate | ( | void | ) |
: Pull up the ENGATE pin of the DRV8302 to activate the gate
| void FOC | ( | Motor * | mHandle | ) |
: FOC loop funtion, including electric angle conversion, phase current filtering, Clarke/Park transformation, PID calculation, and SVPWM generation Called in ADC_DMA callback every control period
| [in] | mHandle motor instance handler | |
| [out] |
| void FOCcheck | ( | Motor * | mHandle | ) |
: [Auxiliary Debug Function] call to print FOC PID parameters
| [in] | mHandle motor instance handler | |
| [out] | mHandle->idPID, mHandle->iqPID, mHandle->speedPID, mHandle->anglePID |
| void GainSelection | ( | DRV_GAIN | gain | ) |
: Select the gain of the buit-in amplifier of DRV8302 If GAIN = LOW, the internal current shunt amplifiers have a gain of 10V/V. If GAIN = HIGH, the current shunt amplifiers have a gain of 40V/V
| [in] | gain DRV_GAIN_10, DRV_GAIN_40 |
| void getEangle | ( | Motor * | mHandle | ) |
: Calculate electric angle from mechanical angle, store in mHandle->E_angle
| [in] | mHandle motor instance handler | |
| [out] | mHandle->E_angle |
| void GetSector | ( | Motor * | mHandle | ) |
: Calculate the sector of the target space voltage vector
| [in] | mHandle->Voltage.alpha, mHandle->Voltage.beta | |
| [out] | mHandle->sector |
| void getSpeed | ( | Motor * | mHandle | ) |
: Calculate the motor speed in rpm, store in mHandle->speed
| [in] | mHandle motor instance handler | |
| [out] | mHandle->speed |
| void GetSVPWM | ( | Motor * | mHandle | ) |
: Original SVPWM calculation function using complex numbers, slow
| [in] | ||
| [out] |
| void IdPID | ( | Motor * | mHandle | ) |
: Current loop Id PID calculation, calculate target Vd from Id with current kalman filter, integral limiter, and over voltage limiter
| [in] | mHandle motor instance handler | |
| [out] | mHandle->Voltage.d |
| void InvPark | ( | Coordinates * | pComponents, |
| int | theta | ||
| ) |
: Inverse Park Transformation function, convert d, q to alpha, beta Original park: q leads d axis by 90 deg Modified park: d leads q axis by 90 deg
| [in] | pComponents coordinates pointer (pComponents->d, pComponents->q) | |
| [out] | pComponents coordinates pointer (pComponents->alpha, pComponents->beta) |
| void IqPID | ( | Motor * | mHandle | ) |
: Current loop Iq PID calculation, calculate target Vq from Iq with current kalman filter, integral limiter, and over voltage limiter
| [in] | mHandle motor instance handler | |
| [out] | mHandle->Voltage.q |
| void ModeSelection | ( | Motor * | mHandle, |
| ControlMode | mode | ||
| ) |
: Mode selection function
| [in] | mHandle motor instance handler | |
| [in] | mode operation mode |
| void ModulationLimit | ( | Motor * | mHandle | ) |
: Overmodulation limiting function, prevent overmodulation
| [in] | mHandle->T1, mHandle->T2 | |
| [out] | mHandle->T1, mHandle->T2 |
| void MotorCalibration | ( | Motor * | mHandle | ) |
: Calibrate motor when power on Measure the angle offset between encoder 0 deg and motor electrical 0 deg(phase a 100%) Angle_E = (Angle_M - Offset)*nPolePairs
| [in] | mHandle motor instance handler |
| void MotorInit | ( | Motor * | mHandle, |
| ADC_HandleTypeDef | hadc, | ||
| TIM_HandleTypeDef | hDriver, | ||
| TIM_HandleTypeDef | hEncoder, | ||
| I2C_HandleTypeDef | hIICEncoder, | ||
| ControlMode | mode | ||
| ) |
: Motor initialization function
| [in] | mHandle motor instance handler | |
| [in] | hadc current sampling adc handler | |
| [in] | hDriver driver timer handler (set the control frequency) | |
| [in] | hEncoder PWM encoder timer handler | |
| [in] | hIICEncoder I2C encoder handler | |
| [in] | mode initial operation mode |
| void MotorOpenLoop | ( | Motor * | mHandle | ) |
: openloop drag control
| [in] | mHandle motor instance handler | |
| [out] |
| void ParkTransform | ( | Coordinates * | pComponents, |
| int | theta | ||
| ) |
: Park Transformation function, convert alpha, beta to d, q Original park: q leads d axis by 90 deg Modified park: d leads q axis by 90 deg
| [in] | pComponents coordinates pointer (pComponents->alpha, pComponents->beta) | |
| [out] | pComponents coordinates pointer (pComponents->d, pComponents->q) |
| void SetAnglePID | ( | Motor * | mHandle, |
| float | kp, | ||
| float | ki, | ||
| float | kd | ||
| ) |
: Set angle loop PID parameters
| [in] | mHandle motor instance handler | |
| [in] | kp, ki, kd | |
| [out] | mHandle->anglePID |
| void SetAngleTarget | ( | Motor * | mHandle, |
| float | Target | ||
| ) |
: Set absolute target angle, only single-turn (0-360 deg)
| [in] | mHandle motor instance handler | |
| [in] | Target | |
| [out] | mHandle->anglePID.target |
| void SetCurrentPID | ( | Motor * | mHandle, |
| float | kp, | ||
| float | ki, | ||
| float | kd | ||
| ) |
: Set PID parameters of both d,q current loop
| [in] | mHandle motor instance handler | |
| [in] | kp, ki, kd | |
| [out] | mHandle->iqPID, mHandle->idPID |
| void SetCurrentTarget | ( | Motor * | mHandle, |
| float | idTarget, | ||
| float | iqTarget | ||
| ) |
: Set target id, iq
| [in] | mHandle motor instance handler | |
| [in] | idTarget, iqTarget | |
| [out] | mHandle->idPID.target, mHandle->iqPID.target |
| void SetIncrementAngle | ( | Motor * | mHandle, |
| float | Target | ||
| ) |
: Set increment target angle, the incremental angle is also restricted in (0-360 deg)
| [in] | mHandle motor instance handler | |
| [out] | SetAngleTarget(mHandle,Target); |
| void SetMotorParam | ( | Motor * | mHandle, |
| uint8_t | DC_supply, | ||
| uint8_t | phase, | ||
| uint8_t | polePairs, | ||
| float | MaxCurrent, | ||
| float | MaxSpeed | ||
| ) |
: Set motor parameters
| [in] | mHandle motor instance handler | |
| [in] | V DC supply voltage | |
| [in] | phase number of phase | |
| [in] | polePairs number of pole pairs | |
| [in] | MaxCurrent maximum current -> maximum phase current | |
| [in] | MaxSpeed maximum speed |
| void SetSpeedPID | ( | Motor * | mHandle, |
| float | kp, | ||
| float | ki, | ||
| float | kd, | ||
| float | outMax | ||
| ) |
: Set speed loop PID parameters and the speed limit
| [in] | mHandle motor instance handler | |
| [in] | kp, ki, kd, outMax | |
| [out] | mHandle->speedPID, mHandle->SpeedLimit |
| void SetSpeedTarget | ( | Motor * | mHandle, |
| float | Target | ||
| ) |
: Set target motor speed, with over speed limit
| [in] | mHandle motor instance handler | |
| [in] | Target | |
| [out] | mHandle->speedPID.target |
| void SetSVPWM | ( | Motor * | mHandle | ) |
: MOSFET dwelling time calculation function, to generate SVPWM
| [in] | mHandle motor instance handler | |
| [out] | mHandle->Ta, mHandle->Tb, mHandle->Tc |
| void SpeedPID | ( | Motor * | mHandle | ) |
: Speed loop PID calculation, set target Id = 0, calculate target Iq from speed with integral limiter, and over current limiter
| [in] | mHandle motor instance handler | |
| [out] | mHandle->iqPID.target |