From 67881bf03bca917c674a28b93dc4c22eb878d616 Mon Sep 17 00:00:00 2001 From: Jason Watkins Date: Mon, 17 Aug 2015 09:17:21 -0700 Subject: [PATCH] Added Java playback example --- Java/Examples/Playback/.idea/compiler.xml | 22 +++ .../.idea/copyright/profiles_settings.xml | 3 + Java/Examples/Playback/.idea/description.html | 1 + .../.idea/libraries/XPlaneConnect.xml | 9 ++ Java/Examples/Playback/.idea/misc.xml | 12 ++ Java/Examples/Playback/.idea/modules.xml | 8 + .../Playback/.idea/project-template.xml | 3 + Java/Examples/Playback/.idea/vcs.xml | 6 + Java/Examples/Playback/Playback.iml | 12 ++ .../Playback/src/gov/nasa/xpc/Main.java | 141 ++++++++++++++++++ MATLAB/PlaybackExample/Playback.m | 32 ++++ MATLAB/PlaybackExample/Record.m | 34 +++++ 12 files changed, 283 insertions(+) create mode 100644 Java/Examples/Playback/.idea/compiler.xml create mode 100644 Java/Examples/Playback/.idea/copyright/profiles_settings.xml create mode 100644 Java/Examples/Playback/.idea/description.html create mode 100644 Java/Examples/Playback/.idea/libraries/XPlaneConnect.xml create mode 100644 Java/Examples/Playback/.idea/misc.xml create mode 100644 Java/Examples/Playback/.idea/modules.xml create mode 100644 Java/Examples/Playback/.idea/project-template.xml create mode 100644 Java/Examples/Playback/.idea/vcs.xml create mode 100644 Java/Examples/Playback/Playback.iml create mode 100644 Java/Examples/Playback/src/gov/nasa/xpc/Main.java create mode 100644 MATLAB/PlaybackExample/Playback.m create mode 100644 MATLAB/PlaybackExample/Record.m diff --git a/Java/Examples/Playback/.idea/compiler.xml b/Java/Examples/Playback/.idea/compiler.xml new file mode 100644 index 0000000..96cc43e --- /dev/null +++ b/Java/Examples/Playback/.idea/compiler.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Java/Examples/Playback/.idea/copyright/profiles_settings.xml b/Java/Examples/Playback/.idea/copyright/profiles_settings.xml new file mode 100644 index 0000000..e7bedf3 --- /dev/null +++ b/Java/Examples/Playback/.idea/copyright/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/Java/Examples/Playback/.idea/description.html b/Java/Examples/Playback/.idea/description.html new file mode 100644 index 0000000..db5f129 --- /dev/null +++ b/Java/Examples/Playback/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/Java/Examples/Playback/.idea/libraries/XPlaneConnect.xml b/Java/Examples/Playback/.idea/libraries/XPlaneConnect.xml new file mode 100644 index 0000000..f0cda69 --- /dev/null +++ b/Java/Examples/Playback/.idea/libraries/XPlaneConnect.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/Java/Examples/Playback/.idea/misc.xml b/Java/Examples/Playback/.idea/misc.xml new file mode 100644 index 0000000..5a65e9a --- /dev/null +++ b/Java/Examples/Playback/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Java/Examples/Playback/.idea/modules.xml b/Java/Examples/Playback/.idea/modules.xml new file mode 100644 index 0000000..7ba8a90 --- /dev/null +++ b/Java/Examples/Playback/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Java/Examples/Playback/.idea/project-template.xml b/Java/Examples/Playback/.idea/project-template.xml new file mode 100644 index 0000000..1f08b88 --- /dev/null +++ b/Java/Examples/Playback/.idea/project-template.xml @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/Java/Examples/Playback/.idea/vcs.xml b/Java/Examples/Playback/.idea/vcs.xml new file mode 100644 index 0000000..6564d52 --- /dev/null +++ b/Java/Examples/Playback/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Java/Examples/Playback/Playback.iml b/Java/Examples/Playback/Playback.iml new file mode 100644 index 0000000..6c4f0ab --- /dev/null +++ b/Java/Examples/Playback/Playback.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/Java/Examples/Playback/src/gov/nasa/xpc/Main.java b/Java/Examples/Playback/src/gov/nasa/xpc/Main.java new file mode 100644 index 0000000..5e64335 --- /dev/null +++ b/Java/Examples/Playback/src/gov/nasa/xpc/Main.java @@ -0,0 +1,141 @@ +package gov.nasa.xpc; + +import java.io.*; +import java.util.Scanner; + +public class Main +{ + + public static void main(String[] args) + { + String[] mainOpts = new String[] { "Record X-Plane", "Playback File", "Exit" }; + + System.out.println("X-Plane Connect Playback Example [Version 1.2.0.0]\n"); + System.out.println("(c) 2013-2015 United States Government as represented by the Administrator\n"); + System.out.println("of the National Aeronautics and Space Administration. All Rights Reserved.\n"); + while(true) + { + int result = displayMenu("What would you like to do?", mainOpts); + } +// char* mainOpts[3] = +// { +// "Record X-Plane", +// "Playback File", +// "Exit" +// }; +// char path[256] = { 0 }; +// +// displayStart("1.2.0.0"); +// while (1) +// { +// switch (displayMenu("What would you like to do?", mainOpts, 3)) +// { +// case 1: +// { +// getString("Enter save file path", path); +// int interval = getInt("Enter interval between frames (milliseconds)"); +// int duration = getInt("Enter duration to record for (seconds)"); +// +// record(path, interval, duration); +// break; +// } +// case 2: +// { +// getString("Enter path to saved playback file", path); +// int interval = getInt("Enter interval between frames (milliseconds)"); +// +// playback(path, interval); +// break; +// } +// case 3: +// displayMsg("Exiting."); +// return 0; +// default: +// displayMsg("Unrecognized option."); +// break; +// } +// } +// +// return 0; + + } + + private static int displayMenu(String title, String[] opts) throws IOException + { + System.out.println(); + System.out.println("+---------------------------------------------- +\n"); + System.out.println(String.format("| %1$-42s |\n", title)); + System.out.println("+---------------------------------------------- +\n"); + for(int i = 0; i < opts.length; ++i) + { + System.out.println(String.format("| %1$2d. %2$-40s |\n", i + 1, opts[i])); + + } + System.out.println("+---------------------------------------------- +\n"); + System.out.print("Please select an option: "); + BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + String result = reader.readLine(); + + return Integer.parseInt(result); + } + + public static void record(String path, int interval, int duration) throws IOException + { + int count = duration * 1000 / interval; + if (count < 1) + { + throw new IllegalArgumentException("duration is less than one interval."); + } + + try (BufferedWriter writer = new BufferedWriter(new FileWriter(path))) + { + System.out.println("Recording..."); + try (XPlaneConnect xpc = new XPlaneConnect()) + { + for (int i = 0; i < count; ++i) + { + float[] posi = xpc.getPOSI(0); + writer.write(String.format("%1$f, %2$f, %3$f, %4$f, %5$f, %6$f, %7$f\n", + posi[0], posi[1], posi[2], posi[3], posi[4], posi[5], posi[6])); + try + { + Thread.sleep(interval); + } + catch (InterruptedException ex) + { + } + } + } + System.out.println("Recording Complete"); + } + } + + public static void playback(String path, int interval) throws IOException + { + try(Scanner reader = new Scanner(new FileReader(path))) + { + System.out.println("Starting playback..."); + try (XPlaneConnect xpc = new XPlaneConnect()) + { + while(reader.hasNextLine()) + { + float[] posi = new float[7]; + for (int i = 0; i < 7; ++i) + { + posi[i] = reader.nextFloat(); + reader.skip(", "); + } + reader.nextLine(); + xpc.sendPOSI(posi); + try + { + Thread.sleep(interval); + } + catch (InterruptedException ex) + { + } + } + } + } + } +} diff --git a/MATLAB/PlaybackExample/Playback.m b/MATLAB/PlaybackExample/Playback.m new file mode 100644 index 0000000..e9f553b --- /dev/null +++ b/MATLAB/PlaybackExample/Playback.m @@ -0,0 +1,32 @@ +%% X-Plane Connect MATLAB Recording Example Script +% This script demonstrates how to playback recorded data from X-Plane. +% (See Record.m) +% Before running this script, ensure that the XPC plugin is installed and +% X-Plane is running. +%% Import XPC +addpath('../') +import XPlaneConnect.* + +%% Setup +% Create variables and open connection to X-Plane +path = 'MyRecording.txt'; % File containing stored data +interval = 0.1; % Time between snapshots in seconds +Socket = openUDP(); % Open connection to X-Plane +fd = fopen(path, 'r'); % Open file + +disp('X-Plane Connect Playback Example Script'); +fprintf('Playing back ''%s'' in %fs increments.\n', path, interval); + +%% Start Recording +count = floor(duration / interval); +for i = 1:count + posi = fscanf(fd, '%f, %f, %f, %f, %f, %f, %f\n'); + sendPOSI(posi); + pause(interval); +end + +%% Close connection and file +closeUDP(Socket); +fclose(fd); + +disp('Recording complete.'); \ No newline at end of file diff --git a/MATLAB/PlaybackExample/Record.m b/MATLAB/PlaybackExample/Record.m new file mode 100644 index 0000000..7e95b00 --- /dev/null +++ b/MATLAB/PlaybackExample/Record.m @@ -0,0 +1,34 @@ +%% X-Plane Connect MATLAB Recording Example Script +% This script demonstrates how to record data from X-Plane that can later +% be played back. (See Playback.m) +% Before running this script, ensure that the XPC plugin is installed and +% X-Plane is running. +%% Import XPC +addpath('../') +import XPlaneConnect.* + +%% Setup +% Create variables and open connection to X-Plane +path = 'MyRecording.txt'; % File to save the data in +interval = 0.1; % Time between snapshots in seconds +duration = 10; % Time to record for in seconds +Socket = openUDP(); % Open connection to X-Plane +fd = fopen(path, 'w'); % Open file + +disp('X-Plane Connect Recording Example Script'); +fprintf('Recording to ''%s'' for $f seconds in %fs increments.\n', path, duration, interval); + +%% Start Recording +count = floor(duration / interval); +for i = 1:count + posi = getPOSI(); + fprintf(fd, '%f, %f, %f, %f, %f, %f, %f\n', ... + posi(1), posi(2), posi(3), posi(4), posi(5), posi(6), posi(7)); + pause(interval); +end + +%% Close connection and file +closeUDP(Socket); +fclose(fd); + +disp('Recording complete.'); \ No newline at end of file