added pit controller firmware, and changed the pit activation to send a command via ESP-Now

This commit is contained in:
2025-04-18 02:36:19 +02:00
parent f53744c23c
commit ee6b87399e
3 changed files with 94 additions and 3 deletions

View File

@@ -1,6 +1,6 @@
void checkPIT() {
if ((digitalRead(!PIT_RELEASE_PIN)) && (millis() - PITopenTimestamp >= PITopenTime)) {
digitalWrite(PIT_RELEASE_PIN, relayOffState);
//digitalWrite(PIT_RELEASE_PIN, relayOffState);
}
}
@@ -8,14 +8,18 @@ void openPIT() {
if (!PITreleased) {
PITreleased = true;
PITopenTimestamp = millis();
digitalWrite(PIT_RELEASE_PIN, relayOnState);
//digitalWrite(PIT_RELEASE_PIN, relayOnState);
sendToPitController.PIT = true;
esp_now_send(broadcastAddressPitController, (uint8_t *) &sendToPitController, sizeof(sendToPitController));
}
}
void openPITmanually() {
PITreleased = true;
PITopenTimestamp = millis();
digitalWrite(PIT_RELEASE_PIN, relayOnState);
//digitalWrite(PIT_RELEASE_PIN, relayOnState);
sendToPitController.PIT = true;
esp_now_send(broadcastAddressPitController, (uint8_t *) &sendToPitController, sizeof(sendToPitController));
}
void arenaLIGHT() {

View File

@@ -255,6 +255,15 @@ void setup() {
return;
}
// Register Pit controller peer
memcpy(peerInfo.peer_addr, broadcastAddressPitController, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add receiver 3");
return;
}
// Initialize both Team button LED states and pit controller state
sendToREDTEAMbutton.TEAMLED = false;
sendToBLUETEAMbutton.TEAMLED = false;

View File

@@ -0,0 +1,78 @@
// Pit controller
#include <WiFi.h>
#include <esp_now.h>
#include <ESP32Servo.h>
// Define the pins for the two servos
#define SERVO1_PIN 9
#define SERVO2_PIN 7
// Define pulse widths (in microseconds)
#define DEFAULT_PULSE 2000 // Default position
#define ACTUATED_PULSE 1000 // Actuated position
// Define the reset time in milliseconds
#define RESET_TIME_MS 500
// Create two Servo objects for the two servos
Servo servo1;
Servo servo2;
// Global variables to manage servo reset timing
volatile bool servoActuated = false; // Tracks if servos are currently actuated
unsigned long actuatedTimestamp = 0; // Stores the time when the servos were actuated
// Updated ESPNow receive callback function with the correct signature
void OnDataRecv(const esp_now_recv_info_t *recv_info, const uint8_t *incomingData, int len) {
// We expect one boolean (1 byte)
bool command = false;
if (len == sizeof(command)) {
memcpy(&command, incomingData, sizeof(command));
if (command) {
// Actuate both servos to the actuated position
servo1.writeMicroseconds(ACTUATED_PULSE);
servo2.writeMicroseconds(ACTUATED_PULSE);
// Record the time when the servos were actuated
actuatedTimestamp = millis();
servoActuated = true;
} else {
// Immediately reset both servos to the default position
servo1.writeMicroseconds(DEFAULT_PULSE);
servo2.writeMicroseconds(DEFAULT_PULSE);
servoActuated = false;
}
}
}
void setup() {
// Initialize WiFi in station mode (required for ESPNow)
WiFi.mode(WIFI_STA);
// Attach the servos to their respective pins and set to default position
servo1.attach(SERVO1_PIN);
servo2.attach(SERVO2_PIN);
servo1.writeMicroseconds(DEFAULT_PULSE);
servo2.writeMicroseconds(DEFAULT_PULSE);
// Initialize ESPNow
if (esp_now_init() != ESP_OK) {
// Optionally handle initialization errors
return;
}
// Register the ESPNow receive callback function
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
// Check if the servos are in the actuated state and if RESET_TIME_MS has elapsed
if (servoActuated && (millis() - actuatedTimestamp >= RESET_TIME_MS)) {
// Reset both servos back to the default position after the delay
servo1.writeMicroseconds(DEFAULT_PULSE);
servo2.writeMicroseconds(DEFAULT_PULSE);
servoActuated = false;
}
// Yield a little time for other tasks
delay(10);
}