Hello all, I'm not new to Arduino but am new to the forums. I appreciate any help in advance.
I have a project that requires me to control three stepper motors via drivers. One of the main functions is individual jog of the steppers with jog switches. I have no problem implementing this in one direction, but I'm struggling with running them in reverse.
My code works by looking for jog switch states using if statements, assigning an appropriate negative or positive value to the variable that stores my stepper's speed, and updating that speed with setSpeed() before using the runSpeed() function to make the motors spin.
This works perfectly until I try to implement the setSpeed() function. Then it falls apart.
Here is the function that is called to jog all motors:
void StepperRun(){ // Runs all steppers at code setup jog speed
Tug1.setSpeed(Tug1Speed);
// Tug2.setSpeed(Tug2Speed);
// Core.setSpeed(CoreSpeed);
Tug1.runSpeed();
Tug2.runSpeed();
Core.runSpeed();
}
In this configuration, only Tug2 and Core operate while Tug1 (with the uncommented setSpeed function) is unresponsive. The idea is that I change Tug1Speed to a negative value and then it will run in the opposite direction. I know setSpeed() will accept a negative value because I've tested this in my setup code, so that's not the problem.
So far I've tried implementing setSpeed() in different parts of the code to see if that will help. This hasn't worked. I'm fairly stuck and beginning to worry that I might need a totally different approach if I'm going to stick with AccelStepper.
Here is my code in full:
#include <AccelStepper.h>
int Tug1_pulse = 6; int Tug1_dir = 5;
int Tug2_pulse = 13; int Tug2_dir = 12;
int Core_pulse = 11; int Core_dir = 10;
int JogSwitchFor = 2; int JogSwitchRev = 3;
int TugSwitchFor = 8; int TugSwitchRev = 9;
int CoreSwitchFor = 4; int CoreSwitchRev = 7;
int CycleSwitch = A0; int CycleRst = A1;
float Tug1Speed, Tug2Speed, CoreSpeed;
int tugSpeed; int tugRPS; int micro;
int Rotation; int Distance; int Distance2; int Rotation2;
AccelStepper Tug1(1, 6, 5);
AccelStepper Tug2(1, 13, 12);
AccelStepper Core(1, 11, 10);
void setup() {
//Outputs to stepper drivers
pinMode(Tug1_pulse,OUTPUT);
pinMode(Tug1_dir,OUTPUT);
pinMode(Tug2_pulse,OUTPUT);
pinMode(Tug2_dir,OUTPUT);
pinMode(Core_pulse,OUTPUT);
pinMode(Core_dir,OUTPUT);
//Switch and button inputs
pinMode(JogSwitchFor,INPUT_PULLUP);
pinMode(JogSwitchRev,INPUT_PULLUP);
pinMode(TugSwitchFor,INPUT_PULLUP);
pinMode(TugSwitchRev,INPUT_PULLUP);
pinMode(CoreSwitchFor,INPUT_PULLUP);
pinMode(CoreSwitchRev,INPUT_PULLUP);
pinMode(CycleSwitch,INPUT_PULLUP);
pinMode(CycleRst,INPUT_PULLUP);
/*
* BUNDLER STEPPER SPEED OUTPUT CALCULATION
* Using Inches Per Rotation and bundle linear velocity (V_bund)
*/
float inchPerRot = 2; //Input for wrap spacing
float V_Bund = 5; //Input for bundle speed (inches/minute)
float CoreGearRatio = 2.045;
int microCore = 8; //MicroStepping Setting THIS MUST BE SAME FOR ALL STEPPER DRIVERS
float CoreRPM = V_Bund/inchPerRot; //Calculate necessary RPM for core orbit around the bundle
float StepperCoreRPM = CoreRPM*CoreGearRatio; //Calculate RPM for the Core stepper motor
float CoreSpeed = (StepperCoreRPM/60)*200*microCore; //Stepper speed output in Steps/Second
/*
* CALCULATE TUG STEPPER SPEED FROM V_Bund
* Uses V_bund for input and assumes a 4.26 inch pulley circumference
*/
int microTug = 1;
float TugRadius = .678; //Tug pulley radius in inches
float TugCirc = TugRadius*3.142*2;
float TugRPM = V_Bund/TugCirc; //Calculate tug stepper RPM
float Tug1Speed = -1*(TugRPM/60)*200*microTug; //Outputs Tug1 Speed
float Tug2Speed = -1*Tug1Speed;
/*
* Stepper motion setup
*/
Tug1.setMaxSpeed(Tug1Speed);
Tug1.setSpeed(Tug1Speed);
//Tug1.setAcceleration(20000);
Tug2.setMaxSpeed(Tug2Speed);
Tug2.setSpeed(Tug2Speed);
//Tug2.setAcceleration(20000);
Core.setMaxSpeed(CoreSpeed);
Core.setSpeed(CoreSpeed);
//Core.setAcceleration(20000);
/*
* Cycle Run Length calculation
*/
float CycleLength = 1; //Cut length of the bundle. The machine will run this length and stop after cycle
//start switch is turned on
float TugStepPerRev = micro*200;
}
void loop() {
// Digital read the control switches
int JogFor = digitalRead(JogSwitchFor);
int JogRev = digitalRead(JogSwitchRev);
int CoreFor = digitalRead(CoreSwitchFor);
int CoreRev = digitalRead(CoreSwitchRev);
int TugFor = digitalRead(TugSwitchFor);
int TugRev = digitalRead(TugSwitchRev);
int Cyclerun = digitalRead(CycleSwitch);
int CycleRst = digitalRead(CycleRst);
/*
* Detects switch positions and interprets to give jog outputs
* Pin low is active position
*/
if (!JogFor && JogRev && CoreFor && CoreRev && TugFor && TugRev && Cyclerun && CycleRst){
Tug1Speed = abs(Tug1Speed);
Tug2Speed = abs(Tug2Speed);
CoreSpeed = abs(CoreSpeed);
StepperRun();
}
if (!JogRev && JogFor && CoreFor && CoreRev && TugFor && TugRev && Cyclerun && CycleRst){
Tug1Speed = -1*abs(Tug1Speed);
Tug2Speed = -1*abs(Tug2Speed);
CoreSpeed = -1*abs(CoreSpeed);
StepperRun();
}
if (!CoreFor && JogFor && JogRev && CoreRev && TugFor && TugRev && Cyclerun && CycleRst){
CoreSpeed = abs(CoreSpeed);
Core.setSpeed(CoreSpeed);
CoreRun();
}
if (!CoreRev && JogFor && JogRev && CoreFor && TugFor && TugRev && Cyclerun && CycleRst){
CoreSpeed = -1*abs(CoreSpeed);
Core.setSpeed(CoreSpeed);
CoreRun();
}
if (!TugFor && JogFor && JogRev && CoreFor && CoreRev && TugRev && Cyclerun && CycleRst){
Tug1Speed = abs(Tug1Speed);
Tug2Speed = abs(Tug2Speed);
Tug1.setSpeed(Tug1Speed);
Tug2.setSpeed(Tug2Speed);
TugRun();
}
if (!TugRev && JogFor && JogRev && CoreFor && CoreRev && TugFor && Cyclerun && CycleRst){
Tug1Speed = -1*abs(Tug1Speed);
Tug2Speed = -1*abs(Tug2Speed);
Tug1.setSpeed(Tug1Speed);
Tug2.setSpeed(Tug2Speed);
TugRun();
}
// if (!Cyclerun && JogFor && JogRev && CoreFor && CoreRev && TugFor && TugRev && CycleRst){
// while(motor.currentcurentPosition()<CycleSteps && !Cyclerun && JogFor && JogRev && CoreFor $$ CoreRev $$ TugFor $$ TugRev && CycleRst){
// Tug1Speed = abs(Tug1Speed);
// Tug2Speed = abs(Tug2Speed);
// CoreSpeed = abs(CoreSpeed);
// StepperRun();
// }
//}
//
// if (!CycleRst && JogFor && JogRev && CoreFor && CoreRev && TugFor && TugRev && Cyclerun)
// Tug1.setCurrentPosition(0);
}
void StepperRun(){ // Runs all steppers forward at code setup jog speed
Tug1.setSpeed(Tug1Speed);
// Tug2.setSpeed(Tug2Speed);
// Core.setSpeed(CoreSpeed);
Tug1.runSpeed();
Tug2.runSpeed();
Core.runSpeed();
}
void CoreRun(){ //Runs only the core (for the purpose of jogging)
Core.runSpeed();
// Core.setSpeed(CoreSpeed);
}
void TugRun(){ //Runs only the Tug (for the purpose of jogging)
Tug1.runSpeed();
Tug2.runSpeed();
// Tug1.setSpeed(Tug1Speed);
// Tug2.setSpeed(Tug2Speed);
}