simplefoc / Arduino-FOC

Arduino FOC for BLDC and Stepper motors - Arduino Based Field Oriented Control Algorithm Library
https://docs.simplefoc.com
MIT License
2.06k stars 530 forks source link

Recommendation, driver voltage limit in open loop example #124

Closed Juanduino closed 11 months ago

Juanduino commented 3 years ago

There are two bugs here, with regards to the SAMD/E implementation.

First of, the CCBUF, should not be fed with the channel number, but the wo association. I think, correct me if im wrong?

[Edit], I can see it is working with CCBUF and channel number. My mistake.

void writeSAMDDutyCycle(int chaninfo, float dc) {
    uint8_t tccn = GetTCNumber(chaninfo);
    uint8_t chan = GetTCChannelNumber(chaninfo);
    if (tccn<TCC_INST_NUM) {
        Tcc* tcc = (Tcc*)GetTC(chaninfo);

        Serial.print("Setting PWM CCBUF to :  ");
        Serial.print(dc, 5);
        Serial.print(" for channel :  ");
        Serial.println(chan); // Should be wo variable 

        // set via CCBUF
        while (TCC1->SYNCBUSY.bit.CC0 > 0 );
        tcc->CCBUF[chan].reg = (uint32_t)((SIMPLEFOC_SAMD_PWM_RESOLUTION-1) * dc); // TODO pwm frequency!

//      tcc->STATUS.vec.CCBUFV |= (0x1<<chan);
//      while ( tcc->SYNCBUSY.bit.STATUS > 0 );
//      tcc->CTRLBSET.reg |= TCC_CTRLBSET_CMD(TCC_CTRLBSET_CMD_UPDATE_Val);
//      while ( tcc->SYNCBUSY.bit.CTRLB > 0 );
    }
    else {
        // Tc* tc = (Tc*)GetTC(chaninfo);
        // //tc->COUNT16.CC[chan].reg = (uint32_t)((SIMPLEFOC_SAMD_PWM_RESOLUTION-1) * dc);
        // tc->COUNT8.CC[chan].reg = (uint8_t)((SIMPLEFOC_SAMD_PWM_TC_RESOLUTION-1) * dc);
        // while ( tc->COUNT8.STATUS.bit.SYNCBUSY == 1 );
    }
} 

Danger BUG!

The pwm multiplier, when running open_loop_velocity is around 0.5, no matter which voltage limit I set. This equals 50% duty cycle and potentially a fried controller or motor.

Veird behavier

Juanduino commented 3 years ago

Setting the voltage limit and PSU voltage manually gives better results... hmmm

BLDCDriver3PWM::BLDCDriver3PWM(int phA, int phB, int phC, int en1, int en2, int en3){
  // Pin initialization
  pwmA = phA;
  pwmB = phB;
  pwmC = phC;

  // enable_pin pin
  enableA_pin = en1;
  enableB_pin = en2;
  enableC_pin = en3;

  // default power-supply value
  voltage_power_supply = 12;
  voltage_limit = 1;

}

manual setting

Juanduino commented 3 years ago

If you wonder how open loop velocity mode looks on a LED setup :P

https://www.youtube.com/watch?v=AdnPMwuySow

askuric commented 3 years ago

Hey @Juanduino The first issue is really an issue related to the current implemntaiton of the library. We are mostly handling the pin numbers and do not handle well the wo_assotiations. I am not sure if this makes problems in your case but we will be working on it. @runger1101001 can give you some more updates regarding the samd architecture.

Regarding the voltage being at the half of the duty cycle. this is normal. See in documentation: https://docs.simplefoc.com/bldcdriver3pwm#step-22-voltages

basically we have two voltage limits implemented. One in the driver and one in the motor. The driver one actually does what you expect it to do. It limits the max dc voltage the code is allowed to set.

driver.voltage_power_supply = 12V;
driver.voltage_limit = 10V; // by default  driver.voltage_power_supply

Now the motor voltage limit is the maximal D and Q voltage magnitude allowed. That will more or less define the magnitude of the sine waves that the code is allowed to set to the motor.

motor.voltage_limit = 3V;

And the max you can set for this value is 0.5-0.75 of the driver.voltage_limit. But even if you set this value to more than driver.voltage_limit the values will still be limited to the driver.voltage_limit in the driver.

Lastly in terms of the half of the duty always. That is due to the fact that we are always setting relative voltage to the motor phases and for that setting 0 duty cycle of 0.5 duty cycle to all of them at once is exactly the same. Because the relative voltage in between any of the two phases will be zero. The image in the docs will maybe give you a better idea https://docs.simplefoc.com/bldcdriver3pwm#step-22-voltages

Now we do support feature to disabled the centered pwm (centered at the 0.5 of the driver.voltage_limit ), but it seems that I've forgot to document it. If you wish disable the centered pwm set the:

motor.modulation_centered = 0;
Juanduino commented 3 years ago

I see....

Then I will recommend updating the : https://github.com/simplefoc/Arduino-FOC/blob/master/examples/motion_control/open_loop_motor_control/open_loop_velocity_example/open_loop_velocity_example.ino

Someone might get burnt...

runger1101001 commented 11 months ago

A voltage limit has been added to this example, so I will now close this issue... :-)