Skip to content

Commit

Permalink
fix potential overflow at high supply voltages
Browse files Browse the repository at this point in the history
  • Loading branch information
dzid26 committed May 29, 2024
1 parent e0c5062 commit e5f2c61
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions firmware/src/BSP/motor.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ static void inverse_park_transform(uint16_t elecAngle, int16_t Q, int16_t D, int
//calculate sine and cosine with ripple compensation
int16_t sin = sine_ripple(elecAngle, anticogging_factor);
int16_t cos = cosine_ripple(elecAngle, anticogging_factor);


*A = (int16_t)((((int32_t)cos * D) - ((int32_t)sin * Q)) / (int32_t)SINE_MAX); //convert value with vref max corresponding to 3300mV
*B = (int16_t)((((int32_t)sin * D) + ((int32_t)cos * Q)) / (int32_t)SINE_MAX); //convert value with vref max corresponding to 3300mV
// max practical phase voltage is U_lim*sqrt(2)
int32_t a = ((((int32_t)cos * D) - ((int32_t)sin * Q)) / (int32_t)SINE_MAX);
int32_t b = ((((int32_t)sin * D) + ((int32_t)cos * Q)) / (int32_t)SINE_MAX);

//limit to int16_t (to -32V..32V)
*A = (int16_t)(int32_t)clip(a, INT16_MIN, INT16_MAX);
*B = (int16_t)(int32_t)clip(b, INT16_MIN, INT16_MAX);
}

/**
Expand Down

0 comments on commit e5f2c61

Please sign in to comment.