Skip to content

Commit

Permalink
Fixed some problems with the Android data contract. You can now send …
Browse files Browse the repository at this point in the history
…both Raw data and sensor data
  • Loading branch information
AndersMalmgren committed May 26, 2013
1 parent 6a606c9 commit 12ba478
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 24 deletions.
38 changes: 29 additions & 9 deletions FreePIE.Core.Plugins/AndroidPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ namespace FreePIE.Core.Plugins
[GlobalType(Type = typeof(AndroidGlobal))]
public class AndroidPlugin : Plugin
{
[Flags]
private enum Flags
{
Raw = 0x01,
Orientation = 0x02
}

private UdpClient udpClient;
private bool stopping;
private int udpPort;
Expand Down Expand Up @@ -104,11 +111,19 @@ private void BackGroundWorker()
break;
}

var flag = bytes[0];
var sendRaw = (flag & 1) == 1;
var sendOrientation = (flag & 2) == 2;
var flag = (Flags)bytes[0];
bool sendRaw;
bool sendOrientation;

SetFlags(flag, out sendRaw, out sendOrientation);

var dataInPackage = (Flags) bytes[1];
bool raw;
bool orientation;

if (!freeqSampled && sendRaw)
SetFlags(dataInPackage, out raw, out orientation);

if (!freeqSampled && raw)
{
if (samples == 0)
started = DateTime.Now;
Expand All @@ -117,20 +132,18 @@ private void BackGroundWorker()
var delta = (DateTime.Now - started).TotalSeconds;
if (delta > 1)
{

var freq = samples / (float)delta;
freeqSampled = true;

System.Diagnostics.Debug.WriteLine("Samples / s: {0}", samples);
ahrs = new MahonyAHRS(1f / freq, 0.1f);
}
else
continue;
}

var index = 1;
var index = 2;

if (sendRaw)
if (raw)
{
var ax = GetFloat(bytes, index, 0);
var ay = GetFloat(bytes, index, 4);
Expand All @@ -150,11 +163,12 @@ private void BackGroundWorker()
index += 36;
}

if (sendOrientation)
if (orientation)
{
GoogleYaw = GetFloat(bytes, index, 0);
GooglePitch = GetFloat(bytes, index, 4);
GoogleRoll = GetFloat(bytes, index, 8);
index += 12;
}

newData = true;
Expand All @@ -165,6 +179,12 @@ private static float GetFloat(byte[] buffer, int offset, int index)
{
return BitConverter.ToSingle(buffer, offset + index);
}

private void SetFlags(Flags flag, out bool raw, out bool orientation)
{
raw = (flag & Flags.Raw) == Flags.Raw;
orientation = (flag & Flags.Orientation) == Flags.Orientation;
}
}

[Global(Name = "android")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@

public class UdpSenderTask implements SensorEventListener {

private static final byte SEND_RAW = 0x01;
private static final byte SEND_ORIENTATION = 0x02;
private static final byte SEND_NONE = 0x00;

float[] acc;
float[] mag;
float[] gyr;
float[] imu;

float[] orientation;
float[] rotationVector;
final float[] rotationMatrix = new float[16];

DatagramSocket socket;
Expand All @@ -43,16 +47,16 @@ public class UdpSenderTask implements SensorEventListener {
public void start(TargetSettings target) {
sensorManager = target.getSensorManager();
sendRaw = target.getSendRaw();
sendOrientation = target.getSendOrientation();
sendOrientation = target.getSendOrientation();
sampleRate = target.getSampleRate();
debug = target.getDebug();
debugListener = target.getDebugListener();

sendFlag = (byte)((sendRaw ? 0x01 : 0x00) | (sendOrientation ? 0x02 : 0x00));
sendFlag = getFlagByte(sendRaw, sendOrientation);

sync = new CyclicBarrier(2);

buffer = ByteBuffer.allocate(49);
buffer = ByteBuffer.allocate(50);
buffer.order(ByteOrder.LITTLE_ENDIAN);

try {
Expand Down Expand Up @@ -101,6 +105,11 @@ public void run(){

}

private byte getFlagByte(boolean raw, boolean orientation) {
return (byte)((raw ? SEND_RAW : SEND_NONE) |
(orientation ? SEND_ORIENTATION : SEND_NONE));
}

public void onSensorChanged(SensorEvent sensorEvent) {
switch (sensorEvent.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
Expand All @@ -114,24 +123,29 @@ public void onSensorChanged(SensorEvent sensorEvent) {
gyr = sensorEvent.values.clone();
break;
case Sensor.TYPE_ROTATION_VECTOR:
orientation = sensorEvent.values.clone();
rotationVector = sensorEvent.values.clone();
break;
}

if(sendOrientation && orientation != null) {
SensorManager.getRotationMatrixFromVector(rotationMatrix , orientation);
SensorManager.getOrientation(rotationMatrix, orientation);
imu = orientation;
if(sendOrientation && rotationVector != null) {
SensorManager.getRotationMatrixFromVector(rotationMatrix , rotationVector);
SensorManager.getOrientation(rotationMatrix, rotationVector);
imu = rotationVector;
rotationVector = null;
}

if(debug && acc != null && gyr != null && mag != null)
debugListener.debugRaw(acc, gyr, mag);

if(debug && imu != null)
debugListener.debugImu(imu);
debugListener.debugImu(imu);

releaseSendThread();
}

private void releaseSendThread() {
if(sync.getNumberWaiting() > 0)
sync.reset();
sync.reset();
}

@Override
Expand All @@ -141,14 +155,23 @@ public void onAccuracyChanged(Sensor sensor, int accuracy) {
public void stop() {
running = false;
sensorManager.unregisterListener(this);
releaseSendThread();
}

private void Send() {
boolean raw = sendRaw && acc != null && gyr != null && mag != null;
boolean orientation = sendOrientation && imu != null;

if(!raw && !orientation) return;

buffer.clear();

buffer.put(sendFlag);


buffer.put(getFlagByte(raw, orientation));

This comment has been minimized.

Copy link
@sthalik

sthalik Sep 28, 2014

Contributor

Where Minsc goes, evil stands aside!

Run while you can, evil! @AndersMalmgren won't be as gentle.


if(sendRaw && acc != null && mag != null && gyr != null) {
if(raw) {
//Acc
buffer.putFloat(acc[0]);
buffer.putFloat(acc[1]);
Expand All @@ -163,15 +186,19 @@ private void Send() {
buffer.putFloat(mag[0]);
buffer.putFloat(mag[1]);
buffer.putFloat(mag[2]);

acc = null;
mag = null;
gyr = null;
}

if(sendOrientation && imu != null) {
//Orientation
if(orientation) {
buffer.putFloat(imu[0]);
buffer.putFloat(imu[1]);
buffer.putFloat(imu[2]);
imu = null;
}
}


byte[] arr = buffer.array();
DatagramPacket p = new DatagramPacket(arr, arr.length, endPoint, port);
Expand Down

0 comments on commit 12ba478

Please sign in to comment.