#include<SimpleFOC.h>// MagneticSensorAnalog(uint8_t _pinAnalog, int _min, int _max)// - pinAnalog - the pin that is reading the pwm from magnetic sensor// - min_raw_count - the smallest expected reading. // - max_raw_count - the largest value read. MagneticSensorAnalogsensor=MagneticSensorAnalog(A1,14,1020);voidsetup(){// monitoring portSerial.begin(115200);// initialise magnetic sensor hardwaresensor.init();Serial.println("Sensor ready");_delay(1000);}voidloop(){// iterative function updating the sensor internal variables// it is usually called in motor.loopFOC()// this function reads the sensor hardware and // has to be called before getAngle nad getVelocitysensor.update();// display the angle and the angular velocity to the terminalSerial.print(sensor.getAngle());Serial.print("\t");Serial.println(sensor.getVelocity());}
#include<SimpleFOC.h>// Example of AS5600 configuration MagneticSensorI2Csensor=MagneticSensorI2C(AS5600_I2C);voidsetup(){// monitoring portSerial.begin(115200);// configure i2CWire.setClock(400000);// initialise magnetic sensor hardwaresensor.init();Serial.println("Sensor ready");_delay(1000);}voidloop(){// iterative function updating the sensor internal variables// it is usually called in motor.loopFOC()// this function reads the sensor hardware and // has to be called before getAngle nad getVelocitysensor.update();// display the angle and the angular velocity to the terminalSerial.print(sensor.getAngle());Serial.print("\t");Serial.println(sensor.getVelocity());}
#include<SimpleFOC.h>// MagneticSensorSPI(MagneticSensorSPIConfig_s config, int cs)// config - SPI config// cs - SPI chip select pin MagneticSensorSPIsensor=MagneticSensorSPI(AS5147_SPI,10);voidsetup(){// monitoring portSerial.begin(115200);// initialise magnetic sensor hardwaresensor.init();Serial.println("Sensor ready");_delay(1000);}voidloop(){// iterative function updating the sensor internal variables// it is usually called in motor.loopFOC()// this function reads the sensor hardware and // has to be called before getAngle nad getVelocitysensor.update();// display the angle and the angular velocity to the terminalSerial.print(sensor.getAngle());Serial.print("\t");Serial.println(sensor.getVelocity());}
#include<SimpleFOC.h>// Hall sensor instance// HallSensor(int hallA, int hallB , int cpr, int index)// - hallA, hallB, hallC - HallSensor A, B and C pins// - pp - pole pairsHallSensorsensor=HallSensor(2,3,4,14);voidsetup(){// monitoring portSerial.begin(115200);// initialise sensor hardwaresensor.init();Serial.println("Sensor ready");_delay(1000);}voidloop(){// iterative function updating the sensor internal variables// it is usually called in motor.loopFOC()sensor.update();// display the angle and the angular velocity to the terminalSerial.print(sensor.getAngle());Serial.print("\t");Serial.println(sensor.getVelocity());delay(100);}
#include<SimpleFOC.h>Encoderencoder=Encoder(2,3,500);// interrupt routine initialisationvoiddoA(){encoder.handleA();}voiddoB(){encoder.handleB();}voidsetup(){// monitoring portSerial.begin(115200);// initialise encoder hardwareencoder.init();// hardware interrupt enableencoder.enableInterrupts(doA,doB);Serial.println("Encoder ready");_delay(1000);}voidloop(){// IMPORTANT// read sensor and update the internal variablesencoder.update();// display the angle and the angular velocity to the terminalSerial.print(encoder.getAngle());Serial.print("\t");Serial.println(sensor.getVelocity());}
#include<SimpleFOC.h>// BLDCDriver3PWM(pwmA, pwmB, pwmC, (en optional))BLDCDriver3PWMdriver=BLDCDriver3PWM(9,5,6,8);voidsetup(){// use monitoring with serial Serial.begin(115200);// enable more verbose output for debugging// comment out if not neededSimpleFOCDebug::enable(&Serial);// power supply voltage [V]driver.voltage_power_supply=12;// Max DC voltage allowed - default voltage_power_supplydriver.voltage_limit=12;// driver initif(!driver.init()){Serial.println("Driver init failed!");return;}// enable driverdriver.enable();Serial.println("Driver ready!");_delay(1000);}voidloop(){// setting pwm// phase A: 3V// phase B: 6V// phase C: 5Vdriver.setPwm(3,6,5);}
#include<SimpleFOC.h>// BLDCDriver6PWM(pwmAh, pwmAl, pwmBh, pwmBl, pwmCh, pwmCl, (en optional))BLDCDriver6PWMdriver=BLDCDriver6PWM(5,6,9,10,3,11,8);voidsetup(){// use monitoring with serial Serial.begin(115200);// enable more verbose output for debugging// comment out if not neededSimpleFOCDebug::enable(&Serial);// power supply voltage [V]driver.voltage_power_supply=12;// Max DC voltage allowed - default voltage_power_supplydriver.voltage_limit=12;// daad_zone [0,1] - default 0.02f - 2%driver.dead_zone=0.05f;// driver initif(!driver.init()){Serial.println("Driver init failed!");return;}// enable driverdriver.enable();Serial.println("Driver ready!");_delay(1000);}voidloop(){// setting pwm// phase A: 3V// phase B: 6V// phase C: 5Vdriver.setPwm(3,6,5);}
#include<SimpleFOC.h>// StepperDriver2PWM(pwm1, dir1, pwm2, dir2,(en1, en2 optional))StepperDriver2PWMdriver=StepperDriver2PWM(3,4,5,6,11,12);voidsetup(){// use monitoring with serial Serial.begin(115200);// enable more verbose output for debugging// comment out if not neededSimpleFOCDebug::enable(&Serial);// power supply voltage [V]driver.voltage_power_supply=12;// Max DC voltage allowed - default voltage_power_supplydriver.voltage_limit=12;// driver initif(!driver.init()){Serial.println("Driver init failed!");return;}// enable driverdriver.enable();Serial.println("Driver ready!");_delay(1000);}voidloop(){// setting pwm// phase A: 3V// phase B: 6Vdriver.setPwm(3,6);}
#include<SimpleFOC.h>// StepperDriver4PWM(ph1A, ph1B, ph2A, ph2B, (en1, en2 optional))StepperDriver4PWMdriver=StepperDriver4PWM(5,6,9,10,7,8);voidsetup(){// use monitoring with serial Serial.begin(115200);// enable more verbose output for debugging// comment out if not neededSimpleFOCDebug::enable(&Serial);// power supply voltage [V]driver.voltage_power_supply=12;// Max DC voltage allowed - default voltage_power_supplydriver.voltage_limit=12;// driver initif(!driver.init()){Serial.println("Driver init failed!");return;}// enable driverdriver.enable();Serial.println("Driver ready!");_delay(1000);}voidloop(){// setting pwm// phase A: 3V// phase B: 6Vdriver.setPwm(3,6);}
#include<SimpleFOC.h>// BLDCMotor(pole pair number, phase resistance (optional) );BLDCMotormotor=BLDCMotor(11);// BLDCDriver3PWM(pwmA, pwmB, pwmC, Enable(optional));BLDCDriver3PWMdriver=BLDCDriver3PWM(9,5,6,8);// instantiate the commanderCommandercommand=Commander(Serial);voiddoTarget(char*cmd){command.scalar(&motor.target,cmd);}voiddoLimit(char*cmd){command.scalar(&motor.voltage_limit,cmd);}voidsetup(){// use monitoring with serial Serial.begin(115200);// enable more verbose output for debugging// comment out if not neededSimpleFOCDebug::enable(&Serial);// driver config// power supply voltage [V]driver.voltage_power_supply=12;// limit the maximal dc voltage the driver can set// as a protection measure for the low-resistance motors// this value is fixed on startupdriver.voltage_limit=6;if(!driver.init()){Serial.println("Driver init failed!");return;}// link the motor and the drivermotor.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 1Ampmotor.voltage_limit=3;// [V]// open loop control configmotor.controller=MotionControlType::velocity_openloop;// init motor hardwareif(!motor.init()){Serial.println("Motor init failed!");return;}// set the target velocity [rad/s]motor.target=6.28;// one rotation per second// add target command Tcommand.add('T',doTarget,"target velocity");command.add('L',doLimit,"voltage limit");Serial.println("Motor ready!");Serial.println("Set target velocity [rad/s]");_delay(1000);}voidloop(){// open loop velocity movementmotor.move();// user communicationcommand.run();}
下面是使用 BLDC motor 和 6PWM driver 的开环速度控制示例。
#include<SimpleFOC.h>// BLDCMotor(pole pair number, phase resistance (optional) );BLDCMotormotor=BLDCMotor(11);// BLDCDriver6PWM(pwmAh, pwmAl, pwmBh, pwmBl, pwmCh, pwmCl, (en optional))BLDCDriver6PWMdriver=BLDCDriver6PWM(5,6,9,10,3,11,8);// instantiate the commanderCommandercommand=Commander(Serial);voiddoTarget(char*cmd){command.scalar(&motor.target,cmd);}voiddoLimit(char*cmd){command.scalar(&motor.voltage_limit,cmd);}voidsetup(){// use monitoring with serial Serial.begin(115200);// enable more verbose output for debugging// comment out if not neededSimpleFOCDebug::enable(&Serial);// driver config// power supply voltage [V]driver.voltage_power_supply=12;// limit the maximal dc voltage the driver can set// as a protection measure for the low-resistance motors// this value is fixed on startupif(!driver.init()){Serial.println("Driver init failed!");return;}// link the motor and the drivermotor.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 1Ampmotor.voltage_limit=3;// [V]// open loop control configmotor.controller=MotionControlType::velocity_openloop;// init motor hardwareif(!motor.init()){Serial.println("Motor init failed!");return;}// set the target velocity [rad/s]motor.target=6.28;// one rotation per second// add target command Tcommand.add('T',doTarget,"target velocity");command.add('L',doLimit,"voltage limit");Serial.println("Motor ready!");Serial.println("Set target velocity [rad/s]");_delay(1000);}voidloop(){// open loop velocity movementmotor.move();// user communicationcommand.run();}
下面是使用 Stepper motor 和 2PWM driver的开环速度控制示例。
#include<SimpleFOC.h>// StepperMotor(pole pair number, phase resistance (optional) );StepperMotormotor=StepperMotor(50);// StepperDriver2PWM(pwm1, dir1, pwm2, dir2,(en1, en2 optional))StepperDriver2PWMdriver=StepperDriver2PWM(9,5,6,8);// instantiate the commanderCommandercommand=Commander(Serial);voiddoTarget(char*cmd){command.scalar(&motor.target,cmd);}voiddoLimit(char*cmd){command.scalar(&motor.voltage_limit,cmd);}voidsetup(){// use monitoring with serial Serial.begin(115200);// enable more verbose output for debugging// comment out if not neededSimpleFOCDebug::enable(&Serial);// driver config// power supply voltage [V]driver.voltage_power_supply=12;// limit the maximal dc voltage the driver can set// as a protection measure for the low-resistance motors// this value is fixed on startupdriver.voltage_limit=6;if(!driver.init()){Serial.println("Driver init failed!");return;}// link the motor and the drivermotor.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 1Ampmotor.voltage_limit=3;// [V]// open loop control configmotor.controller=MotionControlType::velocity_openloop;// init motor hardwareif(!motor.init()){Serial.println("Motor init failed!");return;}// set the target velocity [rad/s]motor.target=6.28;// one rotation per second// add target command Tcommand.add('T',doTarget,"target velocity");command.add('L',doLimit,"voltage limit");Serial.println("Motor ready!");Serial.println("Set target velocity [rad/s]");_delay(1000);}voidloop(){// open loop velocity movementmotor.move();// user communicationcommand.run();}
下面是使用 4PWM driver 和 Stepper motor的开环速度控制示例。
#include<SimpleFOC.h>// StepperMotor(pole pair number, phase resistance (optional) );StepperMotormotor=StepperMotor(50);// StepperDriver4PWM(ph1A, ph1B, ph2A, ph2B, (en1, en2 optional))StepperDriver4PWMdriver=StepperDriver4PWM(5,6,9,10,7,8);// instantiate the commanderCommandercommand=Commander(Serial);voiddoTarget(char*cmd){command.scalar(&motor.target,cmd);}voiddoLimit(char*cmd){command.scalar(&motor.voltage_limit,cmd);}voidsetup(){// use monitoring with serial Serial.begin(115200);// enable more verbose output for debugging// comment out if not neededSimpleFOCDebug::enable(&Serial);// driver config// power supply voltage [V]driver.voltage_power_supply=12;// limit the maximal dc voltage the driver can set// as a protection measure for the low-resistance motors// this value is fixed on startupdriver.voltage_limit=6;if(!driver.init()){Serial.println("Driver init failed!");return;}// link the motor and the drivermotor.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 1Ampmotor.voltage_limit=3;// [V]// open loop control configmotor.controller=MotionControlType::velocity_openloop;// init motor hardwareif(!motor.init()){Serial.println("Motor init failed!");return;}// set the target velocity [rad/s]motor.target=6.28;// one rotation per second// add target command Tcommand.add('T',doTarget,"target velocity");command.add('L',doLimit,"voltage limit");Serial.println("Motor ready!");Serial.println("Set target velocity [rad/s]");_delay(1000);}voidloop(){// open loop velocity movementmotor.move();// user communicationcommand.run();}
#include<SimpleFOC.h>// TODO: motor instance // TODO: driver instance// TODO: sensor instance// instantiate the commanderCommandercommand=Commander(Serial);voiddoMotor(char*cmd){command.motor(&motor,cmd);}voidsetup(){// use monitoring with serial Serial.begin(115200);// enable more verbose output for debugging// comment out if not neededSimpleFOCDebug::enable(&Serial);// TODO: driver config// init the sensorsensor.init()// link the motor to the sensormotor.linkSensor(&sensor);// TODO: driver config// init the driverif(!driver.init()){Serial.println("Driver init failed!");return;}// link drivermotor.linkDriver(&driver);// TODO: motor conf// set motion control loop to be usedmotor.torque_controller=TorqueControlType::voltage;motor.controller=MotionControlType::torque;// use monitoring with serial// comment out if not neededmotor.useMonitoring(Serial);// initialize motorif(!motor.init()){Serial.println("Motor init failed!");return;}// align sensor and start FOCif(!motor.initFOC()){Serial.println("FOC init failed!");return;}// set the initial motor targetmotor.target=2;// Volts // add target command Mcommand.add('M',doMotor,"Motor");Serial.println(F("Motor ready."));Serial.println(F("Set the target using serial terminal and command M:"));_delay(1000);}voidloop(){// main FOC algorithm functionmotor.loopFOC();// Motion control functionmotor.move();// user communicationcommand.run();}
下面是使用 3PWM driver 和 Encoder传感器的 BLDC motor 的示例草图。
#include<SimpleFOC.h>// BLDC motor & driver instanceBLDCMotormotor=BLDCMotor(11);BLDCDriver3PWMdriver=BLDCDriver3PWM(9,5,6,8);// encoder instanceEncoderencoder=Encoder(2,3,500);// channel A and B callbacksvoiddoA(){encoder.handleA();}voiddoB(){encoder.handleB();}// instantiate the commanderCommandercommand=Commander(Serial);voiddoMotor(char*cmd){command.motor(&motor,cmd);}voidsetup(){// use monitoring with serial Serial.begin(115200);// enable more verbose output for debugging// comment out if not neededSimpleFOCDebug::enable(&Serial);// initialize encoder sensor hardwareencoder.init();encoder.enableInterrupts(doA,doB);// link the motor to the sensormotor.linkSensor(&encoder);// driver config// power supply voltage [V]driver.voltage_power_supply=12;// driver initif(!driver.init()){Serial.println("Driver init failed!");return;}// link drivermotor.linkDriver(&driver);// aligning voltagemotor.voltage_sensor_align=5;// set motion control loop to be usedmotor.torque_controller=TorqueControlType::voltage;motor.controller=MotionControlType::torque;// comment out if not neededmotor.useMonitoring(Serial);// initialize motorif(!motor.init()){Serial.println("Motor init failed!");return;}// align sensor and start FOCif(!motor.initFOC()){Serial.println("FOC init failed!");return;}// set the initial motor targetmotor.target=2;// Volts // add target command Mcommand.add('M',doMotor,"Motor");Serial.println(F("Motor ready."));Serial.println(F("Set the target using serial terminal and command M:"));_delay(1000);}voidloop(){// main FOC algorithm functionmotor.loopFOC();// Motion control functionmotor.move();// user communicationcommand.run();}
下面是使用 2PWM driver 和 Encoder 传感器的 Stepper motor 的示例草图。
#include<SimpleFOC.h>// Stepper motor & driver instanceStepperMotormotor=StepperMotor(11);StepperDriver2PWMdriver=StepperDriver2PWM(9,5,6,8);// encoder instanceEncoderencoder=Encoder(2,3,500);// channel A and B callbacksvoiddoA(){encoder.handleA();}voiddoB(){encoder.handleB();}// instantiate the commanderCommandercommand=Commander(Serial);voiddoMotor(char*cmd){command.motor(&motor,cmd);}voidsetup(){// use monitoring with serial Serial.begin(115200);// enable more verbose output for debugging// comment out if not neededSimpleFOCDebug::enable(&Serial);// initialize encoder sensor hardwareencoder.init();encoder.enableInterrupts(doA,doB);// link the motor to the sensormotor.linkSensor(&encoder);// driver config// power supply voltage [V]driver.voltage_power_supply=12;// driver initif(!driver.init()){Serial.println("Driver init failed!");return;}// link drivermotor.linkDriver(&driver);// aligning voltagemotor.voltage_sensor_align=5;// set motion control loop to be usedmotor.torque_controller=TorqueControlType::voltage;motor.controller=MotionControlType::torque;// comment out if not neededmotor.useMonitoring(Serial);// initialize motormotor.init();// align sensor and start FOCif(!motor.initFOC()){Serial.println("FOC init failed!");return;}// set the initial motor targetmotor.target=2;// Volts // add target command Mcommand.add('M',doMotor,"Motor");Serial.println(F("Motor ready."));Serial.println(F("Set the target using serial terminal and command M:"));_delay(1000);}voidloop(){// main FOC algorithm functionmotor.loopFOC();// Motion control functionmotor.move();// user communicationcommand.run();}
#include<SimpleFOC.h>// TODO: motor instance // TODO: driver instance// TODO: sensor instance// TODO: current sense instance// instantiate the commanderCommandercommand=Commander(Serial);voiddoMotor(char*cmd){command.motor(&motor,cmd);}voidsetup(){// use monitoring with serial Serial.begin(115200);// enable more verbose output for debugging// comment out if not neededSimpleFOCDebug::enable(&Serial);// TODO: driver config// init the sensorsensor.init()// link the motor to the sensormotor.linkSensor(&sensor);// TODO: driver config// init the driverif(!driver.init()){Serial.println("Driver init failed!");return;}// link drivermotor.linkDriver(&driver);// link driver to cscurrent_sense.linkDriver(&driver);// TODO: current sense config// current sense initif(!current_sense.init()){Serial.println("Current sense init failed!");return;}// link the current sense to the motormotor.linkCurrentSense(¤t_sense);// TODO: motor conf// set motion control loop to be usedmotor.torque_controller=TorqueControlType::voltage;motor.controller=MotionControlType::torque;// use monitoring with serial// comment out if not neededmotor.useMonitoring(Serial);motor.monitor_downsampling=100;// set downsampling can be even more > 100motor.monitor_variables=_MON_CURR_Q|_MON_CURR_D;// set monitoring of d and q currents// initialize motorif(!motor.init()){Serial.println("Motor init failed!");return;}// align sensor and start FOCif(!motor.initFOC()){Serial.println("FOC init failed!");return;}// set the initial motor targetmotor.target=2;// Volts // add target command Mcommand.add('M',doMotor,"Motor");Serial.println(F("Motor ready."));Serial.println(F("Set the target using serial terminal and command M:"));_delay(1000);}voidloop(){// main FOC algorithm functionmotor.loopFOC();// Motion control functionmotor.move();// display the currentsmotor.monitor();// user communicationcommand.run();}
以下是使用 3PWM driver、Encoder 和 Inline Current Sensing 的 BLDC motor 示例代码。
#include<SimpleFOC.h>// BLDC motor & driver instanceBLDCMotormotor=BLDCMotor(11);BLDCDriver3PWMdriver=BLDCDriver3PWM(9,5,6,8);// encoder instanceEncoderencoder=Encoder(2,3,500);// channel A and B callbacksvoiddoA(){encoder.handleA();}voiddoB(){encoder.handleB();}// current sensorInlineCurrentSensecurrent_sense=InlineCurrentSense(0.01,50.0,A0,A2);// instantiate the commanderCommandercommand=Commander(Serial);voiddoMotor(char*cmd){command.motor(&motor,cmd);}voidsetup(){// use monitoring with serial Serial.begin(115200);// enable more verbose output for debugging// comment out if not neededSimpleFOCDebug::enable(&Serial);// initialize encoder sensor hardwareencoder.init();encoder.enableInterrupts(doA,doB);// link the motor to the sensormotor.linkSensor(&encoder);// driver config// power supply voltage [V]driver.voltage_power_supply=12;// driver initif(!driver.init()){Serial.println("Driver init failed!");return;}// link drivermotor.linkDriver(&driver);// link driver to cscurrent_sense.linkDriver(&driver);// current sense init hardwareif(!current_sense.init()){Serial.println("Current sense init failed!");return;}// link the current sense to the motormotor.linkCurrentSense(¤t_sense);// aligning voltagemotor.voltage_sensor_align=5;// set motion control loop to be usedmotor.torque_controller=TorqueControlType::voltage;motor.controller=MotionControlType::torque;// use monitoring with serial// comment out if not neededmotor.useMonitoring(Serial);motor.monitor_downsampling=100;// set downsampling can be even more > 100motor.monitor_variables=_MON_CURR_Q|_MON_CURR_D;// set monitoring of d and q currents// initialize motorif(!motor.init()){Serial.println("Motor init failed!");return;}// align sensor and start FOCif(!motor.initFOC()){Serial.println("FOC init failed!");return;}// set the initial motor targetmotor.target=2;// Volts // add target command Mcommand.add('M',doMotor,"Motor");Serial.println(F("Motor ready."));Serial.println(F("Set the target using serial terminal and command M:"));_delay(1000);}voidloop(){// main FOC algorithm functionmotor.loopFOC();// Motion control functionmotor.move();// display the currentsmotor.monitor();// user communicationcommand.run();}
以下是使用2PWM driver, Encoder 和Inline Current Sensing的Stepper motor示例代码。
#include<SimpleFOC.h>// Stepper motor & driver instanceStepperMotormotor=StepperMotor(50);StepperDriver2PWMdriver=StepperDriver2PWM(9,5,6,8);// encoder instanceEncoderencoder=Encoder(2,3,500);// channel A and B callbacksvoiddoA(){encoder.handleA();}voiddoB(){encoder.handleB();}// current sensorInlineCurrentSensecurrent_sense=InlineCurrentSense(0.01,50.0,A0,A2);// instantiate the commanderCommandercommand=Commander(Serial);voiddoMotor(char*cmd){command.motor(&motor,cmd);}voidsetup(){// use monitoring with serial Serial.begin(115200);// enable more verbose output for debugging// comment out if not neededSimpleFOCDebug::enable(&Serial);// initialize encoder sensor hardwareencoder.init();encoder.enableInterrupts(doA,doB);// link the motor to the sensormotor.linkSensor(&encoder);// driver config// power supply voltage [V]driver.voltage_power_supply=12;// driver initif(!driver.init()){Serial.println("Driver init failed!");return;}// link drivermotor.linkDriver(&driver);// link driver to cscurrent_sense.linkDriver(&driver);// current sense init hardwareif(!current_sense.init()){Serial.println("Current sense init failed!");return;}// link the current sense to the motormotor.linkCurrentSense(¤t_sense);// aligning voltagemotor.voltage_sensor_align=5;// set motion control loop to be usedmotor.torque_controller=TorqueControlType::voltage;motor.controller=MotionControlType::torque;// use monitoring with serial// comment out if not neededmotor.useMonitoring(Serial);motor.monitor_downsampling=100;// set downsampling can be even more > 100motor.monitor_variables=_MON_CURR_Q|_MON_CURR_D;// set monitoring of d and q currents// initialize motorif(!motor.init()){Serial.println("Motor init failed!");return;}// align sensor and start FOCif(!motor.initFOC()){Serial.println("FOC init failed!");return;}// set the initial motor targetmotor.target=2;// Volts // add target command Mcommand.add('M',doMotor,"Motor");Serial.println(F("Motor ready."));Serial.println(F("Set the target using serial terminal and command M:"));_delay(1000);}voidloop(){// main FOC algorithm functionmotor.loopFOC();// Motion control functionmotor.move();// display the currentsmotor.monitor();// user communicationcommand.run();}
这份示例代码与步骤 3 的代码几乎完全相同,因此你在配置电机、传感器和驱动器时应该不会遇到太多困难。本步骤中,你将测试电流检测功能是否正常工作。调用 motor.monitor() 函数时,系统会读取电流检测数据,并将 d 轴电流和 q 轴电流打印到串口终端。你可以打开串口绘图器(Serial Plotter)来可视化这些数据。
让电机自由旋转,观察 d 轴电流和 q 轴电流是否降至低于电机静止时的数值。同时尝试观察:低转速时 d 轴电流几乎为 0,且会随着电机转速的增加而成比例上升。
如果电机未旋转,请通过查看串口终端输出来确认电机和驱动器已成功初始化(确保能看到 ` Motor ready!,而非 Motor init failed!、Driver init failed! 或 Current sense init failed!`)。我们的调试界面会在驱动器、电流检测和电机初始化过程中输出大量信息,可帮助你排查装置可能存在的问题。若电流检测功能出现问题,请查阅 电流检测文档,了解支持的传感器及所有配置参数。