119 lines
2.9 KiB
C
119 lines
2.9 KiB
C
//==========================================================================
|
|
// ESP32 SERVO
|
|
//==========================================================================
|
|
|
|
// Servo Library
|
|
#include "ESP32Servo.h"
|
|
|
|
// Servo Pins
|
|
#define SERVO1_PIN 13
|
|
#define SERVO2_PIN 12
|
|
|
|
// Servo Objects
|
|
Servo servo1;
|
|
Servo servo2;
|
|
|
|
// Constants
|
|
#define servo1_min 52
|
|
#define servo1_max 134
|
|
#define servo1_init 97
|
|
|
|
// Constants
|
|
#define servo2_min 52
|
|
#define servo2_max 140
|
|
#define servo2_init 95
|
|
|
|
|
|
//==========================================================================
|
|
// ESPNOW
|
|
//==========================================================================
|
|
|
|
// Libraries
|
|
#include "l298n_esp32.h"
|
|
|
|
// Motor 1 Pins
|
|
#define MOTOR1_PIN1 27
|
|
#define MOTOR1_PIN2 26
|
|
#define MOTOR1_ENABLE 14
|
|
|
|
// Motor 2 Pins
|
|
#define MOTOR2_PIN1 25
|
|
#define MOTOR2_PIN2 33
|
|
#define MOTOR2_ENABLE 32
|
|
|
|
// Motor State
|
|
enum MotorState {
|
|
STOPPED,
|
|
FORWARD_M1,
|
|
BACKWARD_M1,
|
|
FORWARD_M2,
|
|
BACKWARD_M2
|
|
};
|
|
|
|
// Motor Variables
|
|
MotorState currentMotorState = STOPPED;
|
|
unsigned long motorStateStartTime = 0;
|
|
const unsigned long MOTOR_ACTION_DELAY = 5000; // Delay between motor actions
|
|
|
|
// Motor Objects
|
|
L298N_ESP32 motor1(MOTOR1_PIN1, MOTOR1_PIN2, MOTOR1_ENABLE, 0);
|
|
L298N_ESP32 motor2(MOTOR2_PIN1, MOTOR2_PIN2, MOTOR2_ENABLE, 1);
|
|
|
|
//==========================================================================
|
|
// ESPNOW
|
|
//==========================================================================
|
|
|
|
#include <esp_now.h>
|
|
#include <WiFi.h>
|
|
|
|
// Structure example to receive data
|
|
// Must match the sender structure
|
|
typedef struct struct_message {
|
|
int x;
|
|
int y;
|
|
bool b;
|
|
} struct_message;
|
|
|
|
// Create a struct_message called myData
|
|
struct_message myData;
|
|
|
|
// Callback function that will be executed when data is received
|
|
void OnDataRecv(const esp_now_recv_info_t *esp_now_info, const uint8_t *incomingData, int len) {
|
|
|
|
// Copy incoming data into the myData structure
|
|
memcpy(&myData, incomingData, sizeof(myData));
|
|
|
|
// Processing X
|
|
int x = map(myData.x, -100, 100, servo1_min, servo1_max);
|
|
// Serial.print(myData.x);
|
|
servo1.write(x);
|
|
Serial.print(x);
|
|
Serial.print(" ");
|
|
|
|
// Processing Y
|
|
int y = myData.y;
|
|
if(y < 11 && y > -11) {
|
|
y = 0;
|
|
motor1.stop();
|
|
motor2.stop();
|
|
} else if(y >= 11 && y <= 100) {
|
|
y = map(y, 11, 100, 0, 255);
|
|
motor1.backward();
|
|
motor2.backward();
|
|
motor1.setPercentage(abs(y));
|
|
motor2.setPercentage(abs(y));
|
|
} else if(y <= -11 && y >= -100) {
|
|
y = map(y, -11, -100, 0, 255);
|
|
motor1.forward();
|
|
motor2.forward();
|
|
motor1.setPercentage(abs(y));
|
|
motor2.setPercentage(abs(y));
|
|
}
|
|
Serial.print(y);
|
|
Serial.print(" ");
|
|
|
|
// Processing Button
|
|
Serial.println(myData.b);
|
|
|
|
}
|