|
@@ -3,56 +3,61 @@ using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
using ANT_Managed_Library;
|
|
using ANT_Managed_Library;
|
|
using System;
|
|
using System;
|
|
|
|
+using UnityEngine.Serialization;
|
|
|
|
|
|
-public class SpeedSensorReceiver
|
|
+[System.Serializable]
|
|
|
|
+public struct SpeedSensorConfig
|
|
{
|
|
{
|
|
- private int deviceId = 0;
|
|
+ public int deviceId;
|
|
- private bool connected = false;
|
|
+ public float wheelCircumference;
|
|
- private float wheelCircumference = 2.096f;
|
|
|
|
- private List<AntDevice> scanResult;
|
|
|
|
|
|
|
|
- private float speed;
|
|
+ public SpeedSensorConfig(int deviceId = 0, float wheelCircumference = 2.096f)
|
|
- private float distance;
|
|
+ {
|
|
|
|
+ this.deviceId = deviceId;
|
|
|
|
+ this.wheelCircumference = wheelCircumference;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
|
|
|
|
+public struct SpeedSensorData
|
|
|
|
+{
|
|
|
|
+ public float Speed;
|
|
|
|
+ public float SpeedKmh => Speed * 3.6f;
|
|
|
|
+ public float Distance;
|
|
|
|
+}
|
|
|
|
|
|
|
|
+public class SpeedSensorReceiver
|
|
|
|
+{
|
|
private AntChannel backgroundScanChannel;
|
|
private AntChannel backgroundScanChannel;
|
|
private AntChannel deviceChannel;
|
|
private AntChannel deviceChannel;
|
|
|
|
|
|
- private int stopRevCounter_speed = 0;
|
|
+ private int stopRevCounterSpeed = 0;
|
|
- private int prev_measTime_speed = 0;
|
|
+ private int prevMeasTimeSpeed = 0;
|
|
- private int prev_revCount_speed = 0;
|
|
+ private int prevRevCountSpeed = 0;
|
|
private int revCountZero = 0;
|
|
private int revCountZero = 0;
|
|
|
|
+ private SpeedSensorData sensorData;
|
|
|
|
|
|
- public int DeviceId => deviceId;
|
|
+ public SpeedSensorConfig Config { get; }
|
|
-
|
|
+ public bool Connected { get; private set; }
|
|
- public bool Connected => connected;
|
|
+ public List<AntDevice> ScanResult { get; private set; }
|
|
-
|
|
+ public SpeedSensorData SensorData => sensorData;
|
|
- public List<AntDevice> ScanResult => scanResult;
|
|
|
|
-
|
|
|
|
- public float Speed => speed;
|
|
|
|
-
|
|
|
|
- public float SpeedKmh => speed * 3.6f;
|
|
|
|
-
|
|
|
|
- public float Distance => distance;
|
|
|
|
|
|
|
|
public SpeedSensorReceiver()
|
|
public SpeedSensorReceiver()
|
|
{
|
|
{
|
|
}
|
|
}
|
|
|
|
|
|
- public SpeedSensorReceiver(int deviceId) => this.deviceId = deviceId;
|
|
+ public SpeedSensorReceiver(SpeedSensorConfig config) => this.Config = config;
|
|
|
|
|
|
- public SpeedSensorReceiver(int deviceId, float wheelCircumference)
|
|
+ public SpeedSensorReceiver(int deviceId) => Config = new SpeedSensorConfig(deviceId);
|
|
- {
|
|
+
|
|
- this.deviceId = deviceId;
|
|
+ public SpeedSensorReceiver(int deviceId, float wheelCircumference) =>
|
|
- this.wheelCircumference = wheelCircumference;
|
|
+ Config = new SpeedSensorConfig(deviceId, wheelCircumference);
|
|
- }
|
|
|
|
|
|
|
|
|
|
|
|
public void StartScan()
|
|
public void StartScan()
|
|
{
|
|
{
|
|
Debug.Log("Looking for ANT + Speed sensor");
|
|
Debug.Log("Looking for ANT + Speed sensor");
|
|
AntManager.Instance.Init();
|
|
AntManager.Instance.Init();
|
|
- scanResult = new List<AntDevice>();
|
|
+ ScanResult = new List<AntDevice>();
|
|
backgroundScanChannel = AntManager.Instance.OpenBackgroundScanChannel(0);
|
|
backgroundScanChannel = AntManager.Instance.OpenBackgroundScanChannel(0);
|
|
backgroundScanChannel.onReceiveData += ReceivedBackgroundScanData;
|
|
backgroundScanChannel.onReceiveData += ReceivedBackgroundScanData;
|
|
}
|
|
}
|
|
@@ -68,7 +73,7 @@ public class SpeedSensorReceiver
|
|
{
|
|
{
|
|
int deviceNumber = (data[10]) | data[11] << 8;
|
|
int deviceNumber = (data[10]) | data[11] << 8;
|
|
byte transType = data[13];
|
|
byte transType = data[13];
|
|
- foreach (AntDevice d in scanResult)
|
|
+ foreach (AntDevice d in ScanResult)
|
|
{
|
|
{
|
|
if (d.deviceNumber == deviceNumber && d.transType == transType)
|
|
if (d.deviceNumber == deviceNumber && d.transType == transType)
|
|
return;
|
|
return;
|
|
@@ -82,8 +87,8 @@ public class SpeedSensorReceiver
|
|
foundDevice.period = 8118;
|
|
foundDevice.period = 8118;
|
|
foundDevice.radiofreq = 57;
|
|
foundDevice.radiofreq = 57;
|
|
foundDevice.name = "BikeSpeed(" + foundDevice.deviceNumber + ")";
|
|
foundDevice.name = "BikeSpeed(" + foundDevice.deviceNumber + ")";
|
|
- scanResult.Add(foundDevice);
|
|
+ ScanResult.Add(foundDevice);
|
|
- if (deviceNumber == deviceId)
|
|
+ if (deviceNumber == Config.deviceId)
|
|
{
|
|
{
|
|
Debug.Log($"Desired Speed Sensor with id {deviceNumber} found!");
|
|
Debug.Log($"Desired Speed Sensor with id {deviceNumber} found!");
|
|
ConnectToDevice(foundDevice);
|
|
ConnectToDevice(foundDevice);
|
|
@@ -105,7 +110,7 @@ public class SpeedSensorReceiver
|
|
deviceChannel = AntManager.Instance.OpenChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00,
|
|
deviceChannel = AntManager.Instance.OpenChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00,
|
|
channelID, (ushort) device.deviceNumber, device.deviceType, device.transType, (byte) device.radiofreq,
|
|
channelID, (ushort) device.deviceNumber, device.deviceType, device.transType, (byte) device.radiofreq,
|
|
(ushort) device.period, false);
|
|
(ushort) device.period, false);
|
|
- connected = true;
|
|
+ Connected = true;
|
|
deviceChannel.onReceiveData += Data;
|
|
deviceChannel.onReceiveData += Data;
|
|
deviceChannel.onChannelResponse += ChannelResponse;
|
|
deviceChannel.onChannelResponse += ChannelResponse;
|
|
|
|
|
|
@@ -120,31 +125,31 @@ public class SpeedSensorReceiver
|
|
int revCount_speed = (data[6]) | data[7] << 8;
|
|
int revCount_speed = (data[6]) | data[7] << 8;
|
|
|
|
|
|
|
|
|
|
- if (prev_measTime_speed != 0 && measTime_speed != prev_measTime_speed && prev_measTime_speed < measTime_speed &&
|
|
+ if (prevMeasTimeSpeed != 0 && measTime_speed != prevMeasTimeSpeed && prevMeasTimeSpeed < measTime_speed &&
|
|
- prev_revCount_speed < revCount_speed)
|
|
+ prevRevCountSpeed < revCount_speed)
|
|
{
|
|
{
|
|
- speed = (wheelCircumference * (revCount_speed - prev_revCount_speed) * 1024) /
|
|
+ sensorData.Speed = (Config.wheelCircumference * (revCount_speed - prevRevCountSpeed) * 1024) /
|
|
- (measTime_speed - prev_measTime_speed);
|
|
+ (measTime_speed - prevMeasTimeSpeed);
|
|
- stopRevCounter_speed = 0;
|
|
+ stopRevCounterSpeed = 0;
|
|
}
|
|
}
|
|
else
|
|
else
|
|
- stopRevCounter_speed++;
|
|
+ stopRevCounterSpeed++;
|
|
|
|
|
|
- if (stopRevCounter_speed >= 5)
|
|
+ if (stopRevCounterSpeed >= 5)
|
|
{
|
|
{
|
|
- stopRevCounter_speed = 5;
|
|
+ stopRevCounterSpeed = 5;
|
|
- speed = 0;
|
|
+ sensorData.Speed = 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- prev_measTime_speed = measTime_speed;
|
|
+ prevMeasTimeSpeed = measTime_speed;
|
|
- prev_revCount_speed = revCount_speed;
|
|
+ prevRevCountSpeed = revCount_speed;
|
|
|
|
|
|
|
|
|
|
if (revCountZero == 0)
|
|
if (revCountZero == 0)
|
|
revCountZero = revCount_speed;
|
|
revCountZero = revCount_speed;
|
|
|
|
|
|
- distance = wheelCircumference * (revCount_speed - revCountZero);
|
|
+ sensorData.Distance = Config.wheelCircumference * (revCount_speed - revCountZero);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|