Advertisement
zulfiar_am

Velocity Control DRV8302

Mar 4th, 2023
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. #include <SimpleFOC.h>
  2. #include <PciManager.h>
  3. #include <PciListenerImp.h>
  4. // DRV8302 pins connections
  5. // don't forget to connect the common ground pin
  6. #define EN_GATE 53
  7. #define HAL_U 2
  8. #define HAL_V 3
  9. #define HAL_W 4
  10. #define M_PWM 5
  11. #define M_OC 6
  12. #define OC_ADJ 7
  13. #define HA 8
  14. #define HB 9
  15. #define HC 10
  16. #define LA 11
  17. #define LB 12
  18. #define LC 13
  19. #define ENC_A 50
  20. #define ENC_B 52
  21.  
  22.  
  23. // Motor instance
  24. BLDCMotor motor = BLDCMotor(4);
  25. BLDCDriver6PWM driver = BLDCDriver6PWM(HA, LA, HB, LB, HC, LC, EN_GATE);
  26. // encoder instance
  27. Encoder encoder = Encoder(ENC_A, ENC_B, 1000, A0);
  28. void doA(){encoder.handleA();}
  29. void doB(){encoder.handleB();}
  30. void doIndex(){encoder.handleIndex();}
  31. // If no available hadware interrupt pins use the software interrupt
  32. PciListener ImplistenerIndex(encoder.index_pin, doIndex);
  33.  
  34.  
  35. // velocity set point variable
  36. float target_velocity = 0;
  37. // instantiate the commander
  38. Commander command = Commander(Serial);
  39. void doTarget(char* cmd) { command.scalar(&target_velocity, cmd); }
  40.  
  41. void setup() {
  42. // put your setup code here, to run once:
  43. // initialize encoder sensor hardware
  44. encoder.init();
  45. encoder.enableInterrupts(doA, doB);
  46. // software interrupts
  47. PciManager.registerListener(&listenerIndex);
  48. // link the motor to the sensor
  49. motor.linkSensor(&encoder);
  50.  
  51. // driver config
  52. // power supply voltage [V]
  53. driver.voltage_power_supply = 12;
  54. driver.init();
  55. // link the motor and the driver
  56. motor.linkDriver(&driver);
  57.  
  58. // DRV8302 specific code
  59. // M_OC - enable overcurrent protection
  60. pinMode(M_OC,OUTPUT);
  61. digitalWrite(M_OC,LOW);
  62. // M_PWM - disable 3pwm mode
  63. pinMode(M_PWM,OUTPUT);
  64. digitalWrite(M_PWM, LOW);
  65. // OD_ADJ - set the maximum overcurrent limit possible
  66. // Better option would be to use voltage divisor to set exact value
  67. pinMode(OC_ADJ,OUTPUT);
  68. digitalWrite(OC_ADJ,HIGH);
  69.  
  70. // aligning voltage [V]
  71. motor.voltage_sensor_align = 3;
  72. // index search velocity [rad/s]
  73. motor.velocity_index_search = 3;
  74.  
  75. // set motion control loop to be used
  76. motor.controller = MotionControlType::velocity;
  77.  
  78. // contoller configuration
  79. // default parameters in defaults.h
  80.  
  81. // velocity PI controller parameters
  82. motor.PID_velocity.P = 0.2f;
  83. motor.PID_velocity.I = 20;
  84. motor.PID_velocity.D = 0;
  85. // default voltage_power_supply
  86. motor.voltage_limit = 12;
  87. // jerk control using voltage voltage ramp
  88. // default value is 300 volts per sec ~ 0.3V per millisecond
  89. motor.PID_velocity.output_ramp = 1000;
  90.  
  91. // velocity low pass filtering time constant
  92. motor.LPF_velocity.Tf = 0.01f;
  93.  
  94. // use monitoring with serial
  95. Serial.begin(115200);
  96. // comment out if not needed
  97. motor.useMonitoring(Serial);
  98.  
  99. // initialize motor
  100. motor.init();
  101. // align sensor and start FOC
  102. motor.initFOC();
  103.  
  104. // add target command T
  105. command.add('T', doTarget, "target velocity");
  106.  
  107. Serial.println(F("Motor ready."));
  108. Serial.println(F("Set the target velocity using serial terminal:"));
  109. _delay(1000);
  110.  
  111.  
  112. }
  113.  
  114. void loop() {
  115. // put your main code here, to run repeatedly:
  116. // main FOC algorithm function
  117. // the faster you run this function the better
  118. // Arduino UNO loop ~1kHz
  119. // Bluepill loop ~10kHz
  120. motor.loopFOC();
  121.  
  122. // Motion control function
  123. // velocity, position or voltage (defined in motor.controller)
  124. // this function can be run at much lower frequency than loopFOC() function
  125. // You can also use motor.move() and set the motor.target in the code
  126. motor.move(target_velocity);
  127.  
  128. // function intended to be used with serial plotter to monitor motor variables
  129. // significantly slowing the execution down!!!!
  130. // motor.monitor();
  131.  
  132. // user communication
  133. command.run();
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement