Board index FlightGear Support Interfacing

What started out....

Connecting two computers, using generic protocol, connecting with Matlab?

What started out....

Postby tripstar76 » Wed Oct 03, 2018 7:31 pm

So, I bought myself a Saitek yoke, and realised that it has a hellish deadband for both ails and elevs! Had a google and found out you can buy a board to sort it out.
Well, I thought, I have plenty of spare cheapduino uno's, surely I could rig something up.
So, de-soldered the wires off the pots in the yoke, done a little bit of copy/mixing/coding for the arduino, linked the output from the pots to input protocol, and the deadband was gone, bonus.
But, a whole arduino using just two pins seems a waste. What else could I use the arduino pins for...., a bit of hole making, a bit of soldering, and 8 buttons later, I have a bit of a modded yoke.

I'll put all the code and stuff I've used to get this going on here, in case it will be any help for others.

Bear in mind, I'm still learning about this flightgear malarky, but I'll try to help if I can. And, I'll add, most of th stuff I needed, I gleened from the forum. I've just put my stuff in one place.


Arduino Code:

Code: Select all
/*
  //Keypad Tester 01. Created by Martyn Currey


  // Global Variables
  const byte keypadPin     = A5;
  int val = 0;

  void setup()
  {
  Serial.begin(9600);
  while (!Serial)   {  ;    }
  Serial.println(F("Start\n"));
  // set the analogue pin for input and turn on the internal pullup resistor
  pinMode(keypadPin, INPUT_PULLUP);
  }

  void loop()
  {
    val = getKeyValue();
    Serial.println( val );
    delay(100);
  }


  int getKeyValue()
  {
    int pinVal;
    pinVal = analogRead(keypadPin);
    return pinVal;
  }
*/
// -------------------------------------------------------------------------------
// /*
//Keypad Tester 02. Created by Martyn Currey, modified by John

const int ailPin = A0; const int elevPin = A1;       // potentiometer pins
const int buttonsPin = A5;  // buttons analog pin on A5
int bState[8]; // Array for button states
int bCount = 8; // number of buttons
int vLow[8] = { 0, 217, 352, 445, 516, 571, 614, 649 } ; //Array for low button analog readings
int vHigh[8] = { 35, 237, 372, 465, 536, 591, 634, 669 } ; //Array for high button analog readings
unsigned long interval = 90; unsigned long previousMillis = millis(); // milli timer for debounce

void setup()
{
  Serial.begin(9600);
  while (!Serial)   { ; }
  pinMode(buttonsPin, INPUT_PULLUP); // sets analog pin for input
  // sets analog buttons as LOW
  for (int thisBut = 0; thisBut < bCount; thisBut++) {
    int (bState[thisBut], LOW);
  }
}

void loop()
{
  // print analog readings
  Serial.print(mapf(analogRead(ailPin), 188, 892, -1, 1)); Serial.print(",");
  Serial.print(mapf(analogRead(elevPin), 182, 902, -1, 1)); Serial.print(",");

  // print button presses to serial
  getKey(); // run analog pin reading
  for (int thisBut = 0; thisBut < bCount; thisBut++) {
    Serial.print(bState[thisBut]); Serial.print(",");
  }
  Serial.println();
}

int getKey()  // convert analog button pin readings to button states
{
  int val = analogRead(buttonsPin); // Read analog pin
  //  Momentary swich resets
  bState[1] = 0;
  bState[3] = 0;
  if ((millis() - previousMillis) >= interval) {
  //  Which button was pressed
  for (int thisBut = 0; thisBut < bCount; thisBut++) {
    if ( (val >= vLow[thisBut]) && (val <= vHigh[thisBut]) ) {
      bState[thisBut] = !bState[thisBut];
    }
  }
  previousMillis = millis();
  }
}

// map analog pot pins to values
float mapf(float x, float in_min, float in_max, float out_min, float out_max) {
  float result;
  result = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  return result;
}


Arduino pins:

Image
http://www.martyncurrey.com/keypads/

Protocol XML stuff:

Code: Select all
<?xml version="1.0"?>
<PropertyList>
 <generic>
  <input>

   <var_separator>,</var_separator>
   <line_separator>\n</line_separator>

<!-- Pot 1  -->
    <chunk>
    <name>aileron</name>
    <type>float</type>
    <node>/controls/flight/aileron</node>
    </chunk>

<!-- Pot 2  -->
    <chunk>
    <name>elevator</name>
    <type>float</type>
    <node>/controls/flight/elevator</node>
    </chunk>

<!-- But 0  -->
    <chunk>
    <name>Battery</name>
    <node>/controls/switches/master-bat</node>
    <type>int</type>
    </chunk>

<!-- But 1  -->
    <chunk>
    <name>Primer</name>
    <node>/controls/arduino/button1</node>
    <type>int</type>
    </chunk>

<!-- But 2  -->
    <chunk>
    <name>Avionic Master</name>
    <node>/controls/switches/master-avionics</node>
    <type>int</type>
    </chunk>

<!-- But 3  -->
    <chunk>
    <name>Heading Bug</name>
    <type>int</type>
    <node>/autopilot/settings/heading-bug-deg</node>
    <relative>true</relative>
    <min>1</min>
    <max>360</max>
    <wrap>true</wrap>
    </chunk>

<!-- But 4  -->
    <chunk>
    <name>Start</name>
    <node>/controls/arduino/button4</node>
    <type>int</type>
    </chunk>

<!-- But 5  -->
    <chunk>
    <name>Alternator</name>
    <node>/controls/switches/master-alt</node>
    <type>bool</type>
    </chunk>

<!-- But 6  -->
    <chunk>
    <name>Lights</name>
    <node>controls/lighting/instruments-norm</node>
    <type>int</type>
    </chunk>

<!-- But 7  -->
    <chunk>
    <name>Pitot</name>
    <node>controls/anti-ice/pitot-heat</node>
    <type>int</type>
    </chunk>

 </input>
 </generic>
</PropertyList>


nas Code(for the trickier buttons)

Code: Select all
setlistener("/controls/arduino/button1", func {
    if (getprop("/controls/arduino/button1") == 1) {
        fgcommand(c172p.pumpPrimer());
    }
}, 0, 0);

setlistener("/controls/arduino/button4", func {
    if (getprop("/controls/arduino/button4") == 1) {
        fgcommand(controls.startEngine(1));
    }
}, 0, 0);



xml file goes in fgdata/Protocol folder
nas file goes in .fgfs/Nasal folder
Arduino code gets uploaded to arduino board


This gets entered in settings - additional settings box:
--generic=serial,in,30,/dev/ttyUSB0,9600,arduyoke
Flightgear 2018.2.2
Linux Mint 18.3
CPU~Quad core Intel Core i7-2670QM (-HT-MCP-) 3100 MHz
Kernel~4.10.0-38-generic x86_64
Mem~7891.0MB
NVIDIA Corporation
4.5.0 NVIDIA 384.130
GeForce GT 540M/PCIe/SSE2
tripstar76
 
Posts: 8
Joined: Sun Aug 19, 2018 10:24 am
Location: United Kingdom, Selby
Version: 2018.2.2
OS: Linux Mint 18.3

Return to Interfacing

Who is online

Users browsing this forum: No registered users and 1 guest