Source code for Active Aerodynamics and Drag Reduction System
Dependencies: mbed Servo mbed-rtos LSM9DS1_Library_cal MPL3115A2
main.cpp
- Committer:
- abir77935
- Date:
- 2020-04-21
- Revision:
- 1:8e8aac99a366
- Parent:
- 0:04fef978a0ab
- Child:
- 2:426f26e9801d
File content as of revision 1:8e8aac99a366:
#include "mbed.h" #include "rtos.h" #include "Servo.h" #include "LSM9DS1.h" #define PI 3.14159 // Used in IMU code #define DECLINATION -4.94 // Declination (degrees) in Atlanta,GA (Used in IMU code) // OBJECTS Servo servoFrontLeft(p21); Servo servoFrontRight(p22); Servo servoBackLeft(p23); Servo servoBackRight(p24); LSM9DS1 IMU(p9, p10, 0xD6, 0x3C); Serial blue(p28,p27); // bluetooth //BusOut myled(LED1,LED2,LED3,LED4); //bluetooth debugging Thread thread1; Serial pc(USBTX, USBRX); // Debugging // VARIABLE DECLARATIONS volatile char bluetoothRead = 0; volatile float xAccel = 0; volatile float yAccel = 0; volatile float zAccel = 0; volatile float tuning = 1; // FUNCTION DECLARATIONS void bluetooth_recv() { bluetoothRead = blue.getc(); /*if (blue.getc()=='!') { if (blue.getc()=='B') { //button data bluetoothRead = blue.getc(); //button number if ((bluetoothRead>='1')&&(bluetoothRead<='4')) //is a number button 1..4 myled[bluetoothRead-'1']=blue.getc()-'0'; //turn on/off that num LED } } */ } // IMU - caluclate pitch and roll void printAttitude(float ax, float ay, float az, float mx, float my, float mz) { float roll = atan2(ay, az); float pitch = atan2(-ax, sqrt(ay * ay + az * az)); // touchy trig stuff to use arctan to get compass heading (scale is 0..360) mx = -mx; float heading; if (my == 0.0) heading = (mx < 0.0) ? 180.0 : 0.0; else heading = atan2(mx, my)*360.0/(2.0*PI); //pc.printf("heading atan=%f \n\r",heading); heading -= DECLINATION; //correct for geo location if(heading>180.0) heading = heading - 360.0; else if(heading<-180.0) heading = 360.0 + heading; else if(heading<0.0) heading = 360.0 + heading; // Convert everything from radians to degrees: //heading *= 180.0 / PI; pitch *= 180.0 / PI; roll *= 180.0 / PI; pc.printf("Pitch: %f, Roll: %f degress\n\r",pitch,roll); pc.printf("Magnetic Heading: %f degress\n\r",heading); } // IMU - read and display magnetometer, gyroscope, acceleration values void getIMUData() { while(!IMU.magAvailable(X_AXIS)); IMU.readMag(); while(!IMU.accelAvailable()); IMU.readAccel(); while(!IMU.gyroAvailable()); IMU.readGyro(); pc.printf(" X axis Y axis Z axis\n\r"); pc.printf("gyro: %9f %9f %9f in deg/s\n\r", IMU.calcGyro(IMU.gx), IMU.calcGyro(IMU.gy), IMU.calcGyro(IMU.gz)); pc.printf("accel: %9f %9f %9f in Gs\n\r", IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az)); pc.printf("mag: %9f %9f %9f in gauss\n\r", IMU.calcMag(IMU.mx), IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz)); printAttitude(IMU.calcAccel(IMU.ax), IMU.calcAccel(IMU.ay), IMU.calcAccel(IMU.az), IMU.calcMag(IMU.mx), IMU.calcMag(IMU.my), IMU.calcMag(IMU.mz)); zAccel = IMU.calcAccel(IMU.az));//setting global variable for storage of z-Axis acceleration yAccel = IMU.calcAccel(IMU.ay)); xAccel = IMU.calcAccel(IMU.ax)); } // THREADS void thread1Name() { while (true) { // things Thread::wait(100); } } //Prints the acceraltion data to the bluetooth UART window void blue_rec() { while(1){ dev.putc(zAccel); Thread::wait(1000); } } /* //take in data from UART window on bluetooth to adjust tuning variables void blue_tuning() { while(dev.readable()) { tuning = dev.getc(); } } */ int main() { // initialise IMU IMU.begin(); if (!IMU.begin()) { pc.printf("Failed to communicate with LSM9DS1.\n"); } IMU.calibrate(1); IMU.calibrateMag(0); // Start threads thread1.start(thread1Name); blue.baud(9600); //set baud rate for UART window blue.attach(&blue_rec, Serial::RxIrq); while(1) { // things Thread::wait(1000); } }