SpeedSensorReceiver.cs 4.8 KB

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