2
0

SpeedDisplay.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using ANT_Managed_Library;
  5. using System;
  6. public class SpeedDisplay : MonoBehaviour {
  7. public bool autoStartScan = true; //start scan on play
  8. public bool connected = false; //will be set to true once connected
  9. public float wheelCircumference = 2.096f; //700*23C, set this to your wheels size
  10. //windows and mac settings
  11. public bool autoConnectToFirstSensorFound = true; //for windows and mac, either connect to the first sensor found or let you pick a sensor manually in the scanResult list with your own UI and call ConnectToDevice(AntDevice device)
  12. public List<AntDevice> scanResult;
  13. //android settings
  14. public bool useAndroidUI = true; //will open the unified ant+ UI on the android app if set to true, otherwise will connect to the first found device
  15. public bool skipPreferredSearch = true; //- True = Don't automatically connect to user's preferred device, but always go to search for other devices.
  16. //the sensor values we receive fron the onReceiveData event
  17. public float speed; // The speed in km/h
  18. public float distance; //the distance in meters
  19. private AntChannel backgroundScanChannel;
  20. private AntChannel deviceChannel;
  21. private int stopRevCounter_speed = 0;
  22. private int prev_measTime_speed = 0;
  23. private int prev_revCount_speed = 0;
  24. private int revCountZero = 0;
  25. public int deviceID = 0; //set this to connect to a specific device ID
  26. void Start() {
  27. if (autoStartScan)
  28. StartScan();
  29. }
  30. //Start a background Scan to find the device
  31. public void StartScan() {
  32. Debug.Log("Looking for ANT + Speed sensor");
  33. #if UNITY_ANDROID && !UNITY_EDITOR
  34. //java: connect_speed(String gameobjectName, float wheel, boolean useAndroidUI, boolean skipPreferredSearch, int deviceID)
  35. AndroidJNI.AttachCurrentThread();
  36. using (AndroidJavaClass javaClass = new AndroidJavaClass("com.ant.plugin.Ant_Connector")) {
  37. using (AndroidJavaObject activity = javaClass.GetStatic<AndroidJavaObject>("mContext")) {
  38. activity.Call("connect_speed", this.gameObject.name, wheelCircumference,useAndroidUI, skipPreferredSearch,deviceID);
  39. }
  40. }
  41. #else
  42. AntManager.Instance.Init();
  43. scanResult = new List<AntDevice>();
  44. backgroundScanChannel = AntManager.Instance.OpenBackgroundScanChannel(0);
  45. backgroundScanChannel.onReceiveData += ReceivedBackgroundScanData;
  46. #endif
  47. }
  48. //Android function
  49. void ANTPLUG_ConnectEvent(string resultCode) {
  50. switch (resultCode) {
  51. case "SUCCESS":
  52. connected = true;
  53. break;
  54. case "CHANNEL_NOT_AVAILABLE":
  55. //Channel Not Available
  56. break;
  57. case "ADAPTER_NOT_DETECTED":
  58. //ANT Adapter Not Available. Built-in ANT hardware or external adapter required.
  59. Debug.Log("ANT Adapter Not Available. Built-in ANT hardware or external adapter required.");
  60. break;
  61. case "BAD_PARAMS":
  62. //Bad request parameters.
  63. break;
  64. case "OTHER_FAILUR":
  65. //RequestAccess failed. See logcat for details.
  66. break;
  67. case "DEPENDENCY_NOT_INSTALLED":
  68. //You need to install the ANT+ Plugins service or you may need to update your existing version if you already have it.
  69. case "USER_CANCELLED":
  70. //USER_CANCELLED
  71. break;
  72. case "UNRECOGNIZED":
  73. //UNRECOGNIZED. PluginLib Upgrade Required?",
  74. break;
  75. default:
  76. //UNRECOGNIZED
  77. break;
  78. }
  79. }
  80. void ANTPLUG_StateChange(string newDeviceState) {
  81. switch (newDeviceState) {
  82. case "DEAD":
  83. connected = false;
  84. break;
  85. case "CLOSED":
  86. break;
  87. case "SEARCHING":
  88. //searching
  89. break;
  90. case "TRACKING":
  91. //tracking
  92. break;
  93. case "PROCESSING_REQUEST":
  94. break;
  95. default:
  96. //UNRECOGNIZED
  97. break;
  98. }
  99. }
  100. void ANTPLUG_Receive_calculatedSpeed(string s) {
  101. speed = float.Parse(s) *3.6f;
  102. }
  103. void ANTPLUG_Receive_CalculatedAccumulatedDistance(string s) {
  104. distance = float.Parse(s);
  105. }
  106. //Windows and mac
  107. //If the device is found
  108. void ReceivedBackgroundScanData(Byte[] data) {
  109. byte deviceType = (data[12]); // extended info Device Type byte
  110. switch (deviceType) {
  111. case AntplusDeviceType.BikeSpeed: {
  112. int deviceNumber = (data[10]) | data[11] << 8;
  113. byte transType = data[13];
  114. foreach (AntDevice d in scanResult) {
  115. if (d.deviceNumber == deviceNumber && d.transType == transType) //device already found
  116. return;
  117. }
  118. Debug.Log("Speed sensor found " + deviceNumber);
  119. AntDevice foundDevice = new AntDevice();
  120. foundDevice.deviceType = deviceType;
  121. foundDevice.deviceNumber = deviceNumber;
  122. foundDevice.transType = transType;
  123. foundDevice.period = 8118;
  124. foundDevice.radiofreq = 57;
  125. foundDevice.name = "BikeSpeed(" + foundDevice.deviceNumber+")";
  126. scanResult.Add(foundDevice);
  127. if (autoConnectToFirstSensorFound) {
  128. ConnectToDevice(foundDevice);
  129. }
  130. break;
  131. }
  132. default: {
  133. break;
  134. }
  135. }
  136. }
  137. void ConnectToDevice(AntDevice device) {
  138. AntManager.Instance.CloseBackgroundScanChannel();
  139. byte channelID = AntManager.Instance.GetFreeChannelID();
  140. deviceChannel = AntManager.Instance.OpenChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00, channelID, (ushort)device.deviceNumber,device.deviceType, device.transType, (byte)device.radiofreq, (ushort)device.period, false);
  141. connected = true;
  142. deviceChannel.onReceiveData += Data;
  143. deviceChannel.onChannelResponse += ChannelResponse;
  144. deviceChannel.hideRXFAIL = true;
  145. }
  146. //Deal with the received Data
  147. public void Data(Byte[] data) {
  148. //SPEED
  149. int measTime_speed = (data[4]) | data[5] << 8;
  150. int revCount_speed = (data[6]) | data[7] << 8;
  151. if (prev_measTime_speed != 0 && measTime_speed != prev_measTime_speed && prev_measTime_speed < measTime_speed && prev_revCount_speed < revCount_speed) {
  152. speed = (wheelCircumference * (revCount_speed - prev_revCount_speed) * 1024) / (measTime_speed - prev_measTime_speed);
  153. speed *= 3.6f; // km/h
  154. stopRevCounter_speed = 0;
  155. } else
  156. stopRevCounter_speed++;
  157. if (stopRevCounter_speed >= 5) {
  158. stopRevCounter_speed = 5;
  159. speed = 0;
  160. }
  161. prev_measTime_speed = measTime_speed;
  162. prev_revCount_speed = revCount_speed;
  163. //DISTANCE
  164. if (revCountZero == 0)
  165. revCountZero = revCount_speed;
  166. distance = wheelCircumference * (revCount_speed - revCountZero);
  167. }
  168. void ChannelResponse(ANT_Response response) {
  169. }
  170. }