AccelStepper Code question

Hi all,
I am stumped on this one so I hope you can help me out. In the first code example I used it to test the stepper motor and Home function. Worked perfect! In the second code example I just changed the Loop section only to interface it to x-plane. In the second code example it runs to the Home position then stops and never goes to the Loop section. If I comment out the StepperHome(); in setup, the stepper works fine with x-plane with the void loop code. Why does it not go to Loop in the second code but goes to Loop fine in first code?? So confused.
#1

#include <AccelStepper.h>

AccelStepper stepper1(1, 1, 0);

const int homeButton = 7;
const int ledPin = 13;
byte hBval;


void setup(){
  stepper1.setMaxSpeed(100); //nice and slow for testing
  stepper1.setAcceleration(70);
  pinMode(homeButton, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  stepperHome(); //runs routine to home motor
}
void loop(){
  stepper1.moveTo(100); // random position to end for testing
  stepper1.runToPosition();
  delay(1000);
  stepper1.moveTo(0);
  stepper1.runToPosition();
  delay(2000);
  stepper1.moveTo(-100); // random position to end for testing
  stepper1.runToPosition();
  delay(1000);
}

void stepperHome(){ //this routine should run the motor
  hBval = digitalRead(homeButton);
  while (hBval == LOW)
  {
    //backwards slowly till it hits the switch and stops
    stepper1.moveTo(-400);
    stepper1.run();
    digitalWrite(ledPin, HIGH); //indicates it's doing something
    hBval = digitalRead(homeButton);
  }
  digitalWrite(ledPin, HIGH); //indicates it's doing something
  stepper1.setCurrentPosition(0); //should set motor position to zero and go back to main routine
}

#2 Code

#include <AccelStepper.h>

AccelStepper stepper1(1, 1, 0);

FlightSimFloat adiroll;
const int homeButton = 7;
const int ledPin = 13;
byte hBval;
float cur_pos;

void setup() {
  stepper1.setMaxSpeed(100); //nice and slow for testing
  stepper1.setAcceleration(70);
  pinMode(homeButton, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  stepperHome(); //runs routine to home motor
  adiroll = XPlaneRef("sim/cockpit2/gauges/indicators/roll_electric_deg_pilot");
}
void loop() {
  FlightSim.update();
  cur_pos = adiroll * -3.33;
  stepper1.moveTo(cur_pos);
  stepper1.runToPosition();
}

void stepperHome() { //this routine should run the motor
  hBval = digitalRead(homeButton);
  while (hBval == LOW)
  {
    //backwards slowly till it hits the switch and stops
    stepper1.moveTo(-400);
    stepper1.run();
    digitalWrite(ledPin, HIGH); //indicates it's doing something
    hBval = digitalRead(homeButton);
  }
  digitalWrite(ledPin, HIGH); //indicates it's doing something
  stepper1.setCurrentPosition(0); //should set motor position to zero and go back to main routine
}

It boggles the mind why anyone would put the stepper motor driver in the hardware serial pins, eliminating the possibility of debugging the program.

PaulS: It boggles the mind why anyone would put the stepper motor driver in the hardware serial pins, eliminating the possibility of debugging the program.

Agreed. There is not much point trying to offer help until that is sorted out and a new version of the code is posted.

...R

I'm using a Teensy 3.1 and TeensyLC, no where in the documentation does it say that pins 0 and 1 are hardware serial only pins. I didn't write the majority of this code, just added the Flightsim specific stuff. I am new and learning so pardon me if all this boggles your mind. I know it does mine hence the reason for the original post. Thanks Rob

I'm using a Teensy 3.1

But, you are posting on an Arduino forum. This should have been stated right up front.

Did you try moving the motor driver to some other pins, and using Serial.print() to see what is happening?

Just how IS the flight simulator thing attached to the (not an) Arduino?

PaulS: But, you are posting on an Arduino forum. This should have been stated right up front.

Did you try moving the motor driver to some other pins, and using Serial.print() to see what is happening?

Just how IS the flight simulator thing attached to the (not an) Arduino?

Yes, I should have stated that up front. Since i'm using the Arduino IDE and language that is why I posted here. Sorry about that. I will try changing pins this evening.

It communicates through the USB with true usb and not emulated serial data like the arduino does.

Rob

void stepperHome() { //this routine should run the motor
  hBval = digitalRead(homeButton);
  while (hBval == LOW)
  {
    //backwards slowly till it hits the switch and stops
    stepper1.moveTo(-400);
    stepper1.run();
    digitalWrite(ledPin, HIGH); //indicates it's doing something
    hBval = digitalRead(homeButton);
  }
  digitalWrite(ledPin, HIGH); //indicates it's doing something
  stepper1.setCurrentPosition(0); //should set motor position to zero and go back to main routine
}

It looks to me as if you are always telling the motor to move to the same place (-400) instead of getting it to move 1 step in the direction of the limit switch.

...R

Each call to run() can only progress the motor at most one step.

The typical mistake is to forget to keep calling run() everywhere it needs to be.

If you code tightly and never use delay() it may be enough to just call run() once in loop(), since loop will be running frequently enough - but if you have loops elsewhere that hog the processor, put a call to run() in them.

MarkT: Each call to run() can only progress the motor at most one step.

That is very true.

But the OPs code tells the stepper to move to -400 in the homing process when the code has no idea where -400 might be. If it thought it was already at -400 it would not move.

Setting a destination only makes sense after you have esablished the zero or home position.

...R

My assumption when I saw the code, which has been found to be true so far, was that running it backwards in this case 400 steps it will eventually trigger the home switch thereby setting the home/0 position ref. It works that way perfectly every single time no matter where it starts from. Then it proceeds to the loop and runs to the 100 0 -100 perfectly every single time. This is the only code I have running on the teensy so maybe that is why i am getting away with calling the run() command frequently? The problem comes in code #2 Once it finds home, the loop section never starts.

It works that way perfectly every single time no matter where it starts from.

Provided that it never needs to step more than 400 times to get to where you want it to stop.

The problem comes in code #2 Once it finds home, the loop section never starts.

So, some serial output would be useful.

I changed to pins 2 and 3, no joy. I will put some debug code in #2 tomorrow

Ok, I added debug serial print to code, see below. It homes the stepper and prints “done with stepper home” then immediately prints Loop has started. But nothing happens in the Loop!? If I comment out the StepperHome() in setup then the Loop function works fine.

Revised Code,

#include <AccelStepper.h>

AccelStepper stepper1(1, 3, 2);

FlightSimFloat adiroll;
const int homeButton = 7;
const int ledPin = 13;
byte hBval;
float cur_pos;

void setup() {
  Serial.begin(9600);
  stepper1.setMaxSpeed(1000); //nice and slow for testing
  stepper1.setAcceleration(900);
  pinMode(homeButton, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  stepperHome(); //runs routine to home motor
  adiroll = XPlaneRef("sim/cockpit2/gauges/indicators/roll_electric_deg_pilot");
}
void loop() {
  Serial.print("loop has started");
  FlightSim.update();
  cur_pos = adiroll * -6.67;
  stepper1.moveTo(cur_pos);
  stepper1.runToPosition();
}

void stepperHome() { //this routine should run the motor
  hBval = digitalRead(homeButton);
  while (hBval == LOW)
  {
    //backwards slowly till it hits the switch and stops
    stepper1.moveTo(-800);
    stepper1.run();
    digitalWrite(ledPin, HIGH); //indicates it's doing something
    hBval = digitalRead(homeButton);
    Serial.print("HomeButton is");
    Serial.println(hBval);
  }
  digitalWrite(ledPin, LOW); //indicates it's doing something
  stepper1.setCurrentPosition(0); //should set motor position to zero and go back to main routine
  Serial.print("Done with stepper Home");
}

There is something wrong with this line

FlightSimFloat adiroll;

...R

  cur_pos = adiroll * -6.67;

I guess I fail to see how the current position of anything is -6.67 times that address of the instance of the FlightSimFloat class, whatever the heck that is.

FlightSimFloat is part of the Class FlightSimControls that is used in the Teensy to communicate with X-plane flight simulator. It is Float value from -90.0 -- 0.0 --- +90.0

The code adiroll * -6.67 outputs the correct position for the stepper to drive to. In my case +30 degree bank would be 30*-6.67=-200 so the stepper then moves to the -200 position in this line,

stepper1.moveTo(cur_pos);

In retrospect, cur_pos should be renamed to command_pos or com_pos;

Regardless, The loop code works pefect if I don't use the StepperHome function. This makes no sense to me so maybe it has something to do with how Teensy and FlightSim class and AccelStepper Class all interact.

I have reported it as a potential bug to the maker of Teensy so I hope he can make heads or tails of it.

Rob

737nut: FlightSimFloat is part of the Class FlightSimControls

In that case it looks like there is a #include missing from your code

...R

Robin2: In that case it looks like there is a #include missing from your code

...R

In typical Arduino world that would be true but in this special case he made it part of the load process so when I upload my sketch I select FlightSimConrols as the Comm method and it is all automatic. It actually makes it very easy to interface to the sim via the Teensy. It actually makes it faster and allows MultiThreaded coms with the Sim.

737nut: It actually makes it very easy to interface to the sim via the Teensy.

It just means I am out of my depth. The problem could be anywhere in the code we can't see.

This may be another example where an attempt to make things "simple" actually makes them much more complex when simple does not work.

I hope you find a solution.

...R

To anyone interested, fixed the problem. Turned out to be simple, change this line stepper1.runToPosition();

to stepper1.run();

thats it, works great!