代码讲解
1.构造函数Axis::Axis
Axis::Axis(int axis_num,
const AxisHardwareConfig_t& hw_config,
Config_t& config,
Encoder& encoder,
SensorlessEstimator& sensorless_estimator,
Controller& controller,
OnboardThermistorCurrentLimiter& fet_thermistor,
OffboardThermistorCurrentLimiter& motor_thermistor,
Motor& motor,
TrapezoidalTrajectory& trap,
Endstop& min_endstop,
Endstop& max_endstop)
: axis_num_(axis_num),
hw_config_(hw_config),
config_(config),
encoder_(encoder),
sensorless_estimator_(sensorless_estimator),
controller_(controller),
fet_thermistor_(fet_thermistor),
motor_thermistor_(motor_thermistor),
motor_(motor),
trap_traj_(trap),
min_endstop_(min_endstop),
max_endstop_(max_endstop),
current_limiters_(make_array(
static_cast<CurrentLimiter*>(&fet_thermistor),
static_cast<CurrentLimiter*>(&motor_thermistor))),
thermistors_(make_array(
static_cast<ThermistorCurrentLimiter*>(&fet_thermistor),
static_cast<ThermistorCurrentLimiter*>(&motor_thermistor)))
{
encoder_.axis_ = this;
sensorless_estimator_.axis_ = this;
controller_.axis_ = this;
fet_thermistor_.axis_ = this;
motor_thermistor.axis_ = this;
motor_.axis_ = this;
trap_traj_.axis_ = this;
min_endstop_.axis_ = this;
max_endstop_.axis_ = this;
decode_step_dir_pins(); // 解析步进/方向引脚配置
watchdog_feed(); // 喂狗(防止硬件看门狗超时)
}
1. 构造函数功能
- 作用:组装一个完整的电机轴控制系统,包含电机、编码器、控制器、温度监控等模块。
- 设计思想:依赖注入(Dependency Injection),通过参数传入所有依赖对象,实现模块化设计。
2. 参数详解
参数
|
类型
|
功能
|
axis_num
|
int
|
轴编号(如0或1,对应ODrive的双轴)
|
hw_config
|
AxisHardwareConfig_t
|
硬件配置(GPIO引脚、PWM定时器等)
|
config
|
Config_t
|
软件配置(控制参数、校准值等)
|
encoder_
|
Encoder
|
编码器接口(位置反馈)
|
sensorless_estimator_
|
SensorlessEstimator
|
无传感器速度/位置估计
|
controller_
|
Controller
|
位置/速度/电流控制器
|
fet_thermistor_
|
OnboardThermistorCurrentLimiter
|
板载MOSFET温度监控
|
motor_thermistor_
|
OffboardThermistorCurrentLimiter
|
电机温度监控
|
motor_
|
Motor
|
电机驱动(FOC算法、PWM生成)
|
trap
|
TrapezoidalTrajectory
|
梯形轨迹规划器
|
min/max_endstop
|
Endstop
|
限位开关接口
|
//电流限制器数组:将温度监控模块统一抽象为 CurrentLimiter 接口,便于批量处理限流逻辑。
current_limiters_(make_array(
static_cast<CurrentLimiter*>(&fet_thermistor),
static_cast<CurrentLimiter*>(&motor_thermistor))),
thermistors_(make_array(
static_cast<ThermistorCurrentLimiter*>(&fet_thermistor),
static_cast<ThermistorCurrentLimiter*>(&motor_thermistor)))
3.反向引用设置
- 目的:实现双向关联,允许子模块访问轴级状态(如报错时标记 axis_->error_)。


总结

2.default_calibration()
这段代码定义了 ODrive 电机轴(Axis)在校准(Calibration)过程中使用的默认锁定(Lock-in)参数配置,主要用于电机初始位置检测或编码器校准。
Axis::LockinConfig_t Axis::default_calibration() {
Axis::LockinConfig_t config;
config.current = 10.0f; // [A]
config.ramp_time = 0.4f; // [s]
config.ramp_distance = 1 * M_PI; // [rad]
config.accel = 20.0f; // [rad/s^2]
config.vel = 40.0f; // [rad/s]
config.finish_distance = 100.0f * 2.0f * M_PI; // [rad]
config.finish_on_vel = false;
config.finish_on_distance = true;
config.finish_on_enc_idx = true;
return config;
}
参数 |
类型 |
单位 |
默认值 |
说明 |
current |
float |
A |
10.0 |
校准电流(影响扭矩大小) |
ramp_time |
float |
s |
0.4 |
电流斜坡上升时间 |
ramp_distance |
float |
rad |
π |
斜坡阶段的机械位移 |
accel |
float |
rad/s² |
20.0 |
加速度 |
vel |
float |
rad/s |
40.0 |
目标速度 |
finish_distance |
float |
rad |
200π |
最大校准行程(约100圈) |
finish_on_vel |
bool |
- |
false |
是否在达到目标速度时结束 |
finish_on_distance |
bool |
- |
true |
是否在达到最大行程时结束 |
finish_on_enc_idx |
bool |
- |
true |
是否在遇到编码器索引信号时结束 |
3.default_sensorless()
这段代码定义了 ODrive 在无传感器模式(Sensorless Mode)下使用的默认锁定(Lock-in)参数配置,主要用于电机启动时的转子位置辨识和速度拉入同步。参数同上表
Axis::LockinConfig_t Axis::default_sensorless() {
Axis::LockinConfig_t config;
config.current = 10.0f; // [A]
config.ramp_time = 0.4f; // [s]
config.ramp_distance = 1 * M_PI; // [rad]
config.accel = 200.0f; // [rad/s^2]
config.vel = 400.0f; // [rad/s]
config.finish_distance = 100.0f; // [rad]
config.finish_on_vel = true;
config.finish_on_distance = false;
config.finish_on_enc_idx = false;
return config;
}
4.step_cb_wrapper(void* ctx)
这是一个中断回调适配器函数,用于将普通的 C 回调(如在 HAL 或中断中注册的函数)转发到 Axis 类的成员函数 step_cb()。因为 C 风格的中断不支持 this 指针,所以需要传入 void* ctx,然后用 reinterpret_cast 还原成类指针。
static void step_cb_wrapper(void* ctx) {
reinterpret_cast<Axis*>(ctx)->step_cb();
}
5.run_state_machine_loop_wrapper(void* ctx)
启动状态机循环:run_state_machine_loop_wrapper():适配 osThreadCreate() 线程入口要求。
static void run_state_machine_loop_wrapper(void* ctx) {
reinterpret_cast<Axis*>(ctx)->run_state_machine_loop();
reinterpret_cast<Axis*>(ctx)->thread_id_valid_ = false;
}