fix(bldc_motor): Remove use of static in init / calibration function; fix some copy-pasta in constructor; improve open-loop control#656
Open
finger563 wants to merge 1 commit into
Open
Conversation
… fix some copy-pasta in constructor; improve open-loop control
|
✅Static analysis result - no issues found! ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes five issues in the BLDC motor FOC control code (
components/bldc_motor/include/bldc_motor.hpp), found during a review of the control logic. The FOC math itself (Park/Clarke, SVPWM, trapezoidal commutation) was correct; the bugs were concentrated in the open-loop and calibration paths plus a couple of initialization footguns.Open-loop timestamp shared across motor instances. Both
velocity_openloop()andangle_openloop()used a function-localstatictimestamp to compute the sample timeTs. For a templatedBldcMotor, that static is shared by all instances of the same type, so two motors (e.g. the dual-motor MotorGo Mini) clobbered each other's timing and computed a wrongTs, corrupting open-loop velocity/angle integration. Replaced with a per-instance memberopenloop_timestamp_.Open-loop functions ignored their
targetargument.velocity_openloop(float target)andangle_openloop(float target)integrated the membertarget_shaft_velocity_/target_shaft_angle_instead of the passedtarget. This is harmless on the normalmove()path (which sets the member first), but it brokesearch_absolute_zero(), which callsangle_openloop(1.5f * _2PI)to sweep the motor — the argument was discarded, the motor never moved, and the index-searchwhileloop could spin forever for sensors whoseneeds_zero_search()stays true. The functions now usetarget.Bare
abs()on floats inangle_openloop(). Unqualifiedabs(...)on float operands can bind to the integer::abs(truncating the values) depending on which headers are in scope. Changed tostd::abs(...), matching the rest of the file.Wrong PID config in the constructor init list.
pid_velocity_andpid_angle_were both constructed fromconfig.current_pid_config. They were corrected by laterchange_gains()calls, so behavior was correct today, but it was a copy-paste trap. They are now constructed fromconfig.velocity_pid_configandconfig.angle_pid_configrespectively.Provided sensor direction ignored unless a non-zero offset was also given. In
init_foc()the sensor direction was only applied inside theif (zero_electric_offset)block, soinitialize(0, SOME_DIRECTION)discarded the direction and re-ran direction detection. The sensor direction and the electrical zero offset are now applied independently (each skips only its own calibration step), andinit_foc()'s defaultsensor_directionparameter is nowUNKNOWNto matchinitialize()instead of silently forcingCLOCKWISE.Motivation and Context
These are correctness bugs in the motor control / calibration code. (1) affects any multi-motor board using two
BldcMotorinstances of the same type (most notably MotorGo Mini), where open-loop modes would behave erratically due to the shared timing state. (2) (compounded by (3)) could hang the index-search calibration for index-based encoders. (4) and (5) are robustness/API-correctness fixes that prevent silently-wrong configuration. The control code is a port of SimpleFOC; 1, 2, and 5 restore behavior that matches the upstream design (per-object open-loop timestamp, target-driven open-loop functions, independently-applied direction/offset).How has this been tested?
esp32s3.bldc_motorexample — clean build.motorgo-mini(dual-motor) example, the consumer most affected by the shared-timestamp fix — clean build.Screenshots (if appropriate, e.g. schematic, board, console logs, lab pictures):
N/A
Types of changes
Checklist:
Software
.github/workflows/build.ymlfile to add my new test to the automated cloud build github action.