Advertisement
microrobotics

SparkFun Brushless Motor Driver - 3-Phase (TMC6300)

Jun 5th, 2023
1,573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. We will initialize the TMC6300 motor driver with the sensorless operation feature, and use stallGuard for stall detection. We will also enable features like spreadCycle, stealthChop, and coolStep, and configure some related parameters.
  3.  
  4. In this example, the driver is set up in sensorless mode. It checks for stalls every loop, and if it detects a stall, it will stop the motor, wait for a second, and then try to restart the motor.
  5.  
  6. Remember that the parameters used here are just examples and you should adjust them to suit your motor's needs. Always check the datasheets and technical documents for your specific motor and driver to make sure you're setting safe and appropriate values.
  7.  
  8. You can find the documentation for the TMC6300 library here: https://www.trinamic.com/fileadmin/assets/Products/ICs_Documents/TMC6300_Datasheet_V100.pdf
  9.  
  10. This code was written for the Arduino platform. If you're using a different platform or microcontroller, you may need to adjust the code to suit your needs. Always remember to be cautious when working with motors, as they can draw high currents that could damage your driver, motor, or microcontroller.
  11.  
  12. This code assumes the library for TMC6300 from Trinamic is installed, which contains all the necessary methods and definitions to communicate with the driver and control the motor. It's also assumed you're using the SPI interface.
  13. */
  14.  
  15. #include <SPI.h>
  16. #include <TMC6300.h>
  17.  
  18. // Define pins
  19. #define PIN_MOSI  11
  20. #define PIN_MISO  12
  21. #define PIN_SCK   13
  22. #define PIN_CS    10
  23.  
  24. // Instantiate a TMC6300 instance
  25. TMC6300Stepper driver(PIN_CS);
  26.  
  27. void setup() {
  28.   // Initialize SPI
  29.   SPI.begin();
  30.   SPI.setClockDivider(SPI_CLOCK_DIV4);
  31.  
  32.   // Initialize driver
  33.   driver.begin();
  34.  
  35.   // Enable and configure sensorless operation
  36.   driver.S2VSA(3);  // Set sensorless amplifier voltage gain (1-3)
  37.   driver.S2VSB(3);  // Set sensorless blanking time (1-3)
  38.  
  39.   // Set stall detection parameters
  40.   driver.sgFilter(1);   // StallGuard filter enable
  41.   driver.sgThreshold(5); // StallGuard threshold
  42.  
  43.   // Enable features
  44.   driver.stealthChop(1); // Enable stealthChop
  45.   driver.spreadCycle(1); // Enable spreadCycle
  46.  
  47.   // Configure coolStep (assumes the driver is in coolStep mode)
  48.   driver.lowerSgThreshold(1); // Lower StallGuard threshold for coolStep
  49.   driver.SGTHRS(1); // Set coolStep threshold
  50.   driver.semin(5); // Minimum coolStep current
  51.   driver.semax(2); // Maximum coolStep current
  52.   driver.sedn(0b01); // coolStep current decrement speed
  53.   driver.seup(0b01); // coolStep current increment speed
  54.  
  55.   // Set the motor to run at 1000 RPM
  56.   driver.VACTUAL(1000);
  57. }
  58.  
  59. void loop() {
  60.   // Check for a stall
  61.   if (driver.sgResult() < driver.sgThreshold()) {
  62.     Serial.println("Motor stall detected!");
  63.     driver.VACTUAL(0); // Stop the motor
  64.     delay(1000);
  65.     driver.VACTUAL(1000); // Try to restart the motor
  66.   }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement