Skip to content

Commit

Permalink
update bluetooth data type
Browse files Browse the repository at this point in the history
  • Loading branch information
teamclouday committed Dec 19, 2019
1 parent a5fc269 commit 5501a9a
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 39 deletions.
4 changes: 2 additions & 2 deletions Android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions Android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidsteering"
android:versionCode="0"
android:versionCodeMajor="1"
android:versionName="Version 1.0">
android:versionCode="2"
android:versionCodeMajor="2"
android:versionName="Version 2.0">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -775,7 +776,7 @@ class MyBuffer
private final int MAX_SIZE = 50;
private ArrayList<MyMove> 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;
Expand All @@ -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()
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 25 additions & 3 deletions Windows/SteeringWheel/MyBluetooth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -216,7 +222,7 @@ private bool TestSender()
}

/// <summary>
/// decode incoming data, 4 byte as an int
/// decode incoming data, 4 bytes as an int
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
Expand All @@ -227,6 +233,22 @@ private int DecodeData(byte[] array)
return BitConverter.ToInt32(array, 0);
}

/// <summary>
/// decode 4 bytes as a float
/// </summary>
/// <param name="array"></param>
/// <returns></returns>
private float DecodeFloatData(byte[] array)
{
if (BitConverter.IsLittleEndian)
Array.Reverse(array);
return BitConverter.ToSingle(array, 0);
}

/// <summary>
/// get connected device info
/// </summary>
/// <returns></returns>
public string[] GetDeviceInfo()
{
lock(this)
Expand Down
18 changes: 9 additions & 9 deletions Windows/SteeringWheel/MyWheel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -60,7 +60,7 @@ public class GlobBuffer
{
private Queue<MyMove> buffer = new Queue<MyMove>();
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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -339,9 +339,9 @@ public void ProcessData()
/// </summary>
/// <param name="inputAngle"></param>
/// <returns></returns>
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
Expand Down
4 changes: 2 additions & 2 deletions Windows/SteeringWheel/SteeringWheel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
<ProductName>Steering Wheel Service</ProductName>
<PublisherName>Sida Zhu</PublisherName>
<SuiteName>Teamclouday</SuiteName>
<ApplicationRevision>7</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<ApplicationRevision>2</ApplicationRevision>
<ApplicationVersion>1.1.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>
<BootstrapperEnabled>true</BootstrapperEnabled>
Expand Down

0 comments on commit 5501a9a

Please sign in to comment.