SimpleFOC源码学习03(v2.3.2) - 时间工具模块time_utils.cpp与time_utils.h
前言github源码https://github.com/simplefoc/Arduino-FOC/tree/master/src/common为什么需要time_utils.cpp与time_util.h在电机控制中时间非常重要PID 控制器需要知道距上次运行过了多久dt低通滤波器也需要时间间隔来计算延时函数用于初始化等待(指的是_delay()相当于HAL_Delay())一、time_util.h#ifndefTIME_UTILS_H#defineTIME_UTILS_H#includefoc_utils.h/** * Function implementing delay() function in milliseconds * - blocking function * - hardware specific * param ms number of milliseconds to wait */void_delay(unsignedlongms);/** * Function implementing timestamp getting function in microseconds * hardware specific */unsignedlong_micros();#endif要点#ifndef / #define / #endif→ 防止头文件被重复引入标准保护只声明了两个函数实现在.cpp里函数名前加_是惯例表示这是内部/底层函数二、time_util.cpp#includetime_utils.h// function buffering delay()// arduino uno function doesnt work well with interruptsvoid_delay(unsignedlongms){HAL_Delay(ms);// HAL库的串行延时函数}// function buffering _micros()// arduino function doesnt work well with interruptsunsignedlong_micros(){returnDWT_Get_Microsecond();// 从DWT定时器获取us级的时间戳}我重写两个函数的实现。对于simpleFOC的其他模块来说_delay()作用是串行延时所以功能完全等同于STM32的HAL库的HAL_Delay()_micros() 作用是获取当前的时间戳微秒三、它在PID里是怎么被用到的3.1、PID 控制器每次被调用都经历这几个阶段3.2、逐行读pid.cpp构造函数 —_micros()第一次出场PIDController::PIDController(floatP,floatI,floatD,floatramp,floatlimit):P(P),I(I),D(D),...,integral_prev(0.0f){timestamp_prev_micros();// ← 记录出生时刻}目的是记录一个基准时间戳。第一次被调用operator()时用这个时间戳算出第一个Ts。operator()— 每次控制循环都调用它floatPIDController::operator()(floaterror){unsignedlongtimestamp_now_micros();// ① 拿当前时间floatTs(timestamp_now-timestamp_prev)*1e-6f;// ② 算时间差转成秒if(Ts0||Ts0.5f)Ts1e-3f;// ③ 异常保护1e-6f是把微秒换算成秒的系数1微秒 0.000001秒。Ts代表这次调用距上次过了多久。三个环节如何用Ts// 比例项不需要时间直接乘增益floatproportionalP*error;// 积分项Tustin 变换梯形积分Ts 越大积分面积越大floatintegralintegral_prevI*Ts*0.5f*(errorerror_prev);// 微分项误差变化率Ts 越大斜率越小floatderivativeD*(error-error_prev)/Ts;下面这张图直观展示三项与时间的关系output_ramp— 还有一处用到Tsif(output_ramp0){floatoutput_rate(output-output_prev)/Ts;// 输出变化速率if(output_rateoutput_ramp)outputoutput_prevoutput_ramp*Ts;// 限制最大加速}这保证输出不会突变比如电压不能从 0 瞬间跳到 12V限制变化速率单位V/s。总结_micros()在 PID 里的角色就是一个精准秒表每次控制循环开始时按一下算出Ts积分项乘它时间越长积累越多微分项除它时间越长变化越平缓限速项也靠它把速率换算成实际输出增量。没有准确的TsPID 三项计算全部失准。四、_micro()被哪些模块调用
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2500538.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!