Skip to content

Commit

Permalink
wow used the wrong value for acceleration
Browse files Browse the repository at this point in the history
  • Loading branch information
ahiuchingau committed Sep 17, 2024
1 parent b66c27a commit 6ad1b09
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
46 changes: 40 additions & 6 deletions stm32-modules/flex-stacker/src/motor_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,49 @@ MovementProfile::MovementProfile(uint32_t ticks_per_second,
}

auto MovementProfile::reset() -> void {
// seems like we'd have miss the distance traveled by (0->start velocity)?
_velocity = _start_velocity;
_current_distance = 0;
_tick_tracker = 0;
_accel_distance = 0;
}

auto MovementProfile::tick() -> TickReturn {
bool step = false;
// Acceleration gets clamped to _peak_velocity
if (_velocity < _peak_velocity) {
_velocity += _acceleration;
if (_velocity > _peak_velocity) {
_velocity = _peak_velocity;
if (_type == MovementType::FixedDistance) {
// Acceleration phase (until half of the profile is reached)
if (_velocity < _peak_velocity &&
_current_distance < _target_distance / 2) {
_velocity += _acceleration;
if (_velocity > _peak_velocity) {
_velocity = _peak_velocity;
// we're done accelerating
_accel_distance = _current_distance;
}
} else {
if (_accel_distance == 0) {
_accel_distance = _current_distance;
}
if (remaining_distance() <= _accel_distance) {
// Deceleration phase (after more than half of the target
// distance is covered)
_velocity -= _acceleration;
if (_velocity < _start_velocity) {
_velocity = _start_velocity;
}
}
}
} else {
// TODO: re-evaluate if we need to accelerate during open loop movement
// Acceleration gets clamped to _peak_velocity
if (_velocity < _peak_velocity) {
_velocity += _acceleration;
if (_velocity > _peak_velocity) {
_velocity = _peak_velocity;
}
}
}

auto old_tick_track = _tick_tracker;
_tick_tracker += _velocity;
// The bit _tick_flag represents a "whole" step. Once this flips,
Expand All @@ -66,7 +95,7 @@ auto MovementProfile::tick() -> TickReturn {
return _velocity;
}

/** Returns the number of ticks that have been taken.*/
/** Returns the number of total number of ticks that should be taken.*/
[[nodiscard]] auto MovementProfile::target_distance() const -> ticks {
return _target_distance;
}
Expand All @@ -80,3 +109,8 @@ auto MovementProfile::tick() -> TickReturn {
[[nodiscard]] auto MovementProfile::movement_type() const -> MovementType {
return _type;
}

/** Returns the number of ticks yet to be taken.*/
[[nodiscard]] auto MovementProfile::remaining_distance() const -> ticks {
return _target_distance - _current_distance;
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct MotorState {
return speed_mm_per_sec * steps_per_mm;
}
[[nodiscard]] auto get_accel() const -> float {
return accel_mm_per_sec_sq * steps_per_mm * steps_per_mm;
return accel_mm_per_sec_sq * steps_per_mm;
}
[[nodiscard]] auto get_speed_discont() const -> float {
return speed_mm_per_sec_discont * steps_per_mm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class MovementProfile {
/** Returns the movement type.*/
[[nodiscard]] auto movement_type() const -> MovementType;

[[nodiscard]] auto remaining_distance() const -> ticks;

private:
uint32_t _ticks_per_second; // Tick frequency
steps_per_tick _velocity = 0; // Current velocity
Expand All @@ -104,6 +106,7 @@ class MovementProfile {
ticks _target_distance; // Distance for the movement
ticks _current_distance = 0; // Distance this movement has reached
q31_31 _tick_tracker = 0; // Running tracker for the tick motion
ticks _accel_distance = 0; // Distance covered during acceleration phase

// When incrementing position tracker, if this bit changes then
// a step should take place.
Expand Down

0 comments on commit 6ad1b09

Please sign in to comment.