main
radex 2023-01-21 13:13:34 +01:00
parent 2a98ecfb21
commit 7ea42e6ca7
2 changed files with 79 additions and 37 deletions

View File

@ -13,6 +13,8 @@ platform = atmelavr
board = uno
framework = arduino
monitor_speed = 115200
lib_deps =
SPI
TMCStepper

View File

@ -19,20 +19,9 @@
// 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 MICROSTEPS 2
#define STEPS_PER_REV 200 * MICROSTEPS
void setup() {
@ -42,7 +31,7 @@ void setup() {
pinMode(DIAG_PIN, INPUT);
digitalWrite(EN_PIN, LOW);
Serial.begin(9600);
Serial.begin(115200);
Serial.println("Start...");
// Enable one according to your setup
@ -50,14 +39,22 @@ void setup() {
//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.internal_Rsense(true);
driver.begin();
driver.push();
driver.rms_current(300); // Set motor RMS current
driver.microsteps(MICROSTEPS); // Set microsteps to 1/16th
// driver.irun(10);
// driver.ihold(10);
// TOFF General enable for the motor driver, the actual value does not influence StealthChop
driver.toff(4);
// RMS (mA) current for running, second arg - hold current multiplier
driver.rms_current(1500, 0.5);
driver.microsteps(0);
// Enable stealthChop
driver.en_spreadCycle(false);
// When using the UART interface, the configuration pin should be disabled via GCONF.pdn_disable = 1.
// Program IHOLD as desired for standstill periods.
driver.pdn_disable(true);
// driver.en_spreadCycle(true); // Toggle spreadCycle on TMC2208/2209/2224
// driver.hysteresis_start(8);
@ -66,12 +63,28 @@ void setup() {
// 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.TCOOLTHRS(0xFFFFF); // 20bit max
// The driver.SG_RESULT() returns the result in the legacy 10 bit format, where the first and the last bit are always set to 0.
// Dividing it by 2 gives us the value in the same range as driver.SGTHRS(STALL_VALUE);
// if the (converted) SG_RESULT <= 2 * SGTHRS, stall is reported
driver.SGTHRS(30);
// default value: SGTHRS / 16 + 1
// for sgResult = semin * 16, the current starts getting increased to resist the resistance
// the bigger it is, the bigger the chance the motor is going to react to adversity
// by increasing the current
// 0..15
driver.semin(9);
// SEMAX is used to determine when the extra current should be disabled.
// the higher it is, the harder it's going to be to go back to energy efficient mode
// 0 to 2 recommended
// 0..15
driver.semax(15);
// If the StallGuard4result is equal to or above (SEMIN+SEMAX+1)*32 the motro current becomes decreased to save energy
driver.sedn(0b01);
// driver.en_pwm_mode(true); // Toggle stealthChop on TMC2130/2160/5130/5160
@ -98,6 +111,7 @@ bool shaft = false;
unsigned int stepsDelay = 28000 / MICROSTEPS;
bool shouldRun = true;
int stepsMade = 0;
int stallSigs = 0;
void loop() {
if (Serial.available()) {
@ -133,6 +147,12 @@ void loop() {
Serial.print("PWM_SCALE: 0b");
Serial.println(driver.PWM_SCALE(), DEC);
Serial.print("SGTHRS: ");
Serial.println(driver.SGTHRS(), DEC);
Serial.print("GCONF: ");
Serial.println(driver.GCONF(), DEC);
Serial.print("CHOPCONF: 0b");
Serial.println(driver.CHOPCONF(), BIN);
}
@ -155,30 +175,50 @@ void loop() {
if (shouldRun) {
if (stepsMade >= STEPS_PER_REV) {
stepsMade = 0;
shaft = !shaft;
driver.shaft(shaft);
// shaft = !shaft;
// driver.shaft(shaft);
}
for (uint32_t i = 100; i>0; i--) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(5);
for (uint32_t i = 20; i>0; i--) {
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(1);
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(1);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(1);
delayMicroseconds(stepsDelay);
// if (digitalRead(DIAG_PIN) == HIGH) {
// Serial.println("STALL");
// }
if (digitalRead(DIAG_PIN) == HIGH) {
stallSigs += 1;
// Serial.println("");
// shouldRun = false;
}
}
stepsMade += 100;
if (stallSigs) {
Serial.print("STALLs ");
Serial.println(stallSigs, DEC);
if (stallSigs > 5) {
delay(200);
shaft = !shaft;
driver.shaft(shaft);
}
stallSigs = 0;
}
// if (driver.SG_RESULT() > 105) {
// shaft = !shaft;
// driver.shaft(shaft);
// }
// Serial.println(driver.SG_RESULT(), DEC);
// Serial.print(driver.SG_RESULT()/2, DEC);
// Serial.print(" ");
// Serial.print(digitalRead(DIAG_PIN), DEC);
// Serial.print(" ");
// Serial.println(driver.cs2rms(driver.cs_actual()), DEC);
// // Serial.print(" ");
// // Serial.println(driver.cs2rms(driver.cs_actual()), DEC);
// Serial.println();
}
}