Hello guys!
I have a problem with the sensor allignment, because it always fails even though I have the right amount of polepairs.
This is my current code:
#include <SimpleFOC.h>
int U_EN = 18; // inhu
int V_EN = 5; // inhv
int W_EN = 17; // inhw
float target_velocity = 0;
// Motor instance
BLDCMotor motor = BLDCMotor(1);
// driver instance
BLDCDriver3PWM driver = BLDCDriver3PWM(16, 4, 32);
HallSensor sensor = HallSensor(25, 26, 27, 1);
// Interrupt routine initialization
// channel A, B and C callbacks
void doA(){sensor.handleA();}
void doB(){sensor.handleB();}
void doC(){sensor.handleC();}
// instantiate the commander
Commander command = Commander(Serial);
void doTarget(char* cmd) { command.scalar(&target_velocity, cmd); }
void setup() {
pinMode(U_EN, OUTPUT);
pinMode(V_EN, OUTPUT);
pinMode(W_EN, OUTPUT);
digitalWrite(U_EN, HIGH);
digitalWrite(V_EN, HIGH);
digitalWrite(W_EN, HIGH);
Serial.begin(115200);
SimpleFOCDebug::enable(&Serial);
// initialize sensor hardware
sensor.init();
// hardware interrupt enable
//sensor.enableInterrupts(doA, doB, doC);
// link the motor and the sensor
motor.linkSensor(&sensor);
// driver config
// power supply voltage [V]
driver.voltage_power_supply = 12;
// driver init
if(!driver.init()){
Serial.println("Driver init failed!");
return;
}
// link driver
motor.linkDriver(&driver);
// set motion control loop to be used
motor.controller = MotionControlType::velocity;
// aligning voltage [V]
motor.voltage_sensor_align = 1;
// contoller configuration
// default parameters in defaults.h
// velocity PI controller parameters
motor.PID_velocity.P = 0.1f;
motor.PID_velocity.I = 0.1f;
motor.PID_velocity.D = 0;
// default voltage_power_supply
motor.voltage_limit = 12;
// default value is 300 volts per sec ~ 0.3V per millisecond
motor.PID_velocity.output_ramp = 300;
// velocity low pass filtering time constant
motor.LPF_velocity.Tf = 0.1f;
// comment out if not needed
motor.useMonitoring(Serial);
// initialize motor
motor.init();
// align sensor and start FOC
motor.initFOC();
// add target command T
command.add('T', doTarget, "target voltage");
Serial.println(F("Motor ready."));
Serial.println(F("Set the target velocity using serial terminal:"));
_delay(1000);
}
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);
// function intended to be used with serial plotter to monitor motor variables
// significantly slowing the execution down!!!!
// motor.monitor();
// user communication
command.run();
}
Thanks for your help!