Skip to content

Instantly share code, notes, and snippets.

@thinkier
Last active January 24, 2025 11:23
Show Gist options
  • Save thinkier/385b080ec6a8fb88eda25767a95ad5e3 to your computer and use it in GitHub Desktop.
Save thinkier/385b080ec6a8fb88eda25767a95ad5e3 to your computer and use it in GitHub Desktop.
Test script for the steppers of BTT SKR Pico. Derived from janelia-arduino/TMC2209's example of the same name.
#include <TMC2209.h>
// This example will not work on Arduino boards without HardwareSerial ports,
// such as the Uno, Nano, and Mini.
//
// See this reference for more details:
// https://www.arduino.cc/reference/en/language/functions/communication/serial/
HardwareSerial & serial_stream = Serial2;
const long SERIAL_BAUD_RATE = 115200;
const int DELAY = 200;
// current values may need to be reduced to prevent overheating depending on
// specific motor and power supply voltage
const uint8_t RUN_CURRENT_PERCENT = 100;
const int32_t VELOCITY = 2000;
const uint8_t STALL_GUARD_THRESHOLD = 50;
// X Z Y E
const uint8_t STEPPER_ENA[4] = { 12, 2, 7, 15 };
const uint8_t STEPPER_STP[4] = { 11, 19, 6, 14 };
const uint8_t STEPPER_DIR[4] = { 10, 28, 5, 13 };
const uint8_t STEPPER_INT[4] = { 4, 25, 3, 16 };
const TMC2209::SerialAddress STEPPER_ADDR[4] = { TMC2209::SERIAL_ADDRESS_0, TMC2209::SERIAL_ADDRESS_1, TMC2209::SERIAL_ADDRESS_2, TMC2209::SERIAL_ADDRESS_3 };
// Instantiate TMC2209
TMC2209 stepper_driver;
void setup()
{
uint8_t channel = 0;
pinMode(STEPPER_ENA[channel], OUTPUT);
Serial.begin(SERIAL_BAUD_RATE);
stepper_driver.setup(serial_stream, SERIAL_BAUD_RATE, STEPPER_ADDR[channel]);
stepper_driver.setRunCurrent(RUN_CURRENT_PERCENT);
stepper_driver.setStallGuardThreshold(STALL_GUARD_THRESHOLD);
stepper_driver.setMicrostepsPerStepPowerOfTwo(1);
pinMode(STEPPER_STP[channel], OUTPUT);
digitalWrite(STEPPER_ENA[channel], 0);
analogWriteFreq(2000);
analogWrite(STEPPER_STP[channel], 127);
}
void loop()
{
if (not stepper_driver.isSetupAndCommunicating())
{
Serial.println("Stepper driver not setup and communicating!");
return;
}
Serial.print("run_current_percent = ");
Serial.println(RUN_CURRENT_PERCENT);
Serial.print("stall_guard_threshold = ");
Serial.println(STALL_GUARD_THRESHOLD);
uint16_t stall_guard_result = stepper_driver.getStallGuardResult();
Serial.print("half_stall_guard_result = ");
Serial.println(stall_guard_result / 2);
Serial.println();
delay(DELAY);
}
@thinkier
Copy link
Author

The run current is all messed up... this is effectively useless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment