|
| 1 | +/** |
| 2 | + * Comprehensive BLDC motor control example using magnetic sensor MT6816 |
| 3 | + * |
| 4 | + * Using serial terminal user can send motor commands and configure the motor and FOC in real-time: |
| 5 | + * - configure PID controller constants |
| 6 | + * - change motion control loops |
| 7 | + * - monitor motor variabels |
| 8 | + * - set target values |
| 9 | + * - check all the configuration values |
| 10 | + * |
| 11 | + * See more info in docs.simplefoc.com/commander_interface |
| 12 | + */ |
| 13 | +#include <SimpleFOC.h> |
| 14 | +#include <SimpleFOCDrivers.h> |
| 15 | +#include <encoders/mt6816/MagneticSensorMT6816.h> |
| 16 | + |
| 17 | +// magnetic sensor instance - MT6816 SPI mode |
| 18 | +MagneticSensorMT6816 sensor = MagneticSensorMT6816(5); |
| 19 | + |
| 20 | + |
| 21 | +// BLDC motor & driver instance |
| 22 | +BLDCMotor motor = BLDCMotor(7); |
| 23 | +BLDCDriver3PWM driver = BLDCDriver3PWM(32, 25, 26, 33); |
| 24 | + |
| 25 | +// Inline Current Sense instance |
| 26 | +InlineCurrentSense current_sense = InlineCurrentSense(0.01, 50.0, 35, 34); |
| 27 | + |
| 28 | +// commander interface |
| 29 | +Commander command = Commander(Serial); |
| 30 | +void onMotor(char* cmd){ command.motor(&motor, cmd); } |
| 31 | + |
| 32 | +void setup() { |
| 33 | + |
| 34 | + // initialise magnetic sensor hardware |
| 35 | + sensor.init(); |
| 36 | + // link the motor to the sensor |
| 37 | + motor.linkSensor(&sensor); |
| 38 | + |
| 39 | + // driver config |
| 40 | + // power supply voltage [V] |
| 41 | + driver.voltage_power_supply = 12; |
| 42 | + driver.init(); |
| 43 | + // link driver |
| 44 | + motor.linkDriver(&driver); |
| 45 | + |
| 46 | + // choose FOC modulation |
| 47 | + motor.foc_modulation = FOCModulationType::SpaceVectorPWM; |
| 48 | + |
| 49 | + // set control loop type to be used |
| 50 | + motor.controller = MotionControlType::torque; |
| 51 | + |
| 52 | + // contoller configuration based on the control type |
| 53 | + motor.PID_velocity.P = 0.2f; |
| 54 | + motor.PID_velocity.I = 20; |
| 55 | + motor.PID_velocity.D = 0; |
| 56 | + // default voltage_power_supply |
| 57 | + motor.voltage_limit = 12; |
| 58 | + |
| 59 | + // velocity low pass filtering time constant |
| 60 | + motor.LPF_velocity.Tf = 0.01f; |
| 61 | + |
| 62 | + // angle loop velocity limit |
| 63 | + motor.velocity_limit = 50; |
| 64 | + |
| 65 | + // use monitoring with serial for motor init |
| 66 | + // monitoring port |
| 67 | + Serial.begin(115200); |
| 68 | + // comment out if not needed |
| 69 | + motor.useMonitoring(Serial); |
| 70 | + |
| 71 | + current_sense.linkDriver(&driver); |
| 72 | + current_sense.init(); |
| 73 | + current_sense.gain_b *= -1; |
| 74 | + current_sense.skip_align = true; |
| 75 | + motor.linkCurrentSense(¤t_sense); |
| 76 | + |
| 77 | + // initialise motor |
| 78 | + motor.init(); |
| 79 | + // align encoder and start FOC |
| 80 | + motor.initFOC(); |
| 81 | + |
| 82 | + // set the inital target value |
| 83 | + motor.target = 2; |
| 84 | + |
| 85 | + // define the motor id |
| 86 | + command.add('A', onMotor, "motor"); |
| 87 | + |
| 88 | + // Run user commands to configure and the motor (find the full command list in docs.simplefoc.com) |
| 89 | + Serial.println(F("Motor commands sketch | Initial motion control > torque/voltage : target 2V.")); |
| 90 | + |
| 91 | + _delay(1000); |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +void loop() { |
| 96 | + // iterative setting FOC phase voltage |
| 97 | + motor.loopFOC(); |
| 98 | + |
| 99 | + // iterative function setting the outter loop target |
| 100 | + // velocity, position or voltage |
| 101 | + // if tatget not set in parameter uses motor.target variable |
| 102 | + motor.move(); |
| 103 | + |
| 104 | + // user communication |
| 105 | + command.run(); |
| 106 | +} |
0 commit comments