Creating a AccelStepper & AMIS30543 object in a class

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

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()

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

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.

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

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?

There is an example you could look at

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.