SpeedSensorReceiver.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using ANT_Managed_Library;
  4. using UnityEngine;
  5. namespace Sensors.ANT
  6. {
  7. [Serializable]
  8. public struct SpeedSensorConfig
  9. {
  10. public int deviceId;
  11. public float wheelCircumference;
  12. public SpeedSensorConfig(int deviceId = 0, float wheelCircumference = 2.096f)
  13. {
  14. this.deviceId = deviceId;
  15. this.wheelCircumference = wheelCircumference;
  16. }
  17. }
  18. public struct SpeedSensorData
  19. {
  20. public float Speed;
  21. public float SpeedKmh => Speed * 3.6f;
  22. public float Distance;
  23. }
  24. public class SpeedSensorReceiver
  25. {
  26. private AntChannel backgroundScanChannel;
  27. private AntChannel deviceChannel;
  28. private int prevMeasTimeSpeed;
  29. private int prevRevCountSpeed;
  30. private int revCountZero;
  31. private SpeedSensorData sensorData;
  32. private int stopRevCounterSpeed;
  33. public SpeedSensorReceiver()
  34. {
  35. }
  36. public SpeedSensorReceiver(SpeedSensorConfig config)
  37. {
  38. Config = config;
  39. }
  40. public SpeedSensorReceiver(int deviceId)
  41. {
  42. Config = new SpeedSensorConfig(deviceId);
  43. }
  44. public SpeedSensorReceiver(int deviceId, float wheelCircumference)
  45. {
  46. Config = new SpeedSensorConfig(deviceId, wheelCircumference);
  47. }
  48. public SpeedSensorConfig Config { get; }
  49. public bool Connected { get; private set; }
  50. public List<AntDevice> ScanResult { get; private set; }
  51. public SpeedSensorData SensorData => sensorData;
  52. //Start a background Scan to find the device
  53. public void StartScan()
  54. {
  55. Debug.Log("Looking for ANT + Speed sensor");
  56. AntManager.Instance.Init();
  57. ScanResult = new List<AntDevice>();
  58. backgroundScanChannel = AntManager.Instance.OpenBackgroundScanChannel(0);
  59. backgroundScanChannel.onReceiveData += ReceivedBackgroundScanData;
  60. }
  61. //If the device is found
  62. private void ReceivedBackgroundScanData(byte[] data)
  63. {
  64. var deviceType = data[12]; // extended info Device Type byte
  65. switch (deviceType)
  66. {
  67. case AntplusDeviceType.BikeSpeed:
  68. {
  69. var deviceNumber = data[10] | (data[11] << 8);
  70. var transType = data[13];
  71. foreach (var d in ScanResult)
  72. if (d.deviceNumber == deviceNumber && d.transType == transType) //device already found
  73. return;
  74. var foundDevice = new AntDevice();
  75. foundDevice.deviceType = deviceType;
  76. foundDevice.deviceNumber = deviceNumber;
  77. foundDevice.transType = transType;
  78. foundDevice.period = 8118;
  79. foundDevice.radiofreq = 57;
  80. foundDevice.name = "BikeSpeed(" + foundDevice.deviceNumber + ")";
  81. ScanResult.Add(foundDevice);
  82. if (deviceNumber == Config.deviceId)
  83. {
  84. Debug.Log($"Desired Speed Sensor with id {deviceNumber} found!");
  85. ConnectToDevice(foundDevice);
  86. }
  87. else
  88. {
  89. Debug.Log($"Speed sensor ({deviceNumber}) found and added to ScanResult");
  90. }
  91. break;
  92. }
  93. }
  94. }
  95. private void ConnectToDevice(AntDevice device)
  96. {
  97. AntManager.Instance.CloseBackgroundScanChannel();
  98. var channelID = AntManager.Instance.GetFreeChannelID();
  99. deviceChannel = AntManager.Instance.OpenChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00,
  100. channelID, (ushort) device.deviceNumber, device.deviceType, device.transType, (byte) device.radiofreq,
  101. (ushort) device.period, false);
  102. Connected = true;
  103. deviceChannel.onReceiveData += Data;
  104. deviceChannel.onChannelResponse += ChannelResponse;
  105. deviceChannel.hideRXFAIL = true;
  106. }
  107. //Deal with the received Data
  108. private void Data(byte[] data)
  109. {
  110. //SPEED
  111. var measTime_speed = data[4] | (data[5] << 8);
  112. var revCount_speed = data[6] | (data[7] << 8);
  113. if (prevMeasTimeSpeed != 0 && measTime_speed != prevMeasTimeSpeed && prevMeasTimeSpeed < measTime_speed &&
  114. prevRevCountSpeed < revCount_speed)
  115. {
  116. sensorData.Speed = Config.wheelCircumference * (revCount_speed - prevRevCountSpeed) * 1024 /
  117. (measTime_speed - prevMeasTimeSpeed);
  118. stopRevCounterSpeed = 0;
  119. }
  120. else
  121. {
  122. stopRevCounterSpeed++;
  123. }
  124. if (stopRevCounterSpeed >= 5)
  125. {
  126. stopRevCounterSpeed = 5;
  127. sensorData.Speed = 0;
  128. }
  129. prevMeasTimeSpeed = measTime_speed;
  130. prevRevCountSpeed = revCount_speed;
  131. //DISTANCE
  132. if (revCountZero == 0)
  133. revCountZero = revCount_speed;
  134. sensorData.Distance = Config.wheelCircumference * (revCount_speed - revCountZero);
  135. }
  136. private void ChannelResponse(ANT_Response response)
  137. {
  138. }
  139. }
  140. }