Skip to content

Commit

Permalink
start adding remote joystick control
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-8 committed Sep 27, 2024
1 parent 3d6275a commit abaa907
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
51 changes: 50 additions & 1 deletion gbg_program/_wifi.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// for rpi pico w

#if defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(ESP32)

const unsigned long signalLossTimeout = 1100;

#include <WiFi.h>
#include <WebServer.h>

Expand Down Expand Up @@ -29,13 +30,61 @@ void setupWifi() {
webServer.send(200);
});
webServer.on("/status", []() {
if (webServer.args() == 2 && webServer.argName(0) == "fb" && webServer.argName(1) == "lr") {
lastRemoteCommandMillis = millis();
remoteFB = webServer.arg("fb").toFloat();
remoteFB = webServer.arg("lr").toFloat();
}
webServer.send(200, "text/plain", activatedByRemote ? "on" : "off");
});

webServer.on("/timeoutOn", []() {
deactivateIfRemoteDisconnects = true;
webServer.send(200);
});
webServer.on("/timeoutOff", []() {
deactivateIfRemoteDisconnects = false;
webServer.send(200);
});

webServer.on("/remoteMode", []() {
for (int i = 0; i < webServer.args(); i++) {
Serial.print(webServer.argName(i));
Serial.print(",");
Serial.print(webServer.arg(i));
Serial.println();
webServer.send(200, "text/html", indexHTML);
}
});

webServer.begin();

}

void runWifiInput(float& speedInput, float& turnInput) {
switch (remoteMode) {
default:
case 0: // car is driving
if (deactivateIfRemoteDisconnects && (millis() - lastRemoteCommandMillis > signalLossTimeout)) {
speedInput = 0;
turnInput = 0;
}
break;
case 1:
if (deactivateIfRemoteDisconnects && (millis() - lastRemoteCommandMillis > signalLossTimeout)) {
speedInput = 0;
turnInput = 0;
} else {
speedInput = remoteFB;
turnInput = remoteLR;
}
break;
}
}


void runWifi() {
webServer.handleClient();
}

#endif
36 changes: 35 additions & 1 deletion gbg_program/_wifi_site.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,35 @@
<button onclick='fetch("/deactivate");' style='font-size: 200px; float: right;'>STOP</button>
<br>
<div id="status" style="font-size: 50px;">status: </div>
<br>
<br>
<div>remote override mode</div>
<input type="radio" id="ctl-car" name="remote-mode" value="0" checked
onclick='document.getElementById("timeout").disabled=false;document.getElementById("timeout").checked=timeoutWasChecked;sendTimeout();sendRemoteMode(0);' />
<label for="ctl-car">car has control</label>
<br>
<input type="radio" id="ctl-remote" name="remote-mode" value="1"
onclick='document.getElementById("timeout").disabled=true;timeoutWasChecked=document.getElementById("timeout").checked;document.getElementById("timeout").checked=true;sendTimeout();sendRemoteMode(1);' />
<label for="ctl-remote">remote has control</label>
<br>
<br>
<input type="checkbox" id='timeout' onclick='sendTimeout();' style='font-size: 100px;'>deactivate if signal
lost</button>
<br>


</body>
<script>
var timeoutWasChecked = false;

async function loop() {
let errorTimeout;
let abortcontroller = new AbortController();
errorTimeout = setTimeout(() => {
abortcontroller.abort("aborted because timeout");
}, 400);
try {
var fetched = await fetch('http://10.0.0.1/status', { signal: abortcontroller.signal });
var fetched = await fetch('/status', { signal: abortcontroller.signal });
fetchedResponse = await fetched.text();
document.getElementById("status").innerHTML = "status: " + fetchedResponse;
} catch (e) {
Expand All @@ -29,6 +48,21 @@
clearTimeout(errorTimeout);
}
setInterval(loop, 500);

function sendTimeout() {
if (document.getElementById("timeout").checked) {
console.log("yes");
fetch("/timeoutOn");
} else {
console.log("no");
fetch("/timeoutOff");
}
}

function sendRemoteMode(mode) {
fetch("/remoteMode?mode=" + mode);
}

</script>

</html>
13 changes: 13 additions & 0 deletions gbg_program/gbg_program.ino
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ const int PRINT_VARIABLES_INTERVAL_MILLIS = 100; // or -1 makes it not print va

#if defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(ESP32)
boolean activatedByRemote = true;

boolean deactivateIfRemoteDisconnects = false;
byte remoteMode = 0; //0 is car, 1 is remote
float remoteFB = 0;
float remoteLR = 0;
unsigned long lastRemoteCommandMillis = 0;
#endif

//////////////////////////////////////////////////////// VARIABLES ///////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -338,6 +344,7 @@ void setupPins() {
rightMotorController.writeMicroseconds(RIGHT_MOTOR_CENTER);//tell the motor controller to not move
delay(100);
}

void loop()
{
#ifdef AVR
Expand Down Expand Up @@ -368,6 +375,12 @@ void loop()
InputReader_Buttons(!USE_BUTTON_MODE_PIN || (digitalRead(BUTTON_MODE_PIN) == LOW), true, NUM_DRIVE_BUTTONS, driveButtons, turnInput, speedInput, LOW);
}

#if defined(ARDUINO_RASPBERRY_PI_PICO_W) || defined(ESP32)
if (joyOK) {
runWifiInput(speedInput, turnInput); // references, so the function can edit the values
}
#endif

////////////////////////////// PUT INPUT PROCESSORS HERE ///////////////////////
/**
float InputProcessor_LimitAccelerationFourSettings(float velocity, float velocityTarget, float scale, float ACCELERATION_FORWARD, float DECELERATION_FORWARD, float ACCELERATION_BACKWARD, float DECELERATION_BACKWARD, float timeInterval)
Expand Down

0 comments on commit abaa907

Please sign in to comment.