Use FOC current mode in position control mode

Hello everyone! I use v2.0.2 shield to run the angle control of simpleFOC, and use the FOC current control inside. But after my operation, my motor spins wildly, I don’t know why? This is my code, thanks

#include <SimpleFOC.h>

MagneticSensorSPI sensor = MagneticSensorSPI(9, 14, 0x3FFF); 
BLDCMotor motor = BLDCMotor(14);     
BLDCDriver3PWM driver = BLDCDriver3PWM(10, 5, 6, 8);  
InlineCurrentSense current_sense = InlineCurrentSense(0.01, 50.0, A1, A3);


float target_angle = 0;  
Commander command = Commander(Serial);    
void doTarget(char* cmd) { command.scalar(&target_angle, cmd); }    
void onMotor(char* cmd){ command.motor(&motor,cmd); }    


void setup() {
  sensor.init();
  motor.linkSensor(&sensor);

  driver.voltage_power_supply = 12;
  driver.init();
  motor.linkDriver(&driver);

  current_sense.gain_b *= -1;
  current_sense.init();
  motor.linkCurrentSense(&current_sense);      

  motor.foc_modulation = FOCModulationType::SpaceVectorPWM;

  motor.controller = MotionControlType::angle;
  motor.torque_controller = TorqueControlType::foc_current;
  
  motor.PID_velocity.P = 0.2;
  motor.PID_velocity.I = 10;
  motor.PID_velocity.D = 0;
  motor.P_angle.P = 10;

  motor.voltage_limit = 6;
  motor.velocity_limit = 5;
  motor.LPF_velocity.Tf = 0.01;

  Serial.begin(115200);
  motor.useMonitoring(Serial);

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

  command.add('T', doTarget, "target angle");
  command.add('M',onMotor,"my motor"); 
  
  Serial.println(F("Motor ready."));
  Serial.println(F("Set the target angle using serial terminal:"));
  _delay(1000);
}


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

Hi @Cc1924, welcome to the forum!

Can you tell us a bit more about your hardware setup (microcontroller, motor and sensor)?

Can you also confirm to us that “simpler” FOC modes are working already like open loop control? And after that closed loop with TorqueControlType::voltage?

Thank you for your reply!
This is my hardware setup: microcontroller (Arduino UNO), motor (24N28P BLDC Motor), encoder (AS5048A), simpleFOC shield (V2.0.2)
It works well in open-loop mode and closed-loop mode with TorqueControlType::voltage.
But when I use the closed-loop mode with TorqueControlType::foc_current, the motor becomes uncontrollable. I don’t know why.

Sounds like you need to tune the PI controllers that are used for:

TorqueControlType::foc_current

I recommend you give a reading to this section of the documentation: