• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 #ifndef HPM_BLDC_DEFINE_H
8 #define HPM_BLDC_DEFINE_H
9 
10 #if defined(__cplusplus)
11 extern "C" {
12 #endif /* __cplusplus */
13 
14 /**
15  * @addtogroup  middleware_mcl_interfaces HPMicro Motor Control Library
16  * @ingroup middleware_interfaces
17  *
18  */
19 
20 #ifdef CONFIG_BLDC_HAS_EXTRA_CONFIG
21 #include CONFIG_BLDC_HAS_EXTRA_CONFIG
22 #endif
23 
24 #include "hpm_motor_math.h"
25 /**
26  * @addtogroup  mcl_common HPMicro MCL
27  * @ingroup middleware_mcl_interfaces
28  * @{
29  *
30  */
31 
32 /**
33  * @brief bldc motor rotation direction
34  *
35  */
36 #define BLDC_MOTOR_DIR_FORWARD 0
37 #define BLDC_MOTOR_DIR_REVERSE 1
38 
39 /**
40  * @brief Motor pin definition U, V, W three pairs
41  *
42  */
43 #define BLDC_PWM_PIN_UH     0
44 #define BLDC_PWM_PIN_UL     1
45 #define BLDC_PWM_PIN_VH     2
46 #define BLDC_PWM_PIN_VL     3
47 #define BLDC_PWM_PIN_WH     4
48 #define BLDC_PWM_PIN_WL     5
49 
50 /**
51  * @brief Motor serial number, listing four motors
52  *
53  */
54 #define BLDC_MOTOR0_INDEX                (1)
55 #define BLDC_MOTOR1_INDEX                (2)
56 #define BLDC_MOTOR2_INDEX                (3)
57 #define BLDC_MOTOR3_INDEX                (4)
58 
59 /**
60  * @brief Serial number of the current sampling array
61  *
62  */
63 #define ADCU_INDEX                 (0)
64 #define ADCV_INDEX                 (1)
65 #define ADCW_INDEX                 (2)
66 
67 /**
68  * @brief PWM output channel definition, used for internal calculations
69  *
70  */
71 #define BLDC_PWM_U          0
72 #define BLDC_PWM_V          1
73 #define BLDC_PWM_W          2
74 
75 /**
76  * @brief Get ADC data with 12bit valid bits
77  *
78  */
79 #define GET_ADC_12BIT_VALID_DATA(x)             ((x & 0xffff) >> 4)
80 
81 /**
82  * @brief All function pointers defined in the middleware point to this function,
83  * and if the implementation of the function is not initialized to make the call,
84  * an error will be reported through this function.
85  *
86  */
87 extern void hpm_mcl_nullcallback_func(void);
88 
89 /**
90  * @brief The mounting angle of the Hall sensor, 60 degrees or 120 degrees,
91  * is an inherent property of the motor.
92  *
93  */
94 typedef enum bldc_hall_phase {
95     bldc_hall_phase_60, /*60 °*/
96     bldc_hall_phase_120 /*120 °*/
97 } bldc_hall_phase_t;
98 
99 /**
100  * @brief Motor related structure definition.
101  * Parameters that need to be initialized follow this rule
102  * Naming rules:
103  * physical property description definition.
104  * Input (i)/output (o)_physical_volume_description_units_of_physical_volume.
105  *
106  */
107 typedef struct hpm_motor_par {
108     float  i_rstator_ohm;           /**< Stator resistance (in ohm) */
109     float  i_poles_n;               /**< polar logarithm */
110     float  i_maxspeed_rs;           /**< Maximum speed r/s */
111     float  i_lstator_h;             /**< Stator inductor */
112     float  i_phasecur_a;            /**< Rated current */
113     float  i_phasevol_v;            /**< Rated voltage */
114     float  i_samplingper_s;         /**< Current sampling period */
115     HPM_MOTOR_MATH_TYPE  o_smc_f;   /**< Sliding mode control factor1 */
116     HPM_MOTOR_MATH_TYPE  o_smc_g;   /**< Sliding mode control factor2 */
117     void   (*func_smc_const)(void *str);     /**< Calculate the function of the sliding mode control coefficient */
118 } hpm_motor_para_t;
119 
120 #define BLDC_MOTOR_PARA_DEFAULTS {0, 0, 0,\
121                                         0, 0, 0,\
122                                         0, 0, 0,\
123                                         NULL}
124 
125 /**
126  * @brief Speed calculation parameters
127  *
128  */
129 typedef struct  bldc_contrl_spd_par {
130     uint16_t            i_speedacq;               /**< Update velocity data once after collecting n times of angle data */
131     uint16_t            num;                    /**< Internal Data */
132     HPM_MOTOR_MATH_TYPE       i_speedlooptime_s;        /**< Time for n cycles. Unit s */
133     HPM_MOTOR_MATH_TYPE       speedtheta;             /**< Current motor angle */
134     HPM_MOTOR_MATH_TYPE       speedlasttheta;          /**< Internal Data */
135     HPM_MOTOR_MATH_TYPE       speedthetalastn;        /**< Internal Data, Initialization Clear */
136     HPM_MOTOR_MATH_TYPE       i_speedfilter;            /**< Low-pass filter coefficient */
137     HPM_MOTOR_MATH_TYPE       o_speedout_filter;        /**< Speed after filter */
138     HPM_MOTOR_MATH_TYPE       o_speedout;               /**< Speed before filter */
139     hpm_motor_para_t     *i_motorpar;                /**< Motor operating parameters */
140     void    (*func_getspd)(void *str);                     /**< Speed calculation function */
141 } BLDC_CONTRL_SPD_PARA;
142 
143 #define BLDC_CONTRL_SPD_PARA_DEFAULTS {0, 0, 0, 0, 0,\
144                                         0, 0, 0, 0, NULL,\
145                                         NULL}
146 
147 /**
148  * @brief pid control parameters
149  *
150  */
151 typedef struct bldc_contrl_pid_par {
152     HPM_MOTOR_MATH_TYPE       i_kp;                   /**< Kp */
153     HPM_MOTOR_MATH_TYPE       i_ki;                   /**< Ki */
154     HPM_MOTOR_MATH_TYPE       i_kd;                   /**< Kd */
155     HPM_MOTOR_MATH_TYPE       i_max;                  /**< Output max,  min = -max */
156     HPM_MOTOR_MATH_TYPE       target;                 /**< Target parameters */
157     HPM_MOTOR_MATH_TYPE       mem;                    /**< Intenal Data */
158     HPM_MOTOR_MATH_TYPE       cur;                    /**< Sampling data  */
159     HPM_MOTOR_MATH_TYPE       outval;                 /**< Output Data */
160     void (*func_pid)(void *str);                               /**< Pid function */
161 } BLDC_CONTRL_PID_PARA;
162 #define BLDC_CONTRL_PID_PARA_DEFAULTS {0, 0, 0, 0,\
163                                         0, 0, 0, 0,\
164                                        NULL}
165 
166 /**
167  * @brief Current sampling parameters
168  *
169  */
170 typedef struct bldc_contrl_current_par {
171     uint16_t            adc_u;                  /**< u Phase current AD sampling value */
172     uint16_t            adc_v;                  /**< v Phase current AD sampling value */
173     uint16_t            adc_w;                  /**< W Phase current AD sampling value */
174     uint16_t            adc_u_middle;           /**< u Phase current midpoint AD sampling value */
175     uint16_t            adc_v_middle;           /**< v Phase current midpoint AD sampling value */
176     uint16_t            adc_w_middle;           /**< w Phase current midpoint AD sampling value */
177     HPM_MOTOR_MATH_TYPE       cal_u;            /**< Calculated U-phase current */
178     HPM_MOTOR_MATH_TYPE       cal_v;            /**< Calculated V-phase current */
179     HPM_MOTOR_MATH_TYPE       cal_w;            /**< Calculated W-phase current */
180     void *userdata;                             /**< user data */
181     void (*func_sampl)(void *str);                       /**< current samples */
182 } BLDC_CONTROL_CURRENT_PARA;
183 #define BLDC_CONTROL_CURRENT_PARA_DEFAULTS {0, 0, 0,\
184                                             0, 0, 0,\
185                                             0, 0, 0,\
186                                             NULL, NULL}
187 
188 /**
189  * @brief PWM output parameters
190  *
191  */
192 typedef struct bldc_control_pwmout_par {
193     uint8_t             i_motor_id;           /**< Motor id @ref BLDC_MOTOR0_INDEX ... BLDC_MOTOR3_INDEX */
194     uint8_t             i_sync_id;            /**< Synchronization id */
195     uint32_t            pwm_u;                /**< u pwm duty cycle */
196     uint32_t            pwm_v;                /**< v pwm duty cycle */
197     uint32_t            pwm_w;                /**< w pwm duty cycle */
198     uint32_t            i_pwm_reload;         /**< pwm reload value, pwm configuration related */
199     void (*func_set_pwm)(void *str);                   /**< pwm output function */
200 } BLDC_CONTROL_PWMOUT_PARA;
201 #define BLDC_CONTROL_PWMOUT_PARA_DEFAULTS {0, 0, 0,\
202                                             0, 0, 0,\
203                                             NULL}
204 /**
205  * @brief svpwm parameters
206  *
207  */
208 typedef struct bldc_control_pwm_par {
209     HPM_MOTOR_MATH_TYPE       target_alpha; /**< alpha voltage */
210     HPM_MOTOR_MATH_TYPE       target_beta;  /**< beta voltage */
211     int8_t             sector;             /**< Sector Number */
212     uint32_t            i_pwm_reload_max;       /**< Maximum duty cycle the pwm module can output */
213     BLDC_CONTROL_PWMOUT_PARA    pwmout; /**< @ref BLDC_CONTROL_PWMOUT_PARA */
214     void (*func_spwm)(void *str);                    /**< svpwm function */
215 } BLDC_CONTROL_PWM_PARA;
216 #define BLDC_CONTROL_PWM_PARA_DEFAULTS {0, 0, 0, 0,\
217                                            BLDC_CONTROL_PWMOUT_PARA_DEFAULTS,\
218                                             NULL}
219 /**
220  * @brief Location estimation function
221  *
222  */
223 typedef struct bldc_func_cal {
224     void *par;
225     void (*func)(void *str);
226 } BLDC_FUNC_CAL;
227 
228 #define BLDC_FUNC_CAL_DEFAULTS {NULL, NULL}
229 
230 /**
231  * @brief foc control
232  *
233  */
234 typedef struct bldc_contrl_foc_par {
235     BLDC_CONTRL_PID_PARA        currentdpipar;             /**< D-axis current pi parameters */
236     BLDC_CONTRL_PID_PARA        currentqpipar;             /**< Q-axis current pi parameters */
237     BLDC_CONTRL_SPD_PARA        speedcalpar;               /**< Speed calculation parameters */
238     HPM_MOTOR_MATH_TYPE         electric_angle;            /**< Electric angle */
239     BLDC_CONTROL_CURRENT_PARA   samplcurpar;               /**< Sampling current */
240     hpm_motor_para_t             motorpar;                  /**< Motor parameters */
241     BLDC_CONTROL_PWM_PARA       pwmpar;                    /**< PWM parameters */
242     BLDC_FUNC_CAL               pos_estimator_par;         /**< Null pointers do not run the position estimation algorithm, pointers are assigned for position estimation */
243     HPM_MOTOR_MATH_TYPE               ualpha;             /**< alpha voltage */
244     HPM_MOTOR_MATH_TYPE               ubeta;              /**< beta voltage */
245     HPM_MOTOR_MATH_TYPE               ialpha;             /**< alpha current */
246     HPM_MOTOR_MATH_TYPE               ibeta;              /**< beta current */
247     void (*func_dqsvpwm)(void *str, void *str1, void *str2, void *data);                               /**< dq axis current to svpwm function */
248 } BLDC_CONTROL_FOC_PARA;
249 #define BLDC_CONTROL_FOC_PARA_DEFAULTS {BLDC_CONTRL_PID_PARA_DEFAULTS, BLDC_CONTRL_PID_PARA_DEFAULTS,\
250                                         BLDC_CONTRL_SPD_PARA_DEFAULTS, 0,\
251                                         BLDC_CONTROL_CURRENT_PARA_DEFAULTS, BLDC_MOTOR_PARA_DEFAULTS,\
252                                         BLDC_CONTROL_PWM_PARA_DEFAULTS,\
253                                         BLDC_FUNC_CAL_DEFAULTS,\
254                                         0, 0, 0, 0, NULL}
255 
256 /**
257  * @}
258  *
259  */
260 
261 #if defined(__cplusplus)
262 }
263 #endif /* __cplusplus */
264 #endif
265