try to fix submodule
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
#include <Arduino.h>
|
||||
#include <SimpleFOC.h>
|
||||
|
||||
BLDCMotor motor = BLDCMotor(11);
|
||||
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 10, 11, 8);
|
||||
//StepperMotor motor = StepperMotor(50);
|
||||
//StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6, 8);
|
||||
MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0X0C, 4);
|
||||
|
||||
|
||||
/**
|
||||
* This measures how closely sensor and electrical angle agree and how much your motor is affected by 'cogging'.
|
||||
* It can be used to investigate how much non linearity there is between what we set (electrical angle) and what we read (sensor angle)
|
||||
* This non linearity could be down to magnet placement, coil winding differences or simply that the magnetic field when travelling through a pole pair is not linear
|
||||
* An alignment error of ~10 degrees and cogging of ~4 degrees is normal for small gimbal.
|
||||
* The following article is an interesting read
|
||||
* https://hackaday.com/2016/02/23/anti-cogging-algorithm-brings-out-the-best-in-your-hobby-brushless-motors/
|
||||
*/
|
||||
void testAlignmentAndCogging(int direction) {
|
||||
|
||||
motor.move(0);
|
||||
_delay(200);
|
||||
|
||||
sensor.update();
|
||||
float initialAngle = sensor.getAngle();
|
||||
|
||||
const int shaft_rotation = 720; // 720 deg test - useful to see repeating cog pattern
|
||||
int sample_count = int(shaft_rotation * motor.pole_pairs); // test every electrical degree
|
||||
|
||||
float stDevSum = 0;
|
||||
|
||||
float mean = 0.0f;
|
||||
float prev_mean = 0.0f;
|
||||
|
||||
|
||||
for (int i = 0; i < sample_count; i++) {
|
||||
|
||||
float shaftAngle = (float) direction * i * shaft_rotation / sample_count;
|
||||
float electricAngle = (float) shaftAngle * motor.pole_pairs;
|
||||
// move and wait
|
||||
motor.move(shaftAngle * PI / 180);
|
||||
_delay(5);
|
||||
|
||||
// measure
|
||||
sensor.update();
|
||||
float sensorAngle = (sensor.getAngle() - initialAngle) * 180 / PI;
|
||||
float sensorElectricAngle = sensorAngle * motor.pole_pairs;
|
||||
float electricAngleError = electricAngle - sensorElectricAngle;
|
||||
|
||||
// plot this - especially electricAngleError
|
||||
Serial.print(electricAngle);
|
||||
Serial.print("\t");
|
||||
Serial.print(sensorElectricAngle );
|
||||
Serial.print("\t");
|
||||
Serial.println(electricAngleError);
|
||||
|
||||
// use knuth standard deviation algorithm so that we don't need an array too big for an Uno
|
||||
prev_mean = mean;
|
||||
mean = mean + (electricAngleError-mean)/(i+1);
|
||||
stDevSum = stDevSum + (electricAngleError-mean)*(electricAngleError-prev_mean);
|
||||
|
||||
}
|
||||
|
||||
Serial.println();
|
||||
Serial.println(F("ALIGNMENT AND COGGING REPORT"));
|
||||
Serial.println();
|
||||
Serial.print(F("Direction: "));
|
||||
Serial.println(direction);
|
||||
Serial.print(F("Mean error (alignment): "));
|
||||
Serial.print(mean);
|
||||
Serial.println(" deg (electrical)");
|
||||
Serial.print(F("Standard Deviation (cogging): "));
|
||||
Serial.print(sqrt(stDevSum/sample_count));
|
||||
Serial.println(F(" deg (electrical)"));
|
||||
Serial.println();
|
||||
Serial.println(F("Plotting 3rd column of data (electricAngleError) will likely show sinusoidal cogging pattern with a frequency of 4xpole_pairs per rotation"));
|
||||
Serial.println();
|
||||
|
||||
}
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(115200);
|
||||
while (!Serial) ;
|
||||
|
||||
// driver config
|
||||
driver.voltage_power_supply = 12;
|
||||
driver.init();
|
||||
motor.linkDriver(&driver);
|
||||
|
||||
motor.voltage_sensor_align = 3;
|
||||
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
|
||||
|
||||
motor.controller = MotionControlType::angle_openloop;
|
||||
motor.voltage_limit=motor.voltage_sensor_align;
|
||||
|
||||
sensor.init();
|
||||
motor.linkSensor(&sensor);
|
||||
|
||||
motor.useMonitoring(Serial);
|
||||
motor.init();
|
||||
motor.initFOC();
|
||||
|
||||
testAlignmentAndCogging(1);
|
||||
|
||||
motor.move(0);
|
||||
Serial.println(F("Press any key to test in CCW direction"));
|
||||
while (!Serial.available()) { }
|
||||
|
||||
testAlignmentAndCogging(-1);
|
||||
|
||||
Serial.println(F("Complete"));
|
||||
|
||||
motor.voltage_limit = 0;
|
||||
motor.move(0);
|
||||
while (true) ; //do nothing;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void loop() {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
*
|
||||
* Find KV rating for motor with encoder
|
||||
*
|
||||
* Motor KV rating is defiend as the increase of the motor velocity expressed in rotations per minute [rpm] per each 1 Volt int voltage control mode.
|
||||
*
|
||||
* This example will set your motor in the torque control mode using voltage and set 1 volt to the motor. By reading the velocity it will calculat the motors KV rating.
|
||||
* - To make this esimation more credible you can try increasing the target voltage (or decrease in some cases)
|
||||
* - The KV rating should be realatively static number - it should not change considerably with the increase in the voltage
|
||||
*
|
||||
*/
|
||||
#include <SimpleFOC.h>
|
||||
|
||||
|
||||
// BLDC motor & driver instance
|
||||
BLDCMotor motor = BLDCMotor(11);
|
||||
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);
|
||||
// Stepper motor & driver instance
|
||||
//StepperMotor motor = StepperMotor(50);
|
||||
//StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6, 8);
|
||||
|
||||
// encoder instance
|
||||
Encoder sensor = Encoder(2, 3, 8192);
|
||||
|
||||
// Interrupt routine intialisation
|
||||
// channel A and B callbacks
|
||||
void doA(){sensor.handleA();}
|
||||
void doB(){sensor.handleB();}
|
||||
|
||||
|
||||
// voltage set point variable
|
||||
float target_voltage = 1;
|
||||
|
||||
// instantiate the commander
|
||||
Commander command = Commander(Serial);
|
||||
void doTarget(char* cmd) { command.scalar(&target_voltage, cmd); }
|
||||
void calcKV(char* cmd) {
|
||||
// calculate the KV
|
||||
Serial.println(motor.shaft_velocity/motor.target*30.0f/_PI);
|
||||
|
||||
}
|
||||
|
||||
void setup() {
|
||||
|
||||
// initialize encoder sensor hardware
|
||||
sensor.init();
|
||||
sensor.enableInterrupts(doA, doB);
|
||||
// link the motor to the sensor
|
||||
motor.linkSensor(&sensor);
|
||||
|
||||
// driver config
|
||||
// IMPORTANT!
|
||||
// make sure to set the correct power supply voltage [V]
|
||||
driver.voltage_power_supply = 12;
|
||||
driver.init();
|
||||
// link driver
|
||||
motor.linkDriver(&driver);
|
||||
|
||||
// aligning voltage
|
||||
motor.voltage_sensor_align = 3;
|
||||
|
||||
// set motion control loop to be used
|
||||
motor.controller = MotionControlType::torque;
|
||||
|
||||
// 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");
|
||||
command.add('K', calcKV, "calculate KV rating");
|
||||
|
||||
Serial.println(F("Motor ready."));
|
||||
Serial.println(F("Set the target voltage : - commnad T"));
|
||||
Serial.println(F("Calculate the motor KV : - command K"));
|
||||
_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_voltage);
|
||||
|
||||
// user communication
|
||||
command.run();
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
*
|
||||
* Find KV rating for motor with Hall sensors
|
||||
*
|
||||
* Motor KV rating is defiend as the increase of the motor velocity expressed in rotations per minute [rpm] per each 1 Volt int voltage control mode.
|
||||
*
|
||||
* This example will set your motor in the torque control mode using voltage and set 1 volt to the motor. By reading the velocity it will calculat the motors KV rating.
|
||||
* - To make this esimation more credible you can try increasing the target voltage (or decrease in some cases)
|
||||
* - The KV rating should be realatively static number - it should not change considerably with the increase in the voltage
|
||||
*/
|
||||
#include <SimpleFOC.h>
|
||||
|
||||
|
||||
// BLDC motor & driver instance
|
||||
BLDCMotor motor = BLDCMotor(11);
|
||||
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);
|
||||
|
||||
// hall sensor instance
|
||||
HallSensor sensor = HallSensor(2, 3, 4, 11);
|
||||
|
||||
// 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 = 1;
|
||||
|
||||
// instantiate the commander
|
||||
Commander command = Commander(Serial);
|
||||
void doTarget(char* cmd) { command.scalar(&target_voltage, cmd); }
|
||||
void calcKV(char* cmd) {
|
||||
// calculate the KV
|
||||
Serial.println(motor.shaft_velocity/motor.target*30.0f/_PI);
|
||||
|
||||
}
|
||||
|
||||
void setup() {
|
||||
|
||||
// initialize encoder sensor hardware
|
||||
sensor.init();
|
||||
sensor.enableInterrupts(doA, doB, doC);
|
||||
// link the motor to the sensor
|
||||
motor.linkSensor(&sensor);
|
||||
|
||||
// driver config
|
||||
// IMPORTANT!
|
||||
// make sure to set the correct power supply voltage [V]
|
||||
driver.voltage_power_supply = 12;
|
||||
driver.init();
|
||||
// link driver
|
||||
motor.linkDriver(&driver);
|
||||
|
||||
// aligning voltage
|
||||
motor.voltage_sensor_align = 3;
|
||||
|
||||
// set motion control loop to be used
|
||||
motor.controller = MotionControlType::torque;
|
||||
|
||||
// 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");
|
||||
command.add('K', calcKV, "calculate KV rating");
|
||||
|
||||
Serial.println(F("Motor ready."));
|
||||
Serial.println(F("Set the target voltage : - commnad T"));
|
||||
Serial.println(F("Calculate the motor KV : - command K"));
|
||||
_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_voltage);
|
||||
|
||||
// user communication
|
||||
command.run();
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* Find KV rating for motor with magnetic sensors
|
||||
*
|
||||
* Motor KV rating is defiend as the increase of the motor velocity expressed in rotations per minute [rpm] per each 1 Volt int voltage control mode.
|
||||
*
|
||||
* This example will set your motor in the torque control mode using voltage and set 1 volt to the motor. By reading the velocity it will calculat the motors KV rating.
|
||||
* - To make this esimation more credible you can try increasing the target voltage (or decrease in some cases)
|
||||
* - The KV rating should be realatively static number - it should not change considerably with the increase in the voltage
|
||||
*/
|
||||
#include <SimpleFOC.h>
|
||||
|
||||
// magnetic sensor instance - SPI
|
||||
MagneticSensorSPI sensor = MagneticSensorSPI(AS5147_SPI, 10);
|
||||
// magnetic sensor instance - I2C
|
||||
// MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
|
||||
// magnetic sensor instance - analog output
|
||||
// MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
|
||||
|
||||
// BLDC motor & driver instance
|
||||
BLDCMotor motor = BLDCMotor(11);
|
||||
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);
|
||||
// Stepper motor & driver instance
|
||||
//StepperMotor motor = StepperMotor(50);
|
||||
//StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6, 8);
|
||||
|
||||
// voltage set point variable
|
||||
float target_voltage = 1;
|
||||
|
||||
// instantiate the commander
|
||||
Commander command = Commander(Serial);
|
||||
void doTarget(char* cmd) { command.scalar(&target_voltage, cmd); }
|
||||
void calcKV(char* cmd) {
|
||||
// calculate the KV
|
||||
Serial.println(motor.shaft_velocity/motor.target*30.0f/_PI);
|
||||
|
||||
}
|
||||
|
||||
void setup() {
|
||||
|
||||
// initialize encoder sensor hardware
|
||||
sensor.init();
|
||||
// link the motor to the sensor
|
||||
motor.linkSensor(&sensor);
|
||||
|
||||
// driver config
|
||||
// IMPORTANT!
|
||||
// make sure to set the correct power supply voltage [V]
|
||||
driver.voltage_power_supply = 12;
|
||||
driver.init();
|
||||
// link driver
|
||||
motor.linkDriver(&driver);
|
||||
|
||||
// aligning voltage
|
||||
motor.voltage_sensor_align = 3;
|
||||
|
||||
// set motion control loop to be used
|
||||
motor.controller = MotionControlType::torque;
|
||||
|
||||
// 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");
|
||||
command.add('K', calcKV, "calculate KV rating");
|
||||
|
||||
Serial.println(F("Motor ready."));
|
||||
Serial.println(F("Set the target voltage : - commnad T"));
|
||||
Serial.println(F("Calculate the motor KV : - command K"));
|
||||
_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_voltage);
|
||||
|
||||
// user communication
|
||||
command.run();
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
/**
|
||||
* Utility arduino sketch which finds pole pair number of the motor
|
||||
*
|
||||
* To run it just set the correct pin numbers for the BLDC driver and encoder A and B channel as well as the encoder PPR value.
|
||||
*
|
||||
* The program will rotate your motor a specific amount and check how much it moved, and by doing a simple calculation calculate your pole pair number.
|
||||
* The pole pair number will be outputted to the serial terminal.
|
||||
*
|
||||
* If the pole pair number is well estimated your motor will start to spin in voltage mode with 2V target.
|
||||
*
|
||||
* If the code calculates negative pole pair number please invert your encoder A and B channel pins or motor connector.
|
||||
*
|
||||
* Try running this code several times to avoid statistical errors.
|
||||
* > But in general if your motor spins, you have a good pole pairs number.
|
||||
*/
|
||||
#include <SimpleFOC.h>
|
||||
|
||||
// BLDC motor instance
|
||||
// its important to put pole pairs number as 1!!!
|
||||
BLDCMotor motor = BLDCMotor(1);
|
||||
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);
|
||||
// Stepper motor instance
|
||||
// its important to put pole pairs number as 1!!!
|
||||
//StepperMotor motor = StepperMotor(1);
|
||||
//StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6, 8);
|
||||
|
||||
// Encoder(int encA, int encB , int cpr, int index)
|
||||
Encoder encoder = Encoder(2, 3, 2048);
|
||||
// interrupt routine intialisation
|
||||
void doA(){encoder.handleA();}
|
||||
void doB(){encoder.handleB();}
|
||||
|
||||
void setup() {
|
||||
|
||||
// initialise encoder hardware
|
||||
encoder.init();
|
||||
// hardware interrupt enable
|
||||
encoder.enableInterrupts(doA, doB);
|
||||
// link the motor to the sensor
|
||||
motor.linkSensor(&encoder);
|
||||
|
||||
// power supply voltage
|
||||
// default 12V
|
||||
driver.voltage_power_supply = 12;
|
||||
driver.init();
|
||||
motor.linkDriver(&driver);
|
||||
|
||||
|
||||
// initialize motor
|
||||
motor.init();
|
||||
// monitoring port
|
||||
Serial.begin(115200);
|
||||
|
||||
// pole pairs calculation routine
|
||||
Serial.println("Pole pairs (PP) estimator");
|
||||
Serial.println("-\n");
|
||||
|
||||
float pp_search_voltage = 4; // maximum power_supply_voltage/2
|
||||
float pp_search_angle = 6*_PI; // search electrical angle to turn
|
||||
|
||||
// move motor to the electrical angle 0
|
||||
motor.controller = MotionControlType::angle_openloop;
|
||||
motor.voltage_limit=pp_search_voltage;
|
||||
motor.move(0);
|
||||
_delay(1000);
|
||||
// read the encoder angle
|
||||
encoder.update();
|
||||
float angle_begin = encoder.getAngle();
|
||||
_delay(50);
|
||||
|
||||
// move the motor slowly to the electrical angle pp_search_angle
|
||||
float motor_angle = 0;
|
||||
while(motor_angle <= pp_search_angle){
|
||||
motor_angle += 0.01f;
|
||||
motor.move(motor_angle);
|
||||
_delay(1);
|
||||
}
|
||||
_delay(1000);
|
||||
// read the encoder value for 180
|
||||
encoder.update();
|
||||
float angle_end = encoder.getAngle();
|
||||
_delay(50);
|
||||
// turn off the motor
|
||||
motor.move(0);
|
||||
_delay(1000);
|
||||
|
||||
// calculate the pole pair number
|
||||
int pp = round((pp_search_angle)/(angle_end-angle_begin));
|
||||
|
||||
Serial.print(F("Estimated PP : "));
|
||||
Serial.println(pp);
|
||||
Serial.println(F("PP = Electrical angle / Encoder angle "));
|
||||
Serial.print(pp_search_angle*180/_PI);
|
||||
Serial.print("/");
|
||||
Serial.print((angle_end-angle_begin)*180/_PI);
|
||||
Serial.print(" = ");
|
||||
Serial.println((pp_search_angle)/(angle_end-angle_begin));
|
||||
Serial.println();
|
||||
|
||||
|
||||
// a bit of monitoring the result
|
||||
if(pp <= 0 ){
|
||||
Serial.println(F("PP number cannot be negative"));
|
||||
Serial.println(F(" - Try changing the search_voltage value or motor/encoder configuration."));
|
||||
return;
|
||||
}else if(pp > 30){
|
||||
Serial.println(F("PP number very high, possible error."));
|
||||
}else{
|
||||
Serial.println(F("If PP is estimated well your motor should turn now!"));
|
||||
Serial.println(F(" - If it is not moving try to relaunch the program!"));
|
||||
Serial.println(F(" - You can also try to adjust the target voltage using serial terminal!"));
|
||||
}
|
||||
|
||||
|
||||
// set FOC loop to be used
|
||||
motor.controller = MotionControlType::torque;
|
||||
// set the pole pair number to the motor
|
||||
motor.pole_pairs = pp;
|
||||
//align encoder and start FOC
|
||||
motor.initFOC();
|
||||
_delay(1000);
|
||||
|
||||
Serial.println(F("\n Motor ready."));
|
||||
Serial.println(F("Set the target voltage using serial terminal:"));
|
||||
}
|
||||
|
||||
// uq voltage
|
||||
float target_voltage = 2;
|
||||
|
||||
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_voltage);
|
||||
|
||||
// communicate with the user
|
||||
serialReceiveUserCommand();
|
||||
}
|
||||
|
||||
|
||||
// utility function enabling serial communication with the user to set the target values
|
||||
// this function can be implemented in serialEvent function as well
|
||||
void serialReceiveUserCommand() {
|
||||
|
||||
// a string to hold incoming data
|
||||
static String received_chars;
|
||||
|
||||
while (Serial.available()) {
|
||||
// get the new byte:
|
||||
char inChar = (char)Serial.read();
|
||||
// add it to the string buffer:
|
||||
received_chars += inChar;
|
||||
// end of user input
|
||||
if (inChar == '\n') {
|
||||
|
||||
// change the motor target
|
||||
target_voltage = received_chars.toFloat();
|
||||
Serial.print("Target voltage: ");
|
||||
Serial.println(target_voltage);
|
||||
|
||||
// reset the command buffer
|
||||
received_chars = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
/**
|
||||
* Utility arduino sketch which finds pole pair number of the motor
|
||||
*
|
||||
* To run it just set the correct pin numbers for the BLDC driver and sensor CPR value and chip select pin.
|
||||
*
|
||||
* The program will rotate your motor a specific amount and check how much it moved, and by doing a simple calculation calculate your pole pair number.
|
||||
* The pole pair number will be outputted to the serial terminal.
|
||||
*
|
||||
* If the pole pair number is well estimated your motor will start to spin in voltage mode with 2V target.
|
||||
*
|
||||
* If the code calculates negative pole pair number please invert your motor connector.
|
||||
*
|
||||
* Try running this code several times to avoid statistical errors.
|
||||
* > But in general if your motor spins, you have a good pole pairs number.
|
||||
*/
|
||||
#include <SimpleFOC.h>
|
||||
|
||||
// BLDC motor instance
|
||||
// its important to put pole pairs number as 1!!!
|
||||
BLDCMotor motor = BLDCMotor(1);
|
||||
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);
|
||||
// Stepper motor instance
|
||||
// its important to put pole pairs number as 1!!!
|
||||
//StepperMotor motor = StepperMotor(1);
|
||||
//StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6, 8);
|
||||
|
||||
// magnetic sensor instance - SPI
|
||||
MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
|
||||
// magnetic sensor instance - I2C
|
||||
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0X0C, 4);
|
||||
// magnetic sensor instance - analog output
|
||||
// MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
|
||||
|
||||
void setup() {
|
||||
|
||||
// initialise magnetic sensor hardware
|
||||
sensor.init();
|
||||
// link the motor to the sensor
|
||||
motor.linkSensor(&sensor);
|
||||
|
||||
// power supply voltage
|
||||
// default 12V
|
||||
driver.voltage_power_supply = 12;
|
||||
driver.init();
|
||||
motor.linkDriver(&driver);
|
||||
|
||||
// initialize motor hardware
|
||||
motor.init();
|
||||
|
||||
// monitoring port
|
||||
Serial.begin(115200);
|
||||
|
||||
// pole pairs calculation routine
|
||||
Serial.println("Pole pairs (PP) estimator");
|
||||
Serial.println("-\n");
|
||||
|
||||
float pp_search_voltage = 4; // maximum power_supply_voltage/2
|
||||
float pp_search_angle = 6*_PI; // search electrical angle to turn
|
||||
|
||||
// move motor to the electrical angle 0
|
||||
motor.controller = MotionControlType::angle_openloop;
|
||||
motor.voltage_limit=pp_search_voltage;
|
||||
motor.move(0);
|
||||
_delay(1000);
|
||||
// read the sensor angle
|
||||
sensor.update();
|
||||
float angle_begin = sensor.getAngle();
|
||||
_delay(50);
|
||||
|
||||
// move the motor slowly to the electrical angle pp_search_angle
|
||||
float motor_angle = 0;
|
||||
while(motor_angle <= pp_search_angle){
|
||||
motor_angle += 0.01f;
|
||||
sensor.update(); // keep track of the overflow
|
||||
motor.move(motor_angle);
|
||||
_delay(1);
|
||||
}
|
||||
_delay(1000);
|
||||
// read the sensor value for 180
|
||||
sensor.update();
|
||||
float angle_end = sensor.getAngle();
|
||||
_delay(50);
|
||||
// turn off the motor
|
||||
motor.move(0);
|
||||
_delay(1000);
|
||||
|
||||
// calculate the pole pair number
|
||||
int pp = round((pp_search_angle)/(angle_end-angle_begin));
|
||||
|
||||
Serial.print(F("Estimated PP : "));
|
||||
Serial.println(pp);
|
||||
Serial.println(F("PP = Electrical angle / Encoder angle "));
|
||||
Serial.print(pp_search_angle*180/_PI);
|
||||
Serial.print(F("/"));
|
||||
Serial.print((angle_end-angle_begin)*180/_PI);
|
||||
Serial.print(F(" = "));
|
||||
Serial.println((pp_search_angle)/(angle_end-angle_begin));
|
||||
Serial.println();
|
||||
|
||||
|
||||
// a bit of monitoring the result
|
||||
if(pp <= 0 ){
|
||||
Serial.println(F("PP number cannot be negative"));
|
||||
Serial.println(F(" - Try changing the search_voltage value or motor/sensor configuration."));
|
||||
return;
|
||||
}else if(pp > 30){
|
||||
Serial.println(F("PP number very high, possible error."));
|
||||
}else{
|
||||
Serial.println(F("If PP is estimated well your motor should turn now!"));
|
||||
Serial.println(F(" - If it is not moving try to relaunch the program!"));
|
||||
Serial.println(F(" - You can also try to adjust the target voltage using serial terminal!"));
|
||||
}
|
||||
|
||||
|
||||
// set motion control loop to be used
|
||||
motor.controller = MotionControlType::torque;
|
||||
// set the pole pair number to the motor
|
||||
motor.pole_pairs = pp;
|
||||
//align sensor and start FOC
|
||||
motor.initFOC();
|
||||
_delay(1000);
|
||||
|
||||
Serial.println(F("\n Motor ready."));
|
||||
Serial.println(F("Set the target voltage using serial terminal:"));
|
||||
}
|
||||
|
||||
// uq voltage
|
||||
float target_voltage = 2;
|
||||
|
||||
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_voltage);
|
||||
|
||||
// communicate with the user
|
||||
serialReceiveUserCommand();
|
||||
}
|
||||
|
||||
|
||||
// utility function enabling serial communication with the user to set the target values
|
||||
// this function can be implemented in serialEvent function as well
|
||||
void serialReceiveUserCommand() {
|
||||
|
||||
// a string to hold incoming data
|
||||
static String received_chars;
|
||||
|
||||
while (Serial.available()) {
|
||||
// get the new byte:
|
||||
char inChar = (char)Serial.read();
|
||||
// add it to the string buffer:
|
||||
received_chars += inChar;
|
||||
// end of user input
|
||||
if (inChar == '\n') {
|
||||
|
||||
// change the motor target
|
||||
target_voltage = received_chars.toFloat();
|
||||
Serial.print("Target voltage: ");
|
||||
Serial.println(target_voltage);
|
||||
|
||||
// reset the command buffer
|
||||
received_chars = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* Simple example intended to help users find the zero offset and natural direction of the sensor.
|
||||
*
|
||||
* These values can further be used to avoid motor and sensor alignment procedure.
|
||||
* To use these values add them to the code:");
|
||||
* motor.sensor_direction=Direction::CW; // or Direction::CCW
|
||||
* motor.zero_electric_angle=1.2345; // use the real value!
|
||||
*
|
||||
* This will only work for abosolute value sensors - magnetic sensors.
|
||||
* Bypassing the alignment procedure is not possible for the encoders and for the current implementation of the Hall sensors.
|
||||
* library version 1.4.2.
|
||||
*
|
||||
*/
|
||||
#include <SimpleFOC.h>
|
||||
|
||||
// magnetic sensor instance - SPI
|
||||
//MagneticSensorSPI sensor = MagneticSensorSPI(10, 14, 0x3FFF);
|
||||
// magnetic sensor instance - I2C
|
||||
//MagneticSensorI2C sensor = MagneticSensorI2C(0x36, 12, 0X0C, 4);
|
||||
// magnetic sensor instance - analog output
|
||||
MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
|
||||
|
||||
// BLDC motor instance
|
||||
BLDCMotor motor = BLDCMotor(11);
|
||||
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);
|
||||
// Stepper motor instance
|
||||
//StepperMotor motor = StepperMotor(50);
|
||||
//StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6, 8);
|
||||
|
||||
void setup() {
|
||||
|
||||
// power supply voltage
|
||||
driver.voltage_power_supply = 12;
|
||||
driver.init();
|
||||
motor.linkDriver(&driver);
|
||||
|
||||
// initialise magnetic sensor hardware
|
||||
sensor.init();
|
||||
// link the motor to the sensor
|
||||
motor.linkSensor(&sensor);
|
||||
|
||||
// aligning voltage
|
||||
motor.voltage_sensor_align = 7;
|
||||
|
||||
// set motion control loop to be used
|
||||
motor.controller = MotionControlType::torque;
|
||||
|
||||
// force direction search - because default is CW
|
||||
motor.sensor_direction = Direction::UNKNOWN;
|
||||
|
||||
// initialize motor
|
||||
motor.init();
|
||||
// align sensor and start FOC
|
||||
motor.initFOC();
|
||||
|
||||
|
||||
Serial.begin(115200);
|
||||
Serial.println("Sensor zero offset is:");
|
||||
Serial.println(motor.zero_electric_angle, 4);
|
||||
Serial.println("Sensor natural direction is: ");
|
||||
Serial.println(motor.sensor_direction == Direction::CW ? "Direction::CW" : "Direction::CCW");
|
||||
|
||||
Serial.println("To use these values add them to the code:");
|
||||
Serial.print(" motor.sensor_direction=");
|
||||
Serial.print(motor.sensor_direction == Direction::CW ? "Direction::CW" : "Direction::CCW");
|
||||
Serial.println(";");
|
||||
Serial.print(" motor.zero_electric_angle=");
|
||||
Serial.print(motor.zero_electric_angle, 4);
|
||||
Serial.println(";");
|
||||
|
||||
_delay(1000);
|
||||
Serial.println("If motor is not moving the alignment procedure was not successfull!!");
|
||||
}
|
||||
|
||||
|
||||
void loop() {
|
||||
|
||||
// main FOC algorithm function
|
||||
motor.loopFOC();
|
||||
|
||||
// Motion control function
|
||||
motor.move(2);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user