PDA

View Full Version : Building a system using Arduino



kolaf
06-09-2012, 04:37 AM
Hi guys,

I just made my first hardware switch control and events in the flight simulator (P3D) using arduino and lua. I have connected a simple switch to the arduino board which sends commands over a serial link (through USB) to a lua script. The script subscribes to the parking brake event.

Every time I flicked the switch it turns the parking brake off and on, and whenever an internal simulation events (such as pressing the brake pedal) tries to turn the parking brake off, this information is sent to the board which can verify if this is in sync with the hardware switch. If the hardware switch says that the parking brake should still be on, it re-sends the parking brake command.

I plan to expand this to build my own panels with switches, displays and rotary knobs to take care of the most common FSX functions.

This is a picture of the setup. Impressive, huh?
6723

The arduino sketch is here:




#define STRLEN 20

char buffer[STRLEN];
int bufferIndex = 0;
int parking_break = 100;
int parking_break_pin = 4;
int digital_read_value = 100;
long last_debounce=0;
long debounce_delay=100;


void setup()
{
Serial.begin(115200);
Serial.flush();
pinMode(parking_break_pin, INPUT);
digitalWrite(parking_break_pin, HIGH);
}

void loop()
{
if( Serial.available())
{
char ch = Serial.read();
if( ch == '\n') // is this the terminating carriage return
{
buffer[ bufferIndex ] = 0; // terminate the strin
bufferIndex = 0; // reset the index ready for another string
parse_string(buffer);
// do something with the string
}
else
buffer[ bufferIndex++ ] = ch; // add the character into the buffer
}
check_input();
}

void parse_string (char* buffer)
{
char *name = NULL, *value = NULL;
//Serial.write(buffer);
name =strtok (buffer, ":");
value =strtok( NULL, ":");
if (name && value) {
handle_command (name, value);
}
}

void handle_command (char* name,char* value)
{
if (strcmp( name, "parkingb") == 0)
{
if(parking_break !=atoi( value))
{
send_value ("parkingb", parking_break);
}
}
}

void send_value (char* name,int value)
{
char buffer [20];
sprintf (buffer, "%s:%d\0", name, value);
Serial.write (buffer);
}

void check_input ()
{
digital_read_value = digitalRead(parking_break_pin);
if (digital_read_value!= parking_break) {
parking_break = digital_read_value;
last_debounce= millis();
}
if (last_debounce!=0 && (millis() - last_debounce) > debounce_delay) {
//Serial.write("changed value");

last_debounce=0;
send_value ("parkingb", parking_break);
}
}

And the lua script is here:


speed = 115200
handshake = 0

dev = com.open("COM8", speed, handshake)
if dev == 0 then
ipc.display("Could not open VRIdevice port")
ipc.exit()
end



function handlecom(handle, str)
ipc.log("com=" .. str)
if string.match(str, "parkingb:(%d)") then
state = tonumber(string.match(str, "parkingb:(%d)"))
if state == 1 then
ipc.writeUD(0x0BC8, 32767)
else
ipc.writeUD(0x0BC8, 0)
end
end
end

-- ----------------------------- Callbacks -------------------
function parking_break (offset, value)
if value == 0 then
com.write(dev, "parkingb:0\n")
else
com.write(dev, "parkingb:1\n")
end
end


ipc.sleep(500)
event.com(dev, 20, 5, 0, "handlecom")
event.offset(0x0BC8, "UW", "parking_break")

geneb
06-09-2012, 12:10 PM
Great job!

g.

Simbuilder
06-09-2012, 12:27 PM
How many switches can you connect to the arduino?

AK Mongo
06-09-2012, 01:55 PM
That is outstanding sir!

Thanks for the input!

Reid

Jim NZ
06-09-2012, 03:55 PM
Ditto what Reid said ,, great we have another way to get the Arduino into the cockpit.

Neat stuff and thanks ,, Jim

AK Mongo
06-26-2012, 12:52 PM
Kolaf,

I would love to be able to just read an FSUIPC offset and send to serial. Do you have an idea how your code could be modified to do so?

It seems like it should be simple enough, but LUA eludes me.

If I could do this, it would be easier to create all of my gauges, without asking Jim to provide extractions for all of them.

Any help you can give would be appreciated!

Reid

forty-2
09-08-2012, 05:26 PM
Mongo -

here's a somewhat simplified snippit from my ardiuno radio project (to be posted soon!)



--set up serial port
speed = 57600
handshake = 0
arduinoPort = "COM3"
Serial_In = 0
Com1S = 0

--this is the function called when the offset changes
--could be prettier, but its a work in progress.
function com1aSend(offset, value)
buf = string.format("%X", ipc.readStruct(0x034E, "UW")) --assigns offset to variable
buf = string.format("%s", buf) --makes it a string - redundant?
com.write(dev, string.format("%s\n", buf)) --sends sends via serial w/ newline
end

--this calls the above function whenever the offset changes
event.offset(0x034E, "UW", "com1aSend")


This just spits the value out to the serial port as a string. If you're planning on sending out more then one value, you'll want to prepend each with an identifying character that the arduino uses to determine which value you've sent.

Thank you Kolaf & Jim for your excellent work and posts here. I've found your posts a number of times while researching my own project, and they've always steered me in the right direction!

kolaf
09-10-2012, 09:31 AM
I have recently completed my prototype, and the current version of my software can be found here, together with a demo video https://code.google.com/p/arduino-fs/ .

The source code is by no means elegant, but it works :-)