Skip to content

Instantly share code, notes, and snippets.

@brian-kiplagat
Created July 18, 2019 09:19
Show Gist options
  • Save brian-kiplagat/b932ddbd723af3b7f21658da90e617a7 to your computer and use it in GitHub Desktop.
Save brian-kiplagat/b932ddbd723af3b7f21658da90e617a7 to your computer and use it in GitHub Desktop.
/*This arduino controls orientation...it is always on standby and is only activated by the master arduino
immedietly the rocket starts to free fall.the function of this is to make sure the
rocket is always on the right way up for effective landing.This is also resposible for deploying the parachute
THIS SYSTEM IS ON THE I2C BUS HENCE IS A SLAVE
Depending on the fall acceleration the microcontroller can trigger master to fire to reduce speed
Has a unique adress to identify it from all other devices on the bus
Need to bring altitude sensor to this side
*/
#include <Wire.h>
#include <Servo.h>//include servo library
Servo a, b, c, d; //define four servo objects
//-----SETUP ACCELEROMETER---------------
const int MPU_addr = 0x68;
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
int minVal = 265;
int maxVal = 402;
double x;
double y;
double z;
//-------SETUP BMP--------------
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
unsigned long delayTime;
//------------------------------------------------------------------
const int parachute=12;
void setup() {
//----------------------------------------------------------
// initialize the serial port:
Serial.begin(9600);
//-----------SETUP ACCELEROMETER--------------
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
//------------SETUP SERVOS---------------------
a.attach(3);//attach servos to pwm pins
b.attach(5);
c.attach(6);
d.attach(9);
//------------SETUP PARACHUTE--------------------
pinMode(parachute,OUTPUT);//relay to initiate parachute explosive bolts
//---------------SETUP BMP-------------------------
//
while (!Serial); // time to get serial running
Serial.println(F("BME280 test"));
unsigned status;
// default settings
// (you can also pass in a Wire library object like &Wire2)
status = bme.begin();
/* if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(), 16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
while (1);
}
Serial.println("-- Default Test --");
delayTime = 1000;
Serial.println();
*/
//usefull for debug
//---------------------------------------------------
}
void loop() {
a.write(90);//deploy grid fins..fins deployed by unfolding from the initial position of 0*
b.write(90);//deploy grid fins
c.write(90);//deploy grid fins
d.write(90);//deploy grid fins
digitalWrite(parachute,HIGH);//deploy small stabilising parachute
Serial.print("Pressure = ");//get pressure
Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa");
Serial.print("Approx. Altitude = ");
int height = bme.readAltitude(SEALEVELPRESSURE_HPA);
Serial.print(height);
Serial.println(" m");
//initiate landing from sensor reading
delay(400);
//------------DATA from mpu----------------
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr, 14, true);
AcX = Wire.read() << 8 | Wire.read();
AcY = Wire.read() << 8 | Wire.read();
AcZ = Wire.read() << 8 | Wire.read();
int xAng = map(AcX, minVal, maxVal, -90, 90);
int yAng = map(AcY, minVal, maxVal, -90, 90);
int zAng = map(AcZ, minVal, maxVal, -90, 90);
x = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI);
y = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI);
z = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);
/* Serial.print("AngleX= ");
Serial.println(x);
Serial.print("AngleY= ");
Serial.println(y);
Serial.print("AngleZ= ");
Serial.println(z);
Serial.println("-----------------------------------------");
*/
int xpos1 = map(x, 0, -90, 0, 180); //directly map change in x to servo b and d
int xpos2 = map(x, 0, 90, 0, 180); //directly map change in x to servo b and d
int ypos1 = map(y, 0, -90, 0, 180); //directly map change in x to servo b and d
int ypos2 = map(y, 0, 90, 0, 180); //directly map change in x to servo b and d
c.write(xpos2);//slighly reduce surface area to correct orient
d.write(xpos1);//slighly reduce surface area to correct orient
b.write(ypos2);//slighly reduce surface area to correct orient
a.write(ypos1);//slighly reduce surface area to correct orient
if ((height<=2000)&& (AcY>=8000)){ //if altitude is less than two kilometres && the deceleraation from sensor is too high(falling too fast) the sensor can direct the master to fire rockets
//initiate full rocket fire by triggering master on the same bus
Serial.write('1');//send over number 1 as a char through serial port the other arduino will read this as a command...........much faster than i2c at a baud rate of 2000,000
}
if ((height<=1000)){ //if altitude is less than one kilometres (for backup)the sensor can direct the master to fire rockets
//initiate rocket fire by triggering master on the same bus
Serial.write('2');//send over number 1 as a char through serial port the other arduino will read this as a command
}
if (height<=500){ //if altitude is less than 500 metres the sensor can direct the master to fire rockets and deploy legs by opening a valve
//initiate full rocket fire by triggering master on the same bus
Serial.write('3');//send over number 1 as a char through serial port the other arduino will read this as a command
}
if (height<=1){ //if altitude is less than one metres the sensor can direct the master to shut down rockets
//initiate full rocket fire by triggering master on the same bus
Serial.write('4');//send over number 1 as a char through serial port the other arduino will read this as a command
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment