JOYSTICK ARM AND STOP — ESP32 simulation

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by josephkinuthia541.

Components

Sketch code

#include <Arduino.h>

// ==================================================
// ESP32 #002 — DRONE CONTROLLER
// ==================================================

// =========================
// JOYSTICK PINS
// =========================
const int THROTTLE_PIN = 34;
const int ROLL_PIN     = 35;
const int PITCH_PIN    = 32;
const int YAW_PIN      = 33;


// =========================
// BUTTON PINS
// =========================
const int ARM_BUTTON   = 18;
const int STOP_BUTTON  = 19;
const int RESET_BUTTON = 23;


// =========================
// JOYSTICK SETTINGS
// =========================
const int CENTER   = 2047;
const int DEADZONE = 300;


// =========================
// CONTROL PACKET
// =========================
struct ControlData
{
  int throttle;
  int roll;
  int pitch;
  int yaw;

  bool arm;
  bool stop;
};

ControlData control;


// =========================
// SAFETY STATES
// =========================
bool armed = false;
bool emergencyStop = false;


// ==================================================
// CONVERT JOYSTICK TO -100 ... +100
// ==================================================
int joystickToPercent(int raw)
{
  int value = raw - CENTER;

  // Deadzone around center
  if (abs(value) < DEADZONE)
  {
    return 0;
  }

  int output = map(raw, 0, 4095, -100, 100);

  return constrain(output, -100, 100);
}


// ==================================================
// CONVERT THROTTLE TO 0 ... 255
// ==================================================
int throttleToPWM(int raw)
{
  // Current throttle direction:
  // Raw 0    -> PWM 255
  // Raw 2047 -> PWM 128
  // Raw 4095 -> PWM 0

  int throttle = map(raw, 0, 4095, 255, 0);

  return constrain(throttle, 0, 255);
}


// ==================================================
// SET ALL CONTROL VALUES TO ZERO
// ==================================================
void zeroControls()
{
  control.throttle = 0;
  control.roll     = 0;
  control.pitch    = 0;
  control.yaw      = 0;
}


// ==================================================
// SETUP
// ==================================================
void setup()
{
  Serial.begin(115200);

  // Joysticks
  pinMode(THROTTLE_PIN, INPUT);
  pinMode(ROLL_PIN, INPUT);
  pinMode(PITCH_PIN, INPUT);
  pinMode(YAW_PIN, INPUT);

  // Buttons
  pinMode(ARM_BUTTON, INPUT_PULLUP);
  pinMode(STOP_BUTTON, INPUT_PULLUP);
  pinMode(RESET_BUTTON, INPUT_PULLUP);

  // Initial safety state
  armed = false;
  emergencyStop = false;

  zeroControls();

  control.arm = false;
  control.stop = false;


  Serial.println();
  Serial.println("========================================");
  Serial.println("       ESP32 #002 DRONE CONTROLLER");
  Serial.println("========================================");

  Serial.println();
  Serial.println("ARM   = GPIO 18");
  Serial.println("STOP  = GPIO 19");
  Serial.println("RESET = GPIO 23");

  Serial.println();
  Serial.println("Drone starts DISARMED.");
  Serial.println("Press ARM to enable drone operation.");
  Seria

More projects by josephkinuthia541