#include <SimpleFOC.h>
// BLDC motor & driver instance
BLDCMotor motor = BLDCMotor(22, 1.2);
// BLDCDriver6PWM driver = BLDCDriver6PWM(pwm HIGH , pwm LOW , pwmHIGH , pwm LOW , pwm HIGH , pwm LOW );
BLDCDriver6PWM driver = BLDCDriver6PWM(PA8 , PB13 , PA9 , PB14 , PA10 , PB15 );
// InlineCurrentSensor constructor
// - mVpA - mV per Amp ratio
// - phA - A phase adc pin
// - phB - B phase adc pin
// - phC - C phase adc pin (optional)
//InlineCurrentSense current_sense = //InlineCurrentSense(40.0, PB0 , PC1 , PC0 );
// hall sensor instance
HallSensor sensor = HallSensor(PB3, PB4, PB5, 22);
// Interrupt routine intialisation
// channel A and B callbacks
void doA(){sensor.handleA();}
void doB(){sensor.handleB();}
void doC(){sensor.handleC();}
// voltage set point variable
float target_voltage = 0;
// instantiate the commander
Commander command = Commander(Serial);
void doTarget (char* cmd){ command.scalar (&target_voltage, cmd); }
void setup() {
// initialize encoder sensor hardware
sensor.init();
sensor.enableInterrupts(doA, doB, doC);
// link the motor to the sensor
motor.linkSensor(&sensor);
// driver config
// power supply voltage [V]
driver.voltage_power_supply = 65;
driver.init();
// link the driver to the current sense
//current_sense.linkDriver(&driver);
// link driver
motor.linkDriver(&driver);
// aligning voltage
motor.voltage_sensor_align = 3;
// init current sense
//current_sense.init();
// init current sense
//if (current_sense.init()) Serial.println(âCurrent sense init success!â);
//else{
// Serial.println(âCurrent sense init failed!â);
// return;
}
// link motor and current sense
// motor.linkCurrentSense(¤t_sense);
// choose FOC modulation (optional)
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
// set torque mode:
//motor.torque_controller
//TorqueControlType::foc_current;
// set motion control loop to be used
motor.controller = MotionControlType::torque;
// foc current control parameters (Arduino UNO/Mega)
//motor.PID_current_q.P = 5;
//motor.PID_current_q.I= 1000;
//motor.PID_current_d.P= 5;
//motor.PID_current_d.I = 1000;
//motor.LPF_current_q.Tf = 0.005;
//motor.LPF_current_d.Tf = 0.005;
// use monitoring with serial
Serial.begin(115200);
// 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 voltage using serial terminal:â));
_delay(1000);
}
void loop() {
motor.loopFOC();
motor.move(target_voltage);
command.run();
}