123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- using ANT_Managed_Library;
- using System.Collections.Generic;
- using System;
- public class SpeedSensor {
- public int stopRevCounter_speed;
- public int prev_measTime_speed;
- public int prev_revCount_speed;
- public int revCountZero;
- public float wheelCircumference = 2.096f;
- public float speed;
- public int deviceID;
- public float GetSpeed(Byte[] data) {
-
-
- int measTime_speed = (data[5]) | data[6] << 8;
- int revCount_speed = (data[7]) | data[8] << 8;
- if (prev_measTime_speed != 0 && measTime_speed != prev_measTime_speed && prev_measTime_speed < measTime_speed && prev_revCount_speed < revCount_speed) {
-
- speed = (wheelCircumference * (revCount_speed - prev_revCount_speed) * 1024) / (measTime_speed - prev_measTime_speed);
- speed *= 3.6f;
- stopRevCounter_speed = 0;
- } else
- stopRevCounter_speed++;
- if (stopRevCounter_speed >= 5) {
- stopRevCounter_speed = 5;
- speed = 0;
- }
- prev_measTime_speed = measTime_speed;
- prev_revCount_speed = revCount_speed;
- return speed;
- }
- }
- public class ContinousScanExample : MonoBehaviour {
- List<SpeedSensor> speedSensorList;
-
- void Start () {
- if (AntManager.Instance.device == null) {
- AntManager.Instance.Init();
-
-
- }
- AntChannel scanChannel = AntManager.Instance.OpenContinuousScanChannel(57);
-
- scanChannel.onReceiveData += ReceiveContinuouScanData;
- }
- void ReceiveContinuouScanData(Byte[] data) {
- if (speedSensorList == null)
- speedSensorList = new List<SpeedSensor>();
-
- int pageNumber = data[1] >> 1;
-
- int deviceNumber = ((data[10]) | data[11] << 8);
- int deviceType = (data[12]);
-
- SpeedSensor sensor = null;
- foreach (SpeedSensor s in speedSensorList) {
- if (s.deviceID == deviceNumber) {
-
- Debug.Log("speed for sensor #" + s.deviceID + ": " + s.GetSpeed(data));
- break;
- }
- }
-
- if (sensor == null && deviceType == AntplusDeviceType.BikeSpeed) {
-
- sensor = new SpeedSensor();
- sensor.deviceID = deviceNumber;
- speedSensorList.Add(sensor);
- }
- }
- }
|