fix(polarizer-wheel): cleaner ramp, starts at 2s/turn speed#469
Conversation
There was a problem hiding this comment.
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_frequencyfields 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.
There was a problem hiding this comment.
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.
| 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 " |
There was a problem hiding this comment.
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.
| @@ -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); | |||
There was a problem hiding this comment.
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().
d2ab09b to
c2f6d5f
Compare
exact symetry of the pulses between the 2 positions cleaned code a bit Signed-off-by: Cyril Fougeray <cyril.fougeray@toolsforhumanity.com>
c2f6d5f to
8cd51c3
Compare
exact symetry of the pulses between the 2 positions cleaned code a bit