178 lines
5.8 KiB
C++
178 lines
5.8 KiB
C++
#include <TMCStepper.h>
|
|
|
|
#define EN_PIN 7 // Enable
|
|
#define DIR_PIN 9 // Direction
|
|
#define STEP_PIN 8 // Step
|
|
#define DIAG_PIN 10 // Diagnostic
|
|
#define CS_PIN 42 // Chip select
|
|
#define SW_MOSI 66 // Software Master Out Slave In (MOSI)
|
|
#define SW_MISO 44 // Software Master In Slave Out (MISO)
|
|
#define SW_SCK 64 // Software Slave Clock (SCK)
|
|
#define SW_RX 5 // TMC2208/TMC2224 SoftwareSerial receive pin
|
|
#define SW_TX 5 // TMC2208/TMC2224 SoftwareSerial transmit pin
|
|
#define SERIAL_PORT Serial1 // TMC2208/TMC2224 HardwareSerial port
|
|
#define DRIVER_ADDRESS 0b00 // TMC2209 Driver address according to MS1 and MS2
|
|
|
|
#define R_SENSE 0.11f // Match to your driver
|
|
// SilentStepStick series use 0.11
|
|
// UltiMachine Einsy and Archim2 boards use 0.2
|
|
// Panucatt BSD2660 uses 0.1
|
|
// Watterott TMC5160 uses 0.075
|
|
|
|
// Select your stepper driver type
|
|
//TMC2130Stepper driver(CS_PIN, R_SENSE); // Hardware SPI
|
|
//TMC2130Stepper driver(CS_PIN, R_SENSE, SW_MOSI, SW_MISO, SW_SCK); // Software SPI
|
|
//TMC2660Stepper driver(CS_PIN, R_SENSE); // Hardware SPI
|
|
//TMC2660Stepper driver(CS_PIN, R_SENSE, SW_MOSI, SW_MISO, SW_SCK);
|
|
//TMC5160Stepper driver(CS_PIN, R_SENSE);
|
|
//TMC5160Stepper driver(CS_PIN, R_SENSE, SW_MOSI, SW_MISO, SW_SCK);
|
|
|
|
// TMC2208Stepper driver(&SERIAL_PORT, R_SENSE); // Hardware Serial
|
|
//TMC2208Stepper driver(SW_RX, SW_TX, R_SENSE); // Software serial
|
|
//TMC2209Stepper driver(&SERIAL_PORT, R_SENSE, DRIVER_ADDRESS);
|
|
TMC2209Stepper driver(SW_RX, SW_TX, R_SENSE, DRIVER_ADDRESS);
|
|
|
|
#define MICROSTEPS 16
|
|
#define STEPS_PER_REV 200 * MICROSTEPS
|
|
|
|
void setup() {
|
|
pinMode(EN_PIN, OUTPUT);
|
|
pinMode(STEP_PIN, OUTPUT);
|
|
pinMode(DIR_PIN, OUTPUT);
|
|
pinMode(DIAG_PIN, INPUT);
|
|
digitalWrite(EN_PIN, LOW);
|
|
|
|
Serial.begin(9600);
|
|
Serial.println("Start...");
|
|
|
|
// Enable one according to your setup
|
|
//SPI.begin(); // SPI drivers
|
|
//SERIAL_PORT.begin(115200); // HW UART drivers
|
|
// driver.beginSerial(115200); // SW UART drivers
|
|
|
|
driver.begin(); // SPI: Init CS pins and possible SW SPI pins
|
|
// UART: Init SW UART (if selected) with default 115200 baudrate
|
|
|
|
driver.push();
|
|
driver.rms_current(300); // Set motor RMS current
|
|
driver.microsteps(MICROSTEPS); // Set microsteps to 1/16th
|
|
// driver.irun(10);
|
|
// driver.ihold(10);
|
|
|
|
// driver.en_spreadCycle(true); // Toggle spreadCycle on TMC2208/2209/2224
|
|
// driver.hysteresis_start(8);
|
|
// driver.hysteresis_end(hysteresis_end);
|
|
// driver.blank_time(54);
|
|
// driver.toff(5);
|
|
// driver.freewheel(0b01);
|
|
|
|
// driver.TCOOLTHRS(0xFFFFF); // 20bit max
|
|
// driver.semin(5);
|
|
// driver.semax(2);
|
|
// driver.sedn(0b01);
|
|
// driver.SGTHRS(64);
|
|
driver.toff(4); // Enables driver in software
|
|
|
|
|
|
// driver.en_pwm_mode(true); // Toggle stealthChop on TMC2130/2160/5130/5160
|
|
// driver.pwm_autoscale(true); // Needed for stealthChop
|
|
|
|
// delay(200);
|
|
Serial.print(F("\nTesting connection..."));
|
|
uint8_t result = driver.test_connection();
|
|
if (result) {
|
|
Serial.println(F("failed!"));
|
|
Serial.print(F("Likely cause: "));
|
|
switch(result) {
|
|
case 1: Serial.println(F("loose connection")); break;
|
|
case 2: Serial.println(F("Likely cause: no power")); break;
|
|
}
|
|
Serial.println(F("Fix the problem and reset board."));
|
|
delay(200);
|
|
abort();
|
|
}
|
|
Serial.println(F("OK"));
|
|
}
|
|
|
|
bool shaft = false;
|
|
unsigned int stepsDelay = 28000 / MICROSTEPS;
|
|
bool shouldRun = true;
|
|
|
|
void loop() {
|
|
if (Serial.available()) {
|
|
char c = Serial.read();
|
|
if (c == 'r') {
|
|
shaft = !shaft;
|
|
driver.shaft(shaft);
|
|
Serial.println("Shaft reversed");
|
|
} else if (c == '+') {
|
|
stepsDelay *= 0.8;
|
|
if (stepsDelay < 10) {
|
|
stepsDelay = 10;
|
|
}
|
|
Serial.println("Speed: " + String(stepsDelay));
|
|
} else if (c == '-') {
|
|
stepsDelay *= 1.2;
|
|
Serial.println("Speed: " + String(stepsDelay));
|
|
} else if (c == 's') {
|
|
shouldRun = false;
|
|
Serial.println("Stopped.");
|
|
} else if (c == '0') {
|
|
shouldRun = false;
|
|
digitalWrite(EN_PIN, HIGH);
|
|
Serial.println("Stopped & disabled.");
|
|
} else if (c == 'g') {
|
|
shouldRun = true;
|
|
digitalWrite(EN_PIN, LOW);
|
|
Serial.println("Running.");
|
|
} else if (c == 't') {
|
|
// Serial.print("LOST_STEPS: 0b");
|
|
// Serial.println(driver.LOST_STEPS(), DEC);
|
|
|
|
Serial.print("PWM_SCALE: 0b");
|
|
Serial.println(driver.PWM_SCALE(), DEC);
|
|
|
|
Serial.print("CHOPCONF: 0b");
|
|
Serial.println(driver.CHOPCONF(), BIN);
|
|
}
|
|
// else if (c == 'h') {
|
|
// hysteresis_end = hysteresis_end + 1;
|
|
// if (hysteresis_end > 12) {
|
|
// hysteresis_end = -3;
|
|
// }
|
|
// driver.hysteresis_end(hysteresis_end);
|
|
// Serial.println("Hysteresis end: " + String(hysteresis_end));
|
|
// } else if (c == '8') {
|
|
// while (driver.cur_a() < 240) {
|
|
// digitalWrite(STEP_PIN, HIGH);
|
|
// digitalWrite(STEP_PIN, LOW);
|
|
// delayMicroseconds(3);
|
|
// }
|
|
// }
|
|
}
|
|
|
|
if (shouldRun) {
|
|
for (uint32_t i = 100; i>0; i--) {
|
|
digitalWrite(STEP_PIN, HIGH);
|
|
delayMicroseconds(5);
|
|
digitalWrite(STEP_PIN, LOW);
|
|
delayMicroseconds(stepsDelay);
|
|
|
|
// if (digitalRead(DIAG_PIN) == HIGH) {
|
|
// Serial.println("STALL");
|
|
// }
|
|
}
|
|
|
|
if (driver.SG_RESULT() > 105) {
|
|
shaft = !shaft;
|
|
driver.shaft(shaft);
|
|
}
|
|
|
|
// Serial.println(driver.SG_RESULT(), DEC);
|
|
// Serial.print(" ");
|
|
// Serial.print(digitalRead(DIAG_PIN), DEC);
|
|
// Serial.print(" ");
|
|
// Serial.println(driver.cs2rms(driver.cs_actual()), DEC);
|
|
}
|
|
}
|