experiment with tmc2209 uart settings
parent
bb8327de4b
commit
bffe353b21
167
src/main.cpp
167
src/main.cpp
|
@ -1,20 +1,14 @@
|
|||
/**
|
||||
* Author Teemu Mäntykallio
|
||||
* Initializes the library and runs the stepper
|
||||
* motor in alternating directions.
|
||||
*/
|
||||
|
||||
#include <TMCStepper.h>
|
||||
#include <AccelStepper.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 6 // TMC2208/TMC2224 SoftwareSerial receive pin
|
||||
#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
|
||||
|
@ -38,16 +32,15 @@
|
|||
//TMC2209Stepper driver(&SERIAL_PORT, R_SENSE, DRIVER_ADDRESS);
|
||||
TMC2209Stepper driver(SW_RX, SW_TX, R_SENSE, DRIVER_ADDRESS);
|
||||
|
||||
AccelStepper accel = AccelStepper(accel.DRIVER, STEP_PIN, DIR_PIN);
|
||||
|
||||
#define MICROSTEPS 256
|
||||
#define MICROSTEPS 16
|
||||
#define STEPS_PER_REV 200 * MICROSTEPS
|
||||
|
||||
void setup() {
|
||||
pinMode(EN_PIN, OUTPUT);
|
||||
pinMode(STEP_PIN, OUTPUT);
|
||||
pinMode(DIR_PIN, OUTPUT);
|
||||
digitalWrite(EN_PIN, LOW); // Enable driver in hardware
|
||||
pinMode(DIAG_PIN, INPUT);
|
||||
digitalWrite(EN_PIN, LOW);
|
||||
|
||||
Serial.begin(9600);
|
||||
Serial.println("Start...");
|
||||
|
@ -55,62 +48,130 @@ void setup() {
|
|||
// Enable one according to your setup
|
||||
//SPI.begin(); // SPI drivers
|
||||
//SERIAL_PORT.begin(115200); // HW UART drivers
|
||||
//driver.beginSerial(115200); // SW 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.toff(4); // Enables driver in software
|
||||
|
||||
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.en_spreadCycle(false); // Toggle spreadCycle on TMC2208/2209/2224
|
||||
// driver.pwm_autoscale(true); // Needed for stealthChop
|
||||
|
||||
Serial.print("DRV_STATUS=0b");
|
||||
Serial.println(driver.DRV_STATUS(), BIN);
|
||||
|
||||
accel.setMaxSpeed(50000.0);
|
||||
accel.setAcceleration(2000.0);
|
||||
accel.setMinPulseWidth(1);
|
||||
accel.setEnablePin(EN_PIN);
|
||||
accel.setPinsInverted(false, false, true);
|
||||
accel.enableOutputs();
|
||||
// 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 = 80;
|
||||
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;
|
||||
// Serial.println("Speed: " + String(stepsDelay));
|
||||
// } else if (c == '-') {
|
||||
// stepsDelay *= 1.2;
|
||||
// Serial.println("Speed: " + String(stepsDelay));
|
||||
// }
|
||||
// }
|
||||
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);
|
||||
|
||||
// // Run 5000 steps and switch direction in software
|
||||
// for (uint32_t i = 17066; i>0; i--) {
|
||||
// digitalWrite(STEP_PIN, HIGH);
|
||||
// delayMicroseconds(10);
|
||||
// digitalWrite(STEP_PIN, LOW);
|
||||
// delayMicroseconds(stepsDelay);
|
||||
// }
|
||||
// delay(500);
|
||||
Serial.print("PWM_SCALE: 0b");
|
||||
Serial.println(driver.PWM_SCALE(), DEC);
|
||||
|
||||
if (accel.distanceToGo() == 0) {
|
||||
accel.disableOutputs();
|
||||
delay(500);
|
||||
accel.move(17066);
|
||||
accel.enableOutputs();
|
||||
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);
|
||||
}
|
||||
accel.run();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue