@@ -10,12 +10,14 @@ class RootViewController: UIViewController, UIGestureRecognizerDelegate {
10
10
11
11
var hasPlacedField = false
12
12
13
- var NTClient : NT4Client !
13
+ var NTHandler : NetworkTablesHandler !
14
+
15
+ var statusLabel : PaddedLabel !
14
16
15
17
override func loadView( ) {
16
18
super. loadView ( )
17
19
18
- sceneView = ARSceneView ( frame: self . view . frame )
20
+ sceneView = ARSceneView ( frame: UIScreen . main . bounds )
19
21
sceneView. autoenablesDefaultLighting = true
20
22
self . view. addSubview ( sceneView)
21
23
}
@@ -43,51 +45,40 @@ class RootViewController: UIViewController, UIGestureRecognizerDelegate {
43
45
robotNode = robot
44
46
// Create a dummy node so that I can offset the position of the robot
45
47
let dummyNode = SCNNode ( )
46
- dummyNode. position = SCNVector3 ( 0.35 , - 0.35 , - 0.8 )
48
+ dummyNode. position = SCNVector3 ( 0.35 , - 0.35 , - 0.875 )
47
49
dummyNode. addChildNode ( robotNode)
48
50
fieldNode. addChildNode ( dummyNode)
49
-
50
-
51
- let tapGestureRecognizer = UITapGestureRecognizer ( target: self , action: #selector( handleTap) )
52
- tapGestureRecognizer. delegate = self
53
- sceneView. addGestureRecognizer ( tapGestureRecognizer)
54
-
55
- let rotateGestureRecognizer = UIRotationGestureRecognizer ( target: self , action: #selector( handleRotate) )
56
- rotateGestureRecognizer. delegate = self
57
- sceneView. addGestureRecognizer ( rotateGestureRecognizer)
58
-
59
- let pinchGestureRecognizer = UIPinchGestureRecognizer ( target: self , action: #selector( handlePinch) )
60
- pinchGestureRecognizer. delegate = self
61
- sceneView. addGestureRecognizer ( pinchGestureRecognizer)
62
-
63
- let doubleTapGestureRecognizer = UITapGestureRecognizer ( target: self , action: #selector( handleDoubleTap) )
64
- doubleTapGestureRecognizer. delegate = self
65
- doubleTapGestureRecognizer. numberOfTapsRequired = 2
66
- sceneView. addGestureRecognizer ( doubleTapGestureRecognizer)
67
-
68
- NTClient = NT4Client ( appName: " ARKit " , serverBaseAddr: " 192.168.1.130 " , onTopicAnnounce: { topic in
69
- NSLog ( " Announced topic: \( topic. name) " )
70
- } , onTopicUnannounce: { topic in
71
- NSLog ( " Unannounced topic: \( topic. name) " )
72
- } , onNewTopicData: { topic, timestamp, data in
73
- NSLog ( " New data for topic \( topic. name) : \( data) " )
74
- if topic. name == " /SmartDashboard/Field/Robot " {
75
- // [x, y, rot (degrees)]
76
- let newPos = topic. getDoubleArray ( ) ;
77
- // 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
78
- self . robotNode. position = SCNVector3 ( - newPos![ 0 ] + 8.25 , 0 , newPos![ 1 ] - 4 )
79
- self . robotNode. eulerAngles. y = Float ( newPos![ 2 ] * . pi / 180 )
80
- }
81
- } , onConnect: {
82
- NSLog ( " Connected to NetworkTables " )
83
- } , onDisconnect: ( ( String, UInt16) - > Void) ? { reason, code in
84
- NSLog ( " Disconnected from NetworkTables, reason: \( reason) , code: \( code) " )
85
- } )
86
-
87
- NTClient . connect ( )
88
- NTClient . subscribe ( key: " /SmartDashboard/Field/Robot " , periodic: 0.001 )
51
+
52
+ addGestureRecognizers ( )
53
+ statusLabel = PaddedLabel ( )
54
+
55
+ statusLabel. text = " NT: Disconnected "
56
+ statusLabel. font = UIFont . systemFont ( ofSize: 14 )
57
+ statusLabel. textColor = UIColor . white
58
+ statusLabel. backgroundColor = UIColor . red. withAlphaComponent ( 0.4 )
59
+ statusLabel. textAlignment = . center
60
+ statusLabel. layer. cornerRadius = 10
61
+ statusLabel. layer. masksToBounds = true
62
+ statusLabel. sizeToFit ( )
63
+ statusLabel. setContentHuggingPriority ( . defaultHigh, for: . vertical)
64
+ statusLabel. setContentHuggingPriority ( . defaultHigh, for: . horizontal)
65
+
66
+ statusLabel. leftInset = 10
67
+ statusLabel. rightInset = 10
68
+
69
+ statusLabel. translatesAutoresizingMaskIntoConstraints = false
70
+ sceneView. addSubview ( statusLabel)
71
+ NSLayoutConstraint . activate ( [
72
+ statusLabel. centerXAnchor. constraint ( equalTo: view. centerXAnchor) ,
73
+ statusLabel. bottomAnchor. constraint ( equalTo: view. safeAreaLayoutGuide. bottomAnchor) ,
74
+ statusLabel. heightAnchor. constraint ( equalToConstant: 30 )
75
+ ] )
76
+
77
+ NTHandler = NetworkTablesHandler ( robotNode: robotNode, statusLabel: statusLabel)
78
+ NTHandler . connect ( )
89
79
}
90
80
81
+ // Done to allow rotation and pinch gestures to work simultaneously
91
82
func gestureRecognizer( _ gestureRecognizer: UIGestureRecognizer , shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer ) -> Bool {
92
83
return true
93
84
}
@@ -97,15 +88,10 @@ class RootViewController: UIViewController, UIGestureRecognizerDelegate {
97
88
sceneView. session. pause ( )
98
89
}
99
90
91
+ // This is done to make sure the ARSceneView is resized when the device is rotated
100
92
override func viewWillTransition( to size: CGSize , with coordinator: UIViewControllerTransitionCoordinator ) {
101
93
super. viewWillTransition ( to: size, with: coordinator)
102
94
103
95
sceneView. frame = CGRect ( origin: . zero, size: size)
104
96
}
105
-
106
- // When the app is brought to the foreground, resume the WS connection
107
- override func viewWillAppear( _ animated: Bool ) {
108
- super. viewWillAppear ( animated)
109
- NTClient . connect ( )
110
- }
111
97
}
0 commit comments