Hi all,
I am attempting to control two stepper motors using AccelStepper and a AMIS30543 motor driver. The Arduino board doesn't seem to initialize.
class StepMotor : public AccelStepper
{
public:
// Constructor for each Stepper Motor
StepMotor(int stepPin, int dirPin, int slaveSelectPin, long homePostion, long fullTravelSteps) : AccelStepper(DRIVER, stepPin, dirPin)
{
AMIS30543 amisStepper;
amisStepper.init( slaveSelectPin );
delay(1);
amisStepper.resetSettings();
amisStepper.setCurrentMilliamps(1000);
amisStepper.setStepMode(32);
amisStepper.enableDriver();
setMaxSpeed(1000);
setAcceleration(500);
}
All help is appreciated.
Thank you
J-M-L
September 12, 2022, 7:19pm
2
why do you think AMIS30543 created an init() method and did not put that into the constructor?
hint: when is the class constructor called for global objects?
The class constructor is called right before void setup()
J-M-L
September 12, 2022, 10:08pm
4
Yes, way before, so any hardware setting might be undone by the setup process. Provide an init() method for your subclass where you complete what needs to happen once setup() is called
Such as this? I looks to be working. Thank you.
#include <SPI.h>
#include <AMIS30543.h>
#include <AccelStepper.h>
const uint8_t STEPPER1_DIR_PIN = 2;
const uint8_t STEPPER1_STEP_PIN = 3;
const uint8_t STEPPER1_SLAVE_SELECT_PIN = 4;
const uint8_t STEPPER2_DIR_PIN = 7;
const uint8_t STEPPER2_STEP_PIN = 6;
const uint8_t STEPPER2_SLAVE_SELECT_PIN = 8;
// Command Definitions
class StepMotor : public AccelStepper
{
public:
// Constructor for each Stepper Motor
StepMotor(int stepPin, int dirPin) : AccelStepper(DRIVER, stepPin, dirPin)
{
setMaxSpeed(1000);
setAcceleration(500);
}
void init( int slaveSelectPin ) {
AMIS30543 amisStepper;
amisStepper.init( slaveSelectPin );
delay(1);
amisStepper.resetSettings();
amisStepper.setCurrentMilliamps(1000);
amisStepper.setStepMode(32);
amisStepper.enableDriver();
}
void Update()
{
//do something
}
};
StepMotor stepper1(STEPPER1_STEP_PIN, STEPPER1_DIR_PIN );
StepMotor stepper2(STEPPER2_STEP_PIN, STEPPER2_DIR_PIN );
void setup() {
// Initialize Variables
// Start Serial Monitor
Serial.begin(9600); //start serial monitor
SPI.begin();
stepper1.init( STEPPER1_SLAVE_SELECT_PIN );
stepper2.init( STEPPER2_SLAVE_SELECT_PIN );
Serial.println("Loaded");
} //End of Setup
void loop( ) {
stepper1.Update();
stepper2.Update();
} // End of void Loop
gfvalvo
September 13, 2022, 1:16pm
6
Your AMIS30543 object is local to the init() function. It will cease to exist after that function returns.
You are correct. Could you suggest a solution? I am at a loss what to do next.
gfvalvo
September 13, 2022, 2:12pm
8
Why not make it an instance member rather than an object local to init()?
Also, when you post the code again, please take out some of the pointless white space.
I think I am starting to understand, something like this?
const uint8_t STEPPER1_DIR_PIN = 2;
const uint8_t STEPPER1_STEP_PIN = 3;
const uint8_t STEPPER1_SLAVE_SELECT_PIN = 4;
const uint8_t STEPPER2_DIR_PIN = 7;
const uint8_t STEPPER2_STEP_PIN = 6;
const uint8_t STEPPER2_SLAVE_SELECT_PIN = 8;
class StepMotor : public AccelStepper
{
public:
// Constructor for each Stepper Motor
StepMotor(int stepPin, int dirPin) : AccelStepper(DRIVER, stepPin, dirPin) {
setMaxSpeed(1000);
setAcceleration(500);
}
void begin( int slaveSelectPin );
void Update() {
//do something
}
};
void StepMotor::begin( int slaveSelectPin ) {
AMIS30543 amisStepper;
amisStepper.init( slaveSelectPin );
delay(1);
amisStepper.resetSettings();
amisStepper.setCurrentMilliamps(1300);
amisStepper.setStepMode(32);
amisStepper.enableDriver();
}
StepMotor stepper1(STEPPER1_STEP_PIN, STEPPER1_DIR_PIN );
StepMotor stepper2(STEPPER2_STEP_PIN, STEPPER2_DIR_PIN );
void setup() {
Serial.begin(9600); //start serial monitor
SPI.begin();
stepper1.begin( STEPPER1_SLAVE_SELECT_PIN );
stepper2.begin( STEPPER2_SLAVE_SELECT_PIN );
Serial.println("Loaded");
} //End of Setup
void loop( ) {
stepper1.Update();
stepper2.Update();
} // End of void Loop
J-M-L
September 13, 2022, 3:34pm
10
when you do this
you declare a local variable amisStepper
which gets destroyed when the begin() function terminates.
add
AMIS30543 amisStepper;
as instance variable and initialise this amisStepper in the begin() method
side question about the design : why you need a subclass of AccelStepper to have an instance of AMIS30543 ?
I don't need it to be a subclass of AccelStepper. I guess I am not sure how to use AMIS30543. What is the best way to use it?
J-M-L
September 13, 2022, 3:53pm
12
There is an example you could look at
/* This example shows how to use the AMIS-30543 stepper motor
driver with the AccelStepper library.
You will need to install the AccelStepper library for this
example to work. Documentation and installation instructions for
the AccelStepper library are available here:
http://www.airspayce.com/mikem/arduino/AccelStepper/
Before using this example, be sure to change the
setCurrentMilliamps line to have an appropriate current limit for
your system. Also, see this library's documentation for
information about how to connect the driver:
http://pololu.github.io/amis-30543-arduino/
*/
#include <SPI.h>
#include <AMIS30543.h>
#include <AccelStepper.h>
This file has been truncated. show original
system
Closed
March 12, 2023, 3:53pm
13
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.