full control
This commit is contained in:
@@ -1,9 +1,33 @@
|
|||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <IRremote.hpp>
|
||||||
|
|
||||||
#include "credentials.h"
|
#include "credentials.h"
|
||||||
|
|
||||||
|
#define APPLY_PERIOD 100
|
||||||
|
#define COOLDOWN_MIN 29000
|
||||||
|
#define COOLDOWN_MAX 30000
|
||||||
|
|
||||||
WiFiServer server(1337);
|
WiFiServer server(1337);
|
||||||
|
|
||||||
|
enum IRCommands {
|
||||||
|
UP = 0xF20DF20DFE01,
|
||||||
|
DOWN = 0xEE11EE11FE01,
|
||||||
|
PWR = 0xFC03FC03FE01,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum Commands {
|
||||||
|
SPEED = 0x01,
|
||||||
|
POWER = 0x02,
|
||||||
|
};
|
||||||
|
|
||||||
|
byte targetSpeed = 1;
|
||||||
|
byte currentSpeed = 1;
|
||||||
|
bool currentPowerStatus = true;
|
||||||
|
bool targetPowerStatus = true;
|
||||||
|
|
||||||
|
long lastApplied = -APPLY_PERIOD;
|
||||||
|
long lastWakeUp = -COOLDOWN_MAX;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
@@ -27,22 +51,106 @@ void setup() {
|
|||||||
|
|
||||||
server.begin();
|
server.begin();
|
||||||
Serial.println("Server started");
|
Serial.println("Server started");
|
||||||
|
|
||||||
|
IrSender.begin(12);
|
||||||
|
}
|
||||||
|
|
||||||
|
void readControlData(byte* data) {
|
||||||
|
byte command = data[0];
|
||||||
|
|
||||||
|
switch (command) {
|
||||||
|
case Commands::SPEED:
|
||||||
|
targetSpeed = data[1];
|
||||||
|
break;
|
||||||
|
case Commands::POWER:
|
||||||
|
if (data[1] == 0x01) {
|
||||||
|
targetPowerStatus = true;
|
||||||
|
} else {
|
||||||
|
targetPowerStatus = false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void printStatus() {
|
||||||
|
Serial.print("targetSpeed: ");
|
||||||
|
Serial.println(targetSpeed, DEC);
|
||||||
|
Serial.print("targetPowerStatus: ");
|
||||||
|
Serial.println(targetPowerStatus, DEC);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sendIR(IRCommands command) {
|
||||||
|
Serial.print("Sending command: ");
|
||||||
|
Serial.println(command, HEX);
|
||||||
|
|
||||||
|
long currentTime = millis();
|
||||||
|
|
||||||
|
Serial.print("currentTime: ");
|
||||||
|
Serial.println(currentTime);
|
||||||
|
Serial.print("lastWakeUp: ");
|
||||||
|
Serial.println(lastWakeUp);
|
||||||
|
Serial.print("COOLDOWN_MIN: ");
|
||||||
|
Serial.println(COOLDOWN_MIN);
|
||||||
|
if (currentTime > lastWakeUp + COOLDOWN_MIN && command != IRCommands::PWR) {
|
||||||
|
if (currentTime < lastWakeUp + COOLDOWN_MAX) {
|
||||||
|
Serial.print("Applying grace delay ");
|
||||||
|
Serial.println(currentTime - lastWakeUp - COOLDOWN_MIN);
|
||||||
|
delay(currentTime - lastWakeUp - COOLDOWN_MIN);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wake up
|
||||||
|
Serial.print("Waking up after ");
|
||||||
|
Serial.println(currentTime - lastWakeUp);
|
||||||
|
IrSender.sendPulseDistanceWidth(38, 9050, 4450, 600, 1600, 600, 500, IRCommands::UP, 48, PROTOCOL_IS_LSB_FIRST, 0, NO_REPEATS);
|
||||||
|
delay(APPLY_PERIOD);
|
||||||
|
}
|
||||||
|
|
||||||
|
IrSender.sendPulseDistanceWidth(38, 9050, 4450, 600, 1600, 600, 500, command, 48, PROTOCOL_IS_LSB_FIRST, 0, NO_REPEATS);
|
||||||
|
lastWakeUp = millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
void applyCommands() {
|
||||||
|
if (targetPowerStatus != currentPowerStatus) {
|
||||||
|
sendIR(IRCommands::PWR);
|
||||||
|
currentPowerStatus = targetPowerStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (targetSpeed > currentSpeed) {
|
||||||
|
sendIR(IRCommands::UP);
|
||||||
|
currentSpeed++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (targetSpeed < currentSpeed) {
|
||||||
|
sendIR(IRCommands::DOWN);
|
||||||
|
currentSpeed--;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
WiFiClient client = server.available();
|
WiFiClient client = server.accept();
|
||||||
|
|
||||||
if (client) {
|
if (client) {
|
||||||
Serial.println("\n[Client connected]");
|
Serial.println("\n[Client connected]");
|
||||||
while (client.connected()) {
|
while (client.connected()) {
|
||||||
// read line by line what the client (web browser) is requesting
|
byte data[3];
|
||||||
if (client.available()) {
|
if (client.available()) {
|
||||||
char line = client.read();
|
client.readBytesUntil(0xFF, data, 3);
|
||||||
Serial.print(line);
|
readControlData(data);
|
||||||
|
printStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (millis() > lastApplied + APPLY_PERIOD) {
|
||||||
|
applyCommands();
|
||||||
|
lastApplied = millis();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
client.stop();
|
client.stop();
|
||||||
Serial.println("[Client disconnected]");
|
Serial.println("[Client disconnected]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (millis() > lastApplied + APPLY_PERIOD) {
|
||||||
|
applyCommands();
|
||||||
|
lastApplied = millis();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import socket
|
||||||
|
import sys
|
||||||
|
|
||||||
|
commands = {
|
||||||
|
"speed": 0x01,
|
||||||
|
"power": 0x02,
|
||||||
|
}
|
||||||
|
|
||||||
|
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
client.connect(("10.42.50.102", 1337))
|
||||||
|
|
||||||
|
command = sys.argv[1]
|
||||||
|
value = int(sys.argv[2])
|
||||||
|
|
||||||
|
print(hex(value))
|
||||||
|
|
||||||
|
_ = client.send(bytes([commands[command], value, 0xFF]))
|
||||||
|
|
||||||
|
client.close()
|
||||||
+60
-11
@@ -1,17 +1,66 @@
|
|||||||
import socket
|
import socket
|
||||||
import struct
|
import struct
|
||||||
import math
|
import threading
|
||||||
import time
|
import time
|
||||||
|
import signal
|
||||||
|
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
commands = {
|
||||||
sock.bind(("127.0.0.1", 1337))
|
"speed": 0x01,
|
||||||
|
"power": 0x02,
|
||||||
|
}
|
||||||
|
|
||||||
|
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
server.bind(("127.0.0.1", 1337))
|
||||||
|
|
||||||
|
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
client.connect(("10.42.50.102", 1337))
|
||||||
|
|
||||||
|
speed = 0
|
||||||
|
closed = False
|
||||||
|
|
||||||
|
|
||||||
|
def quit(_, __):
|
||||||
|
global closed
|
||||||
|
|
||||||
|
closed = True
|
||||||
|
server.close()
|
||||||
|
client.close()
|
||||||
|
|
||||||
|
|
||||||
|
def map_range(x: float, in_min: int, in_max: int, out_min: int, out_max: int):
|
||||||
|
return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
|
||||||
|
|
||||||
|
|
||||||
|
def receiveForzaTelemetry():
|
||||||
|
global speed
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
data, addr = sock.recvfrom(1024)
|
if closed:
|
||||||
caca = struct.unpack('<iI27f4i20f5i' + 'i19fH6B4b', data)
|
break
|
||||||
# for value in caca:
|
|
||||||
# v = math.floor(value)
|
data, _ = server.recvfrom(1024)
|
||||||
# print(f"{v:08}\t", end='')
|
telemetry = struct.unpack("<iI27f4i20f5i" + "i19fH6B4b", data)
|
||||||
# print()
|
speed = max(round(telemetry[10] * 3.6, 3), 0)
|
||||||
print(round(time.time(), 2), round(caca[10]*3.6, 3))
|
# print(round(time.time(), 2), speed)
|
||||||
# print("received message: %s" % caca[244])
|
|
||||||
|
|
||||||
|
def sendData():
|
||||||
|
while True:
|
||||||
|
if closed:
|
||||||
|
break
|
||||||
|
|
||||||
|
value = min(int(map_range(speed, 0, 250, 1, 12)), 12)
|
||||||
|
print(value)
|
||||||
|
_ = client.send(bytes([commands["speed"], value, 0xFF]))
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
|
receiveThread = threading.Thread(target=receiveForzaTelemetry)
|
||||||
|
sendThread = threading.Thread(target=sendData)
|
||||||
|
|
||||||
|
|
||||||
|
receiveThread.start()
|
||||||
|
sendThread.start()
|
||||||
|
|
||||||
|
|
||||||
|
_ = signal.signal(signal.SIGINT, quit)
|
||||||
|
|||||||
Reference in New Issue
Block a user