2
0

ContinousScanExample.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using ANT_Managed_Library;
  5. using System.Collections.Generic;
  6. using System;
  7. /*
  8. * ContinouScanExample
  9. *
  10. * With continuous scan you can connect up to ~75 sensors with a single usb dongle
  11. * open a channel with a given radio frequency AntManager.Instance.OpenContinuousScanChannel(57);
  12. * and then filter the incomming device type and ID in the Receive Data event
  13. */
  14. //Speed sensor object, for every new device ID that match the speed sensor type, we create a new SpeedSensor
  15. public class SpeedSensor {
  16. public int stopRevCounter_speed;
  17. public int prev_measTime_speed;
  18. public int prev_revCount_speed;
  19. public int revCountZero;
  20. public float wheelCircumference = 2.096f;
  21. public float speed;
  22. public int deviceID;
  23. public float GetSpeed(Byte[] data) {
  24. //SPEED
  25. int measTime_speed = (data[5]) | data[6] << 8;
  26. int revCount_speed = (data[7]) | data[8] << 8;
  27. if (prev_measTime_speed != 0 && measTime_speed != prev_measTime_speed && prev_measTime_speed < measTime_speed && prev_revCount_speed < revCount_speed) {
  28. speed = (wheelCircumference * (revCount_speed - prev_revCount_speed) * 1024) / (measTime_speed - prev_measTime_speed);
  29. speed *= 3.6f; // km/h
  30. stopRevCounter_speed = 0;
  31. } else
  32. stopRevCounter_speed++;
  33. if (stopRevCounter_speed >= 5) {
  34. stopRevCounter_speed = 5;
  35. speed = 0;
  36. }
  37. prev_measTime_speed = measTime_speed;
  38. prev_revCount_speed = revCount_speed;
  39. return speed;
  40. }
  41. }
  42. public class ContinousScanExample : MonoBehaviour {
  43. List<SpeedSensor> speedSensorList;
  44. // Use this for initialization
  45. void Start () {
  46. if (AntManager.Instance.device == null) {
  47. AntManager.Instance.Init();
  48. // AntManager.Instance.onDeviceResponse += OnDeviceResponse;
  49. // AntManager.Instance.onSerialError += OnSerialError; //if usb dongle is unplugged for example
  50. }
  51. AntChannel scanChannel = AntManager.Instance.OpenContinuousScanChannel(57);
  52. // scanChannel.onChannelResponse += OnChannelResponse;
  53. scanChannel.onReceiveData += ReceiveContinuouScanData;
  54. }
  55. void ReceiveContinuouScanData(Byte[] data) {
  56. if (speedSensorList == null)
  57. speedSensorList = new List<SpeedSensor>();
  58. // first byte is the channel ID, 0
  59. int pageNumber = data[1] >> 1;
  60. //device number to filter devices
  61. int deviceNumber = ((data[10]) | data[11] << 8);
  62. int deviceType = (data[12]);
  63. SpeedSensor sensor = null;
  64. foreach (SpeedSensor s in speedSensorList) {
  65. if (s.deviceID == deviceNumber) {
  66. //WARNING Byte[] data contains the channel ID in the first byte, shift the payload bytes from the array accordingly
  67. Debug.Log("speed for sensor #" + s.deviceID + ": " + s.GetSpeed(data));
  68. break;
  69. }
  70. }
  71. //if sensor object not created and is of correct type, create and store in list
  72. if (sensor == null && deviceType == AntplusDeviceType.BikeSpeed) {
  73. //found new sensor
  74. sensor = new SpeedSensor();
  75. sensor.deviceID = deviceNumber;
  76. speedSensorList.Add(sensor);
  77. }
  78. }
  79. }