Added Java playback example
This commit is contained in:
22
Java/Examples/Playback/.idea/compiler.xml
generated
Normal file
22
Java/Examples/Playback/.idea/compiler.xml
generated
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="CompilerConfiguration">
|
||||||
|
<resourceExtensions />
|
||||||
|
<wildcardResourcePatterns>
|
||||||
|
<entry name="!?*.java" />
|
||||||
|
<entry name="!?*.form" />
|
||||||
|
<entry name="!?*.class" />
|
||||||
|
<entry name="!?*.groovy" />
|
||||||
|
<entry name="!?*.scala" />
|
||||||
|
<entry name="!?*.flex" />
|
||||||
|
<entry name="!?*.kt" />
|
||||||
|
<entry name="!?*.clj" />
|
||||||
|
<entry name="!?*.aj" />
|
||||||
|
</wildcardResourcePatterns>
|
||||||
|
<annotationProcessing>
|
||||||
|
<profile default="true" name="Default" enabled="false">
|
||||||
|
<processorPath useClasspath="true" />
|
||||||
|
</profile>
|
||||||
|
</annotationProcessing>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
3
Java/Examples/Playback/.idea/copyright/profiles_settings.xml
generated
Normal file
3
Java/Examples/Playback/.idea/copyright/profiles_settings.xml
generated
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
<component name="CopyrightManager">
|
||||||
|
<settings default="" />
|
||||||
|
</component>
|
||||||
1
Java/Examples/Playback/.idea/description.html
generated
Normal file
1
Java/Examples/Playback/.idea/description.html
generated
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html>
|
||||||
9
Java/Examples/Playback/.idea/libraries/XPlaneConnect.xml
generated
Normal file
9
Java/Examples/Playback/.idea/libraries/XPlaneConnect.xml
generated
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<component name="libraryTable">
|
||||||
|
<library name="XPlaneConnect">
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$PROJECT_DIR$/../../out/artifacts/XPlaneConnect_jar/XPlaneConnect.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES />
|
||||||
|
</library>
|
||||||
|
</component>
|
||||||
12
Java/Examples/Playback/.idea/misc.xml
generated
Normal file
12
Java/Examples/Playback/.idea/misc.xml
generated
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="EntryPointsManager">
|
||||||
|
<entry_points version="2.0" />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectKey">
|
||||||
|
<option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
8
Java/Examples/Playback/.idea/modules.xml
generated
Normal file
8
Java/Examples/Playback/.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/Playback.iml" filepath="$PROJECT_DIR$/Playback.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
3
Java/Examples/Playback/.idea/project-template.xml
generated
Normal file
3
Java/Examples/Playback/.idea/project-template.xml
generated
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
<template>
|
||||||
|
<input-field default="com.company">IJ_BASE_PACKAGE</input-field>
|
||||||
|
</template>
|
||||||
6
Java/Examples/Playback/.idea/vcs.xml
generated
Normal file
6
Java/Examples/Playback/.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
12
Java/Examples/Playback/Playback.iml
Normal file
12
Java/Examples/Playback/Playback.iml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
<orderEntry type="library" name="XPlaneConnect" level="project" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
141
Java/Examples/Playback/src/gov/nasa/xpc/Main.java
Normal file
141
Java/Examples/Playback/src/gov/nasa/xpc/Main.java
Normal file
@@ -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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
32
MATLAB/PlaybackExample/Playback.m
Normal file
32
MATLAB/PlaybackExample/Playback.m
Normal file
@@ -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.');
|
||||||
34
MATLAB/PlaybackExample/Record.m
Normal file
34
MATLAB/PlaybackExample/Record.m
Normal file
@@ -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.');
|
||||||
Reference in New Issue
Block a user