fix(bldc_motor): Ensure reading the shaft angle doesnt change the filter / update state#632
Merged
Conversation
…ter / update state
|
✅Static analysis result - no issues found! ✅ |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adjusts the BLDC motor API so that reading shaft angle/velocity is side-effect free (doesn’t implicitly touch the sensor or advance filter state). It does this by turning the public getters into cached accessors and introducing explicit internal sampling helpers that update the cached state.
Changes:
- Changed
get_shaft_angle()/get_shaft_velocity()to return cached state instead of reading/filtering the sensor. - Added
sample_shaft_angle()/sample_shaft_velocity()and updatedmove()andinit_foc()to call them to refresh cached values.
Comment on lines
484
to
+489
| /** | ||
| * @brief Return the shaft angle, in radians. | ||
| * @return Motor shaft angle in radians. | ||
| * @brief Return the most recently sampled (or estimated in open-loop) shaft | ||
| * angle, in radians. | ||
| * @return Cached motor shaft angle in radians. | ||
| */ | ||
| float get_shaft_angle() { | ||
| // if no sensor linked return previous value ( for open loop ) | ||
| if (!sensor_) | ||
| return shaft_angle_; | ||
| auto sensed = angle_filter_ ? angle_filter_(sensor_->get_radians()) : sensor_->get_radians(); | ||
| return (float)sensor_direction_ * sensed - sensor_offset_; | ||
| } | ||
| float get_shaft_angle() const { return shaft_angle_; } |
Comment on lines
491
to
+496
| /** | ||
| * @brief Return the shaft velocity, in radians per second (rad/s). | ||
| * @return Motor shaft velocity (rad/s). | ||
| * @brief Return the most recently sampled (or estimated in open-loop) shaft | ||
| * velocity, in radians per second (rad/s). | ||
| * @return Cached motor shaft velocity (rad/s). | ||
| */ | ||
| float get_shaft_velocity() { | ||
| // if no sensor linked return previous value ( for open loop ) | ||
| if (!sensor_) | ||
| return shaft_velocity_; | ||
| auto sensed = velocity_filter_ ? velocity_filter_(sensor_->get_rpm()) : sensor_->get_rpm(); | ||
| return (float)sensor_direction_ * sensed * RPM_TO_RADS; | ||
| } | ||
| float get_shaft_velocity() const { return shaft_velocity_; } |
Comment on lines
+729
to
+735
| if (run_sensor_update_) { | ||
| sensor_->update(ec); | ||
| shaft_angle_ = get_shaft_angle(); | ||
| if (ec) { | ||
| logger_.error("Sensor update failed during init_foc: {}", ec.message()); | ||
| status_ = Status::FAILED_CALIBRATION; | ||
| return; | ||
| } |
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
Ensure the getters for shaft angle and velocity simply return state (and are const) rather than inadvertently mutating state by executing the filter.
Motivation and Context
Prevents logging of state from inadvertently affecting state.
How has this been tested?
Build and run
bldc_motor/exampleandbldc_haptics/exampleScreenshots (if appropriate, e.g. schematic, board, console logs, lab pictures):
Types of changes
Checklist:
Software
.github/workflows/build.ymlfile to add my new test to the automated cloud build github action.