Cockpit

* User Profile

Welcome, Guest. Please login or register.

Login with username, password and session length

Who's Online

  • *Users: 2
  • *Guests: 43
  • *Total: 45

Our Discord

Author Topic: Wet Compass Code (Arduino/Teensy)  (Read 5139 times)

Offline RayS

  • I spend way too much on this forum!
  • *
  • Posts: 817
  • Private Pilot, Sim Enthusiast
    • Anthony Scott Photography
  • First Name: Ray Sotkiewicz
  • Home Location: Seattle, WA
Wet Compass Code (Arduino/Teensy)
« on: November 22, 2016, 06:29:38 PM »
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

Offline sagrada737

  • 737NG Home Cockpit:
  • I may as well be Staff!
  • *
  • Posts: 507
  • N737CM - Keep your airspeed up !
    • Imaging The Heavens
  • First Name: Mike
  • Home Location: Las Cruces, New Mexico USA
Re: Wet Compass Code (Arduino/Teensy)
« Reply #1 on: November 23, 2016, 05:28:18 AM »
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
P3d v3.x with Sim-Avionics (two computers), FDS MIP, 3-Optoma HD GT180 projection display driven by a single nvidia GTX980.

Offline RayS

  • I spend way too much on this forum!
  • *
  • Posts: 817
  • Private Pilot, Sim Enthusiast
    • Anthony Scott Photography
  • First Name: Ray Sotkiewicz
  • Home Location: Seattle, WA
Re: Wet Compass Code (Arduino/Teensy)
« Reply #2 on: November 23, 2016, 11:51:54 AM »
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.



« Last Edit: November 23, 2016, 11:52:36 AM by RayS »
Ray Sotkiewicz

Offline Trevor Hale

  • Administrator
  • I am chained to this website!
  • *
  • Posts: 3,838
    • Cockpitbuilders.com
  • First Name: Trevor
  • Home Location: New Liskeard, Ontario
Re: Wet Compass Code (Arduino/Teensy)
« Reply #3 on: November 23, 2016, 03:00:54 PM »
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:

Offline fsaviator

  • I am chained to this website!
  • *
  • Posts: 1,362
  • Aviation Enthusiast
    • Boeing 737 Next Generation Simulator
  • First Name: Warren
  • Home Location: Riverview, Florida
Re: Wet Compass Code (Arduino/Teensy)
« Reply #4 on: November 24, 2016, 04:14:02 AM »
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 "MTN385"
Florida

http://www.B737NG-SIM.com and https://www.facebook.com/fsaviator on facebook
FSX, Prosim737 Suite, FDS DSTD 2.0 MIP, RevSim Proline TQ, ACE Dual-linked Yokes, Intel 3.5GHz i7-3770K, MSI Z77A-G45, 16GB G.Skill TridentX DDR3-2400, Galaxy GTX670 GC 4GB, OCZ 60GB Vertex 3

Offline RayS

  • I spend way too much on this forum!
  • *
  • Posts: 817
  • Private Pilot, Sim Enthusiast
    • Anthony Scott Photography
  • First Name: Ray Sotkiewicz
  • Home Location: Seattle, WA
Re: Wet Compass Code (Arduino/Teensy)
« Reply #5 on: November 24, 2016, 11:04:49 PM »
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

Offline RayS

  • I spend way too much on this forum!
  • *
  • Posts: 817
  • Private Pilot, Sim Enthusiast
    • Anthony Scott Photography
  • First Name: Ray Sotkiewicz
  • Home Location: Seattle, WA
Re: Wet Compass Code (Arduino/Teensy)
« Reply #6 on: November 25, 2016, 12:15:09 AM »
And the how-to video:
https://youtu.be/1zuljwyB92E
Ray Sotkiewicz

Offline fsaviator

  • I am chained to this website!
  • *
  • Posts: 1,362
  • Aviation Enthusiast
    • Boeing 737 Next Generation Simulator
  • First Name: Warren
  • Home Location: Riverview, Florida
Re: Wet Compass Code (Arduino/Teensy)
« Reply #7 on: November 25, 2016, 05:53:17 AM »
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 "MTN385"
Florida

http://www.B737NG-SIM.com and https://www.facebook.com/fsaviator on facebook
FSX, Prosim737 Suite, FDS DSTD 2.0 MIP, RevSim Proline TQ, ACE Dual-linked Yokes, Intel 3.5GHz i7-3770K, MSI Z77A-G45, 16GB G.Skill TridentX DDR3-2400, Galaxy GTX670 GC 4GB, OCZ 60GB Vertex 3

Offline RayS

  • I spend way too much on this forum!
  • *
  • Posts: 817
  • Private Pilot, Sim Enthusiast
    • Anthony Scott Photography
  • First Name: Ray Sotkiewicz
  • Home Location: Seattle, WA
Re: Wet Compass Code (Arduino/Teensy)
« Reply #8 on: November 26, 2016, 03:27:00 PM »
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>
« Last Edit: November 26, 2016, 05:17:35 PM by RayS »
Ray Sotkiewicz

Offline fsaviator

  • I am chained to this website!
  • *
  • Posts: 1,362
  • Aviation Enthusiast
    • Boeing 737 Next Generation Simulator
  • First Name: Warren
  • Home Location: Riverview, Florida
Re: Wet Compass Code (Arduino/Teensy)
« Reply #9 on: November 26, 2016, 04:05:21 PM »
That is pretty nice.  I think I can get started with that!

Thanks!
Warren "MTN385"
Florida

http://www.B737NG-SIM.com and https://www.facebook.com/fsaviator on facebook
FSX, Prosim737 Suite, FDS DSTD 2.0 MIP, RevSim Proline TQ, ACE Dual-linked Yokes, Intel 3.5GHz i7-3770K, MSI Z77A-G45, 16GB G.Skill TridentX DDR3-2400, Galaxy GTX670 GC 4GB, OCZ 60GB Vertex 3

Offline XOrionFE

  • I am chained to this website!
  • *
  • Posts: 1,698
  • First Name: Scott
  • Home Location: Chicago, IL
Re: Wet Compass Code (Arduino/Teensy)
« Reply #10 on: November 26, 2016, 07:44:14 PM »
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

 

FlightSimExpo 2019

FsExpo

Recent Posts

Flooring Installed!
by helloo
[Today at 04:00:36 PM]
How much room between center pedestal and seat?
by jackpilot
[Today at 08:19:42 AM]
PM MCP.ini Question...
by ScottThePilot
[Today at 03:14:37 AM]
2020 Microsoft Flight Simulator - new at E3 2019
by blueskydriver
[June 24, 2019, 02:29:16 PM]
How to install DZUS fasteners?
by Buster99
[June 22, 2019, 04:12:34 PM]
Desert Air Spares - Trustworthy?
by kattz
[June 22, 2019, 12:40:51 PM]
Wanted - 737 Audio Panels - OEM
by jackpilot
[June 20, 2019, 12:26:51 PM]
Throttletek 737 Throttle For Sale
by kevinsayre
[June 19, 2019, 03:57:33 PM]
For sale - rudder pedals and linkages - pending to Craig
by Caflyt
[June 19, 2019, 07:49:14 AM]
737-800 Arduino X-Plane Build
by kurt-olsson
[June 18, 2019, 11:26:12 AM]
737 Throttle Conversion OC + ProSim
by kattz
[June 16, 2019, 11:39:03 AM]
A friendly FYI on Boeing rudder pedals
by kattz
[June 15, 2019, 10:36:39 AM]
737 starter switch mechanism
by paulnd
[June 13, 2019, 02:58:39 PM]
Make up your mind, Microsoft!
by jmlohrenz
[June 12, 2019, 07:25:10 PM]
What have you done for your simulator today?
by C150DIY
[June 12, 2019, 12:53:27 PM]
Complete 737-800 cockpit - moving sale
by FaustoP
[June 12, 2019, 01:26:39 AM]
Flyengravity desktop MIP
by patiju
[June 10, 2019, 01:01:06 PM]
the perfect thing to complete your sim hanger
by fsaviator
[June 09, 2019, 03:12:50 PM]
Wanted - COMPLETE set of Boeing 727 or 737 rudder pedals
by kattz
[June 08, 2019, 01:26:09 PM]
Visual system screen by Northern Flight Sim for sale $500
by peterkwu
[June 08, 2019, 06:07:39 AM]
Wanted: GoFlight T8
by C150DIY
[June 07, 2019, 02:32:29 PM]
For Sale - CPFlight MCP737 + EFIS
by ZillaUK
[June 07, 2019, 09:54:58 AM]
737 FWD Overhead Switch Diagram
by bernard S
[June 07, 2019, 08:21:46 AM]
Change the Forum Theme to suit your own Needs.
by AdamMilstead
[June 07, 2019, 06:20:41 AM]
Aileron & Elevator jump back to center
by iwik
[June 06, 2019, 08:01:24 PM]
Join me on Vatsim Tonight
by Trevor Hale
[June 05, 2019, 05:17:13 PM]
Machinery for home cockpit building
by giaviv
[June 04, 2019, 04:12:10 PM]
PROMOCION STEERING FREE
by alvaro castillo
[June 04, 2019, 11:42:43 AM]
FLIGHTSIMEXPO LAUNCHES DISCOUNTED ADVANCE REGISTRATION FOR ORLANDO 2019 EVENT
by fsaviator
[June 03, 2019, 06:52:44 PM]
Question about linking the yokes
by jackpilot
[June 01, 2019, 02:22:10 PM]