I am trying to understand this github code for speed control of AC Induction Motor. It calculates slip value to adjust the speed of motor. I wonder if its the same thing that is called constant V/f speed control? Is it just difference in the terminology, V and f being electrical while slip and torque being mechanical?

Here is the code for quick look.

    // Globals for data that is either persistent or used for debuging
int i1, i2, i3;    // Phase currents
int total_current; // Total instantaneous current
int angle;         // The angle of the stator voltage
int voltage;       // The magnitude of the stator voltage
int encoder_position_previous; // The previous encoder value

// Analog to digital converter DMA data
uint16_t adc_data[4]; // [i1, i2, i3, throttle]

// Motor parameters
#define MOTOR_POLES 4
#define GAIN 2

// This runs at the PWM frequency: 9765.625Hz
// It runs 0.000048s after it's triggered (approx half the PWM period)
// This means data is collected during the first half of each
// pwm cycle ready for calculations in the second half.
void DMA1_Channel1_IRQHandler(void) {
  // Fetch the phase currents
  i1 = adc_data[0] - 32840; // Offset to 0A
  i2 = adc_data[1] - 32840; // Offset to 0A
  i3 = adc_data[2] - 32840; // Offset to 0A
  // Calculate the total current (FPU operation)
  total_current = sqrtf((float)i1*(float)i1 + (float)i2*(float)i2 + (float)i3*(float)i3);

  // Calculate encoder ticks change since the last iteration
  uint32_t encoder_position_current = TIM2->CNT; // Atomic as possible
  int encoder_position_change = encoder_position_current - encoder_position_previous; // Nasty casting to signed here
  encoder_position_previous = encoder_position_current;

  // Determine slip from the throttle
  // By happy coincidence this gives +/- 32768 (+/- 5Hz)
  int slip = adc_data[3] - 32768;
  // Increment the angle by the requested slip
  angle += slip;
  // Increment the angle by the encoder measurement
  angle += encoder_position_change * 67108864 / ENCODER_RESOLUTION * MOTOR_POLES / 2;

  // Calculate the target current.
  // This starts at MAGNETIZING_CURRENT and increases wih slip
  // We use floating point operations here to avoid overflows
  if(slip >= 0) {
    target_current = (float)MAGNETIZING_CURRENT + (float)MAX_TORQUE_CURRENT * (float)slip / 32768.0f;
  } else {
    target_current = (float)MAGNETIZING_CURRENT - (float)MAX_TORQUE_CURRENT * (float)slip / 32768.0f;

  // Apply current error to voltage output
  if(total_current < target_current && voltage < 4000) voltage += GAIN;
  if(total_current > target_current && voltage > 0)    voltage -= GAIN;

  // Update PWM outputs from SVM style lookup table
  TIM1->CCR3 = (voltage * table1[angle >> 13]) >> 15;
  TIM1->CCR2 = (voltage * table2[angle >> 13]) >> 15;
  TIM1->CCR1 = (voltage * table3[angle >> 13]) >> 15;

  // Clear interrupt

void TIM1_UP_TIM16_IRQHandler(void) {
  // Start ADC conversions each time PWM restarts
  // This runs at 80000000/8192 = 9765.625Hz
  • \$\begingroup\$ i have included the code above but its all jumbled up. how do i correct it?.. now formatting done ok. \$\endgroup\$
    – homecloud
    Commented Jun 11, 2020 at 10:57
  • \$\begingroup\$ looks great, thank you! \$\endgroup\$ Commented Jun 11, 2020 at 11:05

1 Answer 1


From the link " this github code"

STM32 variable frequency drive controller

This project aims to be an open source torque controller for AC induction motors.


The software generates a 3-phase output using PWM. The frequency and voltage are adjusted to create a torque proportional to the "throttle" input.

As stated above, the code is for controlling the torque of the motor not the speed. The speed is controlled manually by the person operating the throttle.

  • \$\begingroup\$ If the load is constant and i increase the torque, then will be equivalent to increasing the motor speed? \$\endgroup\$
    – homecloud
    Commented Jun 11, 2020 at 11:04
  • \$\begingroup\$ Yes. If the speed and torque are constant, the torque produced is equal to the torque required by the load to operate at that speed. Any increase in torque will be applied to accelerate the mass. \$\endgroup\$
    – user80875
    Commented Jun 11, 2020 at 11:10
  • \$\begingroup\$ So can we safely say that the above software code is same as constant V/f speed control of AC Induction motor? \$\endgroup\$
    – homecloud
    Commented Jun 11, 2020 at 11:46
  • \$\begingroup\$ No.The V/f is apparently adjusted to achieve the torque setting at any speed that is detected by the encoder. My answer is based on the authors description of the intent and statement that the results have been good. I have no knowledge of the system and code. \$\endgroup\$
    – user80875
    Commented Jun 11, 2020 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.