Guest User

Untitled

a guest
Dec 4th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. /**
  2. *
  3. * Torque control example using voltage control loop.
  4. *
  5. * Most of the low-end BLDC driver boards doesn't have current measurement therefore SimpleFOC offers
  6. * you a way to control motor torque by setting the voltage to the motor instead of the current.
  7. *
  8. * This makes the BLDC motor effectively a DC motor, and you can use it in a same way.
  9. */
  10. #define polepairs 11
  11. #define seconds_to_ramp
  12.  
  13. #include <Arduino.h>
  14. #include <SimpleFOC.h>
  15. #include <SimpleFOCDrivers.h>
  16. #include <encoders/smoothing/SmoothingSensor.h>
  17. BLDCMotor motor = BLDCMotor(polepairs);
  18. BLDCDriver6PWM driver = BLDCDriver6PWM(PA8, PB13, PA9, PB14, PA10, PB15);
  19. // hall sensor instance
  20. HallSensor sensor = HallSensor(PC6, PB11, PA3, polepairs);
  21. SmoothingSensor smooth(sensor, motor);
  22.  
  23. // Interrupt routine intialisation
  24. // channel A and B callbacks
  25. void doA(){sensor.handleA();}
  26. void doB(){sensor.handleB();}
  27. void doC(){sensor.handleC();}
  28.  
  29.  
  30. // voltage set point variable
  31. float target_voltage = 17;
  32.  
  33. void setup() {
  34. pinMode(PC6, INPUT_PULLUP); // LED_BUILTIN
  35. pinMode(PB11, INPUT_PULLUP);
  36. pinMode(PA3, INPUT_PULLUP);
  37. smooth.phase_correction = -_PI_6;
  38. Serial.begin(115200);
  39. // enable more verbose output for debugging
  40. // comment out if not needed
  41. // SimpleFOCDebug::enable(&Serial);
  42.  
  43. // initialize encoder sensor hardware
  44. sensor.init();
  45. sensor.enableInterrupts(doA, doB, doC);
  46. // link the motor to the sensor
  47. motor.linkSensor(&smooth);
  48.  
  49. // driver config
  50. // power supply voltage [V]
  51. driver.voltage_power_supply = 24;
  52. driver.init();
  53. // link driver
  54. motor.linkDriver(&driver);
  55.  
  56. // aligning voltage
  57. motor.voltage_sensor_align = 5;
  58.  
  59. // choose FOC modulation (optional)
  60. motor.foc_modulation = FOCModulationType::SinePWM;
  61.  
  62. // set motion control loop to be used
  63. motor.controller = MotionControlType::torque;
  64. // initialize motor
  65. motor.init();
  66. // align sensor and start FOC
  67. motor.initFOC();
  68. for (float i = 0.0f; i < 600000.0f; i++){// assume 33 khz is what motor.loopfoc runs at if looped so 0.03 msec per loop so 600,000 should be 18 sec
  69. motor.loopFOC();
  70. motor.move(i*target_voltage/600000);
  71. }
  72. }
  73.  
  74.  
  75. void loop() {
  76.  
  77. // main FOC algorithm function
  78. // the faster you run this function the better
  79. // Arduino UNO loop ~1kHz
  80. // Bluepill loop ~10kHz
  81. motor.loopFOC();
  82.  
  83. // Motion control function
  84. // velocity, position or voltage (defined in motor.controller)
  85. // this function can be run at much lower frequency than loopFOC() function
  86. // You can also use motor.move() and set the motor.target in the code
  87. motor.move(target_voltage);
  88. }
Advertisement
Add Comment
Please, Sign In to add comment