Welcome to Cockpitbuilders.com. Please login or sign up.

March 28, 2024, 05:46:58 PM

Login with username, password and session length

PROUDLY ENDORSING


Fly Elise-ng
125 Guests, 0 Users
Members
Stats
  • Total Posts: 59,639
  • Total Topics: 7,853
  • Online today: 148
  • Online ever: 582
  • (January 22, 2020, 08:44:01 AM)
Users Online
Users: 0
Guests: 125
Total: 125

COUNTDOWN TO WF2022


WORLDFLIGHT TEAM USA

Will Depart in...

Recent

Welcome

Wet Compass Code (Arduino/Teensy)

Started by RayS, November 22, 2016, 02:29:38 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

RayS

The code below runs on a Teensy 3.2 but could be easily ported to run on an Arduino.
It uses the Stepper libraries and the Teensy-Duino library found here:
https://www.pjrc.com/teensy/td_flightsim.html

Project uses a 720-steps per revolution stepper motor and an optical sensor so it can slave to
a known starting point before feeding it heading info.

================================================================
#include <SoftwareSerial.h>
#include <serLCD.h>
#include <Stepper.h>

const int stepsPerRevolution = 720;  // change this to fit the number of steps per revolution
// for your motor
    int Direction = 0; // 0 = CCW, 1 = CW
    int sensorVal = 0;
     
// initialize the stepper library on pins 2 through 5:
    Stepper myStepper(stepsPerRevolution, 2,3,4,5);

// X-Plane Stuff
FlightSimFloat aircraftHeading;

int _aircraftHeading = 0;
int compassHeading;
   
void setup() {
  pinMode(23, INPUT_PULLUP);
  pinMode(13, OUTPUT);

  // set the speed at 60 rpm:
  myStepper.setSpeed(30);
 
  // SET UP X-PLANE REFS
  aircraftHeading = XPlaneRef("sim/cockpit/misc/compass_indicated");

   //---- Start Seek To Zero ----//
   // Test if we're blocking the sensor
      sensorVal = digitalRead(23);
      if (sensorVal == LOW)
      {
      Direction = 1;
      myStepper.step(80); // Get it out of the way
      }

      sensorVal = digitalRead(23);
      while (sensorVal == HIGH)
      {
      Direction = 1;
      myStepper.step(1);
      sensorVal = digitalRead(23);
      } 

  // Now Slew to North
  myStepper.step(196); //<< Your results may vary

  delay(1000);
  // Set Known starting points   
  _aircraftHeading = 0;
  compassHeading  = 0;

 
}

void loop() {
   // Query X-Plane Data
  FlightSim.update();
  _aircraftHeading = (int)aircraftHeading;

  // Compensate for zero-crossing
  if(compassHeading - _aircraftHeading > 180) {_aircraftHeading = _aircraftHeading + 360;}
  else if (compassHeading - _aircraftHeading < -180) {_aircraftHeading = _aircraftHeading - 360;}
 
  // Slew Right (Delay(5) adds rotational dampening)
  if(_aircraftHeading > compassHeading)
  {
    compassHeading += 1;
    myStepper.step(1);
    delay(5);   
    myStepper.step(1);
    delay(5);
    if (compassHeading > 359) {compassHeading = 0;}
    }


  // Slew Left (Delay(5) adds rotational dampening)
  else if(_aircraftHeading < compassHeading)
  {
    compassHeading -= 1;
    myStepper.step(-1);
    delay(5);   
    myStepper.step(-1);
    delay(5);
    if (compassHeading < 1) {compassHeading = 360;}
   }
   
}





Ray Sotkiewicz

sagrada737

Hi Ray,

Thanks for posting your code for the Teensy control of a magnetic compass function.   I understand that you are using XPlane, but do you know of a way that this could be done with P3D?

Also, any photos/videos of your prototype setup for this project?

Keep up the good work - sounds like a fun project.

Mike
Full-scale 737-800 Sim; P3d v5.3x with Sim-Avionics (two computers), FDS MIP,  FlightIllusion hardware.  3-Optoma ZH406ST Laser HD projectors, with 4K inputs from a single Nvidia RTX-4090 GPU (new), resulting in a 210 deg wrap-around display.  6dof Motion Platform using BFF 6dof motion software, driven by a Thanos Servo Controller to 6.2 KW Servos, Lever type actuators.

RayS

#2
Hi Mike,

Unfortunately I don't know too much about interfacing P3D with Arduino/Teensy, although I imagine it might look something like the attached picture...  ;) :angel:

You might find some good tidbits here:
https://code.google.com/archive/p/arduino-fs/

That's the really cool thing about X-Plane and Teensy. The libraries are already in place and it's just a simple matter of writing a few dozen lines of code to get it to interact.



Ray Sotkiewicz

Trevor Hale

Mike if you have a look around in the arduino forum, I have posted some examples of my arduino code.  the language is basically the same and can almost integrate directly into the program I wrote.  Just need to define the Stepper info.

I am using my arduino with FSX and FSUIPC, no reason it wouldn't work with P3D.

Trev
Trevor Hale

Owner
http://www.cockpitbuilders.com

Director of Operations
Worldflight Team USA
http://www.worldflightusa.com

VATSIM:

fsaviator

Ray, Happy Thanksgiving!

I'll be home on the 6th of December.  Let's try to fit some time in over the holidays at some point.  Definitely want to sit with you for some help getting started (finally) with arduino and this compass is perfect.  It is exactly what I need to literally top off my budget GA pit!  Of course it will then help out as it is the last piece of my 737 that I haven't sourced yet.

I want to get this thing up and running as I head out to Thailand, Cambodia, and Nepal in mid-February for almost a month.

I'll go ahead and order a couple of teensy's in the meantime as I needed one anyway.  Should I get the 3.2 or 3.6?  Pins or no pins?  One of each?

;D ;D
Warren "FSAviator"
http://www.B737NG-Sim.com  |  https://www.facebook.com/fsaviator/
P3D45/ Prosim737 2/ ACE Dual-linked Yokes/ RevSim Proline TQ and Dual-linked Rudders/ CPFlight MCP PRO3 and EFIS'; MIP737ICS_FULL and SIDE737; Forward and Aft Overheads; Pedestal/ FDS MIP

RayS

Hi Warren,

Great to hear from you again!

I forgot if you were X-Plane or P3D/FSX. If X-Plane, definitely get yourself a couple of Teensy 3.2 boards with the pin and header kits. The code will logically work with an Arduino but you will need to jump through a few more hoops than you would need to if you were using X-Plane / Teensy.

The parts you'll need:

OBP990L11Z (Or similar device) Opto-Interrupter.
You can cross-reference thse at:
http://hu.farnell.com/photo-interrupters/prl/talalatok/5

Stepper motor:
X27 168 720 Steps/Revolution
Cross reference here:
https://www.adafruit.com/product/2424
(You can pick these up on eBay for MUCH cheaper though..)

I'll be posting a how-to video shortly. I'll post the link to it here...

Hope this helps!
Ray Sotkiewicz

RayS

Ray Sotkiewicz

fsaviator

Ordered.  I am using FSX so I'll have to figure that out.
I have everything I need, to include these speedo steppers.  I just need a hands on walkthrough to figure out the process.
Warren "FSAviator"
http://www.B737NG-Sim.com  |  https://www.facebook.com/fsaviator/
P3D45/ Prosim737 2/ ACE Dual-linked Yokes/ RevSim Proline TQ and Dual-linked Rudders/ CPFlight MCP PRO3 and EFIS'; MIP737ICS_FULL and SIDE737; Forward and Aft Overheads; Pedestal/ FDS MIP

RayS

#8
Warren,

Have you seen this guy?
http://www.mobiflight.com/

I just found him and haven't really tried anything out yet from him but it looks WAY easier than the route I took...

One look at my #include header is a dead giveaway as to the complexity of interfacing Arduino with FSX/X-Plane....

#include <thisisreallyugly.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <EthernetClient.h>
#include <Servo.h>
#include <SPI.h>         
#include <Wire.h>
#include <XP_ARDref.h>         // -- include XPlane ARDref library
#include <SoftwareSerial.h>
Ray Sotkiewicz

fsaviator

That is pretty nice.  I think I can get started with that!

Thanks!
Warren "FSAviator"
http://www.B737NG-Sim.com  |  https://www.facebook.com/fsaviator/
P3D45/ Prosim737 2/ ACE Dual-linked Yokes/ RevSim Proline TQ and Dual-linked Rudders/ CPFlight MCP PRO3 and EFIS'; MIP737ICS_FULL and SIDE737; Forward and Aft Overheads; Pedestal/ FDS MIP

XOrionFE

Thank you for this code and the video Ray.   I have had mine up there over my head lit up but immobile more as a static item but now I will make it work.   

Very nice!

Scott

Like the Website ?
Support Cockpitbuilders.com and Click Below to Donate