From 5501a9ae9643695f0bdfdc50e4bd8c5ead9132b8 Mon Sep 17 00:00:00 2001 From: teamclouday Date: Thu, 19 Dec 2019 14:08:44 -0500 Subject: [PATCH] update bluetooth data type --- Android/app/build.gradle | 4 +- Android/app/src/main/AndroidManifest.xml | 6 +-- .../example/androidsteering/MainActivity.java | 39 ++++++++++--------- Android/build.gradle | 2 +- Windows/SteeringWheel/MyBluetooth.cs | 28 +++++++++++-- Windows/SteeringWheel/MyWheel.cs | 18 ++++----- Windows/SteeringWheel/SteeringWheel.csproj | 4 +- 7 files changed, 62 insertions(+), 39 deletions(-) diff --git a/Android/app/build.gradle b/Android/app/build.gradle index fbff6f6..4114209 100644 --- a/Android/app/build.gradle +++ b/Android/app/build.gradle @@ -7,8 +7,8 @@ android { applicationId "com.example.androidsteering" minSdkVersion 19 targetSdkVersion 29 - versionCode 1 - versionName "1.0" + versionCode 2 + versionName "2.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { diff --git a/Android/app/src/main/AndroidManifest.xml b/Android/app/src/main/AndroidManifest.xml index 981d553..65b56ab 100644 --- a/Android/app/src/main/AndroidManifest.xml +++ b/Android/app/src/main/AndroidManifest.xml @@ -1,9 +1,9 @@ + android:versionCode="2" + android:versionCodeMajor="2" + android:versionName="Version 2.0"> diff --git a/Android/app/src/main/java/com/example/androidsteering/MainActivity.java b/Android/app/src/main/java/com/example/androidsteering/MainActivity.java index cddd69f..48b5ef1 100644 --- a/Android/app/src/main/java/com/example/androidsteering/MainActivity.java +++ b/Android/app/src/main/java/com/example/androidsteering/MainActivity.java @@ -29,6 +29,7 @@ import java.io.OutputStream; import java.nio.ByteBuffer; import java.util.ArrayList; +import java.util.Locale; import java.util.Objects; import java.util.Set; import java.util.UUID; @@ -124,7 +125,7 @@ public void run() { motionSteer = "None"; break; } - motionSteer += "><" + serviceSensor.motionPitch; + motionSteer += String.format(Locale.ENGLISH, ">< %.2f", serviceSensor.motionPitch); String motionAcc; switch(serviceSensor.motionAcceleration) { @@ -138,7 +139,7 @@ public void run() { motionAcc = "None"; break; } - motionAcc += "><" + serviceSensor.motionRoll; + motionAcc += String.format(Locale.ENGLISH, ">< %.2f", serviceSensor.motionRoll); txtViewDebug.setText(String.format("Steering: %s\nAcceleration: %s", motionSteer, motionAcc)); String bthStatus; @@ -527,7 +528,7 @@ public void writeData() bb.putInt(data.MotionStatus); outputStream.write(bb.array(), 0, 4); bb.clear(); - bb.putInt(data.data); + bb.putFloat(data.data); outputStream.write(bb.array(), 0, 4); } write(outputStream.toByteArray()); @@ -649,8 +650,8 @@ class MySensorService implements SensorEventListener private MotionAcceleration motionAcceleration = MotionAcceleration.NONE; private MotionSteering motionSteering = MotionSteering.NONE; - private int motionPitch = 0; - private int motionRoll = 0; + private float motionPitch = 0.0f; + private float motionRoll = 0.0f; public MySensorService() { @@ -691,15 +692,15 @@ private void update() SensorManager.getRotationMatrix(rotationMatrix, null, accReading, magReading); SensorManager.getOrientation(rotationMatrix, orientationMatrix); - int pitch = (int)Math.toDegrees(orientationMatrix[1]); - int roll = (int)Math.abs(Math.toDegrees(orientationMatrix[2]))-90; + float pitch = (float)Math.toDegrees(orientationMatrix[1]); + float roll = (float)Math.abs(Math.toDegrees(orientationMatrix[2]))-90; // update motion steering - if(pitch > 2 && pitch < 85) + if(pitch > 2.0 && pitch < 85.0) { updateMotionSteer(MotionSteering.LEFT); } - else if(pitch < -2 && pitch > -85) + else if(pitch < -2.0 && pitch > -85.0) { updateMotionSteer(MotionSteering.RIGHT); } @@ -709,11 +710,11 @@ else if(pitch < -2 && pitch > -85) } updatePitch(pitch); - if(roll < -5 && roll > -85) + if(roll < -5.0 && roll > -85.0) { updateMotionAcc(MotionAcceleration.FORWARD); } - else if(roll > 5 && roll < 85) + else if(roll > 5.0 && roll < 85.0) { updateMotionAcc(MotionAcceleration.BACKWARD); } @@ -746,22 +747,22 @@ public synchronized MotionSteering readMotionSteer() return motionSteering; } - private synchronized void updatePitch(int newPitch) + private synchronized void updatePitch(float newPitch) { motionPitch = newPitch; } - public synchronized int readPitch() + public synchronized float readPitch() { return motionPitch; } - private synchronized void updateRoll(int newRoll) + private synchronized void updateRoll(float newRoll) { motionRoll = newRoll; } - public synchronized int readRoll() + public synchronized float readRoll() { return motionRoll; } @@ -775,7 +776,7 @@ class MyBuffer private final int MAX_SIZE = 50; private ArrayList buff = new ArrayList<>(); - public synchronized void addData(MotionSteering s1, MotionAcceleration s2, int pitch, int roll) + public synchronized void addData(MotionSteering s1, MotionAcceleration s2, float pitch, float roll) { if(buff.size() >= MAX_SIZE) return; if(serviceBTH == null || serviceBTH.readStatus() != BluetoothStatus.CONNECTED) return; @@ -787,7 +788,7 @@ public synchronized void addData(MotionButton button) { if(buff.size() >= MAX_SIZE) return; if(serviceBTH == null || serviceBTH.readStatus() != BluetoothStatus.CONNECTED) return; - buff.add(new MyMove(2, button.ordinal(), 0)); + buff.add(new MyMove(2, button.ordinal(), 0.0f)); } public synchronized MyMove getData() @@ -801,8 +802,8 @@ class MyMove { int MotionType; // 0 for acceleration, 1 for steering int MotionStatus; // positive number for related status - int data; // moving data - public MyMove(int type, int status, int d) + float data; // moving data + public MyMove(int type, int status, float d) { MotionType = type; MotionStatus = status; diff --git a/Android/build.gradle b/Android/build.gradle index fb516be..a5bb815 100644 --- a/Android/build.gradle +++ b/Android/build.gradle @@ -7,7 +7,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.5.2' + classpath 'com.android.tools.build:gradle:3.5.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/Windows/SteeringWheel/MyBluetooth.cs b/Windows/SteeringWheel/MyBluetooth.cs index 65b35cc..f2fee91 100644 --- a/Windows/SteeringWheel/MyBluetooth.cs +++ b/Windows/SteeringWheel/MyBluetooth.cs @@ -141,11 +141,16 @@ private void StartConnection() if (actualLength == 0) break; int[] moveData = new int[2]; int pointer = -1; + float specialData = 0.0f; + int data = 0; for (int i = 0; i < actualLength; i += 4) { byte[] subpack = new byte[4]; Array.Copy(pack, i, subpack, 0, 4); - int data = DecodeData(subpack); + if (pointer == 2) + specialData = DecodeFloatData(subpack); + else + data = DecodeData(subpack); if (data == seperator) { goto ENDOFWHILELOOP; @@ -158,7 +163,8 @@ private void StartConnection() { if(pointer == 2) { - Program.globBuffer.AddData(moveData[0], moveData[1], data); + Program.globBuffer.AddData(moveData[0], moveData[1], specialData); + specialData = 0.0f; pointer = -1; } else @@ -216,7 +222,7 @@ private bool TestSender() } /// - /// decode incoming data, 4 byte as an int + /// decode incoming data, 4 bytes as an int /// /// /// @@ -227,6 +233,22 @@ private int DecodeData(byte[] array) return BitConverter.ToInt32(array, 0); } + /// + /// decode 4 bytes as a float + /// + /// + /// + private float DecodeFloatData(byte[] array) + { + if (BitConverter.IsLittleEndian) + Array.Reverse(array); + return BitConverter.ToSingle(array, 0); + } + + /// + /// get connected device info + /// + /// public string[] GetDeviceInfo() { lock(this) diff --git a/Windows/SteeringWheel/MyWheel.cs b/Windows/SteeringWheel/MyWheel.cs index 06080e4..1280463 100644 --- a/Windows/SteeringWheel/MyWheel.cs +++ b/Windows/SteeringWheel/MyWheel.cs @@ -44,8 +44,8 @@ public class MyMove { public int motionType; public int motionStatus; - public int data; - public MyMove(int type, int status, int d) + public float data; + public MyMove(int type, int status, float d) { motionType = type; motionStatus = status; @@ -60,7 +60,7 @@ public class GlobBuffer { private Queue buffer = new Queue(); private const int MAX_SIZE = 50; - public void AddData(int type, int status, int d) + public void AddData(int type, int status, float d) { lock(this) { @@ -122,10 +122,10 @@ class MyWheel private long axisMax = 0; // set default values - public static int steerLeftMax = 80; - public static int steerRightMax = 80; - public static int steerLeftMin = 10; - public static int steerRightMin = 10; + public static int steerLeftMax = 75; + public static int steerRightMax = 75; + public static int steerLeftMin = 5; + public static int steerRightMin = 5; public static int accForwardMax = 80; public static int accForwardMin = 25; public static int accBackwardMax = 40; @@ -339,9 +339,9 @@ public void ProcessData() /// /// /// - private int ConvertAxis(int inputAngle, int type, int status) + private int ConvertAxis(float inputAngle, int type, int status) { - float val = (float)Math.Abs(inputAngle); + float val = Math.Abs(inputAngle); if(type == 1) { // acceleration diff --git a/Windows/SteeringWheel/SteeringWheel.csproj b/Windows/SteeringWheel/SteeringWheel.csproj index ae35702..b04f726 100644 --- a/Windows/SteeringWheel/SteeringWheel.csproj +++ b/Windows/SteeringWheel/SteeringWheel.csproj @@ -29,8 +29,8 @@ Steering Wheel Service Sida Zhu Teamclouday - 7 - 1.0.0.%2a + 2 + 1.1.0.%2a false true true