@@ -9,7 +9,7 @@ import { useCountdown } from '@/hooks/countdown';
99import classNames from 'classnames' ;
1010import { TaybolIcon } from '@/components/commons/icon/TaybolIcon' ;
1111import { useRestCalibrationTrackers } from '@/hooks/imu-logic' ;
12- import { averageVector , Vector3FromVec3fT } from '@/maths/vector3' ;
12+ import { Vector3FromVec3fT } from '@/maths/vector3' ;
1313import { Vector3 } from 'three' ;
1414import { useTimeout } from '@/hooks/timeout' ;
1515import { useAtomValue } from 'jotai' ;
@@ -23,7 +23,9 @@ export enum CalibrationStatus {
2323}
2424
2525export const IMU_CALIBRATION_TIME = 4 ;
26- const ACCEL_TOLERANCE = 0.2 ; // m/s^2
26+ export const IMU_SETTLE_TIME = 1 ;
27+ const ACCEL_TOLERANCE = 0.5 ; // m/s^2
28+ const ACCEL_HYSTERESIS = 0.1 ; // m/s^2
2729
2830export function CalibrationTutorialPage ( ) {
2931 const { l10n } = useLocalization ( ) ;
@@ -32,43 +34,61 @@ export function CalibrationTutorialPage() {
3234 CalibrationStatus . WAITING
3335 ) ;
3436 const [ skipButton , setSkipButton ] = useState ( false ) ;
37+ const [ settled , setSettled ] = useState ( false ) ;
3538 const { timer, isCounting, startCountdown, abortCountdown } = useCountdown ( {
36- duration : IMU_CALIBRATION_TIME ,
37- onCountdownEnd : ( ) => setCalibrationStatus ( CalibrationStatus . SUCCESS ) ,
39+ duration : settled ? IMU_CALIBRATION_TIME : IMU_SETTLE_TIME ,
40+ onCountdownEnd : ( ) =>
41+ settled
42+ ? setCalibrationStatus ( CalibrationStatus . SUCCESS )
43+ : setSettled ( true ) ,
3844 } ) ;
3945 useTimeout ( ( ) => setSkipButton ( true ) , 10000 ) ;
4046 const connectedIMUTrackers = useAtomValue ( connectedIMUTrackersAtom ) ;
4147 const restCalibrationTrackers =
4248 useRestCalibrationTrackers ( connectedIMUTrackers ) ;
4349 const [ rested , setRested ] = useState ( false ) ;
44- const lastValueMap = useRef ( new Map < number , Vector3 [ ] > ( ) ) ;
50+ const lastValueMap = useRef ( new Map < number , Vector3 > ( ) ) ;
4551 useEffect ( ( ) => {
4652 const accelLength = restCalibrationTrackers . every ( ( x ) => {
4753 if (
54+ x . device ?. id ?. id === undefined ||
4855 x . tracker . trackerId ?. trackerNum === undefined ||
49- x . tracker . trackerId . deviceId ?. id === undefined ||
5056 ! x . tracker . linearAcceleration
5157 )
5258 return false ;
5359
54- const trackerId =
55- x . tracker . trackerId . trackerNum + ( x . tracker . trackerId . trackerNum << 8 ) ;
56- const lastValues = lastValueMap . current . get ( trackerId ) ?? [ ] ;
57- lastValueMap . current . set ( trackerId , lastValues ) ;
60+ const trackerId = x . tracker . trackerId . trackerNum + ( x . device . id . id << 8 ) ;
61+ const lastValue = lastValueMap . current . get ( trackerId ) ?? new Vector3 ( ) ;
62+ lastValueMap . current . set ( trackerId , lastValue ) ;
5863
5964 const vec3 = Vector3FromVec3fT ( x . tracker . linearAcceleration ) ;
60- if ( lastValues . length > 5 ) {
61- lastValues . shift ( ) ;
62- const avg = averageVector ( lastValues ) . lengthSq ( ) ;
63- lastValues . push ( vec3 ) ;
64- return vec3 . lengthSq ( ) <= avg + ACCEL_TOLERANCE ** 2 ;
65+
66+ if ( vec3 . lengthSq ( ) > ACCEL_TOLERANCE ** 2 ) {
67+ return false ;
68+ }
69+
70+ const delta = new Vector3 ( ) ;
71+ delta . subVectors ( lastValue , vec3 ) ;
72+
73+ if ( delta . lengthSq ( ) > ACCEL_HYSTERESIS ** 2 ) {
74+ lastValue . copy ( vec3 ) ;
75+ return false ;
6576 }
66- lastValues . push ( vec3 ) ;
67- return false ;
77+
78+ return true ;
6879 } ) ;
6980
70- setRested ( accelLength || restCalibrationTrackers . length === 0 ) ;
71- } , [ restCalibrationTrackers ] ) ;
81+ if ( accelLength && ! settled && ! isCounting ) {
82+ abortCountdown ( ) ;
83+ startCountdown ( ) ;
84+ } else if ( ! accelLength && ! settled && isCounting ) {
85+ abortCountdown ( ) ;
86+ } else if ( ! accelLength && settled ) {
87+ setSettled ( false ) ;
88+ }
89+
90+ setRested ( settled || restCalibrationTrackers . length === 0 ) ;
91+ } , [ restCalibrationTrackers , settled , isCounting ] ) ;
7292
7393 useEffect ( ( ) => {
7494 if ( calibrationStatus === CalibrationStatus . CALIBRATING && ! rested ) {
@@ -145,7 +165,7 @@ export function CalibrationTutorialPage() {
145165 </ div >
146166 < ProgressBar
147167 progress = {
148- isCounting
168+ isCounting && settled
149169 ? ( IMU_CALIBRATION_TIME - timer ) / IMU_CALIBRATION_TIME
150170 : calibrationStatus === CalibrationStatus . SUCCESS ||
151171 calibrationStatus === CalibrationStatus . ERROR
0 commit comments