I need to control two stepper motors via TMC2208 stepper motor drivers.
If I use the sample code below both motors work fine.
// Author Teemu Mäntykallio, 2017-04-07
// Define pins
#define EN_PIN 22 // LOW: Driver enabled. HIGH: Driver disabled
#define STEP_PIN 23 // Step on rising edge
#define AUTOMODE_PIN 33
#include <TMC2208Stepper.h> // Include library
TMC2208Stepper driver = TMC2208Stepper(&Serial3); // Create driver and use
// HardwareSerial0 for communication
void setup() {
Serial.begin(115200);
Serial.println("Start...");
Serial1.begin(115200); // Start hardware serial 1
driver.push(); // Reset registers
// Prepare pins
pinMode(EN_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
digitalWrite(EN_PIN, HIGH); // Disable driver in hardware
pinMode(AUTOMODE_PIN, INPUT_PULLUP);
driver.pdn_disable(true); // Use PDN/UART pin for communication
driver.I_scale_analog(false); // Use internal voltage reference
driver.rms_current(500); // Set driver current 500mA
driver.toff(2); // Enable driver in software
digitalWrite(EN_PIN, LOW); // Enable driver in hardware
uint32_t data = 0;
Serial.print("DRV_STATUS = 0x");
driver.DRV_STATUS(&data);
Serial.println(data, HEX);
}
void loop() {
if(digitalRead(AUTOMODE_PIN) == LOW){
digitalWrite(MOTA_STEP, !digitalRead(MOTA_STEP));
delayMicroseconds(100);
}
}
As soon as I start using my actual code only stepper motor B works but not A. Please advise?
My code is to long to post. The code can be found here: Code
smithchristof:
My code is to long to post.
Then make an MCVE. Cut out everything from your code but the stuff related to the stepper motors. Provide just enough support code around that so that it can be compiled and demonstrate the problem.
I think the error is with the VACTUAL command?
// Define pins
#define MOTA_DIR 24
#define MOTA_STEP 23
#define MOTA_ENA 22
#define MOTA_MSTEPS 256 // 16
#define MOTA_SERIAL Serial2
#define MOTA_CURRENT_MA 500
#define AUTOMODE_PIN 33
#define MOTA_SPEED_MAX 210000
#define MOTA_SPEED_MIN 15000
#include <TMC2208Stepper.h> // Include library
TMC2208Stepper driverA = TMC2208Stepper(&MOTA_SERIAL); // Create driver and use
// HardwareSerial0 for communication
void setup() {
Serial.begin(115200);
Serial.println("Start...");
Serial2.begin(115200); // Start hardware serial 1
driverA.push();
// Prepare pins
pinMode(MOTA_ENA, OUTPUT);
pinMode(MOTA_STEP, OUTPUT);
pinMode(MOTA_DIR, OUTPUT);
digitalWrite(MOTA_ENA, HIGH); // Disable driver in hardware
driverA.pdn_disable(true); // Use PDN/UART pin for communication
driverA.I_scale_analog(true); // Use internal voltage reference
driverA.rms_current(MOTA_CURRENT_MA); // Set driver current 500mA
// driverB.en_spreadCycle(1);
driverA.toff(2); // Enable driver in software
driverA.mstep_reg_select(true); // ignore MS1 + MS2
driverA.microsteps(MOTA_MSTEPS);
driverA.ihold(0);
digitalWrite(MOTA_DIR,LOW);
digitalWrite(MOTA_ENA, LOW);
pinMode(AUTOMODE_PIN, INPUT_PULLUP);
uint32_t data = 0;
Serial.print("DRV_STATUS = 0x");
driverA.DRV_STATUS(&data);
Serial.println(data, HEX);
}
// derive motor speed from value
uint32_t speedFromValue(float currentValue, float targetValue) {
float delta = targetValue - currentValue;
Serial.print("Delta: ");
Serial.println(delta);
if (delta > 0.8) {
Serial.println("MAX Speed");
return MOTA_SPEED_MAX;
}
if (delta < 0.12) {
Serial.println("MIN Speed");
return MOTA_SPEED_MIN;
}
uint32_t newSpeed = map(delta * 10, 0.12 * 10, 0.8 * 10, MOTA_SPEED_MIN, MOTA_SPEED_MAX);
Serial.print("Speed: ");
Serial.println(newSpeed);
return newSpeed;
}
void loop() {
if(digitalRead(AUTOMODE_PIN) == LOW){
driverA.VACTUAL(speedFromValue(10, 30));
}else{
driverA.VACTUAL((uint32_t) 0);
}
}