// // // // // // // // // // // // // // // // // // // // // Use this for initialization public void Start() { Debug.Log("Devices :: Start"); accelDataCounter = 0; gyroDataCounter = 0; //Initializer code -START GameObject lmObj = GameObject.Find("LinkManager"); if (lmObj == null) { lmObj = new GameObject("LinkManager"); lmObj.AddComponent <NativeCommunication> (); } linkManager = lmObj.GetComponent <NativeCommunication> (); for (int loop = 0; loop < 500; loop++) { Constants.CalibratedData2 newc = new Constants.CalibratedData2(); newc.accel_x = 0; newc.accel_y = 0; newc.accel_z = 0; calibratedList2.Add(newc); } }
public void AccelSensorData(Vector3 accelData) { Constants.CalibratedData calibrateData = new Constants.CalibratedData(); /// OW Change 20 Jan 18 Constants.CalibratedData2 calibrateData2 = new Constants.CalibratedData2(); /// OW Change 20 Jan 18 float scale_val = 0; float cur_time = Time.time; // calibrating the device starts here!! if (shouldLogData) { //Time calculation -START. if (accelDataCounter == 0) { accel_last_time = Time.time; scale_val = 0; } cur_time = Time.time; ///OW change 20 Jan 18 //scale_val = (cur_time - accel_last_time) *10.0f; scale_val = 1.0f; ///OW change 20 Jan 18 accel_last_time = cur_time; //Time calculation -END. if (accelDataCounter <= 100) { calibrateData.accel_x = accelData.x; calibrateData.accel_y = accelData.y; calibrateData.accel_z = accelData.z; calibrateData2.accel_x = accelData.x; calibrateData2.accel_y = accelData.y; calibrateData2.accel_z = accelData.z; } if (accelDataCounter > 100) { calibrateData.accel_x = accelData.x * scale_val; // multiply scale value calibrateData.accel_y = accelData.y * scale_val; // multiply scale value calibrateData.accel_z = accelData.z * scale_val; // multiply scale value ///OW change 23 Jan 18 calibrateData2.accel_x = accelData.x * scale_val; // multiply scale value calibrateData2.accel_y = accelData.y * scale_val; // multiply scale value calibrateData2.accel_z = accelData.z * scale_val; // multiply scale value ///OW change 23 Jan 18 } /*if (accelDataCounter < 500) { * * calibratedData2 [accelDataCounter] = calibrateData2; * * }*/ calibratedList2.Add(calibrateData2); if (calibratedList2.Count > 500) { calibratedList2.RemoveAt(0); } if (accelDataCounter < 100) { // send log to linkManager-> updateAccLogNotification calibratedData [accelDataCounter] = calibrateData; } if (accelDataCounter == 100) { Accel_x_off_100 = Accel_y_off_100 = Accel_z_off_100 = 0.0f; for (int i = 0; i < 100; i++) { Accel_x_off_100 += calibratedData [i].accel_x; Accel_y_off_100 += calibratedData [i].accel_y; Accel_z_off_100 += calibratedData [i].accel_z; } Accel_x_off_100 /= 100.0f; Accel_y_off_100 /= 100.0f; Accel_z_off_100 /= 100.0f; SumStill_100 = 0; for (int i = 0; i < 100; i++) { SumStill_100 += ( Mathf.Pow(calibratedData[i].accel_x - Accel_x_off_100, 2.0f) + Mathf.Pow(calibratedData[i].accel_y - Accel_y_off_100, 2.0f) + Mathf.Pow(calibratedData[i].accel_z - Accel_z_off_100, 2.0f) ); } Accel_x_off = Accel_x_off_100; Accel_y_off = Accel_y_off_100; Accel_z_off = Accel_z_off_100; SumStill_100 = SumStill; Still = 0; } //OW change 23 Jan 18 if (accelDataCounter > 500) { Accel_x_off = 0.0f; Accel_y_off = 0.0f; Accel_z_off = 0.0f; //Debug.Log ("Length of Clibrated Data Array : " + calibratedData2.Length); //calibratedData2 [accelDataCounter] = calibrateData2; int counter = 0; for (int loop = 0; loop < calibratedList2.Count; loop++) { Accel_x_off += calibratedList2 [loop].accel_x; Accel_y_off += calibratedList2 [loop].accel_y; Accel_z_off += calibratedList2 [loop].accel_z; counter++; } Accel_x_off /= (float)counter; Accel_y_off /= (float)counter; Accel_z_off /= (float)counter; } } // Calibrating data ends here. Accel_x_cal = calibrateData.accel_x - Accel_x_off * scale_val; Accel_y_cal = calibrateData.accel_y - Accel_y_off * scale_val; Accel_z_cal = calibrateData.accel_z - Accel_z_off * scale_val; Vector3 calGroup = new Vector3(Accel_x_cal, Accel_y_cal, Accel_z_cal); //fifteenList.Add (calGroup); //if (fifteenList.Count > 15) { // fifteenList.RemoveAt (0); //} if (accelDataCounter > 100) { //Accel_x_Sum_15 = 0 ; //Accel_y_Sum_15 = 0 ; //Accel_z_Sum_15 = 0 ; //for (int loop = 0; loop <15 ; loop++) { //Accel_x_Sum_15 += fifteenList[loop].x; //Accel_y_Sum_15 += fifteenList[loop].y; //Accel_z_Sum_15 += fifteenList[loop].z; //} StillCalc = 0; StillCalc = (Mathf.Pow(Accel_x_cal, 2) + Mathf.Pow(Accel_y_cal, 2) + Mathf.Pow(Accel_z_cal, 2)); if (StillCalc < 1.0f) { Still = 1; } else if (StillCalc > 1.0f) { Still = 0; } //Still = (ushort)(StillCalc < 5.0f ? 1 : 0); int newSumStill = SumStill + Still; SumStill = newSumStill; calibrateData.accel_x = Accel_x_cal; calibrateData.accel_y = Accel_y_cal; calibrateData.accel_z = Accel_z_cal; } //This will be called first since the check is >= 100 MWData newData = new MWData(); if (accelDataCounter >= 100) { newData.accel_x = calibrateData.accel_x; newData.accel_y = calibrateData.accel_y; newData.accel_z = calibrateData.accel_z; //newData.accel_x_15 = Accel_x_Sum_15; //newData.accel_y_15 = Accel_y_Sum_15; //newData.accel_z_15 = Accel_z_Sum_15; //********************************** //When this is executed the 100th Time //********************************** //For the 100th sample still will be zero //This will NOT be zero for 100 onwards data samples since it is being modified in code block above //********************************** // When it is excuted after 100th time //********************************** //self.Still will either be 0 or 1 because of the conditional operator which assign it 1 if it is less than 0.015 newData.still = Still; newData.stillCalc = StillCalc; //********************************** //When this is executed the 100th Time //********************************** //At this point newdata will contain RAW data sample //This will NOT contain RAW values for 100 onwards data samples since it is being modified in code block above //And it contains calibrated values based on Accel_x_cal = calibratedData.accel_x_a - Accel_x_off; AccelHistoricalData.Add(newData); if (shouldLogData) { string accelLog = "Unity:: Device Serial : " + deviceSerialNo + " -- " + accelDataCounter + "," + cur_time + "," + scale_val + "," + accelData.x + "," + accelData.y + "," + accelData.z + "," + Accel_x_cal + "," + Accel_y_cal + "," + Accel_z_cal + "," + Accel_x_off_100 + "," + Accel_y_off_100 + "," + Accel_z_off_100 + "," + Accel_x_off + "," + Accel_y_off + "," + Accel_z_off + "," + Still + "," + SumStill + "," + StillCalc + "," + calibratedList2.Count; linkManager.UpdateAccLogNotification(accelLog); } } //Processed one data sample increment counter if (shouldLogData) { accelDataCounter++; } // send notification after calibrating device if (accelDataCounter >= 212 && !hasNotifiedLink) { Debug.LogError("Calibrated Device"); linkManager.AccelCalibrationCompleteNotification(deviceSerialNo); hasNotifiedLink = true; } }