Skip to content

Commit

Permalink
fix optimization negative angle bug
Browse files Browse the repository at this point in the history
Co-Authored-By: Amanjyoti Mridha <[email protected]>
  • Loading branch information
irvingywang and QuackingBob committed Apr 20, 2024
1 parent 0c68d23 commit 33968b3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/algo/inc/Swerve_Locomotion.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#define Azimuth_Gear_Ratio 1.0f
#define NUMBER_OF_MODULES 4

#define SWERVE_OPTIMIZE

typedef struct
{
float speed; // m/s
Expand Down
5 changes: 5 additions & 0 deletions src/algo/src/Swerve_Locomotion.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ Module_State_t Optimize_Module_Angle(Module_State_t input_state, float measured_
}

float curr_angle_normalized = fmodf(measured_angle, 2 * PI); // get normalized current angle
if (curr_angle_normalized < 0)
{
curr_angle_normalized += 2.0f * PI;
}

float delta = target_angle - curr_angle_normalized; // calculate the angle delta

// these conditions essentially create the step function for angles to "snap" too offset from the measured angle to avoid flipping
Expand Down
4 changes: 3 additions & 1 deletion src/devices/src/dm_motor.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ void DM_Motor_Disable_Motor(DM_Motor_Handle_t *motor)
uint8_t *data = motor->can_instance->tx_buffer;
switch (motor->disable_behavior)
{
case DM_MOTOR_ZERO_CURRENT:
case DM_MOTOR_ZERO_CURRENT:
{
uint16_t pos_temp, vel_temp, kp_temp, kd_temp, torq_temp;

pos_temp = float_to_uint(0, P_MIN, P_MAX, 16);
Expand All @@ -94,6 +95,7 @@ void DM_Motor_Disable_Motor(DM_Motor_Handle_t *motor)
data[6] = ((kd_temp & 0xF) << 4) | (torq_temp >> 8);
data[7] = torq_temp;
break;
}
case DM_MOTOR_HARDWARE_DISABLE:
data[0] = 0xFF;
data[1] = 0xFF;
Expand Down

0 comments on commit 33968b3

Please sign in to comment.