• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 #ifndef HPM_MCL_CONTROL_H
8 #define HPM_MCL_CONTROL_H
9 #include "hpm_mcl_common.h"
10 #include "hpm_mcl_math.h"
11 
12 /**
13  * @brief Control Module Status
14  *
15  */
16 typedef enum {
17     control_status_null = 0,
18     control_status_init = 1,
19     control_status_run = 2,
20     control_status_fail = 3,
21 } mcl_control_status_t;
22 
23 /**
24  * @brief Six sectors of svpwm
25  *
26  */
27 typedef enum {
28     svpwm_sector1 = 1,
29     svpwm_sector2 = 2,
30     svpwm_sector3 = 3,
31     svpwm_sector4 = 4,
32     svpwm_sector5 = 5,
33     svpwm_sector6 = 6,
34 } mcl_svpwm_sector_t;
35 
36 /**
37  * @brief Configuration data for pid
38  *
39  */
40 typedef struct {
41     hpm_mcl_type_t kp;
42     hpm_mcl_type_t ki;
43     hpm_mcl_type_t kd;
44     hpm_mcl_type_t integral_max;    /**< Maximum value of integral */
45     hpm_mcl_type_t integral_min;    /**< Minimum value of the integral */
46     hpm_mcl_type_t output_max;      /**< Maximum value of output */
47     hpm_mcl_type_t output_min;      /**< Minimum value of output */
48 } mcl_control_pid_cfg_t;
49 
50 /**
51  * @brief pid running data
52  *
53  */
54 typedef struct {
55     mcl_control_pid_cfg_t cfg;
56     hpm_mcl_type_t integral;
57 } mcl_control_pid_t;
58 
59 /**
60  * @brief pwm duty cycle
61  *
62  */
63 typedef struct {
64     hpm_mcl_type_t a;
65     hpm_mcl_type_t b;
66     hpm_mcl_type_t c;
67     /**
68      * @brief step motor pwm
69      *
70      */
71     hpm_mcl_type_t a0;
72     hpm_mcl_type_t a1;
73     hpm_mcl_type_t b0;
74     hpm_mcl_type_t b1;
75 } mcl_control_svpwm_duty_t;
76 
77 /**
78  * @brief Supported control algorithms
79  *
80  */
81 typedef struct {
82     hpm_mcl_type_t (*sin_x)(hpm_mcl_type_t x);
83     hpm_mcl_type_t (*cos_x)(hpm_mcl_type_t x);
84     hpm_mcl_type_t (*arctan_x)(hpm_mcl_type_t y, hpm_mcl_type_t x);
85     hpm_mcl_stat_t (*park)(hpm_mcl_type_t alpha, hpm_mcl_type_t beta,
86                 hpm_mcl_type_t sin_x, hpm_mcl_type_t cos_x,
87                 hpm_mcl_type_t *d, hpm_mcl_type_t *q);
88     hpm_mcl_stat_t (*clarke)(hpm_mcl_type_t ia, hpm_mcl_type_t ib, hpm_mcl_type_t ic,
89                 hpm_mcl_type_t *alpha, hpm_mcl_type_t *beta);
90     hpm_mcl_stat_t (*currentd_pid)(hpm_mcl_type_t ref, hpm_mcl_type_t sens, mcl_control_pid_t *pid_x, hpm_mcl_type_t *output);
91     hpm_mcl_stat_t (*currentq_pid)(hpm_mcl_type_t ref, hpm_mcl_type_t sens, mcl_control_pid_t *pid_x, hpm_mcl_type_t *output);
92     hpm_mcl_stat_t (*speed_pid)(hpm_mcl_type_t ref, hpm_mcl_type_t sens, mcl_control_pid_t *pid_x, hpm_mcl_type_t *output);
93     hpm_mcl_stat_t (*position_pid)(hpm_mcl_type_t ref, hpm_mcl_type_t sens, mcl_control_pid_t *pid_x, hpm_mcl_type_t *output);
94     hpm_mcl_stat_t (*invpark)(hpm_mcl_type_t d, hpm_mcl_type_t q, hpm_mcl_type_t sin_x,
95                     hpm_mcl_type_t cos_x, hpm_mcl_type_t *alpha, hpm_mcl_type_t *beta);
96     hpm_mcl_stat_t (*svpwm)(hpm_mcl_type_t alpha, hpm_mcl_type_t beta, hpm_mcl_type_t vbus, mcl_control_svpwm_duty_t *duty);
97     hpm_mcl_stat_t (*step_svpwm)(hpm_mcl_type_t alpha, hpm_mcl_type_t beta, hpm_mcl_type_t vbus, mcl_control_svpwm_duty_t *duty);
98 } mcl_control_method_t;
99 
100 /**
101  * @brief callback function
102  *
103  */
104 typedef struct {
105     void (*init)(void); /**< User-defined function initialisation */
106     _FUNC_OPTIONAL_ mcl_control_method_t method;    /**< User-defined algorithms */
107 } mcl_control_callback_t;
108 
109 /**
110  * @brief Control Function Configuration
111  *
112  */
113 typedef struct {
114     mcl_control_callback_t callback;
115     mcl_control_pid_t currentd_pid_cfg;
116     mcl_control_pid_t currentq_pid_cfg;
117     mcl_control_pid_t speed_pid_cfg;
118     mcl_control_pid_t position_pid_cfg;
119 } mcl_control_cfg_t;
120 
121 /**
122  * @brief Control function running data
123  *
124  */
125 typedef struct {
126     mcl_control_cfg_t *cfg;
127     mcl_control_method_t method;
128 } mcl_control_t;
129 
130 #ifdef __cplusplus
131 extern "C" {
132 #endif
133 
134 /**
135  * @brief Initialise the operating data for the control function
136  *
137  * @param control @ref mcl_control_t
138  * @param cfg @ref mcl_control_cfg_t
139  * @return hpm_mcl_stat_t
140  */
141 hpm_mcl_stat_t hpm_mcl_control_init(mcl_control_t *control, mcl_control_cfg_t *cfg);
142 
143 #ifdef __cplusplus
144 }
145 #endif
146 
147 #endif
148