|
| 1 | +#include <ArduinoFOC.h> |
| 2 | + |
| 3 | +// Only pins 2 and 3 are supported |
| 4 | +#define arduinoInt1 2 // Arduino UNO interrupt 0 |
| 5 | +#define arduinoInt2 3 // Arduino UNO interrupt 1 |
| 6 | + |
| 7 | +// BLDCMotor( int phA, int phB, int phC, int pp, int en) |
| 8 | +// - phA, phB, phC - motor A,B,C phase pwm pins |
| 9 | +// - pp - pole pair number |
| 10 | +// - enable pin - (optional input) |
| 11 | +BLDCMotor motor = BLDCMotor(9, 10, 11, 11, 8); |
| 12 | +// Encoder(int encA, int encB , int cpr, int index) |
| 13 | +// - encA, encB - encoder A and B pins |
| 14 | +// - ppr - impulses per rotation (cpr=ppr*4) |
| 15 | +// - index pin - (optional input) |
| 16 | +Encoder encoder = Encoder(arduinoInt1, arduinoInt2, 8192, 4); |
| 17 | +// interrupt ruotine intialisation |
| 18 | +void doA(){encoder.handleA();} |
| 19 | +void doB(){encoder.handleB();} |
| 20 | + |
| 21 | +void setup() { |
| 22 | + // debugging port |
| 23 | + Serial.begin(115200); |
| 24 | + |
| 25 | + // check if you need internal pullups |
| 26 | + // Pullup::EXTERN - external pullup added - dafault |
| 27 | + // Pullup::INTERN - needs internal arduino pullup |
| 28 | + encoder.pullup = Pullup::EXTERN; |
| 29 | + |
| 30 | + // initialise encoder hardware |
| 31 | + encoder.init(doA, doB); |
| 32 | + |
| 33 | + // set driver type |
| 34 | + // DriverType::unipolar |
| 35 | + // DriverType::bipolar - default |
| 36 | + motor.driver = DriverType::bipolar; |
| 37 | + |
| 38 | + // power supply voltage |
| 39 | + // default 12V |
| 40 | + motor.power_supply_voltage = 12; |
| 41 | + |
| 42 | + // set FOC loop to be used |
| 43 | + // ControlType::voltage |
| 44 | + // ControlType::velocity |
| 45 | + // ControlType::velocity_ultra_slow |
| 46 | + // ControlType::angle |
| 47 | + motor.controller = ControlType::velocity; |
| 48 | + |
| 49 | + // velocity PI controller parameters |
| 50 | + // default K=1.0 Ti = 0.003 |
| 51 | + motor.PI_velocity.K = 1; |
| 52 | + motor.PI_velocity.Ti = 0.003; |
| 53 | + |
| 54 | + // link the motor to the sensor |
| 55 | + motor.linkEncoder(&encoder); |
| 56 | + // intialise motor |
| 57 | + motor.init(); |
| 58 | + // align encoder and start FOC |
| 59 | + motor.initFOC(); |
| 60 | + |
| 61 | + |
| 62 | + Serial.println("Motor ready."); |
| 63 | + delay(1000); |
| 64 | +} |
| 65 | + |
| 66 | +// target velocity variable |
| 67 | +float target_velocity = 3; |
| 68 | +int t = 0; |
| 69 | + |
| 70 | +void loop() { |
| 71 | + // iterative state calculation calculating angle |
| 72 | + // and setting FOC pahse voltage |
| 73 | + // the faster you run this funciton the better |
| 74 | + // in arduino loop it should have ~1kHz |
| 75 | + // the best would be to be in ~10kHz range |
| 76 | + motor.loopFOC(); |
| 77 | + |
| 78 | + // direction chnaging logic |
| 79 | + // change direction each 1000 loop passes |
| 80 | + target_velocity *= (t >= 1000) ? -1 : 1; |
| 81 | + // loop passes counter |
| 82 | + t = (t > 1000) ? t+1 : 0; |
| 83 | + |
| 84 | + |
| 85 | + // iterative function setting the outter loop target |
| 86 | + // velocity, position or voltage |
| 87 | + // this funciton can be run at much lower frequency than loopFOC funciton |
| 88 | + // it can go as low as ~50Hz |
| 89 | + motor.move(target_velocity); |
| 90 | + |
| 91 | + |
| 92 | + // function intended to be used with serial plotter to monitor motor variables |
| 93 | + // significantly slowing the execution down!!!! |
| 94 | + motor_monitor(); |
| 95 | +} |
| 96 | + |
| 97 | +// utility function intended to be used with serial plotter to monitor motor variables |
| 98 | +// significantly slowing the execution down!!!! |
| 99 | +void motor_monitor() { |
| 100 | + switch (motor.controller) { |
| 101 | + case ControlType::velocity_ultra_slow: |
| 102 | + case ControlType::velocity: |
| 103 | + Serial.print(motor.voltage_q); |
| 104 | + Serial.print("\t"); |
| 105 | + Serial.print(motor.shaft_velocity_sp); |
| 106 | + Serial.print("\t"); |
| 107 | + Serial.println(motor.shaft_velocity); |
| 108 | + break; |
| 109 | + case ControlType::angle: |
| 110 | + Serial.print(motor.voltage_q); |
| 111 | + Serial.print("\t"); |
| 112 | + Serial.print(motor.shaft_angle_sp); |
| 113 | + Serial.print("\t"); |
| 114 | + Serial.println(motor.shaft_angle); |
| 115 | + break; |
| 116 | + case ControlType::voltage: |
| 117 | + Serial.print(motor.voltage_q); |
| 118 | + Serial.print("\t"); |
| 119 | + Serial.println(motor.shaft_velocity); |
| 120 | + break; |
| 121 | + } |
| 122 | +} |
| 123 | + |
0 commit comments