MasterFOC 0.0.1
control.h
Go to the documentation of this file.
1
12#ifndef __CONTROL_H
13#define __CONTROL_H
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
19#ifdef __cplusplus
20}
21#endif
22
23
24#include "stm32f4xx_hal.h"
25#include "arm_math.h"
26#include "tim.h"
27#include "stdio.h"
28
29#define pi 3.14159265359f
30#define SQRT_3 1.7320508f
31#define UnitPulse 4096 //pulses from encoder per cycle(2pi)
32//#define BLOCK_SIZE 1
33//#define NUM_TAPS 6
34
39typedef struct Components
40{
41 int16_t a, b, c;
42 float alpha, beta;
43 float d, q;
45
50typedef struct PID_para{
51 float kp, ki, kd;
54
59typedef enum ControlMode
61
66typedef enum UserAPP
68
73typedef enum DRV_GAIN
79typedef struct
80{
81 ADC_HandleTypeDef hMotorADC;
82 TIM_HandleTypeDef hTimDriver;
83 TIM_HandleTypeDef hTimEncoder;
84 I2C_HandleTypeDef hI2CEncoder;
98 uint8_t Vdc;
99 uint8_t nPhase;
100 uint8_t nPolePairs;
102 int16_t SpeedCNT;
103 uint16_t M_angle;
104 uint16_t M_angle_pre;
105 int16_t E_angle;
106 int16_t E_angle_pre;
107 uint16_t Angle_offset;
109 float speed, speed_pre;
110 float CurrentLimit, VoltageLimit, SpeedLimit;
111 int32_t lapCNT,lapCNT_pre;
112 uint8_t sector;
113 float Uref1, Uref2, Uref3;
114 float X, Y, Z;
115 float T1,T2,Tx,Ty,Tz;
116 float Ta, Tb, Tc;
118 uint16_t ADC_DMA_RAW[3];
119 uint16_t DC_offset[2];
120 int16_t ADC_Filtered[3];
121 //int16_t ADC_lastValue[3];
122 int16_t Current_lastValue[5];
124}Motor;
125
126
139void MotorInit(Motor* mHandle,ADC_HandleTypeDef hadc,TIM_HandleTypeDef hDriver, TIM_HandleTypeDef hEncoder, I2C_HandleTypeDef hIICEncoder, ControlMode mode);
140
149void ModeSelection(Motor* mHandle,ControlMode mode);
150
151
160void UserAPPSelection(Motor* mHandle,UserAPP app);
161
168void EnableGate(void);
169
179
189void DC_Calibration(Motor* mHandle);
190
203void SetMotorParam(Motor* mHandle,uint8_t DC_supply, uint8_t phase, uint8_t polePairs, float MaxCurrent, float MaxSpeed);
204
213int calcAngle(Motor* mHandle);
214
224void MotorCalibration(Motor* mHandle);
225
226//void AngelAlignment(Motor* mHandle);
227
236void MotorOpenLoop(Motor* mHandle);
237
248void ADCSample(Motor* mHandle);
249
258void ADCFilter(Motor* mHandle);
259
269
278void getEangle(Motor* mHandle);
279
288void getSpeed(Motor* mHandle);
289//void setDQ(Motor mHandle, double d, double q);
290
299void ClarkeTransform(Coordinates* pComponents);
300
311void ParkTransform(Coordinates* pComponents, int theta);
312
323void InvPark(Coordinates* pComponents, int theta);
324
333void GetSector(Motor* mHandle);
334
343void ModulationLimit(Motor* mHandle);
344
353void SetSVPWM(Motor* mHandle);
354
363void dq2SVPWM(Motor* mHandle);
364
374void SetCurrentPID(Motor* mHandle, float kp, float ki, float kd);
375
385void SetSpeedPID(Motor* mHandle, float kp, float ki, float kd, float outMax);
386
396void SetCurrentTarget(Motor* mHandle, float idTarget, float iqTarget);
397
409void SetSpeedTarget(Motor* mHandle, float Target);
410
420void IdPID(Motor* mHandle);
421
431void IqPID(Motor* mHandle);
432
442void SpeedPID(Motor* mHandle);
443
455void AnglePID(Motor* mHandle);
456
466void SetAnglePID(Motor* mHandle, float kp, float ki, float kd);
467
477void SetAngleTarget(Motor* mHandle, float Target);
478
487void SetIncrementAngle(Motor* mHandle, float Target);
488
499void FOC(Motor* mHandle);
500
509void FOCcheck(Motor* mHandle);
510
519void GetSVPWM(Motor* mHandle);
520
554#endif /* __CONTROL_H */
555
556
void AnglePID(Motor *mHandle)
: Angle loop PID calculation, calculate target speed with integral limiter
void ADCSample(Motor *mHandle)
: Current sampling function, once called, retrieve one set of sampled data from DMA Data stored in mH...
void MotorInit(Motor *mHandle, ADC_HandleTypeDef hadc, TIM_HandleTypeDef hDriver, TIM_HandleTypeDef hEncoder, I2C_HandleTypeDef hIICEncoder, ControlMode mode)
: Motor initialization function
void getEangle(Motor *mHandle)
: Calculate electric angle from mechanical angle, store in mHandle->E_angle
int calcAngle(Motor *mHandle)
: [Auxiliary Debug Function] calculate electric angle from Valpha and Vbeta
void SetMotorParam(Motor *mHandle, uint8_t DC_supply, uint8_t phase, uint8_t polePairs, float MaxCurrent, float MaxSpeed)
: Set motor parameters
void IqPID(Motor *mHandle)
: Current loop Iq PID calculation, calculate target Vq from Iq with current kalman filter,...
void ClarkeTransform(Coordinates *pComponents)
: Clarke Transformation function, convert abc to alpha beta
void FOC(Motor *mHandle)
: FOC loop funtion, including electric angle conversion, phase current filtering, Clarke/Park transfo...
void ModulationLimit(Motor *mHandle)
: Overmodulation limiting function, prevent overmodulation
void getSpeed(Motor *mHandle)
: Calculate the motor speed in rpm, store in mHandle->speed
void IdPID(Motor *mHandle)
: Current loop Id PID calculation, calculate target Vd from Id with current kalman filter,...
DRV_GAIN
DRV8302 amplification gain enumerator.
Definition: control.h:74
@ DRV_GAIN_40
Definition: control.h:74
@ DRV_GAIN_10
Definition: control.h:74
void UserAPPSelection(Motor *mHandle, UserAPP app)
: user application selection function
void dq2SVPWM(Motor *mHandle)
: Generate SVPWM directly from dq voltage
struct Components Coordinates
Coordinates components structure.
void CurrentReconstruction(Motor *mHandle)
: Reconstruct the sampled current from 2 phases to three phases
void EnableGate(void)
: Pull up the ENGATE pin of the DRV8302 to activate the gate
void InvPark(Coordinates *pComponents, int theta)
: Inverse Park Transformation function, convert d, q to alpha, beta Original park: q leads d axis by ...
void ParkTransform(Coordinates *pComponents, int theta)
: Park Transformation function, convert alpha, beta to d, q Original park: q leads d axis by 90 deg M...
void SpeedPID(Motor *mHandle)
: Speed loop PID calculation, set target Id = 0, calculate target Iq from speed with integral limiter...
void DC_Calibration(Motor *mHandle)
: Calibrate the DC offset of the current shunt amplifier should be called after EnableGate()
void SetCurrentPID(Motor *mHandle, float kp, float ki, float kd)
: Set PID parameters of both d,q current loop
void MotorOpenLoop(Motor *mHandle)
: openloop drag control
void SetAnglePID(Motor *mHandle, float kp, float ki, float kd)
: Set angle loop PID parameters
void ADCFilter(Motor *mHandle)
: Filter the sampled current
void FOCcheck(Motor *mHandle)
: [Auxiliary Debug Function] call to print FOC PID parameters
void SetIncrementAngle(Motor *mHandle, float Target)
: Set increment target angle, the incremental angle is also restricted in (0-360 deg)
void GetSector(Motor *mHandle)
: Calculate the sector of the target space voltage vector
void SetSpeedPID(Motor *mHandle, float kp, float ki, float kd, float outMax)
: Set speed loop PID parameters and the speed limit
void ModeSelection(Motor *mHandle, ControlMode mode)
: Mode selection function
void MotorCalibration(Motor *mHandle)
: Calibrate motor when power on Measure the angle offset between encoder 0 deg and motor electrical 0...
void SetSpeedTarget(Motor *mHandle, float Target)
: Set target motor speed, with over speed limit
UserAPP
UserAPP enumerator.
Definition: control.h:67
@ ForceFeedback
Definition: control.h:67
@ Switch
Definition: control.h:67
@ Drag
Definition: control.h:67
@ SpaceWalk
Definition: control.h:67
void SetCurrentTarget(Motor *mHandle, float idTarget, float iqTarget)
: Set target id, iq
void SetAngleTarget(Motor *mHandle, float Target)
: Set absolute target angle, only single-turn (0-360 deg)
void GainSelection(DRV_GAIN gain)
: Select the gain of the buit-in amplifier of DRV8302 If GAIN = LOW, the internal current shunt ampli...
void SetSVPWM(Motor *mHandle)
: MOSFET dwelling time calculation function, to generate SVPWM
void GetSVPWM(Motor *mHandle)
: Original SVPWM calculation function using complex numbers, slow
ControlMode
ControlMode enumerator.
Definition: control.h:60
@ Position
Definition: control.h:60
@ Speed
Definition: control.h:60
@ Torque
Definition: control.h:60
@ Idle
Definition: control.h:60
Coordinates components structure.
Definition: control.h:40
int16_t b
Definition: control.h:41
float q
Definition: control.h:43
float d
Definition: control.h:43
float beta
Definition: control.h:42
float alpha
Definition: control.h:42
int16_t c
Definition: control.h:41
int16_t a
Definition: control.h:41
Motor parameter structure.
Definition: control.h:80
int16_t E_angle_pre
Definition: control.h:106
ControlMode Mode
Definition: control.h:86
uint16_t Angle_offset
Definition: control.h:107
PID_para iqPID
Definition: control.h:93
float Ta
Definition: control.h:116
uint8_t nPolePairs
Definition: control.h:100
I2C_HandleTypeDef hI2CEncoder
Definition: control.h:84
int16_t E_angle
Definition: control.h:105
float CurrentLimit
Definition: control.h:110
TIM_HandleTypeDef hTimEncoder
Definition: control.h:83
UserAPP APP
Definition: control.h:87
uint8_t sector
Definition: control.h:112
PID_para idPID
Definition: control.h:92
float speed
Definition: control.h:109
ADC_HandleTypeDef hMotorADC
Definition: control.h:81
Coordinates Voltage
Definition: control.h:89
uint8_t nCalibrationFlag
Definition: control.h:97
PID_para speedPID
Definition: control.h:94
uint16_t M_angle_pre
Definition: control.h:104
uint8_t Vdc
Definition: control.h:98
Coordinates Current
Definition: control.h:90
int16_t SpeedCNT
Definition: control.h:102
int32_t lapCNT
Definition: control.h:111
float X
Definition: control.h:114
float Uref1
Definition: control.h:113
float T1
Definition: control.h:115
uint16_t M_angle
Definition: control.h:103
PID_para anglePID
Definition: control.h:95
uint8_t nPhase
Definition: control.h:99
TIM_HandleTypeDef hTimDriver
Definition: control.h:82
PID parameter structure.
Definition: control.h:50
float ki
Definition: control.h:51
float err_sum
Definition: control.h:52
float kp
Definition: control.h:51
float err
Definition: control.h:52
float kd
Definition: control.h:51
float target
Definition: control.h:52
float err_pre
Definition: control.h:52