Hi, I'm hoping for a little help. i am working with an Adafruit motor shield v2.
I have used one of the example files Accel_MultiStepper. I have amended the code to suit my setup, one motor shield and two stepper motors, and the code behaved as hoped when uploaded to the Arduino (uno). however i want to use this code (or a variant of it) as a library class. I have split it out into a cpp and header file, and call it from a test script, it compiles, but for some reason does not behave as expected (it does nothing). After using some serial print lines at various points, it seems like its the creation of my new class where the problem is.
I'm only just getting back into writing code after a very long break, and seem to have forgotten more than i remember, so any help would be really appreciated.
the working example script:
#include <Wire.h>
#include <AccelStepper.h>
#include <Adafruit_MotorShield.h>
Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers
// Connect two steppers with 200 steps per revolution (1.8 degree)
// to the top shield
Adafruit_StepperMotor *myrightMotor = AFMStop.getStepper(200, 1);
Adafruit_StepperMotor *myleftMotor = AFMStop.getStepper(200, 2);
// you can change these to SINGLE, DOUBLE, INTERLEAVE or MICROSTEP!
// wrappers for the first motor!
void forwardstep1() {
myrightMotor->onestep(FORWARD, DOUBLE);
}
void backwardstep1() {
myrightMotor->onestep(BACKWARD, DOUBLE);
}
// wrappers for the second motor!
void forwardstep2() {
myleftMotor->onestep(FORWARD, DOUBLE);
}
void backwardstep2() {
myleftMotor->onestep(BACKWARD, DOUBLE);
}
// Now we'll wrap the 3 steppers in an AccelStepper object
AccelStepper rightMotor(forwardstep1, backwardstep1);
AccelStepper leftMotor(backwardstep2, forwardstep2);
bool stepsAreSet = 0;
void setup()
{
AFMStop.begin(); // Start the top shield
rightMotor.setMaxSpeed(200.0);
rightMotor.setAcceleration(100.0);
leftMotor.setMaxSpeed(200.0);
leftMotor.setAcceleration(100.0);
Serial.begin(9600);
Serial.println("setup ");
Serial.print("\n");
}
void loop()
{
if(!stepsAreSet)
{
rightMotor.moveTo(50000);
leftMotor.moveTo(50000);
stepsAreSet = 1;
}
// Change direction at the limits
if (rightMotor.distanceToGo() == 0)
rightMotor.moveTo(-rightMotor.currentPosition());
if (leftMotor.distanceToGo() == 0)
leftMotor.moveTo(-leftMotor.currentPosition());
rightMotor.run();
leftMotor.run();
}
script:
#include <Robot_Tracks.h>
Robot_Tracks Tracks = Robot_Tracks();
void setup()
{
Tracks.begin();
}
void loop()
{
Tracks.forwardTo();
}
cpp:
#include <Wire.h>
#include <AccelStepper.h>
#include <Adafruit_MotorShield.h>
#include <Robot_Tracks.h>
Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers
// Connect two steppers with 200 steps per revolution (1.8 degree)
// to the top shield
Adafruit_StepperMotor *myrightMotor = AFMStop.getStepper(200, 1);
Adafruit_StepperMotor *myleftMotor = AFMStop.getStepper(200, 2);
// you can change these to SINGLE, DOUBLE, INTERLEAVE or MICROSTEP!
// wrappers for the first motor!
void forwardstep1() {
myrightMotor->onestep(FORWARD, DOUBLE);
}
void backwardstep1() {
myrightMotor->onestep(BACKWARD, DOUBLE);
}
// wrappers for the second motor!
void forwardstep2() {
myleftMotor->onestep(FORWARD, DOUBLE);
}
void backwardstep2() {
myleftMotor->onestep(BACKWARD, DOUBLE);
}
// Now we'll wrap the 3 steppers in an AccelStepper object
AccelStepper rightMotor(forwardstep1, backwardstep1);
AccelStepper leftMotor(backwardstep2, forwardstep2);
bool stepsAreSet = 0;
Robot_Tracks::Robot_Tracks() {
}
void Robot_Tracks::begin() {
AFMStop.begin(); // Start the top shield
rightMotor.setMaxSpeed(200.0);
rightMotor.setAcceleration(100.0);
leftMotor.setMaxSpeed(200.0);
leftMotor.setAcceleration(100.0);
}
void Robot_Tracks::forwardTo() {
if (!stepsAreSet) {
rightMotor.moveTo(50000);
leftMotor.moveTo(50000);
stepsAreSet = 1;
}
// Change direction at the limits
if (rightMotor.distanceToGo() == 0)
rightMotor.moveTo(-rightMotor.currentPosition());
if (leftMotor.distanceToGo() == 0)
leftMotor.moveTo(-leftMotor.currentPosition());
rightMotor.run();
leftMotor.run();
}
header:
#ifndef Robot_Tracks_h
#define Robot_Tracks_h
#include <Wire.h>
#include <AccelStepper.h>
#include <Adafruit_MotorShield.h>
#include <Robot_Tracks.h>
class Robot_Tracks
{
public:
Robot_Tracks();
void begin();
void forwardstep1();
void backwardstep1();
void forwardstep2();
void backwardstep2();
void forwardTo();
private:
bool stepsAreSet;
Adafruit_MotorShield AFMStop;
Adafruit_StepperMotor *myrightMotor;
Adafruit_StepperMotor *myleftMotor;
AccelStepper rightMotor;
AccelStepper lefttMotor;
};
#endif