@@ -46,6 +46,15 @@ class ViewController: GLKViewController, ARSessionDelegate, RTABMapObserver, UIP
4646 private var mReviewRequested = false
4747
4848 private var mMaximumMemory : Int = 0
49+ private struct MotionSample {
50+ let timestamp : TimeInterval
51+ let distanceTravelled : Float
52+ let yaw : Float
53+ let pitch : Float
54+ let roll : Float
55+ }
56+ private let debugMotionWindowSeconds : TimeInterval = 5.0
57+ private var debugMotionSamples : [ MotionSample ] = [ ]
4958
5059 // UI states
5160 private enum State {
@@ -381,6 +390,11 @@ class ViewController: GLKViewController, ARSessionDelegate, RTABMapObserver, UIP
381390 pitch: Float ,
382391 yaw: Float )
383392 {
393+ let now = ProcessInfo . processInfo. systemUptime
394+ if loopClosureId > 0 {
395+ self . debugMotionSamples. removeAll ( )
396+ }
397+ let motionMetrics = self . updateDebugMotionMetrics ( timestamp: now, distanceTravelled: distanceTravelled, yaw: yaw, pitch: pitch, roll: roll)
384398 let availableMem = self . getAvailableMemory ( )
385399 let usedMem = self . mMaximumMemory - availableMem;
386400
@@ -458,6 +472,8 @@ class ViewController: GLKViewController, ARSessionDelegate, RTABMapObserver, UIP
458472 " Hypothesis (%): \( Int ( hypothesis*100) ) / \( Int ( self . mLoopThr*100) ) ( \( loopClosureId> 0 ? loopClosureId : highestHypId) ) \n " +
459473 String( format: " FPS (rendering): %.1f Hz \n " , fps) +
460474 String( format: " Travelled distance: %.2f m \n " , distanceTravelled) +
475+ String( format: " Average speed (last %.1f s): %.2f m/s \n " , self . debugMotionWindowSeconds, motionMetrics. linearMetersPerSecond) +
476+ String( format: " Turn rate (last %.1f s): %.1f deg/s \n " , self . debugMotionWindowSeconds, motionMetrics. turnDegreesPerSecond) +
461477 String( format: " Pose (x,y,z): %.2f %.2f %.2f " , x, y, z)
462478 }
463479 if ( self . mState == . STATE_MAPPING || self . mState == . STATE_VISUALIZING_CAMERA)
@@ -531,6 +547,47 @@ class ViewController: GLKViewController, ARSessionDelegate, RTABMapObserver, UIP
531547 }
532548 }
533549 }
550+
551+ private func updateDebugMotionMetrics( timestamp: TimeInterval , distanceTravelled: Float , yaw: Float , pitch: Float , roll: Float ) -> ( linearMetersPerSecond: Float , turnDegreesPerSecond: Float ) {
552+ debugMotionSamples. append ( MotionSample ( timestamp: timestamp, distanceTravelled: distanceTravelled, yaw: yaw, pitch: pitch, roll: roll) )
553+
554+ let minimumTimestamp = timestamp - debugMotionWindowSeconds
555+ debugMotionSamples. removeAll { $0. timestamp < minimumTimestamp }
556+
557+ guard let firstSample = debugMotionSamples. first, let lastSample = debugMotionSamples. last else {
558+ return ( 0.0 , 0.0 )
559+ }
560+
561+ let deltaTime = max ( Float ( lastSample. timestamp - firstSample. timestamp) , 0.001 )
562+ let deltaDistance = max ( 0.0 , lastSample. distanceTravelled - firstSample. distanceTravelled)
563+ let linearMetersPerSecond = deltaDistance / deltaTime
564+
565+ var accumulatedAngleDelta : Float = 0.0
566+ if debugMotionSamples. count >= 2 {
567+ for index in 1 ..< debugMotionSamples. count {
568+ let prev = debugMotionSamples [ index - 1 ]
569+ let curr = debugMotionSamples [ index]
570+ let yawDelta = shortestAngleDeltaRadians ( from: prev. yaw, to: curr. yaw)
571+ let pitchDelta = shortestAngleDeltaRadians ( from: prev. pitch, to: curr. pitch)
572+ let rollDelta = shortestAngleDeltaRadians ( from: prev. roll, to: curr. roll)
573+ accumulatedAngleDelta += sqrt ( yawDelta*yawDelta + pitchDelta*pitchDelta + rollDelta*rollDelta)
574+ }
575+ }
576+ let turnDegreesPerSecond = accumulatedAngleDelta * 180.0 / . pi / deltaTime
577+
578+ return ( linearMetersPerSecond, turnDegreesPerSecond)
579+ }
580+
581+ private func shortestAngleDeltaRadians( from start: Float , to end: Float ) -> Float {
582+ var delta = end - start
583+ while delta > . pi {
584+ delta -= 2.0 * . pi
585+ }
586+ while delta < - . pi {
587+ delta += 2.0 * . pi
588+ }
589+ return delta
590+ }
534591
535592 func cameraInfoEventReceived( _ rtabmap: RTABMap , type: Int , key: String , value: String ) {
536593 if ( self . debugShown && key == " UpstreamRelocationFiltered " )
0 commit comments