joystick roll pitch yaw throttle — ESP32 simulation

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

Components

Sketch code

const int ROLL_PIN     = 35;
const int THROTTLE_PIN = 34;

const int PITCH_PIN = 32;
const int YAW_PIN   = 33;

const int CENTER = 2047;
const int DEADZONE = 300;

void setup() {
  Serial.begin(115200);
}

void loop() {

  // =========================
  // READ ALL CONTROLS
  // =========================

  int rollInput     = analogRead(ROLL_PIN);
  int throttleInput = analogRead(THROTTLE_PIN);

  int pitchInput = analogRead(PITCH_PIN);
  int yawInput   = analogRead(YAW_PIN);


  // =========================
  // THROTTLE
  // =========================

  int throttle = 0;

  if (throttleInput < CENTER - DEADZONE) {

    throttle = map(
      throttleInput,
      CENTER - DEADZONE,
      0,
      0,
      255
    );
  }

  throttle = constrain(throttle, 0, 255);


  // =========================
  // ROLL
  // =========================

  int roll = 0;

  if (rollInput < CENTER - DEADZONE) {

    roll = map(
      rollInput,
      CENTER - DEADZONE,
      0,
      0,
      100
    );

  }
  else if (rollInput > CENTER + DEADZONE) {

    roll = map(
      rollInput,
      CENTER + DEADZONE,
      4095,
      0,
      -100
    );
  }

  roll = constrain(roll, -100, 100);


  // =========================
  // PITCH
  // =========================

  int pitch = 0;

  if (pitchInput < CENTER - DEADZONE) {

    pitch = map(
      pitchInput,
      CENTER - DEADZONE,
      0,
      0,
      100
    );

  }
  else if (pitchInput > CENTER + DEADZONE) {

    pitch = map(
      pitchInput,
      CENTER + DEADZONE,
      4095,
      0,
      -100
    );
  }

  pitch = constrain(pitch, -100, 100);


  // =========================
  // YAW
  // =========================

  int yaw = 0;

  if (yawInput < CENTER - DEADZONE) {

    yaw = map(
      yawInput,
      CENTER - DEADZONE,
      0,
      0,
      100
    );

  }
  else if (yawInput > CENTER + DEADZONE) {

    yaw = map(
      yawInput,
      CENTER + DEADZONE,
      4095,
      0,
      -100
    );
  }

  yaw = constrain(yaw, -100, 100);


  // =========================
  // START WITH THROTTLE
  // =========================

  int motor1 = throttle;
  int motor2 = throttle;
  int motor3 = throttle;
  int motor4 = throttle;


  // =========================
  // ROLL MIXING
  // =========================

  motor1 += roll;
  motor4 += roll;

  motor2 -= roll;
  motor3 -= roll;


  // =========================
  // PITCH MIXING
  // =========================

  motor1 += pitch;
  motor2 += pitch;

  motor3 -= pitch;
  motor4 -= pitch;


  // =========================
  // YAW MIXING
  // =========================

  motor1 += yaw;
  motor3 += yaw;

  motor2 -= yaw;
  motor4 -= yaw;


  // =========================
  // LIMIT MOTOR VALUES
  // =========================

  motor1 = constrain(motor1, 0, 255);
  motor2 = constrain(motor2, 0, 255);
  motor3 = constrain(motor3, 0, 255);
  motor4 = constrain(motor4, 0, 255);


  // =========================
  // DISPLAY
  // =========================

  Serial.p

More projects by josephkinuthia541