Newbie struggles to connect Teensy LC to Xplane11

Status
Not open for further replies.

ELK

New member
Hi I am a newbie to Teensy and Xplane.

The scope of what I am doing is as follows.

I am a student pilot and want to build a Gimbal Cabri G2 cockpit so I can practice my Ground school, Navigation and Radio work at home. I realise there is currently no model yet for the G2 in XPlane, but I am reliably informed it is being made and should be available later this year.

My intention is to build a fully working G2 Cockpit. As I am an experienced builder of electronics and fittings for motorcycles, I am confident I can build all the physical items of a small six pack dash, and all the Heli controls needed to fly the G2.

Input wise I have gone the route of the Leobodnar BU0836x board. I did this because it's plug and play. This allows me to use hall sensors without lots of code. That in turn let me build a custom cyclic and pedals to match the size, weight, and resistance of the real aircraft, without spending £1,000 plus on a pro built set of controls.

While I am purchasing the building a new dedicate PC to fly XPlane from, I have a fast Windows Laptop I am using to test connectivity, inputs and outputs on. As long as I do not attempt to fly the aircraft this Laptop is coping with my panel testing. The system I write on code is a MacBook Pro (I write C# code on my MAC so it seemed like I should stick with it for this exercise).

Input wise everything I have built and tested works great in Xplane. My test inputs are a cyclic, rudder pedals, a twist throttle, a collective, various dash on-off-on switches, rotary encoders, and momentary switches.

Outputwise I want to use Teensy boards as there is great support for integration with XPlane11.

The outputs I am trying to build are the following:

Dash lights - LEDs 12v, however for testing I am using small LEDs suited to an Arduino.
Steeper motors - I have old Aircraft gauges I am gutting and will run with the steeper motors. My intent is to complete a small six pack in real gauges.
3 LCD screens - I will use as the glass touch screens.

While all that is interesting I am starting small with the Teensy LC.

My test platform is a teensy LC, which I have soldered to legs and wires that allow me to attach any pin on the teensy board to a wiring bread board port.

TeensyLCTestBoard.jpg

I loaded all the teensy add ons to the Arduino coding editor.

in Code the board is set to TeensyLC, and the USB is set to "Flight Sim Controls"

My first test was the blink program. This works fine. The result is a blinking onboard LED.

My second test was the same blink program with an extra LED added to the bread board. I am using a 62 Ohm resistor which is a correct size for the LEDs I have and the amps from the Teensy. This test works fine, and I get my extra added LED blinking along with the on board LED.

My Thrid test was to follow the example outlined in this thread https://simelectronics.wordpress.com/2012/10/24/gear-panel-tutorial-part-1-three-greens/. This is where I am struggling.

1. The code is straightforward to follow and I have verified in my version of XPlane has the mention XPlane Ref "sim/cockpit/radios/transponder_light". However when I plug in my teensy to my windows laptop running XPlane nothing happens.

So I assumed I had done something wrong regarding the Transponder light. As I wanted to test the landing lights anyway (I have a physical switch working on the landing light in the Cessna Skyhawk model I am using to test inputs and outputs). I changed the code to fit with my actual test goal.

Code:
// Our LED pin number

const int blueLedPin = 9;


// Special variable to access the landing light
FlightSimInteger landingLight;

// setup runs once
void setup() {
  // set pin mode to out put
  pinMode(blueLedPin, OUTPUT);

  
  landingLight = XPlaneRef("sim/cockpit/electrical/landing_lights_on");
  landingLight.onChange(updateLED);
  pinMode(LED_BUILTIN, OUTPUT);  // pin 11 on Teensy 2.0, pin 6 on Teensy++ 2.0
}

// loop runs repetitively, as long as power is on
void loop() {
  FlightSim.update(); // causes X-Plane's changes to be received
}

// updateLED runs only when X-Plane changes the Landing Light state
void updateLED(long value) {
  if (value == 0) { // Landing light is off
    //Turn LED off
    digitalWrite(blueLedPin, LOW);
    digitalWrite(LED_BUILTIN, LOW);
  } else { // Landing light must be on
    // Turn LED on
    digitalWrite(blueLedPin, HIGH);
    digitalWrite(LED_BUILTIN, HIGH);
  }
}

Applying this to the teensy board is fine, but when I attach the board to the XPlane Laptop, nothing happens.

Can anyone help with this problem. I think a number of things are going on.

A: I assume I need to set the teensy board as a Serial device, as I already have a Joy Stick controller assigned to XPlane, the Leobodnar BU0836x board?
B: In Xplane do I need to set anything in the Data Output tab, "GeneralData Output" or "Dataref Read/Write" sections?

In the DataRef ReadWrite section I can see there is a item for the landing lights on state ("sim/cockpit/electrical/landing_lights_on"), and you can send that to either a network connected address or a COM port, as I am connecting to USB I assume if I set a Dataref Read/Write I'll need to change to COM port (3?)

Then in the Teensy code do I need to set up a serial read, or does "FlightSim.update();" handle this by default?

Anyway any help the forum can offer is great appreciated.

Kind regards

Duncan
 
Ok so I Think I found the issue that was stopping my Landing Light example working.

I had not loaded the Teensy plugin to XPlane 11 https://github.com/PaulStoffregen/cores/commit/372698e9efe01c77382d0d156b99a1cc2ec191c5#diff-04730aa3ab799c3240239be7821140c5. I downloaded the plugin from this GitHub location and followed the how to add Plugins to XPlane11 https://www.youtube.com/watch?v=JGozo9cjxhI at this youtube location.

I made no changes in the Data Output tab.

I did not change the code on the teensy it self at all. So I am assuming the "FlightSim.update();" handles all the serial output from XPlane.

As soon as I restarted Xplane and enabled the Teensy Controls plugin. my Teensy started to react with my landing light.

So I now have a Physical switch that controls a software switch in Xplane, which in turn controls a physical LED.

With the switch off (right most two way switch)

LEDOFF_SwitchOff.jpg

With the switch on (right most two way switch).

LEDon_SwitchON.jpg.

Next test is reading a six pack Altimeter Gauge and sending increments to a stepper motor.

Once I have a test environment I am happy to share more of, I'll add more photos and code to help other newbies out.

Kind regards

Duncan
 
Ok just to complete the circle on this pots.

I am adding the completed Landing light code so anyone else attempting this exercise has a starting point.

The Setup is this:

I am using the Teensy LC board to capture output from X-Plane only, inputs are via another board (see above).
I have a LED on a bread board with the positive leg going to one side of a 62Ohm resistor and the other leg of the resistor going to Pin 9 on the teensy board. The negative leg of the LED is going to a ground pin on the teensy board.
In the photos above you'll see a load of other wires, ignore these, I have simply permanently wired up every pin on a Teensy LC to a bread board so I have unlimited test setup scenarios.

Xplane is setup as a standard sim.
I have the Teensy Controls plugin loaded and enabled. without this running my teensy LC board would not see any DataRef changes.
I also install the X Plane plugin DataRefEditor. This is a screen that popup in X plane and allows to to locate DataRefs that the Teensy board can interact with. which helps with coding the correct calls in the Teensy code editor. I have also found it helpful to see what data changes when virtual switches are altered. You can then set the correct parameters in code based upon what the detail in the DataRefEditor was displaying.
I am using a default aircraft for testing my control panel, which allows me to call the default cockpit or instrument DataRefs. Once I start working on a specific aircraft I'll establish if I need to alter my code to address the other models available within X Plane.

Now for the Code.

I have the Arduino editor installed and have the Teensy plugin added to that, which brings in all the extras your teensy board requires, inclusive of the board type and FlightSim USB settings.

In this example, my goal was to do the following. Turn a landing light waring light (the LED on the teensy) whenever the landing light on the aircraft was live. While I have a physical switch working with X Plane (see explanation above), this example will work fine using just the virtual switches within X Plane itself.

Criteria I have taken into account that has resulted in the code example below.

1. I have assumed no power to the aircraft is possible if the ignition key is in the off position.
2. The ignition key has 3 positions it can be in regardless of a running engine where power to the aircraft is available mag1, mag2, and both (turning to start is a momentary position that returns to the both position once the starter is released).
3. The Master battery switch needs to be on before any instruments or lights can be powered.
4. The Landing lights switch has to be on for the Landing lights to work.

Therefore for the Landing lights to be on The ignition switch must be in position 1 to 3, the Master Battery switch must be on, and the Landing light switch must be on.

Because of these criteria I have made three X Plane checks that establish the state of each of the three switches, whenever any of the switches state changes.

I am sure this code can be compressed but for now the example works. That said if anyone else has tidier code and wants to update this example please feel free do to so.

Code:
/*
 * This example code setups up a external LED on a Teensy LC microncroller that is turned on and off
 * by oppertaing switches with X plane.
 * 
 * To create some realisum, the landing light switch does not work on its own, it requires the ignition key is in one of the 3 positions that give power to the aircraf
 * and that the Master Battery switch is on.
 * 
 * Once this code is on your Teensy board make sure the Teensy Controls plugin is loaded and enabled within Xplane.
 * 
 * This code was written for and tested on X Plane 11, I have not tested it on any other version of the software.
 *
 */

// The LED pin number
const int blueLedPin = 9;

// Varibles that hold the last reported state of the three key switches considered in the IF statement. The default state being off or zero
long ignitionOnState = 0; // state can be 0 off, 1 mag1, 2 mag2, or 3 both
long batteryOnState = 0;  // 0 off, or 1 on
long landingLightOnState = 0; // zer off, or 1 on


// Special variable to access the landing light DataRef value
FlightSimInteger landingLight;
// Special variable to access the engine ignition key DataRef value
FlightSimInteger igitionKey;
// Special variable to access the battery on DataRef value
FlightSimInteger masterBatterySwitch;

// setup runs once
void setup() {
  // set pin mode to out put
  pinMode(blueLedPin, OUTPUT);

  //check current state of the ignition key
  igitionKey = XPlaneRef("sim/cockpit/engine/ignition_on");
  igitionKey.onChange(updateIgnitionKey); // send the current state to the ignition key function
  
  //check the current state of the master battery switch
  masterBatterySwitch = XPlaneRef("sim/cockpit/electrical/battery_on");
  masterBatterySwitch.onChange(updateMasterBattery); // send the current state to the master battery function

  // check the current state of the landing light switch
  landingLight = XPlaneRef("sim/cockpit/electrical/landing_lights_on");
  landingLight.onChange(updateLandingLight); // send the current state to the landing light function
  
  
  pinMode(LED_BUILTIN, OUTPUT);  // pin 11 on Teensy 2.0, pin 6 on Teensy++ 2.0
}

// loop runs repetitively, as long as power is on
void loop() {
  FlightSim.update(); // causes X-Plane's changes to be received by the teensy, as long as the Teensy Controls plugin is active.
}

// updateLandingLight function runs when X-Plane changes the Ignition Key state, Master Battery state or Landing Light state
void updateLandingLight(long value) {

    landingLightOnState = value; // grab the latest Landing light DataRef value and update the variable with it

   // This IF statement is checking to see what switch combinations are in play
  if ((ignitionOnState == 0 & batteryOnState == 0 & landingLightOnState == 0) |
      (ignitionOnState > 0 & batteryOnState == 0 & landingLightOnState == 0) |
      (ignitionOnState > 0 & batteryOnState == 1 & landingLightOnState == 0) |
      (ignitionOnState == 0 & batteryOnState == 1 & landingLightOnState == 1) |
      (ignitionOnState == 0 & batteryOnState == 1 & landingLightOnState == 0) |
      (ignitionOnState > 0 & batteryOnState == 0 & landingLightOnState == 1) |
      (ignitionOnState == 0 & batteryOnState == 0 & landingLightOnState == 1) ){ // Landing Light  is off
    
    //If any of the above combinations are set the teensy will Turn off the LED
    digitalWrite(blueLedPin, LOW);
    digitalWrite(LED_BUILTIN, LOW);
 
    
  } else { // If none of the above criteria apply then the Landing Light must be on
    // The teensy turns on the
    digitalWrite(blueLedPin, HIGH);
    digitalWrite(LED_BUILTIN, HIGH);
    
  }
}

// updateIgnitionKey only runs when X-Plane changes the Ignition Key state
void updateIgnitionKey(long value) {

  ignitionOnState = value; // here we set the current state DataRef value to the iginition key variable
  updateLandingLight(landingLightOnState); // As the ignition key has moved we need to prompt the landing light to check it is still on
                                           //so we call it's function.
}

// upateMasterbattery only runs when X-Plane changes the battery_on state
void updateMasterBattery(long value) {

  batteryOnState = value; // here we set the current state DataRef value to the Master Battery variable
  updateLandingLight(landingLightOnState);// As the Master battery switch has moved we need to prompt the landing light to check it is still on
                                           //so we call it's function.
  
}


I hope this helps someone.

Enjoy, Duncan
 
Hello Duncan,

Thanks for this, I am just starting out with teensy and x plane 11. Will this work with the teensy 3.2?
 
Status
Not open for further replies.
Back
Top