@Candas1 @Valentine The code is pretty basic. Its the open loop example with a few extras to slowly ramp up the motor speed and then reverse direction.
#include <SimpleFOC.h>
// BLDC motor & driver instance
// BLDCMotor motor = BLDCMotor(pole pair number);
BLDCMotor motor = BLDCMotor(11);
// BLDCDriver3PWM driver = BLDCDriver3PWM(pwmA, pwmB, pwmC, Enable(optional));
BLDCDriver3PWM driver = BLDCDriver3PWM(11, 10, 9, 8);//Arduino pins Original
//BLDCDriver3PWM driver = BLDCDriver3PWM(25, 26, 27, 33);//ESP32 pins
//BLDCDriver3PWM driver = BLDCDriver3PWM(PA6, PA7, PB0, PA11);//STM32F030C8T6 pins
//target variable
float target_velocity = 0.0;
//variable to reverse direction
float reverse = 1.0;
//Start timer to follow millis()
unsigned long myTime;
//Create point to start a duration
unsigned long resetTime;
//Time since last reset point
unsigned long duration;
//const int MaxSpeed = 5;//Set max speed
const int MaxSpeed = 50;//Set max speed
//Create floating point time variable
float timer;
void setup() {
// driver config
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
driver.pwm_frequency = 32000;
// power supply voltage [V]
driver.voltage_power_supply = 8;
// limit the maximal dc voltage the driver can set
// as a protection measure for the low-resistance motors
// this value is fixed on startup
driver.voltage_limit = 8;
driver.init();
// link the motor and the driver
motor.linkDriver(&driver);
// limiting motor movements
// limit the voltage to be set to the motor
// start very low for high resistance motors
// current = voltage / resistance, so try to be well under 1Amp
motor.voltage_limit = 4; // [V]
// open loop control config
motor.controller = MotionControlType::velocity_openloop;
// init motor hardware
motor.init();
pinMode(LED_BUILTIN, OUTPUT);
myTime = millis();
}
void loop() {
// open loop velocity movement
// using motor.voltage_limit and motor.velocity_limit
// to turn the motor “backwards”, just set a negative target_velocity
myTime = millis();
duration = myTime - resetTime;//find duration of current sequence
timer = duration*1.0;//convert milliseconds to float
//target_velocity = MaxSpeed;
if(duration < 10000)
{
//Use floating point timer to increase target velocity
target_velocity = MaxSpeed*(timer/10000.0)*(reverse);
//target_velocity = (MaxSpeed*(timer/30000.0)*(reverse))+1;
}
if(duration>10000 && duration<60000)
{
target_velocity = MaxSpeed* (reverse);//Set maximum velocity
}
if(duration>60000 && duration<62000)
{
//digitalWrite(LED_BUILTIN, HIGH);
target_velocity = 0;//Stop and reverse motor
}
if(duration>62000)
{
//digitalWrite(LED_BUILTIN, LOW);
resetTime = millis();
reverse = reverse * (-1.0);
}
motor.move(target_velocity);//Set motor speed
}