Skip to content

Instantly share code, notes, and snippets.

@gleguizamon
Created May 6, 2022 04:38
Show Gist options
  • Save gleguizamon/4817ec085ecbb30a6359a59add6cde01 to your computer and use it in GitHub Desktop.
Save gleguizamon/4817ec085ecbb30a6359a59add6cde01 to your computer and use it in GitHub Desktop.
Code of Flight Rocket Computer
#define ALTITUDE_START
#include <Wire.h>
#include <SoftwareSerial.h>
#include <SPI.h>
// Gyro globals
const int MPU_addr=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
// buzzer globals;
const int PIN_BUZZER = 6;
void setup()
{
Serial.begin(9600);
init_buzzer();
init_gyro();
// beep(frequency, standard_delay); // make beep, mark init completed
Serial.println("Init completed, waiting for rocket start");
}
/* Init functions */
void init_gyro()
{
// Wire.write(0x6B); // PWR_MGMT_1 register
// Wire.write(0x80); --> 0x80 SLEEP
// Wire.write(0); // set to zero (wakes up the MPU-6050)
// Configure Accelerometer Sensitivity - Full Scale Range (default +/- 2g)
// 2g --> 0x00, 4g --> 0x08, 8g --> 0x10, 16g --> 0x18
Wire.beginTransmission(MPU_addr);
Wire.write(0x1C); // Talk to the ACCEL_CONFIG register (1C hex)
Wire.write(0x18);
Wire.endTransmission(true);
Serial.println("Gyro start");
}
void init_buzzer()
{
pinMode(PIN_BUZZER, INPUT);
Serial.println("Buzzer pin set");
}
/* Execution functions */
void init_flight()
{
// rescue_beep();
/* Serial.print(millis());
Serial.print(","); */
Serial.print(get_gyro_data());
Serial.print("\n");
}
String get_gyro_data() {
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
// AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
// AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
// AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
// GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
// GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
String retVal = "90, " + String(AcX) + "°," + String(AcY) + "°," + String(AcZ) + "°," + String(GyX) + "," + String(GyY) + "," + String(GyZ) + ", -90";z
return retVal;
}
void rescue_beep() {
tone(PIN_BUZZER, 1100);
delay(70);
noTone(PIN_BUZZER);
delay(135);
tone(PIN_BUZZER, 1100);
delay(70);
noTone(PIN_BUZZER);
delay(135);
tone(PIN_BUZZER, 1300);
delay(70);
noTone(PIN_BUZZER);
delay(190);
tone(PIN_BUZZER, 1300);
delay(70);
noTone(PIN_BUZZER);
delay(260);
}
void loop() {
init_flight();
// stop_flight();
}
void stop_flight()
{
Serial.println("Landed, saving data and call for recovery");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment