Skip to content

Instantly share code, notes, and snippets.

@telamon
Created February 1, 2018 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telamon/b0cff6a3b548fe468bdedfc92bc4f0ec to your computer and use it in GitHub Desktop.
Save telamon/b0cff6a3b548fe468bdedfc92bc4f0ec to your computer and use it in GitHub Desktop.
TMC2130 driver debugging
/**
* Author Teemu Mäntykallio
* Initializes the library and turns the motor in alternating directions.
*/
#define EN_PIN 48 // Nano v3: 16 Mega: 38 //enable (CFG6)
#define DIR_PIN 16 // 19 55 //direction
#define STEP_PIN 17 // 18 54 //step
#define CS_PIN 31 // 17 64 //chip select
#define MOSI_PIN 51
#define MISO_PIN 50
#define SCK_PIN 52
bool dir = true;
#define TMC2130DEBUG
#include <TMC2130Stepper.h>
// Soft SPI
TMC2130Stepper driver = TMC2130Stepper(EN_PIN, DIR_PIN, STEP_PIN, CS_PIN, MOSI_PIN, MISO_PIN, SCK_PIN);
// Hard SPI
//TMC2130Stepper driver = TMC2130Stepper(EN_PIN, DIR_PIN, STEP_PIN, CS_PIN);
void setup() {
Serial.begin(9600);
while(!Serial);
Serial.println("Start...");
driver.begin(); // Initiate pins and registeries
driver.rms_current(600); // Set stepper current to 600mA. The command is the same as command TMC2130.setCurrent(600, 0.11, 0.5);
driver.microsteps(16);
driver.stealthChop(1); // Enable extremely quiet stepping
digitalWrite(EN_PIN, LOW);
Serial.print("DRV_STATUS=0b");
Serial.println(driver.DRV_STATUS(), BIN);
}
bool isKilled = false;
void loop() {
if(driver.checkOT()){
if(!isKilled){
Serial.print("Overheat flag triggered: ");
Serial.println(driver.getOTPW(),DEC);
driver.TPOWERDOWN();
isKilled = true;
Serial.println("Program stopped");
}else{
delay(1000);
}
}else{
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(10);
uint32_t ms = millis();
static uint32_t last_time = 0;
if ((ms - last_time) > 2000) {
if (dir) {
Serial.println("Dir -> 0");
driver.shaft_dir(0);
} else {
Serial.println("Dir -> 1");
driver.shaft_dir(1);
}
Serial.print("OverTemperature: ");Serial.print(driver.getOTPW(),DEC);
Serial.print(" MSTEP: ");Serial.print(driver.microsteps(),DEC);
Serial.print(" DIR: ");Serial.println(driver.dir(),DEC);
dir = !dir;
last_time = ms;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment