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

March 28, 2024, 03:24:34 PM

Login with username, password and session length

PROUDLY ENDORSING


Fly Elise-ng
131 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: 131
Total: 131

COUNTDOWN TO WF2022


WORLDFLIGHT TEAM USA

Will Depart in...

Recent

Welcome

Arduino Lua and Script Help

Started by CAESAR, April 06, 2015, 05:34:19 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

CAESAR

Quote from: Trevor Hale on February 05, 2015, 02:22:14 PM
SUCCESS!!!!

I can Successfully control my Arduino Gauges without the need of Link2FS  I now use a LUA Script to pass the DATA!

Man, what a great feeling to get past this hurdle.

Trev

Trevor,

Would you please share details of what script you are using? I am now looking at http://openairbuscockpit.org/software/arduino-libraries/ but for some reason the performance is not as expected. Especially when you have to send a data from Arduino to Sim often (let's say using the encoder) it is running functions many times and thus delaying the data to be sent to sim.

The above is using Lua on top of FSUIPC. What I am looking the way to read / write LVARS from Prepar3D to/from arduino with decent performance.

Thanks,
Roman.

Trevor Hale

Hi Roman,

http://www.cockpitbuilders.com/community/index.php?topic=4768.msg35237#msg35237

This is the link where I posted my lua code and Arduino Script.  I have made more changes since this, but you will be able to see from the code structure it isn't hard to add more.

Now:  Note what I have found is that some offsets do not get called enough, for fast response.  So what I do is when I have a specific Offset that I want to be called more.. I just double up the event.offset line of the code, so for some of my gauges (IE THE FLAP) I have made 2 or three instances of that event.offset line.

Ass I added 5 more gauges..  I needed to add a couple lines for the offsets I am using.

If that makes sense.

I am using the ipcready Lua script file so as soon as FSUIPC is loaded it runs the Lua.  Sucks for testing because for every code change it is a fs Restart, however least I don;t have to have a switch assigned to start and stop the script.


Trev

Trevor Hale

Owner
http://www.cockpitbuilders.com

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

VATSIM:

pcpilotmd80

Makes no sense at all, I'm sorry I'm not gifted to know computer code or whatever it's called, I wish I could find a tutorial that was done for dummies like me where none of the techno jargon was used.  I know there's lots of guys out there like me that are "visual" learners.  Before I start dumping money to the next level for gauges I want the confidence of knowing and understanding interfacing however it is.  I so appreciate all the help so far, but I get so discouraged not understanding it.  My past experience has been "well take a class, sorry can't help" or "find some one to teach you" are responses from other sites.  I feel like its the "have" and the "have nots" when it comes to this mystical knowledge to unlock the mysteries of gauge interfacing. Sorry not trying to upset anyone, very greatful for eveyone's help here so far. Seems like Evey youtube video I've found is some one just showing off the end product, not explaining how they did it, and the ones that do explain, you need a masters degree in quantum physics to understand what they said.
Very frustrated builder

Trevor Hale

#3
Don't worry about it. I will try to explain the code as best I can here.

Just give me a day or so and I will try to help you!
Trevor Hale

Owner
http://www.cockpitbuilders.com

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

VATSIM:

KyleH

Hi Mike,

Programming is more about thinking through the logical steps that need to happen, rather than thinking about the computer commands that need to be issued.
The first step in any (proper) programming process is to write out the steps that need to happen on a piece of paper before even opening the programing. i.e.

read airspeed from FS
calculate % of gauge movement
send data to serial port

Each one can be separated out into its own needed steps
i.e. read airspeed from FS

Open FSUIPC
airspeed = read FSUIPC offset #xxxxx
close FSUIPC


Once that is sorted out, then you move to converting it to code.
The basics of coding are essentially the same with each language, its just differences in how they want the statements done and how much the language allows various items to be controlled

If your new to programming and want to make use of it in your sim, I'd suggest you pickup the C++ for Dummies bookhttp://www.amazon.com/Beginning-Programming-Dummies-Stephen-Davis/dp/0470617977/ref=sr_1_2?s=books&ie=UTF8&qid=1428498892&sr=1-2&keywords=c%2B%2B+for+dummies
Work through the exercises in that. It uses a free C compiler and development environment you can download (The book I got had it on a CD in the back). It is a very good intro book and explains it in laymen's terms that you are looking for.

The purpose to doing this is to get used to how coding works. Don't worry about getting code for your sim written just yet. First just get the basic "Hello World" program going, and then through some more complicated stuff that gets into variables, functions and arrays etc.
Stuff you learn with this book will be easily transferred to other languages that you can then use to make the interfacing with fs and the hardware easier.






Kyle

Chief Pilot
Worldflight Team USA
http://www.worldflightusa.com

pcpilotmd80

Ok, sorry I wasn't directing that totally towards you, it was a general speaking "gripe".  Thanks! Are there any books that I could pick up that might help?

Mike

pcpilotmd80

Kyle you da man!  Thanks for good info!!! Like I've always claimed everyone here has been so helpfull.  I will try your suggestions, and your explaination helped!

Mike

KyleH

No problem.

If you do get the book I'd suggest downloading codeblocks from here:
http://www.codeblocks.org/downloads/26
Get the codeblocks-13.12mingw-setup.exe download as it includes the compiler and saves setup time and complication as the CD version needs to have the compiler installed separately.
Kyle

Chief Pilot
Worldflight Team USA
http://www.worldflightusa.com

RayS

Quote from: KyleH on April 08, 2015, 04:59:55 AM
"The first step in any (proper) programming process is to write out the steps that need to happen..."

Actually, the first step is making sure you have enough beer in the house... :angel:
Ray Sotkiewicz

KyleH

Well, they don't let me have beer at work.  :huh:
Kyle

Chief Pilot
Worldflight Team USA
http://www.worldflightusa.com

RayS


What? No Beer? My company has a wet bar on every floor (No kidding!) :-)
Ray Sotkiewicz

Trevor Hale

Quote from: RayS on April 08, 2015, 09:59:28 AM

What? No Beer? My company has a wet bar on every floor (No kidding!) :-)

I am moving to Washington state!!!!!!!!!!!!!!!    :cheers:
Trevor Hale

Owner
http://www.cockpitbuilders.com

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

VATSIM:

pcpilotmd80

BEER!  Check. Thanks everyone, big help!

727737Nut

Question, in the code below, what can I add to it to make it the sim/software match the hardware sw position on sim start up?  I know its possible just not sure how to implement it.
Rob

Quote#include <Bounce.h>
FlightSimInteger wing;
FlightSimInteger anticol;
FlightSimInteger navon;
FlightSimInteger navonbatt;
FlightSimInteger strobe;
FlightSimInteger logo;
FlightSimInteger apustart;
FlightSimInteger apuoff;
FlightSimInteger wiperhigh;
FlightSimInteger taxi;
FlightSimInteger rtturnoff;
FlightSimInteger ltturnoff;
FlightSimInteger rtland;
FlightSimInteger ltland;
FlightSimInteger rtretland;
FlightSimInteger ltretland;
FlightSimInteger start1;
FlightSimInteger flt1;
FlightSimInteger wiperlow;
FlightSimInteger start2;
FlightSimInteger flt2;
FlightSimInteger seatauto;
FlightSimInteger attend;
FlightSimInteger wiperpark;
FlightSimInteger emerglt;
FlightSimInteger nosmokeon;
FlightSimInteger seaton;

Bounce wingSw = Bounce (0, 5);
Bounce anticolSw = Bounce (1, 5);
Bounce navonSw = Bounce (2, 5);
Bounce navonbattSw = Bounce (3, 5);
Bounce strobeSw = Bounce (5, 5);
Bounce logoSw = Bounce (4, 5);
Bounce apustartSw = Bounce (6, 5);
Bounce apuoffSw = Bounce (7, 5);
Bounce wiperhighSw = Bounce (16, 5);
Bounce taxiSw = Bounce (17, 5);
Bounce rtturnoffSw = Bounce (19, 5);
Bounce ltturnoffSw = Bounce (18, 5);
Bounce rtlandSw = Bounce (23, 5);
Bounce ltlandSw = Bounce (22, 5);
Bounce rtretlandSw = Bounce (21, 5);
Bounce ltretlandSw = Bounce (20, 5);
Bounce start1Sw = Bounce (13, 5);
Bounce flt1Sw = Bounce (12, 5);
Bounce wiperlowSw = Bounce (11, 5);
Bounce start2Sw = Bounce (10, 5);
Bounce flt2Sw = Bounce (9, 5);
Bounce seatautoSw = Bounce (8, 5);
Bounce attendSw = Bounce (15, 5);
Bounce wiperparkSw = Bounce (14, 5);
Bounce emergltSw = Bounce (26, 5);
Bounce nosmokeonSw = Bounce (25, 5);
Bounce seatonSw = Bounce (24, 5);

// setup runs once
void setup() {
  wing = XPlaneRef("FJS/732/lights/WingLightSwitch");
  anticol = XPlaneRef("FJS/732/lights/AntiColLightSwitch");
  navon = XPlaneRef("FJS/732/lights/PositionLightSwitch");
  navonbatt = XPlaneRef("FJS/732/lights/PositionLightSwitch");
  strobe = XPlaneRef("FJS/732/lights/StrobeLightSwitch");
  logo = XPlaneRef("FJS/732/lights/LogoLightSwitch");
  apustart = XPlaneRef("FJS/732/Elec/APUStartSwitch");
  apuoff = XPlaneRef("FJS/732/Elec/APUStartSwitch");
  wiperhigh = XPlaneRef("FJS/732/AntiIce/WiperKnob");
  taxi = XPlaneRef("FJS/732/lights/TaxiLightSwitch");
  rtturnoff = XPlaneRef("FJS/732/lights/RunwayTurnoffSwitch2");
  ltturnoff = XPlaneRef("FJS/732/lights/RunwayTurnoffSwitch1");
  rtland = XPlaneRef("FJS/732/lights/InBoundLLightSwitch2");
  ltland = XPlaneRef("FJS/732/lights/InBoundLLightSwitch1");
  rtretland = XPlaneRef("FJS/732/lights/OutBoundLLightSwitch2");
  ltretland = XPlaneRef("FJS/732/lights/OutBoundLLightSwitch1");
  start1 = XPlaneRef("FJS/732/Eng/Engine1StartKnob");
  flt1 = XPlaneRef("FJS/732/Eng/Engine1StartKnob");
  wiperlow = XPlaneRef("FJS/732/AntiIce/WiperKnob");
  start2 = XPlaneRef("FJS/732/Eng/Engine2StartKnob");
  flt2  = XPlaneRef("FJS/732/Eng/Engine2StartKnob");
  seatauto = XPlaneRef("FJS/732/lights/FastenBeltsSwitch");
  attend = XPlaneRef("FJS/732/Radios/AttCallButton");
  wiperpark = XPlaneRef("FJS/732/AntiIce/WiperKnob");
  emerglt = XPlaneRef("FJS/732/lights/EmerExitSwitch");
  nosmokeon = XPlaneRef("FJS/732/lights/NoSmokingSwitch");
  seaton = XPlaneRef("FJS/732/lights/FastenBeltsSwitch");

  pinMode (0, INPUT_PULLUP);
  pinMode (1, INPUT_PULLUP);
  pinMode (2, INPUT_PULLUP);
  pinMode (3, INPUT_PULLUP);
  pinMode (4, INPUT_PULLUP);
  pinMode (5, INPUT_PULLUP);
  pinMode (6, INPUT_PULLUP);
  pinMode (7, INPUT_PULLUP);
  pinMode (8, INPUT_PULLUP);
  pinMode (9, INPUT_PULLUP);
  pinMode (10, INPUT_PULLUP);
  pinMode (11, INPUT_PULLUP);
  pinMode (12, INPUT_PULLUP);
  pinMode (13, INPUT_PULLUP);
  pinMode (14, INPUT_PULLUP);
  pinMode (15, INPUT_PULLUP);
  pinMode (16, INPUT_PULLUP);
  pinMode (17, INPUT_PULLUP);
  pinMode (18, INPUT_PULLUP);
  pinMode (19, INPUT_PULLUP);
  pinMode (20, INPUT_PULLUP);
  pinMode (21, INPUT_PULLUP);
  pinMode (22, INPUT_PULLUP);
  pinMode (23, INPUT_PULLUP);
  pinMode (24, INPUT_PULLUP);
  pinMode (25, INPUT_PULLUP);
  pinMode (26, INPUT_PULLUP);

}

// loop runs repetitively, as long as power is on
void loop() {
  FlightSim.update();
  if (wingSw.update()) {
    if (wingSw.fallingEdge()) {
      wing = 1;
    } else {
      wing = 0;
    }
  }
  if (anticolSw.update()) {
    if (anticolSw.fallingEdge()) {
      anticol = 1;
    } else {
      anticol = 0;
    }
  }
  if (navonSw.update()) {
    if (navonSw.fallingEdge()) {
      navon = 1;
    } else {
      navon = 0;
    }
  }
  if (navonbattSw.update()) {
    if (navonbattSw.fallingEdge()) {
      navonbatt = -1;
    } else {
      navonbatt = 0;
    }
  }
  if (strobeSw.update()) {
    if (strobeSw.fallingEdge()) {
      strobe = 1;
    } else {
      strobe = 0;
    }
  }
  if (logoSw.update()) {
    if (logoSw.fallingEdge()) {
      logo = 1;
    } else {
      logo = 0;
    }
  }
  if (apustartSw.update()) {
    if (apustartSw.fallingEdge()) {
      apustart = 2;
    } else {
      apustart = 1;
    }
  }
  if (apuoffSw.update()) {
    if (apuoffSw.fallingEdge()) {
      apuoff = 0;
    } else {
      apuoff = 1;
    }
  }
  if (wiperhighSw.update()) {
    if (wiperhighSw.fallingEdge()) {
      wiperhigh = 4;
    } else {
      wiperhigh = 0;
    }
  }
  if (taxiSw.update()) {
    if (taxiSw.fallingEdge()) {
      taxi = 1;
    } else {
      taxi = 0;
    }
  }
  if (rtturnoffSw.update()) {
    if (rtturnoffSw.fallingEdge()) {
      rtturnoff = 1;
    } else {
      rtturnoff = 0;
    }
  }
  if (ltturnoffSw.update()) {
    if (ltturnoffSw.fallingEdge()) {
      ltturnoff = 1;
    } else {
      ltturnoff = 0;
    }
  }
  if (rtlandSw.update()) {
    if (rtlandSw.fallingEdge()) {
      rtland = 1;
    } else {
      rtland = 0;
    }
  }
  if (ltlandSw.update()) {
    if (ltlandSw.fallingEdge()) {
      ltland = 1;
    } else {
      ltland = 0;
    }
  }
  if (rtretlandSw.update()) {
    if (rtretlandSw.fallingEdge()) {
      rtretland = 2;
    } else {
      rtretland = 0;
    }
  }
  if (ltretlandSw.update()) {
    if (ltretlandSw.fallingEdge()) {
      ltretland = 2;
    } else {
      ltretland = 0;
    }
  }
  if (start1Sw.update()) {
    if (start1Sw.fallingEdge()) {
      start1 = -1;
    } else {
      start1 = 0;
    }
  }
  if (flt1Sw.update()) {
    if (flt1Sw.fallingEdge()) {
      flt1 = 2;
    } else {
      flt1 = 0;
    }
  }
  if (wiperlowSw.update()) {
    if (wiperlowSw.fallingEdge()) {
      wiperlow = 1;
    } else {
      wiperlow = 0;
    }
  }
  if (start2Sw.update()) {
    if (start2Sw.fallingEdge()) {
      start2 = -1;
    } else {
      start2 = 0;
    }
  }
  if (flt2Sw.update()) {
    if (flt2Sw.fallingEdge()) {
      flt2 = 2;
    } else {
      flt2 = 0;
    }
  }
  if (seatautoSw.update()) {
    if (seatautoSw.fallingEdge()) {
      seatauto = 1;
    } else {
      seatauto = 0;
    }
  }
  if (attendSw.update()) {
    if (attendSw.fallingEdge()) {
      attend = 1;
    } else {
      attend = 0;
    }
  }
  if (wiperparkSw.update()) {
    if (wiperparkSw.fallingEdge()) {
      wiperpark = -1;
    } else {
      wiperpark = 0;
    }
  }
  if (emergltSw.update()) {
    if (emergltSw.fallingEdge()) {
      emerglt = 0;
    } else {
      emerglt = 1;
    }
  }
  if (nosmokeonSw.update()) {
    if (nosmokeonSw.fallingEdge()) {
      nosmokeon = 2;
    } else {
      nosmokeon = 0;
    }
  }
  if (seatonSw.update()) {
    if (seatonSw.fallingEdge()) {
      seaton = 2;
    } else {
      seaton = 0;
    }
  }
}
737 Junkie

KyleH

Hi Rob,

Did you ever get the switch reading figured out?

I'm not too familiar with the specifics of working with the arduino and talking to FS, so the code you posted, does it get loaded onto the arduino, or a program that runs on the pc?


Quote
#include <Bounce.h>
FlightSimInteger wing;

Bounce wingSw = Bounce (0, 5);

void setup() {
  wing = XPlaneRef("FJS/732/lights/WingLightSwitch");
}

void loop() {
  FlightSim.update();
  if (wingSw.update())
       {
        if (wingSw.fallingEdge())
          {
            wing = 1;
           }
       else
          {
             wing = 0;
           }
       }
}


Looking at just the one variable, wing switch;
What does wingSw.update() do? Is it just a True/false value that something has changed?

What does wingSw.fallingEdge() do? does it allow you to read the position of the switch or just the fact that it has changed from one value to the other? looks like another true/false to me
Kyle

Chief Pilot
Worldflight Team USA
http://www.worldflightusa.com

727737Nut

Hello,
I use Teensy which is similar to Arduino cards but much more powerful and has native X-Plane support via a plug-in. Just like Arduino's, It is uploaded to the Arduino or Teensy in my case.

When using the Bounce function you have to use the Sw Update call.  The fallingedge notation means it only changes when pulled to ground. If your switch was backwards you would use RisingEdge.   

The thing about this Arduino stuff is that there are several ways to skin the cat as the saying goes.
737 Junkie

KyleH

The way I see it you basically have 2 options.

You can either write code before your main loop, to check all the switch positions. Presumably there is a function available that just reads the position, not looking for an edge. Since this is at startup, the switches should be static and therefore no de-bouncing should be required.

The other option is that if you can set wingSw.update() = TRUE before the main loop starts than it would force it to read the position of the switch.
I don't know if this option would work unless the wingSw.update() was assigned to a variable instead testing the function directly.

Kyle

Chief Pilot
Worldflight Team USA
http://www.worldflightusa.com

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