Timing problem with AFMS V2, AccelStepper and 2 steppers

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
metallissimus
 
Posts: 5
Joined: Sun Aug 10, 2014 9:33 am

Timing problem with AFMS V2, AccelStepper and 2 steppers

Post by metallissimus »

Hey there,

I am currently working on a project which runs 2 steppermotors and relies on (at least nearly) precise timing. I am using a AFMS V2 and the AccelStepper lib, running on an Atmega328 mounted on the shield. Here's the code before the loop:

Code: Select all

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <AccelStepper.h>
#include <VirtualWire.h>

byte circles = 20; // number of rotor rotations
byte orbTime = 20; // overall time in seconds
byte circlesPerTurn = 40;


Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
Adafruit_StepperMotor *stepper1 = AFMS.getStepper(200, 1);
Adafruit_StepperMotor *stepper2 = AFMS.getStepper(200, 2);

unsigned long time = 0;

int rotoSteps;
float rotoSpeed;
float baseSteps;
float baseSpeed;

void forwardstep1(){
  stepper1->onestep(FORWARD, DOUBLE);
}

void backwardstep1(){
  stepper1->onestep(FORWARD, DOUBLE);
}

void forwardstep2(){
  stepper2->onestep(FORWARD, SINGLE);
}

void backwardstep2(){
  stepper2->onestep(FORWARD, SINGLE);
}

void releaseMotors(){
  stepper1->release();
  stepper2->release();
}

AccelStepper baseMotor(forwardstep1, backwardstep1);
AccelStepper rotoMotor(forwardstep2, backwardstep2);

void getMotorValues(){
  
  rotoSteps = circles * 200;
  rotoSpeed = rotoSteps / orbTime;

  baseSteps = rotoSteps / circlesPerTurn  ;
  baseSpeed = baseSteps / orbTime;
  
}

void setMotorValues(){

  baseMotor.setMaxSpeed(400);
  baseMotor.moveTo(baseSteps);
  baseMotor.setSpeed(baseSpeed);

  rotoMotor.setMaxSpeed(400);
  rotoMotor.moveTo(rotoSteps);
  rotoMotor.setSpeed(rotoSpeed);  
  
}

void setup(){
  AFMS.begin();
}

If I run the following loop:

Code: Select all

void loop(){

  getMotorValues();
  setMotorValues();
  
  while (rotoMotor.distanceToGo() > 0){

    rotoMotor.runSpeed(); 
 //   baseMotor.runSpeed();
    
  }
  
  releaseMotors();
  while(1);

}
and print out times before and after the while loop, I get the following results:

Code: Select all

start - 18ms
finish - 20032ms
I can live with 14ms off after 20 rotations.

But if I let the second stepper run as well

Code: Select all

void loop(){

  getMotorValues();
  setMotorValues();
  
  while (rotoMotor.distanceToGo() > 0){

    rotoMotor.runSpeed(); 
    baseMotor.runSpeed();
    
  }
  
  releaseMotors();
  while(1);

}
timing turns out a lot worse:

Code: Select all

start - 18ms
finish - 20326ms
I tried running both motors at the same stepping mode (single/double), but that didn't change anything.
I'm not even sure where the problem really comes from - is it the library, is it some error in my code, something wrong with my concept?
Any help would be much appreciated!

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Timing problem with AFMS V2, AccelStepper and 2 steppers

Post by adafruit_support_bill »

That may be due to the i2c overhead of talking to both motors. Have you tried increasing the i2c bus rate as suggested in the FAQ (last question on the page)?
https://learn.adafruit.com/adafruit-mot ... rduino/faq

User avatar
metallissimus
 
Posts: 5
Joined: Sun Aug 10, 2014 9:33 am

Re: Timing problem with AFMS V2, AccelStepper and 2 steppers

Post by metallissimus »

Thank you! I did not connect the question in the FAQ to my problem, but obviously the solution is the same - I'm down to 21ms offset now.
Is there possibly some other similarly easy way for even further improvement?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Timing problem with AFMS V2, AccelStepper and 2 steppers

Post by adafruit_support_bill »

This hack will reduce the overhead even more:
http://forums.adafruit.com/viewtopic.ph ... 1&p=292119

User avatar
metallissimus
 
Posts: 5
Joined: Sun Aug 10, 2014 9:33 am

Re: Timing problem with AFMS V2, AccelStepper and 2 steppers

Post by metallissimus »

I'm not sure if I can easily use the quickstep-function together with AccelStepper, I'll give it a try. But removing the microstepping PWM code sounds interesting, is there an explanation somewhere on how to do that? I guess it's somewhere in the Adafruit_MotorShield.cpp, but I'm not experienced enough to find it respectively afraid of breaking something.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Timing problem with AFMS V2, AccelStepper and 2 steppers

Post by adafruit_support_bill »

Just add quickstep to the library and call it instead of onestep in your code.

Code: Select all

void forwardstep1(){
  stepper1->quickstep(FORWARD);
}

void backwardstep1(){
  stepper1->quickstep(FORWARD);
}

void forwardstep2(){
  stepper2->quickstep(FORWARD);
}

void backwardstep2(){
  stepper2->quickstep(FORWARD);
}

User avatar
metallissimus
 
Posts: 5
Joined: Sun Aug 10, 2014 9:33 am

Re: Timing problem with AFMS V2, AccelStepper and 2 steppers

Post by metallissimus »

Ah, of course... I was a little bit put off by your statement
You can adjust the delayMicroseconds to control the speed.
in the other thread, but obviously that's exactly what AccelStepper is taking care of anyways...

I gain another 2ms from this. Do you think removing microstepping might bring any benefit?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Timing problem with AFMS V2, AccelStepper and 2 steppers

Post by adafruit_support_bill »

Do you think removing microstepping might bring any benefit?
That is what quickstep does. It removes the microstepping.

User avatar
metallissimus
 
Posts: 5
Joined: Sun Aug 10, 2014 9:33 am

Re: Timing problem with AFMS V2, AccelStepper and 2 steppers

Post by metallissimus »

Ok, I understand now.
Thank you very much for your assistance!

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”