PowerMeterReceiver.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using ANT_Managed_Library;
  5. using System;
  6. /*
  7. Frequencies such as 4.06 Hz (ANT+ Heart
  8. Rate) and 4.005 Hz (ANT+ Bike Power) will
  9. periodically “drift” into each other, or into
  10. other channel periods that may be present
  11. in the vicinity. During this overlap, channel
  12. collisions may occur as the radio can only
  13. service channel at a time.
  14. */
  15. public class PowerMeterReceiver
  16. {
  17. private int deviceID = 0; //set this to connect to a specific device ID
  18. private bool connected = false; //will be set to true once connected
  19. private List<AntDevice> scanResult;
  20. private int instantaneousPower; // the instantaneous power in watt
  21. private int instantaneousCadence; // crank cadence in RPM if available ( 255 indicates invalid)
  22. private AntChannel backgroundScanChannel;
  23. private AntChannel deviceChannel;
  24. public int DeviceID => deviceID;
  25. public bool Connected => connected;
  26. public List<AntDevice> ScanResult => scanResult;
  27. public int InstantaneousPower => instantaneousPower;
  28. public int InstantaneousCadence => instantaneousCadence;
  29. private byte[] pageToSend;
  30. public PowerMeterReceiver()
  31. {
  32. }
  33. public PowerMeterReceiver(int deviceID) => this.deviceID = deviceID;
  34. public void StartScan()
  35. {
  36. Debug.Log("Looking for ANT + power meter sensor");
  37. AntManager.Instance.Init();
  38. scanResult = new List<AntDevice>();
  39. backgroundScanChannel = AntManager.Instance.OpenBackgroundScanChannel(0);
  40. backgroundScanChannel.onReceiveData += ReceivedBackgroundScanData;
  41. }
  42. void ReceivedBackgroundScanData(Byte[] data)
  43. {
  44. byte deviceType = (data[12]); // extended info Device Type byte
  45. switch (deviceType)
  46. {
  47. case AntplusDeviceType.BikePower:
  48. {
  49. int deviceNumber = (data[10]) | data[11] << 8;
  50. byte transType = data[13];
  51. foreach (AntDevice d in scanResult)
  52. {
  53. if (d.deviceNumber == deviceNumber && d.transType == transType) //device already found
  54. return;
  55. }
  56. AntDevice foundDevice = new AntDevice();
  57. foundDevice.deviceType = deviceType;
  58. foundDevice.deviceNumber = deviceNumber;
  59. foundDevice.transType = transType;
  60. foundDevice.period = 8182;
  61. foundDevice.radiofreq = 57;
  62. foundDevice.name = "Powermeter(" + foundDevice.deviceNumber + ")";
  63. scanResult.Add(foundDevice);
  64. if (deviceNumber == deviceID)
  65. {
  66. Debug.Log($"Desired Power Sensor with id {deviceNumber} found!");
  67. ConnectToDevice(foundDevice);
  68. }
  69. else
  70. {
  71. Debug.Log($"Power sensor ({deviceNumber}) found and added to ScanResult");
  72. }
  73. break;
  74. }
  75. }
  76. }
  77. void ConnectToDevice(AntDevice device)
  78. {
  79. AntManager.Instance.CloseBackgroundScanChannel();
  80. byte channelID = AntManager.Instance.GetFreeChannelID();
  81. deviceChannel = AntManager.Instance.OpenChannel(ANT_ReferenceLibrary.ChannelType.BASE_Slave_Receive_0x00,
  82. channelID, (ushort) device.deviceNumber, device.deviceType, device.transType, (byte) device.radiofreq,
  83. (ushort) device.period, false);
  84. connected = true;
  85. deviceChannel.onReceiveData += Data;
  86. deviceChannel.onChannelResponse += ChannelResponse;
  87. deviceChannel.hideRXFAIL = true;
  88. }
  89. int update_event_count = 0;
  90. int sameEventCounter = 0;
  91. //Deal with the received Data
  92. private void Data(Byte[] data)
  93. {
  94. if (data[0] == 0x10)
  95. {
  96. if (data[1] == update_event_count)
  97. sameEventCounter++;
  98. else
  99. sameEventCounter = 0;
  100. update_event_count = data[1];
  101. if (sameEventCounter > 3)
  102. {
  103. instantaneousPower = 0;
  104. instantaneousCadence = 0;
  105. }
  106. else
  107. {
  108. instantaneousPower = (data[6]) | data[7] << 8;
  109. instantaneousCadence = data[3];
  110. }
  111. }
  112. }
  113. public void Calibrate()
  114. {
  115. Debug.Log("Sending : Manual Zero Calibration request");
  116. pageToSend = new byte[8] {0x01, 0xAA, 0x0FF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  117. deviceChannel.sendAcknowledgedData(pageToSend);
  118. }
  119. void ChannelResponse(ANT_Response response)
  120. {
  121. if (response.getChannelEventCode() == ANT_ReferenceLibrary.ANTEventID.EVENT_TRANSFER_TX_FAILED_0x06)
  122. {
  123. deviceChannel.sendAcknowledgedData(pageToSend); //send the page again if the transfer failed
  124. }
  125. }
  126. }