Skip to content

fix(polarizer-wheel): cleaner ramp, starts at 2s/turn speed#469

Merged
fouge merged 1 commit intomainfrom
fouge/polarizer-wheel-clean-ramp
Feb 12, 2026
Merged

fix(polarizer-wheel): cleaner ramp, starts at 2s/turn speed#469
fouge merged 1 commit intomainfrom
fouge/polarizer-wheel-clean-ramp

Conversation

@fouge
Copy link
Collaborator

@fouge fouge commented Feb 11, 2026

exact symetry of the pulses between the 2 positions cleaned code a bit

@fouge fouge marked this pull request as ready for review February 11, 2026 13:22
@fouge fouge requested a review from Copilot February 11, 2026 14:02
Copy link

Copilot AI left a comment

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 refines the polarizer wheel motion profile by making the triangular ramp more symmetric and changing the ramp’s initial speed to start at 2s/turn, while also removing now-unused frequency bookkeeping fields.

Changes:

  • Add a 2s/turn PWM frequency constant and use it as the ramp start/end frequency.
  • Adjust ramp step computation to improve symmetry across the travel.
  • Remove unused frequency/current_frequency fields and related updates.

Reviewed changes

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

File Description
main_board/src/optics/polarizer_wheel/polarizer_wheel.h Adds a 2s/turn PWM frequency macro used by the updated ramp behavior.
main_board/src/optics/polarizer_wheel/polarizer_wheel.c Updates ramp symmetry math, changes ramp start frequency, and removes unused state fields.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 848 to 854
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

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

ramp_step can underflow when total_dist is 0 (e.g., if execute_set_angle() is called with target_step == current_step but target_position is UNKNOWN, so the early-return doesn’t trigger). With total_dist == 0, the expression (total_dist - distance_traveled - 1) wraps to a huge uint32_t, which then feeds calculate_linear_ramp_frequency() and may produce an invalid/erratic frequency. Consider guarding against zero-distance moves (preferably in execute_set_angle() before starting the motor), or at least handling total_dist == 0 in this ramp calculation to avoid unsigned wraparound. Adding a regression test for the zero-distance ramp move would help prevent this from reoccurring.

Copilot uses AI. Check for mistakes.
Comment on lines 1481 to 1489
g_polarizer_wheel_instance.acceleration.state = ACCELERATION_ACTIVE;
g_polarizer_wheel_instance.acceleration.min_frequency =
POLARIZER_WHEEL_SPIN_PWM_FREQUENCY_DEFAULT;
g_polarizer_wheel_instance.acceleration.current_frequency =
POLARIZER_WHEEL_SPIN_PWM_FREQUENCY_DEFAULT;
POLARIZER_WHEEL_SPIN_PWM_FREQUENCY_2SEC_PER_TURN;
g_polarizer_wheel_instance.acceleration.start_position = start_pos;
g_polarizer_wheel_instance.acceleration.total_distance = total_dist;

/* set initial frequency */
frequency = g_polarizer_wheel_instance.acceleration.min_frequency;
LOG_DBG("Triangular ramp: min %u Hz, total %u steps, accel %u "
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

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

In ramp mode, min_frequency is hard-coded to POLARIZER_WHEEL_SPIN_PWM_FREQUENCY_2SEC_PER_TURN, but polarizer_set_frequency() will reject any frequency above the runtime get_max_frequency() (derived from g_max_speed_ms_per_turn). If max speed is configured slower than 2s/turn (e.g., via polarizer_wheel_set_max_speed()), the initial polarizer_rotate(frequency) will fail immediately. Consider clamping the chosen ramp start frequency to get_max_frequency() (and/or validating set_max_speed inputs) so ramp mode remains usable across supported max-speed configurations.

Copilot uses AI. Check for mistakes.
Comment on lines 1487 to 1508
@@ -1516,8 +1505,7 @@ execute_set_angle(uint32_t frequency, uint32_t angle_decidegrees,
g_polarizer_wheel_instance.debug_stats.max_frequency = 0;
#endif

ret_code_t ret_val = polarizer_rotate(actual_frequency);

ret_code_t ret_val = polarizer_rotate(frequency);
Copy link

Copilot AI Feb 11, 2026

Choose a reason for hiding this comment

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

execute_set_angle() repurposes and mutates the frequency parameter (switching it from the caller-provided value to the internally chosen ramp start frequency). This makes the control flow harder to follow when debugging (especially since use_encoder/use_ramp are derived from the original value). Consider introducing a separate local like start_frequency (or similar) and keeping the input parameter unchanged, then pass the chosen start frequency to polarizer_rotate().

Copilot uses AI. Check for mistakes.
@fouge fouge force-pushed the fouge/polarizer-wheel-clean-ramp branch from d2ab09b to c2f6d5f Compare February 11, 2026 15:33
exact symetry of the pulses between the 2 positions
cleaned code a bit

Signed-off-by: Cyril Fougeray <cyril.fougeray@toolsforhumanity.com>
@fouge fouge force-pushed the fouge/polarizer-wheel-clean-ramp branch from c2f6d5f to 8cd51c3 Compare February 12, 2026 09:43
@fouge fouge merged commit 8277d5e into main Feb 12, 2026
15 checks passed
@fouge fouge deleted the fouge/polarizer-wheel-clean-ramp branch February 12, 2026 16: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.

1 participant

Comments