I'm using AccelStepper for driving a stepper motor. For a slider I created a Slider class in which I call AccelStepper function's. This results in a big performance issue. When I call the stepper functions outside the class it works perfect but when I use the same calls within the class functions I have an unworkable situation.
I stripped and commented the code to make clear what the situation is (I hope...) Someone any idea what's going on? any help is much appreciated. I'm struggling with this for a while now.
Thnx, Fred
// in SldrWagon class (object slider) I call the global stepper object below to let the Accel stepper do it's job (SLDR_wagon.h / SLDR_wagon.cpp)
SldrWagon slider;
// stepper is defined in SLDR_wagon.cpp as global var => AccelStepper stepper( 1, STEPPER_STEP_PIN, STEPPER_DIR_PIN);
extern AccelStepper stepper;
void setup()
{
Serial.begin( 9600 );
Serial.println( "start");
}
/// ------- How I want to get it work with a good performance
/// ----------------- BAD PERFORMANCE, STEPPER HARDLY MOVES -----------------
/// all AccelStepper 'stepper' object functions are called from within the the 'slider' class object
void loop() {
if (! slider.isMoving() ) {
abuttn= keypad.ReadKeypad();
switch ( abuttn ) {
case btnRIGHT :
slider.go( 1000000.0 , 1500.0 , POSITION_LIMIT-1500,BACKWARD); // slider called
slider.isMoving( true );
break;
}
}
slider.run(); // slider called
}
//////////////////////////// WHAT I'VE ALREADY TRIED //////////////////////////
/// ----------------- BAD PERFORMANCE, STEPPER HARDLY MOVES -----------------
/// 'slider' class object is used for setting the global 'stepper' object parameters Speed, Acceleration and MoveTo
/// in loop() stepper.run() is directly called without using the slider class object to do this
void loop() {
if (! slider.isMoving() ) {
abuttn= keypad.ReadKeypad();
switch ( abuttn ) {
case btnRIGHT :
slider.go( 1000000.0 , 1500.0 , POSITION_LIMIT-1500,BACKWARD); // slider called
slider.isMoving( true );
break;
}
}
stepper.run(); // stepper called
}
/// ----------------- SAME BAD PERFORMANCE, STEPPER HARDLY MOVES -----------------
/// the object parameters Speed, Acceleration and MoveTo are directly set by calling the stepper object
/// in the loop the global stepper.run() function is called within the slider class object
void loop() {
if (! slider.isMoving() ) {
abuttn= keypad.ReadKeypad();
switch ( abuttn ) {
case btnRIGHT :
stepper.setAcceleration( 1500.0 ); // stepper called
stepper.setSpeed( 1000000.0 ); // stepper called
stepper.moveTo( -POSITION_LIMIT ); // stepper called
slider.isMoving( true );
break;
}
}
slider.run(); // slider called
}
/// ----------------- GOOD PERFORMANCE !!!! -----------------
/// 'slider' class object not used for AccelStepper functions but only global 'stepper' object
void loop() {
if (! slider.isMoving() ) {
abuttn= keypad.ReadKeypad();
switch ( abuttn ) {
case btnRIGHT :
stepper.setAcceleration( 1500.0 ); // stepper called
stepper.setSpeed( 1000000.0 ); // stepper called
stepper.moveTo( -POSITION_LIMIT ); // stepper called
slider.isMoving( true );
break;
}
}
stepper.run();
}
//////////////////////////////////// SLDR_wagon.cpp //////////////////////////////////////////
// ------------- the called stripped functions in the slider SLDR_wagon.cpp file ------------
void SldrWagon::go( const float speed, const float accel, const float pos, const unsigned char dir ) {
_direction= dir;
_currSpeed= dir ? speed : - speed;
_currAcceleration= accel;
_isMoving= true;
stepper.setAcceleration( accel );
stepper.moveTo( pos );
stepper.setSpeed( speed );
return;
}
void SldrWagon::run( void ) {
stepper.run();
return;
};
/// ----- then tried this one to see if calling the slider object is the bad guy
/// no difference in performance though!!!!
void SldrWagon::run( void ) {
unsigned int i;
for( i=10000; i; i-- ) { stepper.run(); }
return;
};