Here is my v2.3.0 code, I am wondering, is there something motor parameter configuration which is not correct?
#include <SimpleFOC.h>
#include <Arduino.h>
// Motor instance
BLDCMotor motor = BLDCMotor(7);
BLDCDriver3PWM driver = BLDCDriver3PWM(PB4, PB5, PB0, PB12);
// magnetic sensor instance - SPI
MagneticSensorSPI sensor = MagneticSensorSPI(PB15, 14);
// current sensor
InlineCurrentSense current_sense = InlineCurrentSense(0.01f, 50.0f, A0, A1);
// angle,velocity set point variable
float target_velocity = 0;
float target_angle = 0;
// instantiate the commander
Commander command = Commander(Serial);
void doTarget(char *cmd)
{ command.scalar(&target_velocity, cmd); }
void doMotor(char* cmd)
{ command.motor(&motor, cmd); }
void setup()
{
// use monitoring with serial
Serial.begin(115200);
SimpleFOCDebug::enable(&Serial);
_delay(100);
SIMPLEFOC_DEBUG("Hello world!");
// initialize encoder sensor hardware
sensor.init();
// encoder.enableInterrupts(doA, doB);
// link the motor to the sensor
motor.linkSensor(&sensor);
driver.init();
// link the motor and the driver
motor.linkDriver(&driver);
// driver config
// power supply voltage [V]
driver.voltage_power_supply = 12;
// link current sense and the driver 2.2.0 above required
current_sense.linkDriver(&driver);
// aligning voltage [V]
motor.voltage_sensor_align = 1.5;
// if you are not using aligning voltage, you can set current limitation
motor.phase_resistance = 2.6; // [Ohm]
// index search velocity [rad/s]
// motor.velocity_index_search = 3;
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
// set motion control loop to be used
motor.torque_controller = TorqueControlType::voltage;
motor.controller = MotionControlType::angle;
// motor.torque_controller = TorqueControlType::voltage;
// motor.controller = MotionControlType::torque;
// set the inital target value
// motor.target = 0.5;
// default voltage_power_supply
// internal res is 0.35 ou maximum current is 2A
// maximum volts is 0.7
// this is used for open loop
// motor.voltage_limit = 0.7;
// maximal velocity of the position control
// motor.velocity_limit = 20;
motor.target = 13; //
// contoller configuration
// default parameters in defaults.h
// velocity PI controller parameters
motor.PID_velocity.P = 0.01;
motor.PID_velocity.I = 0.003;
// motor.PID_velocity.D = 0.001;
// jerk control using voltage voltage ramp
// default value is 300 volts per sec ~ 0.3V per millisecond
motor.PID_velocity.output_ramp = 1000;
// velocity low pass filtering time constant
motor.LPF_velocity.Tf = 0.01;
motor.voltage_limit = 1.5;
motor.current_limit = 0.8; // Amps
// angle P controller
motor.P_angle.P = 23;
motor.P_angle.D = 0.01;
// comment out if not needed
motor.useMonitoring(Serial);
motor.monitor_downsample = 1; // setting sample rate, can up to 100+
motor.monitor_variables = _MON_TARGET | _MON_CURR_Q | _MON_CURR_D | _MON_VEL | _MON_ANGLE;
//motor.monitor_downsample = 100; // set downsampling can be even more > 100
//motor.monitor_variables = _MON_CURR_Q | _MON_CURR_D; // set monitoring of d and q currents
// initialise magnetic sensor hardware
current_sense.init();
// link the motor to the sensor
motor.linkCurrentSense(¤t_sense);
// invert phase b gain
current_sense.gain_b *=-1;
// skip alignment
// current_sense.skip_align = true;
// initialize motor
motor.init();
// align encoder and start FOC
motor.initFOC();
// motor.target = 13;
// 订阅电机至commander
command.add('M', doMotor,"motor");
// add target command T
// command.add('T', doTarget, "target_velocity");
Serial.println("Motor ready.");
Serial.println("Set the target using serial terminal:");
_delay(300);
}
void loop()
{
// main FOC algorithm function
// the faster you run this function the better
// Arduino UNO loop ~1kHz
// Bluepill loop ~10kHz
motor.loopFOC();
// Motion control function
// velocity, position or voltage (defined in motor.controller)
// this function can be run at much lower frequency than loopFOC() function
// You can also use motor.move() and set the motor.target in the code
// motor.move(target_velocity);
motor.move();
// function intended to be used with serial plotter to monitor motor variables
// significantly slowing the execution down!!!!
motor.monitor();
// user communication
command.run();
// test current
//PhaseCurrent_s currents = current_sense.getPhaseCurrents();
//float current_magnitude = current_sense.getDCCurrent();
//Serial.print(currents.a * 1000); // milli Amps
//Serial.print("\t");
//Serial.print(currents.b * 1000); // milli Amps
//Serial.print("\t");
//Serial.print(currents.c * 1000); // milli Amps
//Serial.print("\t");
//Serial.println(current_magnitude * 1000); // milli Amps
}