How to Modify "motor.voltage_limit" in the loop() in "MotionControlType::angle" mode

Hello,

I try to modify the “motor.voltage_limit” value in the main loop via a serial command but the new value is not applied. I need to call motor.init() for it to be applied but it stop the motor while doing this.

Thanks,

Here is the code :

#include <SimpleFOC.h>

MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
TwoWire I2COne = TwoWire(0);

BLDCMotor motor = BLDCMotor(7);
BLDCDriver3PWM driver = BLDCDriver3PWM(32, 33, 25, 22);

float target_angle = 0;

Commander command = Commander(Serial);
void doTarget(char* cmd) { command.scalar(&target_angle, cmd); }
void doP(char* cmd) { command.scalar(&motor.PID_velocity.P, cmd); }
void doI(char* cmd) { command.scalar(&motor.PID_velocity.I, cmd); }
void doV(char* cmd) { command.scalar(&motor.voltage_limit, cmd); motor.init(); }

void setup() {
  Serial.begin(115200);
  
  I2COne.begin(19, 18, 400000);
  sensor.init(&I2COne);
  motor.linkSensor(&sensor);

  driver.voltage_power_supply = 12;
  driver.voltage_limit = 3;
  driver.init();

  motor.linkDriver(&driver);

  motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
  motor.controller = MotionControlType::angle;
  //motor.controller = MotionControlType::torque;
 
  motor.PID_velocity.P = 0.05f;
  motor.PID_velocity.I = 2;
  motor.voltage_limit = 1;

  motor.LPF_velocity.Tf = 0.01f;

  motor.P_angle.P = 20;
  motor.velocity_limit = 40;

  //motor.useMonitoring(Serial);

  motor.init();
  motor.initFOC();

  command.add('t', doTarget, " target_angle");
  command.add('p', doP, " motor.PID_velocity.P");
  command.add('i', doI, " motor.PID_velocity.I");
  command.add('v', doV, " motor.voltage_limit");

  Serial.println(F("Motor ready."));

  _delay(1000);
}

void loop() {
  motor.loopFOC();
  motor.move(target_angle);
  // motor.monitor();
  command.run();
}

Yes, it’s very confusing. You need to set motor.PID_velocity.limit. During initialization it’s set to either voltage_limit or current_limit depending on whether you provide a method of current limiting (a current sensor or kv and resistance for voltage-based current limiting), so afterward you have to change it directly.

2 Likes

Thanks dekutree64, that worked perfectly :slight_smile: