Skip to content

Commit

Permalink
setting up communication with the robot
Browse files Browse the repository at this point in the history
  • Loading branch information
EdzmLeGoat committed Jan 14, 2025
1 parent 239e934 commit 89ef415
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/main/deploy/autoscore/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,15 @@ <h1 class="reefText" id="areaText">Reef Area: None</h1>
<div class="screenChangeContainer" style="display: none">
<h1 id="msgDisplayer"></h1>
</div>
<script src="network.js"></script>
<script src="ntsetup.js"></script>
<script src="robotCommunication.js"></script>
<script src="menuControl.js"></script>
<script src="processor.js"></script>
<script src="net.js"></script>
<script src="reefPlacement.js"></script>
<script src="coralLevel.js"></script>
<script src="coralIntake.js"></script>
<script src="robotCommunicationBackend.js"></script>

</body>
</html>
17 changes: 13 additions & 4 deletions src/main/deploy/autoscore/menuControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ let reefArea = -1;
let coralSelected = false;
let areaSelected = false;

const menuChoices = {
PROCESSOR: "processor",
REEF: "reef",
SCREEN: "screen",
CHOICE: "choice",
NET: "net",
CORAL_INTAKE: "coral intake"
}

const changeMenu = (menuVal, screenmsg) => {
menu = menuVal;
reefContainer.style.display = "none";
Expand All @@ -32,7 +41,7 @@ const changeMenu = (menuVal, screenmsg) => {
locationContainer.style.display = "";
confirmReefContainer.style.display = "";
confirmReefButton.innerText = "Choose Robot Alignment";
} else if (menu == "screen change") {
} else if (menu == "screen") {
screenChangeContainer.style.display = "";
document.getElementById("msgDisplayer").innerText = screenmsg;
} else if (menu == "choice") {
Expand All @@ -44,8 +53,6 @@ const changeMenu = (menuVal, screenmsg) => {
}
}

changeMenu("choice");

const reefChoice = document.getElementById("reefChoice");
const processorChoice = document.getElementById("processorChoice");
const netChoice = document.getElementById("netChoice");
Expand Down Expand Up @@ -89,4 +96,6 @@ confirmReefButton.onclick = async () => {
reefScoreButtonClickable = true;
resetReefStuff();
}
}
}

changeMenu("none");
Original file line number Diff line number Diff line change
Expand Up @@ -1164,9 +1164,9 @@ class NT4_Client {

ws_connect() {

this.clientIdx = Math.floor(Math.random() * 99999999); //Not great, but using it for now
this.clientIdx = 449; //Not great, but using it for now

var port = 5810; //fallback - unsecured
var port = 8080;
var prefix = "ws://";

this.serverAddr = prefix + this.serverBaseAddr + ":" + port.toString() + "/nt/" + "JSClient_" + this.clientIdx.toString();
Expand Down
13 changes: 1 addition & 12 deletions src/main/deploy/autoscore/robotCommunication.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,4 @@ const scoreReef = async (location, level) => {
console.log(`Scoring on level: ${level} and location: ${location}`);
//communicate with network tables somehow
await new Promise((resolve) => setTimeout(() => resolve(), 1000)); // Simulate asynchronous movement
}

const getAlliance = async () => {
let temporaryAlliance = "Red";
if(temporaryAlliance == "Red") {
document.body.style.background = "radial-gradient(circle at 50% 50%, pink, rgb(114, 114, 138))";
} else if(temporaryAlliance == "Blue") {
document.body.style.background = "radial-gradient(circle at 50% 50%, cornflowerblue, rgb(114, 114, 138))";
}
}

getAlliance();
}
101 changes: 101 additions & 0 deletions src/main/deploy/autoscore/robotCommunicationBackend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//Instantiate the new client
// using `window.location.hostname` causes the client to open a
// NT connection on the same machine as is serving the website.
// It could be hardcoded to point at a roboRIO if needed.
var nt4Client = new NT4_Client(
window.location.hostname,
topicAnnounceHandler,
topicUnannounceHandler,
valueUpdateHandler,
onConnect,
onDisconnect
);
//nt4client IMPORTANT methods
/*
subscribeTopicNames : list of topics name to subscribe to
subscribePeriodic : list of topic names, data rate in seconds
addSample : add a sample value to a topic
publishTopic : name of topic, type
unSubscribe
clearAllSubscriptions
*/


// Allocate a variable to hold the subscription to all topics
var subscription = null;

/**
* Topic Announce Handler
* The NT4_Client will call this function whenever the server announces a new topic.
* It's the user's job to react to the new topic in some useful way.
* @param {NT4_Topic} newTopic The new topic the server just announced.
*/
function topicAnnounceHandler( newTopic ) {

//For this example, when a new topic is announced,
console.log("new topic id: " + newTopic.id.toString());
console.log("new topic name: " + newTopic.name.toString());
console.log("new topic type: " + newTopic.type.toString());
console.log("new topic properties: " + newTopic.getPropertiesString());

}

/**
* Topic UnAnnounce Handler
* The NT4_Client will call this function whenever the server un-announces a topic.
* It's the user's job to react to the un-anncouncement in some useful way.
* @param {NT4_Topic} removedTopic The topic the server just un-announced.
*/
function topicUnannounceHandler( removedTopic ) {
//For this example, when a topic is unannounced, we remove its row.
console.log("removed topic name: " + removedTopic.name.toString());
}

/**
* Value Update Handler
* The NT4_Client will call this function whenever the server sends a value update
* for a topic.
* @param {NT4_Topic} topic The topic with a value update
* @param {double} timestamp_us The server time of the value update occurring
* @param {*} value the new value for the topic
*/
function valueUpdateHandler( topic, timestamp_us, value ) {
console.log(topicAnnounceHandler(topic));
console.log(timestamp_us);
console.log(value);
}

/**
* On Connection Handler
* The NT4_Client will call this after every time it successfully connects to an NT4 server.
*/
function onConnect() {

console.log("robot connected")

}

/**
* On Disconnection Handler
* The NT4_Client will call this after every time it disconnects to an NT4 server.
*/
function onDisconnect() {
changeMenu(menuChoices.SCREEN, "NO ROBOT CONNECTION");
//For this example, we simply mark the status as disconnected.
console.log("robot disconnected")

//throwing an error to stop the reconnect cycle because we have nothing planned currently
throw new Error("Robot disconnected");
//Since we've disconnected from the server, the connection is no longer valid.
subscription = null;
}

const getAlliance = () => {
let temporaryAlliance = "Red";
if(temporaryAlliance == "Red") {
document.body.style.background = "radial-gradient(circle at 50% 50%, pink, rgb(114, 114, 138))";
} else if(temporaryAlliance == "Blue") {
document.body.style.background = "radial-gradient(circle at 50% 50%, cornflowerblue, rgb(114, 114, 138))";
}
}

8 changes: 7 additions & 1 deletion src/main/deploy/autoscore/style.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
body {
background-attachment: fixed;
background: rgb(114, 114, 138);
}

.reefContainer, .processorContainer, .locationContainer, .screenChangeContainer, .netContainer, .coralIntakeContainer, .choiceContainer {
Expand Down Expand Up @@ -69,11 +70,16 @@ h1 {
color: white;
}

#processorChoice, #reefChoice, #netChoice, #coralIntakeChoice {
#processorChoice, #reefChoice, #netChoice, #coralIntakeChoice, #msgDisplayer {
align-self: center;
justify-self: center;
}

#msgDisplayer {
color: red;
font-size: 48px;
}

.reefText {
align-self: center;
justify-self: center;
Expand Down

0 comments on commit 89ef415

Please sign in to comment.