Skip to content

fix(bldc_motor): Ensure reading the shaft angle doesnt change the filter / update state#632

Merged
finger563 merged 3 commits into
mainfrom
fix/bldc-sensor
Jun 6, 2026
Merged

fix(bldc_motor): Ensure reading the shaft angle doesnt change the filter / update state#632
finger563 merged 3 commits into
mainfrom
fix/bldc-sensor

Conversation

@finger563

@finger563 finger563 commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

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/example and bldc_haptics/example

Screenshots (if appropriate, e.g. schematic, board, console logs, lab pictures):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation Update
  • Hardware (schematic, board, system design) change
  • Software change

Checklist:

  • My change requires a change to the documentation.
  • I have added / updated the documentation related to this change via either README or WIKI

Software

  • I have added tests to cover my changes.
  • I have updated the .github/workflows/build.yml file to add my new test to the automated cloud build github action.
  • All new and existing tests passed.
  • My code follows the code style of this project.

Copilot AI review requested due to automatic review settings June 6, 2026 02:23
@github-actions

github-actions Bot commented Jun 6, 2026

Copy link
Copy Markdown

✅Static analysis result - no issues found! ✅

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 updated move() and init_foc() to call them to refresh cached values.

Comment thread components/bldc_motor/include/bldc_motor.hpp Outdated
Comment thread components/bldc_motor/include/bldc_motor.hpp Outdated
Comment thread components/bldc_motor/include/bldc_motor.hpp Outdated
@finger563 finger563 self-assigned this Jun 6, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

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;
}
@finger563 finger563 merged commit a58207f into main Jun 6, 2026
109 of 110 checks passed
@finger563 finger563 deleted the fix/bldc-sensor branch June 6, 2026 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants