drone stabilizer — ESP32 simulation

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

Components

Sketch code

#include <Wire.h>


// =====================================================
// LSM6DS3
// =====================================================

#define LSM6DS3_ADDR 0x6A

#define WHO_AM_I     0x0F
#define CTRL1_XL     0x10
#define CTRL2_G      0x11

#define OUTX_L_G     0x22
#define OUTX_L_XL    0x28


// =====================================================
// JOYSTICK INPUTS
// =====================================================

// First joystick

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


// Second joystick

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


// =====================================================
// MOTOR OUTPUTS
// =====================================================

const int MOTOR1_PIN = 25;
const int MOTOR2_PIN = 26;
const int MOTOR3_PIN = 27;
const int MOTOR4_PIN = 14;


// =====================================================
// JOYSTICK SETTINGS
// =====================================================

const int CENTER = 2047;

const int DEADZONE = 300;


// =====================================================
// ATTITUDE VARIABLES
// =====================================================

float roll = 0.0;

float pitch = 0.0;


// =====================================================
// TIME
// =====================================================

unsigned long previousTime;


// =====================================================
// STABILIZATION GAIN
// =====================================================

// Start small

float rollGain = 2.0;

float pitchGain = 2.0;


// =====================================================
// WRITE REGISTER
// =====================================================

void writeRegister(byte reg, byte value)
{
  Wire.beginTransmission(LSM6DS3_ADDR);

  Wire.write(reg);

  Wire.write(value);

  Wire.endTransmission();
}


// =====================================================
// READ ONE BYTE
// =====================================================

byte readRegister(byte reg)
{
  Wire.beginTransmission(LSM6DS3_ADDR);

  Wire.write(reg);

  Wire.endTransmission(false);

  Wire.requestFrom(LSM6DS3_ADDR, 1);

  return Wire.read();
}


// =====================================================
// READ TWO BYTES
// =====================================================

int16_t read16(byte reg)
{
  Wire.beginTransmission(LSM6DS3_ADDR);

  Wire.write(reg);

  Wire.endTransmission(false);

  Wire.requestFrom(LSM6DS3_ADDR, 2);

  byte low = Wire.read();

  byte high = Wire.read();

  return (int16_t)(high << 8 | low);
}


// =====================================================
// SETUP
// =====================================================

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


  // ===================================================
  // I2C
  // ===================================================

  Wire.begin(21, 22);

  delay(100);


  // ===================================================
  // CHECK IMU
  // ============================================

More projects by josephkinuthia541