HPM SDK
HPMicro Software Development Kit
hpm_mcl_loop.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 #ifndef HPM_MCL_LOOP_H
8 #define HPM_MCL_LOOP_H
9 
10 #include "hpm_mcl_common.h"
11 #include "hpm_mcl_encoder.h"
12 #include "hpm_mcl_analog.h"
13 #include "hpm_mcl_control.h"
14 #include "hpm_mcl_drivers.h"
15 #include "hpm_mcl_path_plan.h"
16 #include "hpm_mcl_debug.h"
17 
18 typedef enum {
24 
29 typedef enum {
36 
41 typedef struct {
46 #if defined(MCL_CFG_EN_SENSORLESS_SMC) && MCL_CFG_EN_SENSORLESS_SMC
47  bool enable_smc;
48 #endif
49 #if defined(MCL_CFG_EN_DQ_AXIS_DECOUPLING) && MCL_CFG_EN_DQ_AXIS_DECOUPLING
50  bool enable_dq_axis_decoupling;
51 #endif
52 #if defined(MCL_CFG_EN_DEAD_AREA_COMPENSATION) && MCL_CFG_EN_DEAD_AREA_COMPENSATION
53  bool enable_dead_area_compensation;
54 #endif
56 
61 typedef enum {
63  loop_chn_iq = 1
65 
70 typedef struct {
71  void (*clc_set_val)(mcl_loop_chn_t chn, int32_t val);
72  int32_t (*convert_float_to_clc_val)(float realdata);
74 
79 typedef enum {
90 
91 typedef struct {
94  float last_ualpha;
95  float last_ubeta;
97  uint32_t tick_count;
99 
104 typedef struct {
117  struct {
118  float *current_ts;
119  float *speed_ts;
120  float *position_ts;
123  } const_time;
124  struct {
125  struct {
127  } block;
130  } rundata;
135  struct {
136  float id;
137  float iq;
138  float speed;
139  float position;
140  } exec_ref;
141  struct {
142  float speed_ts;
143  float position_ts;
144  } time;
145  void *hardware;
146  bool enable;
147 } mcl_loop_t;
148 
149 #ifdef __cplusplus
150 extern "C" {
151 #endif
152 
167  mcl_encoder_t *encoder, mcl_analog_t *analog,
168  mcl_control_t *control, mcl_drivers_t *drivers, mcl_path_plan_t *path);
169 
178 
187 
196 
205 
213 
221 
229 
237 
243 static inline void hpm_mcl_loop_enable(mcl_loop_t *loop)
244 {
245  loop->enable = true;
246 }
247 
253 static inline void hpm_mcl_loop_disable(mcl_loop_t *loop)
254 {
255  loop->enable = false;
256 }
257 
264 {
266  loop->cfg->enable_offline_param_detection = true;
267 }
268 
276 {
278  return true;
279  } else {
280  return false;
281  }
282 }
283 
291 {
293  return true;
294  } else {
295  return false;
296  }
297 }
298 
306 {
307  *result = loop->rundata.offline_detection.result;
308 }
309 
316 static inline void hpm_mcl_loop_mode_set(mcl_loop_t *loop, mcl_loop_mode_t mode)
317 {
318  loop->cfg->mode = mode;
319 }
320 
326 static inline void hpm_mcl_enable_position_loop(mcl_loop_t *loop)
327 {
328  loop->cfg->enable_position_loop = true;
329 }
330 
336 static inline void hpm_mcl_disable_position_loop(mcl_loop_t *loop)
337 {
338  loop->cfg->enable_position_loop = false;
339 }
340 
346 static inline void hpm_mcl_enable_speed_loop(mcl_loop_t *loop)
347 {
348  loop->cfg->enable_speed_loop = true;
349 }
350 
356 static inline void hpm_mcl_disable_speed_loop(mcl_loop_t *loop)
357 {
358  loop->cfg->enable_speed_loop = false;
359 }
360 
367 {
368 #if defined(MCL_CFG_EN_DEAD_AREA_COMPENSATION) && MCL_CFG_EN_DEAD_AREA_COMPENSATION
369  MCL_ASSERT_OPT(loop->cfg != NULL, mcl_invalid_pointer);
370  loop->cfg->enable_dead_area_compensation = false;
371  return mcl_success;
372 #else
373  (void)loop;
374  return mcl_fail;
375 #endif
376 }
377 
384 {
385 #if defined(MCL_CFG_EN_DEAD_AREA_COMPENSATION) && MCL_CFG_EN_DEAD_AREA_COMPENSATION
386  MCL_ASSERT_OPT(loop->cfg != NULL, mcl_invalid_pointer);
387  loop->cfg->enable_dead_area_compensation = true;
388  return mcl_success;
389 #else
390  (void)loop;
391  return mcl_fail;
392 #endif
393 }
394 
401 {
402 #if defined(MCL_CFG_EN_DQ_AXIS_DECOUPLING) && MCL_CFG_EN_DQ_AXIS_DECOUPLING
403  MCL_ASSERT_OPT(loop->cfg != NULL, mcl_invalid_pointer);
404  loop->cfg->enable_dq_axis_decoupling = false;
405  return mcl_success;
406 #else
407  (void)loop;
408  return mcl_fail;
409 #endif
410 }
411 
418 {
419 #if defined(MCL_CFG_EN_DQ_AXIS_DECOUPLING) && MCL_CFG_EN_DQ_AXIS_DECOUPLING
420  MCL_ASSERT_OPT(loop->cfg != NULL, mcl_invalid_pointer);
421  loop->cfg->enable_dq_axis_decoupling = true;
422  return mcl_success;
423 #else
424  (void)loop;
425  return mcl_fail;
426 #endif
427 }
428 
435 static inline uint32_t hpm_mcl_get_current_loop_run_tick(mcl_loop_t *loop)
436 {
437 #if defined(MCL_EN_LOOP_TIME_COUNT) && MCL_EN_LOOP_TIME_COUNT
438  return loop->rundata.current_loop_tick;
439 #else
440  (void)loop;
441  return 0;
442 #endif
443 }
444 
445 #ifdef __cplusplus
446 }
447 #endif
448 
449 #endif
uint32_t hpm_mcl_stat_t
Definition: hpm_mcl_common.h:15
#define MCL_ASSERT_OPT
Definition: hpm_mcl_common.h:88
@ mcl_fail
Definition: hpm_mcl_common.h:36
@ mcl_invalid_pointer
Definition: hpm_mcl_common.h:38
@ mcl_success
Definition: hpm_mcl_common.h:35
static uint32_t hpm_mcl_get_current_loop_run_tick(mcl_loop_t *loop)
Get current loop run tick.
Definition: hpm_mcl_loop.h:435
static bool hpm_mcl_loop_offline_param_detection_is_error(mcl_loop_t *loop)
Get offline parameter is error.
Definition: hpm_mcl_loop.h:290
hpm_mcl_stat_t hpm_mcl_loop_refresh_block(mcl_loop_t *loop)
Call this function in the interrupt function to update the motor's sector.
Definition: hpm_mcl_loop.c:501
hpm_mcl_stat_t hpm_mcl_loop(mcl_loop_t *loop)
Motor Loop, Periodic Recall.
Definition: hpm_mcl_loop.c:525
hpm_mcl_stat_t hpm_mcl_loop_init(mcl_loop_t *loop, mcl_loop_cfg_t *cfg, mcl_cfg_t *mcl_cfg, mcl_encoder_t *encoder, mcl_analog_t *analog, mcl_control_t *control, mcl_drivers_t *drivers, mcl_path_plan_t *path)
Initialisation loop data.
Definition: hpm_mcl_loop.c:10
static void hpm_mcl_enable_speed_loop(mcl_loop_t *loop)
Enable speed loop.
Definition: hpm_mcl_loop.h:346
static void hpm_mcl_enable_position_loop(mcl_loop_t *loop)
Enable position loop.
Definition: hpm_mcl_loop.h:326
static hpm_mcl_stat_t hpm_mcl_enable_dq_axis_decoupling(mcl_loop_t *loop)
enable dq axis decoupling
Definition: hpm_mcl_loop.h:417
static void hpm_mcl_loop_mode_set(mcl_loop_t *loop, mcl_loop_mode_t mode)
Set loop mode.
Definition: hpm_mcl_loop.h:316
static void hpm_mcl_loop_offline_param_detection_get_result(mcl_loop_t *loop, mcl_offline_param_detection_result_t *result)
Get offline parameter detection result.
Definition: hpm_mcl_loop.h:305
hpm_mcl_stat_t hpm_mcl_loop_start_block(mcl_loop_t *loop)
Call this function in the interrupt function to start the motor's sector.
Definition: hpm_mcl_loop.c:513
static hpm_mcl_stat_t hpm_mcl_enable_dead_area_compensation(mcl_loop_t *loop)
enable dead area compensation
Definition: hpm_mcl_loop.h:383
hpm_mcl_stat_t hpm_mcl_loop_set_current_q(mcl_loop_t *loop, mcl_user_value_t iq)
Setting the q-axis current.
Definition: hpm_mcl_loop.c:81
hpm_mcl_stat_t hpm_mcl_loop_set_current_d(mcl_loop_t *loop, mcl_user_value_t id)
Setting the d-axis current.
Definition: hpm_mcl_loop.c:74
static void hpm_mcl_loop_enable(mcl_loop_t *loop)
Enable Loop.
Definition: hpm_mcl_loop.h:243
mcl_loop_chn_t
CLC channel.
Definition: hpm_mcl_loop.h:61
@ loop_chn_iq
Definition: hpm_mcl_loop.h:63
@ loop_chn_id
Definition: hpm_mcl_loop.h:62
hpm_mcl_stat_t hpm_mcl_loop_set_speed(mcl_loop_t *loop, mcl_user_value_t speed)
Setting the speed loop speed feed.
Definition: hpm_mcl_loop.c:88
static void hpm_mcl_loop_enable_offline_param_detecion(mcl_loop_t *loop)
Enables the loop's offline parameter detection.
Definition: hpm_mcl_loop.h:263
static bool hpm_mcl_loop_offline_param_detection_is_done(mcl_loop_t *loop)
Get offline parameter is done.
Definition: hpm_mcl_loop.h:275
static hpm_mcl_stat_t hpm_mcl_disable_dq_axis_decoupling(mcl_loop_t *loop)
disable dq axis decoupling
Definition: hpm_mcl_loop.h:400
hpm_mcl_stat_t hpm_mcl_loop_disable_all_user_set_value(mcl_loop_t *loop)
Invalid user-set values in all loops.
Definition: hpm_mcl_loop.c:102
static void hpm_mcl_disable_position_loop(mcl_loop_t *loop)
Disable position loop.
Definition: hpm_mcl_loop.h:336
mcl_offline_param_detection_mode_t
Internal use, process control in the process.
Definition: hpm_mcl_loop.h:79
@ offline_param_detection_mode_init
Definition: hpm_mcl_loop.h:80
@ offline_param_detection_mode_rs
Definition: hpm_mcl_loop.h:81
@ offline_param_detection_mode_lq
Definition: hpm_mcl_loop.h:83
@ offline_param_detection_mode_ls
Definition: hpm_mcl_loop.h:84
@ offline_param_detection_mode_wait
Definition: hpm_mcl_loop.h:86
@ offline_param_detection_mode_flux
Definition: hpm_mcl_loop.h:85
@ offline_param_detection_mode_end
Definition: hpm_mcl_loop.h:87
@ offline_param_detection_mode_error
Definition: hpm_mcl_loop.h:88
@ offline_param_detection_mode_ld
Definition: hpm_mcl_loop.h:82
static hpm_mcl_stat_t hpm_mcl_disable_dead_area_compensation(mcl_loop_t *loop)
disable dead area compensation
Definition: hpm_mcl_loop.h:366
mcl_loop_mode_t
Algorithms used in loops.
Definition: hpm_mcl_loop.h:29
@ mcl_mode_foc
Definition: hpm_mcl_loop.h:30
@ mcl_mode_step_foc
Definition: hpm_mcl_loop.h:33
@ mcl_mode_block
Definition: hpm_mcl_loop.h:31
@ mcl_mode_hardware_foc
Definition: hpm_mcl_loop.h:32
@ mcl_mode_offline_param_detection
Definition: hpm_mcl_loop.h:34
hpm_mcl_stat_t hpm_mcl_loop_set_position(mcl_loop_t *loop, mcl_user_value_t position)
Setting the position parameters.
Definition: hpm_mcl_loop.c:95
static void hpm_mcl_loop_disable(mcl_loop_t *loop)
Disable Loop.
Definition: hpm_mcl_loop.h:253
mcl_loop_status_t
Definition: hpm_mcl_loop.h:18
@ loop_status_init
Definition: hpm_mcl_loop.h:20
@ loop_status_run
Definition: hpm_mcl_loop.h:21
@ loop_status_null
Definition: hpm_mcl_loop.h:19
@ loop_status_fail
Definition: hpm_mcl_loop.h:22
static void hpm_mcl_disable_speed_loop(mcl_loop_t *loop)
Disable speed loop.
Definition: hpm_mcl_loop.h:356
float hpm_mcl_type_t
Enable Q format.
Definition: hpm_mcl_math.h:30
mcl_motor_dir_t
Definition: hpm_mcl_physical.h:23
Operational data of the analog function.
Definition: hpm_mcl_analog.h:74
Definition: hpm_mcl_common.h:198
Control function running data.
Definition: hpm_mcl_control.h:273
debug data
Definition: hpm_mcl_debug.h:57
Driver function running data.
Definition: hpm_mcl_drivers.h:88
Encoder operation data.
Definition: hpm_mcl_encoder.h:132
Hardware configuration for CLC.
Definition: hpm_mcl_loop.h:70
Loop Configuration.
Definition: hpm_mcl_loop.h:41
bool enable_speed_loop
Definition: hpm_mcl_loop.h:43
bool enable_position_loop
Definition: hpm_mcl_loop.h:44
mcl_loop_mode_t mode
Definition: hpm_mcl_loop.h:42
bool enable_offline_param_detection
Definition: hpm_mcl_loop.h:45
Loop operation data.
Definition: hpm_mcl_loop.h:104
float speed_ts
Definition: hpm_mcl_loop.h:142
void * hardware
Definition: hpm_mcl_loop.h:145
mcl_encoder_t * encoder
Definition: hpm_mcl_loop.h:107
float speed
Definition: hpm_mcl_loop.h:138
uint32_t current_loop_tick
Definition: hpm_mcl_loop.h:129
struct mcl_loop_t::@818 rundata
mcl_drivers_t * drivers
Definition: hpm_mcl_loop.h:110
float offline_detection_wait_ts
Definition: hpm_mcl_loop.h:122
mcl_path_plan_t * path
Definition: hpm_mcl_loop.h:112
float iq
Definition: hpm_mcl_loop.h:137
mcl_user_value_t ref_iq
Definition: hpm_mcl_loop.h:132
float id
Definition: hpm_mcl_loop.h:136
float position
Definition: hpm_mcl_loop.h:139
mcl_user_value_t ref_id
Definition: hpm_mcl_loop.h:131
mcl_debug_t * debug
Definition: hpm_mcl_loop.h:111
float * speed_ts
Definition: hpm_mcl_loop.h:119
mcl_offline_param_detection_rundata_t offline_detection
Definition: hpm_mcl_loop.h:128
float * current_ts
Definition: hpm_mcl_loop.h:118
mcl_loop_cfg_t * cfg
Definition: hpm_mcl_loop.h:106
mcl_motor_dir_t dir
Definition: hpm_mcl_loop.h:126
mcl_user_value_t ref_position
Definition: hpm_mcl_loop.h:134
float dead_area_ts
Definition: hpm_mcl_loop.h:121
bool enable
Definition: hpm_mcl_loop.h:146
hpm_mcl_type_t * flux
Definition: hpm_mcl_loop.h:116
float position_ts
Definition: hpm_mcl_loop.h:143
mcl_analog_t * analog
Definition: hpm_mcl_loop.h:108
hpm_mcl_type_t * ld
Definition: hpm_mcl_loop.h:115
hpm_mcl_type_t * const_vbus
Definition: hpm_mcl_loop.h:113
mcl_user_value_t ref_speed
Definition: hpm_mcl_loop.h:133
hpm_mcl_type_t * lq
Definition: hpm_mcl_loop.h:114
float * position_ts
Definition: hpm_mcl_loop.h:120
mcl_loop_status_t status
Definition: hpm_mcl_loop.h:105
mcl_control_t * control
Definition: hpm_mcl_loop.h:109
Results of offline parameter testing.
Definition: hpm_mcl_control.h:40
Definition: hpm_mcl_loop.h:91
volatile mcl_offline_param_detection_result_t result
Definition: hpm_mcl_loop.h:93
float last_ualpha
Definition: hpm_mcl_loop.h:94
volatile mcl_offline_param_detection_mode_t mode
Definition: hpm_mcl_loop.h:92
mcl_offline_param_detection_mode_t last_mode
Definition: hpm_mcl_loop.h:96
float last_ubeta
Definition: hpm_mcl_loop.h:95
uint32_t tick_count
Definition: hpm_mcl_loop.h:97
Running data for path planning.
Definition: hpm_mcl_path_plan.h:38
User-defined data with data and enable bits.
Definition: hpm_mcl_common.h:29