@@ -14,6 +14,8 @@ import { Vector3 } from 'three';
1414import  {  useTimeout  }  from  '@/hooks/timeout' ; 
1515import  {  useAtomValue  }  from  'jotai' ; 
1616import  {  connectedIMUTrackersAtom  }  from  '@/store/app-store' ; 
17+ import  {  c ,  s  }  from  'vite/dist/node/types.d-aGj9QkWt' ; 
18+ import  {  log  }  from  '@/utils/logging' ; 
1719
1820export  enum  CalibrationStatus  { 
1921  SUCCESS , 
@@ -23,7 +25,9 @@ export enum CalibrationStatus {
2325} 
2426
2527export  const  IMU_CALIBRATION_TIME  =  4 ; 
26- const  ACCEL_TOLERANCE  =  0.2 ;  // m/s^2 
28+ export  const  IMU_SETTLE_TIME  =  1 ; 
29+ const  ACCEL_TOLERANCE  =  0.5 ;  // m/s^2 
30+ const  ACCEL_HYSTERESIS  =  0.1 ;  // m/s^2 
2731
2832export  function  CalibrationTutorialPage ( )  { 
2933  const  {  l10n }  =  useLocalization ( ) ; 
@@ -32,16 +36,17 @@ export function CalibrationTutorialPage() {
3236    CalibrationStatus . WAITING 
3337  ) ; 
3438  const  [ skipButton ,  setSkipButton ]  =  useState ( false ) ; 
39+   const  [ settled ,  setSettled ]  =  useState ( false ) ; 
3540  const  {  timer,  isCounting,  startCountdown,  abortCountdown }  =  useCountdown ( { 
36-     duration : IMU_CALIBRATION_TIME , 
37-     onCountdownEnd : ( )  =>  setCalibrationStatus ( CalibrationStatus . SUCCESS ) , 
41+     duration : settled  ?  IMU_CALIBRATION_TIME  :  IMU_SETTLE_TIME , 
42+     onCountdownEnd : ( )  =>  settled  ?  setCalibrationStatus ( CalibrationStatus . SUCCESS )  :  setSettled ( true ) , 
3843  } ) ; 
3944  useTimeout ( ( )  =>  setSkipButton ( true ) ,  10000 ) ; 
4045  const  connectedIMUTrackers  =  useAtomValue ( connectedIMUTrackersAtom ) ; 
4146  const  restCalibrationTrackers  = 
4247    useRestCalibrationTrackers ( connectedIMUTrackers ) ; 
4348  const  [ rested ,  setRested ]  =  useState ( false ) ; 
44-   const  lastValueMap  =  useRef ( new  Map < number ,  Vector3 [ ] > ( ) ) ; 
49+   const  lastValueMap  =  useRef ( new  Map < number ,  Vector3 > ( ) ) ; 
4550  useEffect ( ( )  =>  { 
4651    const  accelLength  =  restCalibrationTrackers . every ( ( x )  =>  { 
4752      if  ( 
@@ -58,17 +63,32 @@ export function CalibrationTutorialPage() {
5863      lastValueMap . current . set ( trackerId ,  lastValue ) ; 
5964
6065      const  vec3  =  Vector3FromVec3fT ( x . tracker . linearAcceleration ) ; 
61-       if  ( lastValues . length  >  5 )  { 
62-         lastValues . shift ( ) ; 
63-         const  avg  =  averageVector ( lastValues ) . lengthSq ( ) ; 
64-         lastValues . push ( vec3 ) ; 
65-         return  vec3 . lengthSq ( )  <=  avg  +  ACCEL_TOLERANCE  **  2 ; 
66+ 
67+       if  ( vec3 . lengthSq ( )  >  ACCEL_TOLERANCE  **  2 )  { 
68+         return  false ; 
69+       } 
70+ 
71+       const  delta  =  new  Vector3 ( ) ; 
72+       delta . subVectors ( lastValue ,  vec3 ) ; 
73+ 
74+       if  ( delta . lengthSq ( )  >  ACCEL_HYSTERESIS  **  2 )  { 
75+         lastValue . copy ( vec3 ) ; 
76+         return  false ; 
6677      } 
67-        lastValues . push ( vec3 ) ; 
68-       return  false ; 
78+ 
79+       return  true ; 
6980    } ) ; 
7081
71-     setRested ( accelLength  ||  restCalibrationTrackers . length  ===  0 ) ; 
82+     if  ( accelLength  &&  ! settled  &&  ! isCounting )  { 
83+       abortCountdown ( ) ; 
84+       startCountdown ( ) ; 
85+     }  else  if  ( ! accelLength  &&  ! settled  &&  isCounting )  { 
86+       abortCountdown ( ) ; 
87+     }  else  if  ( ! accelLength  &&  settled )  { 
88+       setSettled ( false ) ; 
89+     } 
90+ 
91+     setRested ( settled  ||  restCalibrationTrackers . length  ===  0 ) ; 
7292  } ,  [ restCalibrationTrackers ] ) ; 
7393
7494  useEffect ( ( )  =>  { 
@@ -148,7 +168,7 @@ export function CalibrationTutorialPage() {
148168                </ div > 
149169                < ProgressBar 
150170                  progress = { 
151-                     isCounting 
171+                     isCounting   &&   settled 
152172                      ? ( IMU_CALIBRATION_TIME  -  timer )  /  IMU_CALIBRATION_TIME 
153173                      : calibrationStatus  ===  CalibrationStatus . SUCCESS  || 
154174                          calibrationStatus  ===  CalibrationStatus . ERROR 
0 commit comments