Skip to content

Commit

Permalink
AR ROBOT!!!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
nab138 committed May 12, 2024
1 parent d989cff commit 527dedd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 35 deletions.
6 changes: 3 additions & 3 deletions RootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class RootViewController: UIViewController, UIGestureRecognizerDelegate {
robotNode = robot
// Create a dummy node so that I can offset the position of the robot
let dummyNode = SCNNode()
dummyNode.position = SCNVector3(0.35, -0.35, -0.35)
dummyNode.position = SCNVector3(0.35, -0.35, -0.8)
dummyNode.addChildNode(robotNode)
fieldNode.addChildNode(dummyNode)

Expand All @@ -70,7 +70,7 @@ class RootViewController: UIViewController, UIGestureRecognizerDelegate {
// [x, y, rot (degrees)]
let newPos = topic.getDoubleArray();
// The data is in meters relative to the field center (in the field model scale) so we need to scale it to the ARKit scale
self.robotNode.position = SCNVector3(newPos![0] - 8.25, 0, newPos![1] - 4)
self.robotNode.position = SCNVector3(-newPos![0] + 8.25, 0, newPos![1] - 4)
self.robotNode.eulerAngles.y = Float(newPos![2] * .pi / 180)
}
}, onConnect: {
Expand All @@ -80,7 +80,7 @@ class RootViewController: UIViewController, UIGestureRecognizerDelegate {
})

NTClient.connect()
NTClient.subscribe(key: "/SmartDashboard/Field/Robot")
NTClient.subscribe(key: "/SmartDashboard/Field/Robot", periodic: 0.001)
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
Expand Down
2 changes: 1 addition & 1 deletion deploy.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

mv packages/*.ipa packages_archive/
make package
make clean package
package_name=$(ls packages/*.ipa | awk -F'/' '{print $2}')
$THEOS/bin/sideloader-cli-linux-x86_64 install packages/$package_name -i
55 changes: 24 additions & 31 deletions src/NetworkTables/NT4Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,12 @@ class NT4Client: WebSocketDelegate {
serverConnectionActive = false
onDisconnect?(reason, code)
case .text(let string):
NSLog("Recieved text: \(string)")
if let data = string.data(using: .utf8) {
if let msg = try? JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
NSLog("Received JSON: \(msg)")
// Iterate through the messages
for obj in msg {
let objStr = String(describing: obj)
// Attempt to decode the message as a JSON object
if let msgObj = try? JSONSerialization.jsonObject(with: objStr.data(using: .utf8)!, options: []) as? [String: Any] {
// Handle the message
handleJsonMessage(msg: msgObj)
}
// Handle the message
handleJsonMessage(msg: obj)
}
}
}
Expand Down Expand Up @@ -160,38 +154,41 @@ class NT4Client: WebSocketDelegate {
}

private func handleJsonMessage(msg: [String: Any]) {
NSLog("Recieved Parsed JSON: \(msg)")
if let method = msg["method"] as? String {
if let params = msg["params"] as? [String: Any] {
switch method {
case "announce":
let newTopic = NTTopic(data: params)
serverTopics[newTopic.name] = newTopic
onTopicAnnounce?(newTopic)
NSLog("Announce: \(params)")
default:
NSLog("Unknown method: \(method)")
}
}
}
}

private func handleMsgPackMessage(msg: [Optional<Any>]){
NSLog("Received binary: \(msg)")
NSLog("attempting to decode topic id: \(msg[0]!)")
guard let unwrappedMsg = msg[0], let topicID = unwrappedMsg as? Int else {
NSLog("Failed to decode topicID")
return
}
guard let timestamp = msg[1]! as? Int64 else {
private func handleMsgPackMessage(msg: [Any?]){
NSLog("Type of msg[3]: \(type(of: msg[3]!))")

guard let unsignedTimestamp = msg[1]! as? UInt32 else {
NSLog("Failed to decode timestamp")
return
}
let data = msg[2]
NSLog("topicID: \(topicID), timestamp: \(timestamp), data: \(data!)")


if topicID >= 0 {
let timestamp = Int64(unsignedTimestamp)
let data = msg[3]!

if let topicID = msg[0]! as? Int8 {
if topicID == -1 {
guard let clientTimestamp = data as? Int64 else {
NSLog("Failed to decode clientTimestamp")
return
}
NSLog("Received timestamp: \(clientTimestamp)")
// Handle receive timestamp
wsHandleReceiveTimestamp(serverTimestamp: timestamp, clientTimestamp: clientTimestamp)
}
} else if let topicID = msg[0]! as? UInt16 {
var topic: NTTopic? = nil
// Check to see if the topic ID matches any of the server topics
for serverTopic in serverTopics.values {
Expand All @@ -205,15 +202,11 @@ class NT4Client: WebSocketDelegate {
topic.latestValue = data
topic.latestTimestamp = timestamp
onNewTopicData?(topic, timestamp, data)
} else if topicID == -1 {
guard let clientTimestamp = data as? Int64 else {
NSLog("Failed to decode clientTimestamp")
return
}
NSLog("Received timestamp: \(clientTimestamp)")
// Handle receive timestamp
wsHandleReceiveTimestamp(serverTimestamp: timestamp, clientTimestamp: clientTimestamp)
} else {
NSLog("Failed to decode topicID")
return
}

}

private func wsHandleReceiveTimestamp(serverTimestamp: Int64, clientTimestamp: Int64) {
Expand Down

0 comments on commit 527dedd

Please sign in to comment.