I have 1 master arduino setting the pulse-rate for 2 slaves attached to stepper motors. Both motors are running the same code, but setup to the fact the motors and drivers are different.
Slave 1 is using a Big Easy Driver A4988 with a 400step motor.
Slave 2 is using a TB6600 driver with a 200 step motor ... and struggling to perform.
I expected I would need to set 1/2 microstepping on Slave 2 for them to match speeds (400revs), but for some reason both are set to full-step and maintain the same speed. Slave-2 misses a lot more steps than Slave-1 and stalls at a slightly lower max speed. Amps should be set properly for both boards (1.6a/phase).
Overall, Slave-2 has a difficult time and has quite jumpy resonant sections. I thought slave-2 driver was better quality but not sure what is going on.
Any help getting Slave2 to perform cleaner, or insights on the similar speeds would be appreciated.
Thanks
#include <Wire.h>
#define MS1 4
#define MS2 5
#define MS3 6
#define EN 8
#define directionPin 9
#define stepPin 10
byte b[3]; // byte array
bool flag1 = LOW;
// Timers for Ramping
unsigned long targetInterval = 1000; // read speed from master
unsigned long stepIntervalMicros = 2000; //Current # of micros delay. Primary speed control
unsigned long prevStepMicros = 0; //Timer
unsigned long prevTime2 = 0;
unsigned long timer2 = 10000; //delay when ramping to target time
byte direction = 0;
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(EN, OUTPUT); //for other driver
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
pinMode(MS3, OUTPUT);
digitalWrite(MS1, LOW); //Micro. 000 = full, 100 = 1/2, 010 = 1/4, 110 = 1/8
digitalWrite(MS2, LOW);
digitalWrite(MS3, LOW);
digitalWrite(EN, LOW);
digitalWrite(directionPin, direction);
}
void loop() {
{
if (flag1 == HIGH)
{
if (b[0] == '*')
{
targetInterval = (b[1] << 8) | b[2]; //prev. RPSmicros = b[0] * 256 + b[1];
// Serial.println(highByte(b[0]), HEX); //0x01; upper byte of 500
// Serial.println(highByte(b[1]), HEX); //0x01; upper byte of 500
// Serial.println(lowByte(b[2]), HEX); //0xF4; lower byte of 500
Serial.print("Slave: ");
Serial.println(targetInterval, DEC); //target speed
flag1 = LOW;
}
}
}
moveMotor();
}
void moveMotor() {
if (micros() - prevTime2 >= timer2){ //only adjust towards target speed at defined rate or will fly thru too quickly
prevTime2 = micros();
if (stepIntervalMicros >= targetInterval){ //if running too slow
stepIntervalMicros --; //speed up motor by slowly decreasing pause
}
else if (stepIntervalMicros < targetInterval){ //if running too fast
stepIntervalMicros ++; //slow down motor by slowly increasing pause
}
}
if (micros() - prevStepMicros >= stepIntervalMicros) { //fire motor at current interval timing
prevStepMicros = micros();
singleStep();
}
}
void receiveEvent(int howMany){
for (int i = 0; i < howMany; i++){
b[i] = Wire.read();
}
flag1 = HIGH;
}
void singleStep() {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
}