-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: impl world tracking source feature + broken motion tracking
- Loading branch information
Showing
6 changed files
with
202 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// WorldTrackingSource.swift | ||
// ALVRClient | ||
// | ||
// Created by Viktor Varenik on 23.02.2024. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol WorldTrackingSource { | ||
func getLinearVelocity() -> (Float, Float, Float) | ||
func getPosition() -> (Float, Float, Float) | ||
func getRotation() -> (Float, Float, Float) | ||
|
||
func start() | ||
func stop() | ||
} |
83 changes: 83 additions & 0 deletions
83
ALVRClient/WorldTrackingSources/ARWorldTrackingSource.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// ARWorldTrackingSource.swift | ||
// ALVRClient | ||
// | ||
// Created by Viktor Varenik on 23.02.2024. | ||
// | ||
|
||
import ARKit | ||
|
||
class ARWorldTrackingSource: NSObject, ARSessionDelegate, WorldTrackingSource { | ||
private let dispatchQueue: DispatchQueue | ||
private let configuration: ARWorldTrackingConfiguration // or AROrientationTrackingConfiguration | ||
private let arSession: ARSession | ||
// ARSession have very big impact for battery | ||
// Maybe i should use CoreMotion? | ||
// But ARSession can track position in space | ||
|
||
private var lastTickTime: Int64 = 0 | ||
private var tps = 0 | ||
|
||
// FIXME: Monkey code | ||
private var linearVelocity: (Float, Float, Float) = (Float.zero, Float.zero, Float.zero) | ||
private var position: (Float, Float, Float) = (Float.zero, Float.zero, Float.zero) | ||
private var rotation: (Float, Float, Float) = (Float.zero, Float.zero, Float.zero) | ||
|
||
override init() { | ||
dispatchQueue = .init(label: "ARWorldTrackingSource", qos: .background) | ||
|
||
configuration = .init() | ||
configuration.planeDetection = .horizontal | ||
|
||
arSession = ARSession() | ||
|
||
super.init() | ||
|
||
arSession.delegate = self | ||
} | ||
|
||
func session(_ session: ARSession, didUpdate frame: ARFrame) { | ||
// TODO: linearVelocity | ||
|
||
if let framePosition = arSession.currentFrame?.camera.transform.columns.3 { | ||
// FIXME: Need to calibrate y offset | ||
// One metter offset just matched for initial position on my desk | ||
position = (framePosition.x, framePosition.y + 1.0 /* 1 metter offset */, framePosition.z) | ||
} | ||
|
||
if let frameEuler = arSession.currentFrame?.camera.eulerAngles { | ||
rotation = (frameEuler.x, frameEuler.y, frameEuler.z) | ||
} | ||
|
||
tps += 1 | ||
if Int64.getCurrentMillis() - lastTickTime > 1000 { | ||
lastTickTime = Int64.getCurrentMillis() | ||
// print("World tracker tps is \(tps)") | ||
tps = 0 | ||
} | ||
} | ||
|
||
func start() { | ||
// Start ar session | ||
dispatchQueue.async { [weak self] in | ||
guard let self = self else { return } | ||
self.arSession.run(self.configuration) | ||
} | ||
} | ||
|
||
func stop() { | ||
arSession.pause() | ||
} | ||
|
||
func getLinearVelocity() -> (Float, Float, Float) { | ||
return linearVelocity | ||
} | ||
|
||
func getPosition() -> (Float, Float, Float) { | ||
return position | ||
} | ||
|
||
func getRotation() -> (Float, Float, Float) { | ||
return rotation | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
ALVRClient/WorldTrackingSources/MotionWorldTrackingSource.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// MotionWorldTrackingSource.swift | ||
// ALVRClient | ||
// | ||
// Created by Viktor Varenik on 23.02.2024. | ||
// | ||
|
||
import CoreMotion | ||
|
||
// FIXME: Not working yet! | ||
class MotionWorldTrackingSource: NSObject, WorldTrackingSource { | ||
private let dispatchQueue: DispatchQueue | ||
private let motionManager: CMMotionManager | ||
|
||
private var lastTickTime: Int64 = 0 | ||
private var tps = 0 | ||
|
||
// FIXME: Monkey code | ||
private var linearVelocity: (Float, Float, Float) = (Float.zero, Float.zero, Float.zero) | ||
private var position: (Float, Float, Float) = (Float.zero, Float.zero, Float.zero) | ||
private var rotation: (Float, Float, Float) = (Float.zero, Float.zero, Float.zero) | ||
|
||
override init() { | ||
dispatchQueue = .init(label: "ARWorldTrackingSource", qos: .background) | ||
|
||
motionManager = .init() | ||
motionManager.deviceMotionUpdateInterval = 1000 / 30 // 30 tps | ||
|
||
super.init() | ||
} | ||
|
||
func start() { | ||
motionManager.startDeviceMotionUpdates(to: .main) { [weak self] motion, error in | ||
if error != nil { return } | ||
guard let self = self else { return } | ||
guard let motion = motion else { return } | ||
|
||
let attitude = motion.attitude | ||
rotation = (Float(attitude.roll), Float(attitude.pitch), Float(attitude.yaw)) | ||
|
||
position.2 = 1.7 | ||
} | ||
} | ||
|
||
func stop() { | ||
motionManager.stopDeviceMotionUpdates() | ||
} | ||
|
||
func getLinearVelocity() -> (Float, Float, Float) { | ||
return linearVelocity | ||
} | ||
|
||
func getPosition() -> (Float, Float, Float) { | ||
return position | ||
} | ||
|
||
func getRotation() -> (Float, Float, Float) { | ||
return rotation | ||
} | ||
} |