Cockpitbuilders.com

Arduino Projects => Sketches - => Topic started by: RayS on November 22, 2016, 02:29:38 PM

Title: Wet Compass Code (Arduino/Teensy)
Post by: RayS on November 22, 2016, 02: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 (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;}
   }
   
}





Title: Re: Wet Compass Code (Arduino/Teensy)
Post by: sagrada737 on November 23, 2016, 01: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
Title: Re: Wet Compass Code (Arduino/Teensy)
Post by: RayS on November 23, 2016, 07: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/ (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.



Title: Re: Wet Compass Code (Arduino/Teensy)
Post by: Trevor Hale on November 23, 2016, 11:00:54 AM
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
Title: Re: Wet Compass Code (Arduino/Teensy)
Post by: fsaviator on November 24, 2016, 12: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
Title: Re: Wet Compass Code (Arduino/Teensy)
Post by: RayS on November 24, 2016, 07: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 (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 (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!
Title: Re: Wet Compass Code (Arduino/Teensy)
Post by: RayS on November 24, 2016, 08:15:09 PM
And the how-to video:
https://youtu.be/1zuljwyB92E (https://youtu.be/1zuljwyB92E)
Title: Re: Wet Compass Code (Arduino/Teensy)
Post by: fsaviator on November 25, 2016, 01: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.
Title: Re: Wet Compass Code (Arduino/Teensy)
Post by: RayS on November 26, 2016, 11:27:00 AM
Warren,

Have you seen this guy?
http://www.mobiflight.com/ (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>
Title: Re: Wet Compass Code (Arduino/Teensy)
Post by: fsaviator on November 26, 2016, 12:05:21 PM
That is pretty nice.  I think I can get started with that!

Thanks!
Title: Re: Wet Compass Code (Arduino/Teensy)
Post by: XOrionFE on November 26, 2016, 03: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