Sketch assistance is desperately requested for XP11 with Teensy

EAL727Capt

Well-known member
Greetings!

For the past two (2) months, I have been attempting to write coding/sketch for a stepper motor to be interfaced with my home cockpit and am having zero success.
I am reaching-out for help as, at this point, I'm at a complete loss as to how to do the coding to get this one indicator to mimic what is happening in X-Plane 11.

I'm using a Teensy 3.2 with a DRV8825 stepper driver and a NEMA 17 stepper motor.

The goal is to operate an OEM flaps indicator gauge from a Boeing 727. There are two (2) gauges on the aircraft an show the flaps positions for each wing, both inboard and outboard.

Right now, I'm just working on the OUTBOARD LEFT indications; once I have a successful sketch, I can add the OUTBOARD RIGHT, INBOARD LEFT and INBOARD RIGHT, all based upon the various X-Plane data refs.
I have the Flight Sim Controls loaded onto my Teensy 3.2 and have become familiar with its operations. I have posted some videos on my YouTube.com channel...

www.youtube.com/eal727capt

Thus far, here is my code:

Code:
/*  FLAPS GAUGE  T E S T I N G  WITH MAP AND RETURN MAP FUNCTIONS

  Using Teensy 3.2, DRV8825 stepper driver board and NEMA 17 stepper motor

  200 steps/revolution
  1/16 micro-stepping

  Stepper motor .. 78 teeth
  Transmitter....  78 teeth

*/


#include <AccelStepper.h>

AccelStepper FLAPSobL(1, 14, 15);   //1=Library function, 14=Step, 15=Direction    Outboard Left flaps position

FlightSimFloat FlapOBleft;

float FlapsOBleft_pos;
const long STEPS_PER_REVOLUTION = 200;
const float STEPS_PER_DEGREE = ((float) STEPS_PER_REVOLUTION) / 360.0;

void setup() {
  Serial.begin(9600);
  FLAPSobL.setMaxSpeed(3000);
  FLAPSobL.setAcceleration(3000);
  FlapOBleft = XPlaneRef("sim/flightmodel/parts/flap_def[8]");
}

void loop() {
  FlightSim.update();
  if (FlapOBleft == 0) { // Flaps 0
    return (FlapsOBleft_pos, (0));
    FLAPSobL.moveTo(FlapsOBleft_pos);
    FLAPSobL.run();
  }
  if (FlapOBleft <= 2) { // Flaps 2
    return (FlapsOBleft_pos, (580));
    FLAPSobL.moveTo(FlapsOBleft_pos);
    FLAPSobL.run();
  }
  if (FlapOBleft <= 5) { // Flaps 5
    return (FlapsOBleft_pos, (1350));
    FLAPSobL.moveTo(FlapsOBleft_pos);
    FLAPSobL.run();
  }
  if (FlapsOBleft_pos <= 15) { // Flaps 15
    return map(FlapsOBleft_pos, 5, 15, 1350, 1650);
    FLAPSobL.moveTo(FlapsOBleft_pos);
    FLAPSobL.run();
  }
  if (FlapsOBleft_pos <= 20) { // Flaps 20
    return map(FlapsOBleft_pos, 15, 20, 1650, 1800);
    FLAPSobL.moveTo(FlapsOBleft_pos);
    FLAPSobL.run();
  }
  if (FlapsOBleft_pos <= 25) { // Flaps 25
    return map(FlapsOBleft_pos, 20, 25, 1800, 1900);
    FLAPSobL.moveTo(FlapsOBleft_pos);
    FLAPSobL.run();
  }
  if (FlapsOBleft_pos <= 30) { //Flaps 30
    return map(FlapsOBleft_pos, 25, 30, 1900, 2030);
    FLAPSobL.moveTo(FlapsOBleft_pos);
    FLAPSobL.run();
  }
  else {
    return map(FlapsOBleft_pos, 30, 40, 2030, 2350);// Flaps 40
    FLAPSobL.moveTo(FlapsOBleft_pos);
    FLAPSobL.run();

    long currentPosition = FLAPSobL.currentPosition();
    currentPosition = (currentPosition % STEPS_PER_REVOLUTION + STEPS_PER_REVOLUTION) % STEPS_PER_REVOLUTION; // positive modulo
    long targetPosition = (long)(FlapsOBleft_pos * STEPS_PER_DEGREE);
    long cwSteps;
    long ccwSteps;
    if (targetPosition > currentPosition) {
      cwSteps = targetPosition - currentPosition;
      ccwSteps = currentPosition - targetPosition + STEPS_PER_REVOLUTION;
    } else {
      ccwSteps = currentPosition - targetPosition;
      cwSteps = targetPosition - currentPosition + STEPS_PER_REVOLUTION;
    }
  }
}

The number of steps (which I've previously confirmed for each flap setting) are as follows:

0..........0
2..........580
5..........1350
15........1650
20........1800
25........1900
30........2030
40........2400

Way back when, I had seen a similar project used for FSX and the video demonstrated the flaps gauge needle moving on a similar gauge but for a Boeing 737 and am convinced that this can be accomplished for my project.

In my sketch, nothing I've tried moves the stepper motor...not a single step, not a single micro-step, and I'm rapidly approaching a state of total frustration with this.

I could get the stepper to move (using a different sketch) from flaps 0 to flaps 2, to flaps 5, vice versa, but anything above flaps 5 and the stepper continuously spins.
It should be noted that 0 to 2 to 5 happens relatively quickly and from 5 to 15 and above and the indicator slows down...
Again, I have a video on my YouTube channel to demonstrate.

I would certainly appreciate and welcome any suggestions as to what I'm obviously missing here.
I'm new to coding and am trying my best to get it.

Thank you very much in advance.

Jay
 
The return statements are preventing the code doing anything at all.
Code:
    return (FlapsOBleft_pos, (0));
should presumably be:
Code:
    FlapsOBleft_pos = 0;
and the other return statements also need to be changed.

Pete
 
Thank you for the reply.
My coding was loosely based on a sketch for FSX included here:
Code:
//  Stepper Motor Flap Guage for Link2FS
//  v. 1.0
//  1/20/2014
//
//  Outlines how to control a small stepper motor for a slaps gauge.
//  Use for free. Do not distribute for fee.  Intended to be free forever...
//
//  Borrowed heavily from:
//    Jim's page: http://www.jimspage.co.nz/Link2fs_Multi.htm
//    4tronix page for my specific stepper: http://www.4tronix.co.uk/arduino/Stepper-Motors.php
//
//  Built to work with the cheap, small stepper from amazon:
//  28BYJ-48 28BYJ48 DC 5V 4-Phase 5-Wire Arduino Stepper Motor with ULN2003 Driver Board
//
//  Configured to map incoming data to corresponding steps for 737 gauge
//
////////////////////////////////////////////////
 
 
int CodeIn;          // used on all serial reads
int flapMotorPosition;   // Global variable to track current step count for flap motor
 
 
 
 
//declare variables for pins....
int flapMotorPin1 = 8;   // Blue   - 28BYJ48 pin 1
int flapMotorPin2 = 9;   // Pink   - 28BYJ48 pin 2
int flapMotorPin3 = 10;   // Yellow - 28BYJ48 pin 3
int flapMotorPin4 = 11;   // Orange - 28BYJ48 pin 4
int flapMotorLookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
 
 
 
 
//Main Config variables...
float flapMultiplier = 1.422; //Used to multiply incoming flap degrees to number of steps for stepper. 512 steps per 360 degrees for me....
int flapstepDelay = 3000;  //variable to set stepper speed. lower = faster motor speed...
 
 
//////////////////////////////////////////////////////////////////////////////
void setup() {
 //declare the motor pins as outputs
 pinMode(flapMotorPin1, OUTPUT);
 pinMode(flapMotorPin2, OUTPUT);
 pinMode(flapMotorPin3, OUTPUT);
 pinMode(flapMotorPin4, OUTPUT);
 Serial.begin(115200);   // NEEDS TO BE 115200 TO WORK WITH LINK2FS!!!!
 
 
 flapMotorPosition = 512; //Sets motor position to worst-case scenaio location
 stepTo(0);      //Index motor back to flap position 0. Assumes index pin on gauge face.
 Serial.println("Gauge reset and configured.");
}
 
 
//////////////////////////////////////////////////////////////////////////////
void loop(){
 if (Serial.available()) {
   CodeIn = getChar();//get a character
   //Serial.println(CodeIn);
   if (CodeIn == '<') {LESSTHAN();}// The first identifier is "<"
 }//end of serial check
}
 
 
//////////////////////////////////////////////////////////////////////////////
 
 
 
 
char getChar()// Get a character from the serial buffer
{
 while(Serial.available() == 0);// wait for data
 return((char)Serial.read());// Thanks Doug
}
 
 
//////////////////////////////////////////////////////////////////////////////
 
 
void LESSTHAN(){    // The first identifier was "<"
 
 
 CodeIn = getChar();       // Get another character
 switch(CodeIn) {          // Now lets find what to do with it
   case 'G':               // Found the second identifier ("G" Flaps position)
                            // Initialize some variables needed for flaps...
     String  flapLink2FS;   // Store incoming flap metrics from Link2FS
     float flaps737degree;  // Stores degrees deflection on 737 flap gauge.
     int flapStepPosition;  // Store Position count for flap stepper motor
     
     flapLink2FS += getChar();  //Get three characters for flap data from Link2fs
     flapLink2FS += getChar();
     flapLink2FS += getChar();
     flaps737degree = flapMeasureTo737Degree(flapLink2FS.toInt()); //Converts Link2FS Metric to degree deflection on 737 gauge
     flapStepPosition = flaps737degree * flapMultiplier; // Converts degrees deflection to step count
     stepTo(flapStepPosition);
 }
}
 
 
float flapMeasureTo737Degree(int flapMeasure){
//  flapMeasureTo737Degree converts the code coming from link2FS to degrees
//  the needle deflects on a 737 Flap guage...
//  Input: int flapMeasure (number from 0 to 57).
//  Ouput: int degrees for needle from 0 indication.
 
 
 if (flapMeasure==0){ //Flaps 0 on 737 in FSX
   return 0;
 }
 if (flapMeasure==1){ //Flaps 1
   return 36;
 }
 if (flapMeasure==3){ //Flaps 2
   return 72;
 }
 if (flapMeasure<=7){ //Flaps 5 Setting
   return map(flapMeasure, 3, 7, 72, 114);
 }
 if (flapMeasure<=14){ //Flaps 10 Setting
   return map(flapMeasure, 7, 14, 114, 146);
 }
 if (flapMeasure<=21){ //Flaps 15 Setting
   return map(flapMeasure, 14, 21, 146, 180);
 }
 if (flapMeasure<=36){ //Flaps 25
   return map(flapMeasure, 21, 36, 180, 215);
 }
 if (flapMeasure<=43){ //Flaps 30
   return map(flapMeasure, 36, 43, 215, 240);
 }
 else{
   return map(flapMeasure, 43, 57, 240, 270);
 }
}
 
 
void stepTo(int targetPosition){
 do{
    if(flapMotorPosition < targetPosition){
      clockwise();
      flapMotorPosition++;
    }
    else if(flapMotorPosition > targetPosition){
     anticlockwise();
     flapMotorPosition--;
    }
 } while (flapMotorPosition != targetPosition );
}
 
 
 
 
 
 
void anticlockwise(){
// step motor 1 step anti-clockwise
 for(int i = 0; i < 8; i++)
 {
   flapSetOutput(i);
   delayMicroseconds(flapstepDelay);
 }
}
 
 
void clockwise(){
// step motor 1 step clockwise
 for(int i = 7; i >= 0; i--)
 {
   flapSetOutput(i);
   delayMicroseconds(flapstepDelay);
 }
}
 
 
void flapSetOutput(int out)
{
 digitalWrite(flapMotorPin1, bitRead(flapMotorLookup[out], 0));
 digitalWrite(flapMotorPin2, bitRead(flapMotorLookup[out], 1));
 digitalWrite(flapMotorPin3, bitRead(flapMotorLookup[out], 2));
 digitalWrite(flapMotorPin4, bitRead(flapMotorLookup[out], 3));

I had previously tried it without the (0), (580) and (1350) and nothing moved. I just want the stepper to move X amount of steps based on the flap setting.

Sorry for the confusion.
 
In my sketch, nothing I've tried moves the stepper motor...not a single step, not a single micro-step, and I'm rapidly approaching a state of total frustration with this.

As a quick test to make sure the hardware still works, try disconnecting the STEP signal from Teensy's pin and just touch the wire alternately to GND and 3.3V. The motor should move. If you get no movement, at least you can focus your attention on the hardware side. If it does move, maybe at another quick test upload File > Examples > 01.Basics > Blink, with the pin number changed to whatever pin has the STEP signal connected.
 
The hardware works perfectly…everything is correctly hooked-up.
I had tested earlier in the day, prior to posting.
 
Back
Top