-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
107 lines (94 loc) · 4.34 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
NetworkTables.addWsConnectionListener(function (connected) {
console.log("Websocket connected: " + connected);
}, true);
NetworkTables.addRobotConnectionListener(function (connected) {
console.log("Robot connected: " + connected);
}, true);
setInterval(() => {
document.getElementById("drivetrain_command").innerHTML = NetworkTables.getValue("/SmartDashboard/drivetrain-command");
document.getElementById("drivetrain_rotation").innerHTML = NetworkTables.getValue("/SmartDashboard/drivetrain-rotation");
document.getElementById("drivetrain_pose").innerHTML = NetworkTables.getValue("/SmartDashboard/drivetrain-pose");
document.getElementById("game_time").innerHTML = NetworkTables.getValue("/SmartDashboard/game-time");
document.getElementById("game_alliance").innerHTML = NetworkTables.getValue("/SmartDashboard/game-alliance");
document.getElementById("game_period").innerHTML = NetworkTables.getValue("/SmartDashboard/game-period");
if (NetworkTables.getValue("/SmartDashboard/game-period") == "AUTO")
drawPath();
else {
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
});
setInterval(() => {
bruh = NetworkTables.getValue("/SmartDashboard/drivetrain-pose");
if (bruh == undefined) {
x = map(3, 0, 16.46, 78, 775);
y = map(3, 0, 8.23, 480, 137);
document.getElementById("swerve").style.left = x + "px";
document.getElementById("swerve").style.top = y + "px";
document.getElementById("swerve").style.transform = "rotate(" + 90 + "deg)"
return;
}
x = Number.parseFloat(bruh.slice(0, bruh.indexOf(',')));
y = Number.parseFloat(bruh.slice(bruh.indexOf(',') + 2, bruh.length));
x = map(x, 0, 16.46, 125, 750);
y = map(y, 0, 8.23, 461, 158);
document.getElementById("swerve").style.left = x + "px";
document.getElementById("swerve").style.top = y + "px";
drivetrain_rotation = NetworkTables.getValue("/SmartDashboard/drivetrain-rotation");
if (drivetrain_rotation == undefined) return;
rotation = -Number.parseFloat(drivetrain_rotation.slice(0, drivetrain_rotation.indexOf(" "))) + 90;
document.getElementById("swerve").style.transform = "rotate(" + rotation + "deg)"
});
function drawPath() {
var strings = NetworkTables.getValue("/SmartDashboard/auto-path");
if (strings == undefined) return;
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.moveTo(mapX(Number.parseFloat(strings[0].split(" ")[0])), Number.parseFloat(mapY(strings[0].split(" ")[1])));
for (i = 1; i < strings.length; i++) {
var values = strings[i].split(" ");
ctx.bezierCurveTo(mapX(Number.parseFloat(values[0])), mapY(Number.parseFloat(values[1])), mapX(Number.parseFloat(values[2])), mapY(Number.parseFloat(values[3])), mapX(Number.parseFloat(values[4])), mapY(Number.parseFloat(values[5])));
}
var grad = ctx.createLinearGradient(mapX(Number.parseFloat(strings[0].split(" ")[0])), mapY(Number.parseFloat(strings[0].split(" ")[1])), mapX(Number.parseFloat(strings[strings.length - 1].split(" ")[0])), mapY(Number.parseFloat(strings[strings.length - 1].split(" ")[1])));
grad.addColorStop(0, "#ffadad");
grad.addColorStop(0.25, "#ffd6a5");
grad.addColorStop(0.5, "#caffbf");
grad.addColorStop(0.75, "#9bf6ff");
grad.addColorStop(1.0, "#ffc6ff");
ctx.strokeStyle = grad;
ctx.lineWidth = 3;
ctx.stroke();
}
function mapX(x) {
return map(x, 0, 16.46, 78, 775);
}
function mapY(y) {
return map(y, 0, 8.23, 480, 137);
}
function map(value, minimumInput, maximumInput, minimumOutput, maximumOutput) {
return (value - minimumInput) * (maximumOutput - minimumOutput) / (maximumInput - minimumInput) + minimumOutput;
}
loadCameraOnConnect({
container: '#camera',
port: 1181,
image_url: '/?action=stream',
data_url: '/config.json',
// data_url: '/settings.json',
attrs: {
width: 400,
height: 350
}
});
loadCameraOnConnect({
container: '#camera2',
host: 'photonvision.local',
port: 1183,
image_url: '/stream.mjpg?1643123413248',
data_url: '/config.json',
attrs: {
width: 400,
height: 350
}
});