HPM SDK
HPMicro Software Development Kit
hpm_mcl_loop.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023-2025 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 #include "hpm_mcl_hw_loop.h"
18 
19 typedef enum {
25 
30 typedef enum {
38 
43 typedef struct {
49 #if defined(MCL_CFG_EN_SENSORLESS_SMC) && MCL_CFG_EN_SENSORLESS_SMC
50  bool enable_smc;
51 #endif
52 #if defined(MCL_CFG_EN_DQ_AXIS_DECOUPLING) && MCL_CFG_EN_DQ_AXIS_DECOUPLING
53  bool enable_dq_axis_decoupling;
54 #endif
55 #if defined(MCL_CFG_EN_DEAD_AREA_COMPENSATION) && MCL_CFG_EN_DEAD_AREA_COMPENSATION
56  bool enable_dead_area_compensation;
57 #endif
59 
64 typedef enum {
66  loop_chn_iq = 1
68 
73 typedef struct {
74  void (*clc_set_val)(mcl_loop_chn_t chn, int32_t val);
75  int32_t (*convert_float_to_clc_val)(float realdata);
77 
82 typedef enum {
93 
94 typedef struct {
97  float last_ualpha;
98  float last_ubeta;
100  uint32_t tick_count;
102 
107 typedef struct {
121  struct {
122  float *current_ts;
123  float *speed_ts;
124  float *position_ts;
127  } const_time;
128  struct {
129  struct {
131  } block;
134  } rundata;
139  struct {
140  float id;
141  float iq;
142  float speed;
143  float position;
144  } exec_ref;
145  struct {
146  float speed_ts;
147  float position_ts;
148  } time;
149  void *hardware;
150  bool enable;
151 } mcl_loop_t;
152 
156 typedef enum {
161 
165 typedef struct {
166  float d_current;
167  float q_current;
168  uint32_t delay_ms;
170 
174 typedef struct {
175  struct {
176  float d_current;
177  float q_current;
178  uint32_t delay_ms;
179  } stage1;
181  struct {
182  float d_current;
183  uint32_t delay_ms;
184  } stage2;
186  struct {
187  float d_current;
188  uint32_t delay_ms;
189  } stage3;
191  uint32_t final_delay_ms;
193 
197 typedef union {
201 
205 typedef struct {
209 
229 
230 #ifdef __cplusplus
231 extern "C" {
232 #endif
233 
249  mcl_encoder_t *encoder, mcl_analog_t *analog,
250  mcl_control_t *control, mcl_drivers_t *drivers, mcl_path_plan_t *path, mcl_hw_loop_t *hw_loop);
251 
257  mcl_encoder_t *encoder, mcl_analog_t *analog,
258  mcl_control_t *control, mcl_drivers_t *drivers, mcl_path_plan_t *path)
259 {
260  return hpm_mcl_loop_init_v2(loop, cfg, mcl_cfg, encoder, analog, control, drivers, path, NULL);
261 }
262 
263 /* Macro magic for function overloading based on argument count */
264 #define GET_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, NAME, ...) NAME
265 
273 #define hpm_mcl_loop_init(...) GET_MACRO(__VA_ARGS__, hpm_mcl_loop_init_v2, hpm_mcl_loop_init_v1, dummy, dummy, dummy, dummy, dummy, dummy)(__VA_ARGS__)
274 
283 
292 
301 
310 
318 
326 
334 
342 
348 static inline void hpm_mcl_loop_enable(mcl_loop_t *loop)
349 {
350  loop->enable = true;
351 }
352 
358 static inline void hpm_mcl_loop_disable(mcl_loop_t *loop)
359 {
360  loop->enable = false;
361 }
362 
369 {
371  loop->cfg->enable_offline_param_detection = true;
372 }
373 
381 {
383  return true;
384  } else {
385  return false;
386  }
387 }
388 
396 {
398  return true;
399  } else {
400  return false;
401  }
402 }
403 
411 {
412  *result = loop->rundata.offline_detection.result;
413 }
414 
421 static inline void hpm_mcl_loop_mode_set(mcl_loop_t *loop, mcl_loop_mode_t mode)
422 {
423  loop->cfg->mode = mode;
424 }
425 
431 static inline void hpm_mcl_enable_position_loop(mcl_loop_t *loop)
432 {
433  loop->cfg->enable_position_loop = true;
434 }
435 
441 static inline void hpm_mcl_disable_position_loop(mcl_loop_t *loop)
442 {
443  loop->cfg->enable_position_loop = false;
444 }
445 
451 static inline void hpm_mcl_enable_speed_loop(mcl_loop_t *loop)
452 {
453  loop->cfg->enable_speed_loop = true;
454 }
455 
461 static inline void hpm_mcl_disable_speed_loop(mcl_loop_t *loop)
462 {
463  loop->cfg->enable_speed_loop = false;
464 }
465 
472 {
473 #if defined(MCL_CFG_EN_DEAD_AREA_COMPENSATION) && MCL_CFG_EN_DEAD_AREA_COMPENSATION
474  MCL_ASSERT_OPT(loop->cfg != NULL, mcl_invalid_pointer);
475  loop->cfg->enable_dead_area_compensation = false;
476  return mcl_success;
477 #else
478  (void)loop;
479  return mcl_fail;
480 #endif
481 }
482 
489 {
490 #if defined(MCL_CFG_EN_DEAD_AREA_COMPENSATION) && MCL_CFG_EN_DEAD_AREA_COMPENSATION
491  MCL_ASSERT_OPT(loop->cfg != NULL, mcl_invalid_pointer);
492  loop->cfg->enable_dead_area_compensation = true;
493  return mcl_success;
494 #else
495  (void)loop;
496  return mcl_fail;
497 #endif
498 }
499 
506 {
507 #if defined(MCL_CFG_EN_DQ_AXIS_DECOUPLING) && MCL_CFG_EN_DQ_AXIS_DECOUPLING
508  MCL_ASSERT_OPT(loop->cfg != NULL, mcl_invalid_pointer);
509  loop->cfg->enable_dq_axis_decoupling = false;
510  return mcl_success;
511 #else
512  (void)loop;
513  return mcl_fail;
514 #endif
515 }
516 
523 {
524 #if defined(MCL_CFG_EN_DQ_AXIS_DECOUPLING) && MCL_CFG_EN_DQ_AXIS_DECOUPLING
525  MCL_ASSERT_OPT(loop->cfg != NULL, mcl_invalid_pointer);
526  loop->cfg->enable_dq_axis_decoupling = true;
527  return mcl_success;
528 #else
529  (void)loop;
530  return mcl_fail;
531 #endif
532 }
533 
540 static inline uint32_t hpm_mcl_get_current_loop_run_tick(mcl_loop_t *loop)
541 {
542 #if defined(MCL_EN_LOOP_TIME_COUNT) && MCL_EN_LOOP_TIME_COUNT
543  return loop->rundata.current_loop_tick;
544 #else
545  (void)loop;
546  return 0;
547 #endif
548 }
549 
550 #ifdef __cplusplus
551 }
552 #endif
553 
554 #endif
uint32_t hpm_mcl_stat_t
Definition: hpm_mcl_common.h:15
#define MCL_ASSERT_OPT
Definition: hpm_mcl_common.h:89
@ 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:540
mcl_motor_alignment_algorithm_t
Motor alignment algorithm types.
Definition: hpm_mcl_loop.h:156
@ mcl_alignment_algorithm_basic
Definition: hpm_mcl_loop.h:157
@ mcl_alignment_algorithm_three_stage
Definition: hpm_mcl_loop.h:158
@ mcl_alignment_algorithm_adaptive
Definition: hpm_mcl_loop.h:159
static bool hpm_mcl_loop_offline_param_detection_is_error(mcl_loop_t *loop)
Get offline parameter is error.
Definition: hpm_mcl_loop.h:395
static hpm_mcl_stat_t hpm_mcl_loop_init_v1(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)
Backward compatibility function for API used in v1.9.0 and earlier (without hw_loop parameter)
Definition: hpm_mcl_loop.h:256
hpm_mcl_stat_t hpm_mcl_motor_angle_alignment(mcl_loop_t *loop, mcl_motor_alignment_cfg_t *cfg)
Enhanced motor angle alignment algorithm with multiple algorithm support.
Definition: hpm_mcl_loop.c:761
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:601
hpm_mcl_stat_t hpm_mcl_loop(mcl_loop_t *loop)
Motor Loop, Periodic Recall.
Definition: hpm_mcl_loop.c:625
static void hpm_mcl_enable_speed_loop(mcl_loop_t *loop)
Enable speed loop.
Definition: hpm_mcl_loop.h:451
static void hpm_mcl_enable_position_loop(mcl_loop_t *loop)
Enable position loop.
Definition: hpm_mcl_loop.h:431
static hpm_mcl_stat_t hpm_mcl_enable_dq_axis_decoupling(mcl_loop_t *loop)
enable dq axis decoupling
Definition: hpm_mcl_loop.h:522
static void hpm_mcl_loop_mode_set(mcl_loop_t *loop, mcl_loop_mode_t mode)
Set loop mode.
Definition: hpm_mcl_loop.h:421
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:410
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:613
static hpm_mcl_stat_t hpm_mcl_enable_dead_area_compensation(mcl_loop_t *loop)
enable dead area compensation
Definition: hpm_mcl_loop.h:488
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:89
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:82
static void hpm_mcl_loop_enable(mcl_loop_t *loop)
Enable Loop.
Definition: hpm_mcl_loop.h:348
mcl_loop_chn_t
CLC channel.
Definition: hpm_mcl_loop.h:64
@ loop_chn_iq
Definition: hpm_mcl_loop.h:66
@ loop_chn_id
Definition: hpm_mcl_loop.h:65
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:96
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:368
static bool hpm_mcl_loop_offline_param_detection_is_done(mcl_loop_t *loop)
Get offline parameter is done.
Definition: hpm_mcl_loop.h:380
static hpm_mcl_stat_t hpm_mcl_disable_dq_axis_decoupling(mcl_loop_t *loop)
disable dq axis decoupling
Definition: hpm_mcl_loop.h:505
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:110
static void hpm_mcl_disable_position_loop(mcl_loop_t *loop)
Disable position loop.
Definition: hpm_mcl_loop.h:441
mcl_offline_param_detection_mode_t
Internal use, process control in the process.
Definition: hpm_mcl_loop.h:82
@ offline_param_detection_mode_init
Definition: hpm_mcl_loop.h:83
@ offline_param_detection_mode_rs
Definition: hpm_mcl_loop.h:84
@ offline_param_detection_mode_lq
Definition: hpm_mcl_loop.h:86
@ offline_param_detection_mode_ls
Definition: hpm_mcl_loop.h:87
@ offline_param_detection_mode_wait
Definition: hpm_mcl_loop.h:89
@ offline_param_detection_mode_flux
Definition: hpm_mcl_loop.h:88
@ offline_param_detection_mode_end
Definition: hpm_mcl_loop.h:90
@ offline_param_detection_mode_error
Definition: hpm_mcl_loop.h:91
@ offline_param_detection_mode_ld
Definition: hpm_mcl_loop.h:85
static hpm_mcl_stat_t hpm_mcl_disable_dead_area_compensation(mcl_loop_t *loop)
disable dead area compensation
Definition: hpm_mcl_loop.h:471
mcl_loop_mode_t
Algorithms used in loops.
Definition: hpm_mcl_loop.h:30
@ mcl_mode_foc
Definition: hpm_mcl_loop.h:31
@ mcl_mode_step_foc
Definition: hpm_mcl_loop.h:34
@ mcl_mode_block
Definition: hpm_mcl_loop.h:32
@ mcl_mode_hardware_foc
Definition: hpm_mcl_loop.h:33
@ mcl_mode_offline_param_detection
Definition: hpm_mcl_loop.h:35
@ mcl_mode_hybrid_foc
Definition: hpm_mcl_loop.h:36
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:103
static void hpm_mcl_loop_disable(mcl_loop_t *loop)
Disable Loop.
Definition: hpm_mcl_loop.h:358
hpm_mcl_stat_t hpm_mcl_loop_init_v2(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, mcl_hw_loop_t *hw_loop)
Initialisation loop data (API introduced in v1.10.0 with hw_loop parameter)
Definition: hpm_mcl_loop.c:10
mcl_loop_status_t
Definition: hpm_mcl_loop.h:19
@ loop_status_init
Definition: hpm_mcl_loop.h:21
@ loop_status_run
Definition: hpm_mcl_loop.h:22
@ loop_status_null
Definition: hpm_mcl_loop.h:20
@ loop_status_fail
Definition: hpm_mcl_loop.h:23
static void hpm_mcl_disable_speed_loop(mcl_loop_t *loop)
Disable speed loop.
Definition: hpm_mcl_loop.h:461
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:76
Definition: hpm_mcl_common.h:239
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:73
Definition: hpm_mcl_hw_loop.h:60
Loop Configuration.
Definition: hpm_mcl_loop.h:43
bool enable_step_motor_closed_loop
Definition: hpm_mcl_loop.h:48
bool enable_speed_loop
Definition: hpm_mcl_loop.h:45
bool enable_position_loop
Definition: hpm_mcl_loop.h:46
mcl_loop_mode_t mode
Definition: hpm_mcl_loop.h:44
bool enable_offline_param_detection
Definition: hpm_mcl_loop.h:47
Loop operation data.
Definition: hpm_mcl_loop.h:107
float speed_ts
Definition: hpm_mcl_loop.h:146
void * hardware
Definition: hpm_mcl_loop.h:149
mcl_encoder_t * encoder
Definition: hpm_mcl_loop.h:110
struct mcl_loop_t::@991 rundata
float speed
Definition: hpm_mcl_loop.h:142
uint32_t current_loop_tick
Definition: hpm_mcl_loop.h:133
mcl_drivers_t * drivers
Definition: hpm_mcl_loop.h:113
float offline_detection_wait_ts
Definition: hpm_mcl_loop.h:126
mcl_path_plan_t * path
Definition: hpm_mcl_loop.h:115
float iq
Definition: hpm_mcl_loop.h:141
mcl_user_value_t ref_iq
Definition: hpm_mcl_loop.h:136
float id
Definition: hpm_mcl_loop.h:140
float position
Definition: hpm_mcl_loop.h:143
mcl_user_value_t ref_id
Definition: hpm_mcl_loop.h:135
mcl_debug_t * debug
Definition: hpm_mcl_loop.h:114
float * speed_ts
Definition: hpm_mcl_loop.h:123
mcl_offline_param_detection_rundata_t offline_detection
Definition: hpm_mcl_loop.h:132
float * current_ts
Definition: hpm_mcl_loop.h:122
mcl_loop_cfg_t * cfg
Definition: hpm_mcl_loop.h:109
mcl_hw_loop_t * hybrid_hw_loop
Definition: hpm_mcl_loop.h:120
mcl_motor_dir_t dir
Definition: hpm_mcl_loop.h:130
mcl_user_value_t ref_position
Definition: hpm_mcl_loop.h:138
float dead_area_ts
Definition: hpm_mcl_loop.h:125
bool enable
Definition: hpm_mcl_loop.h:150
hpm_mcl_type_t * flux
Definition: hpm_mcl_loop.h:119
float position_ts
Definition: hpm_mcl_loop.h:147
mcl_analog_t * analog
Definition: hpm_mcl_loop.h:111
hpm_mcl_type_t * ld
Definition: hpm_mcl_loop.h:118
hpm_mcl_type_t * const_vbus
Definition: hpm_mcl_loop.h:116
mcl_user_value_t ref_speed
Definition: hpm_mcl_loop.h:137
hpm_mcl_type_t * lq
Definition: hpm_mcl_loop.h:117
float * position_ts
Definition: hpm_mcl_loop.h:124
mcl_loop_status_t status
Definition: hpm_mcl_loop.h:108
mcl_control_t * control
Definition: hpm_mcl_loop.h:112
Basic alignment configuration (single-stage)
Definition: hpm_mcl_loop.h:165
float d_current
Definition: hpm_mcl_loop.h:166
uint32_t delay_ms
Definition: hpm_mcl_loop.h:168
float q_current
Definition: hpm_mcl_loop.h:167
Motor angle alignment configuration parameters.
Definition: hpm_mcl_loop.h:205
mcl_motor_alignment_algorithm_t algorithm
Definition: hpm_mcl_loop.h:206
mcl_motor_alignment_algorithm_cfg_t config
Definition: hpm_mcl_loop.h:207
Three-stage alignment configuration.
Definition: hpm_mcl_loop.h:174
uint32_t delay_ms
Definition: hpm_mcl_loop.h:178
float d_current
Definition: hpm_mcl_loop.h:176
float q_current
Definition: hpm_mcl_loop.h:177
uint32_t final_delay_ms
Definition: hpm_mcl_loop.h:191
Results of offline parameter testing.
Definition: hpm_mcl_control.h:40
Definition: hpm_mcl_loop.h:94
volatile mcl_offline_param_detection_result_t result
Definition: hpm_mcl_loop.h:96
float last_ualpha
Definition: hpm_mcl_loop.h:97
volatile mcl_offline_param_detection_mode_t mode
Definition: hpm_mcl_loop.h:95
mcl_offline_param_detection_mode_t last_mode
Definition: hpm_mcl_loop.h:99
float last_ubeta
Definition: hpm_mcl_loop.h:98
uint32_t tick_count
Definition: hpm_mcl_loop.h:100
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
Motor alignment configuration union for different algorithms.
Definition: hpm_mcl_loop.h:197
mcl_motor_alignment_basic_cfg_t basic
Definition: hpm_mcl_loop.h:198
mcl_motor_alignment_three_stage_cfg_t three_stage
Definition: hpm_mcl_loop.h:199