near space 2019

Dependencies:   mbed SDFileSystem ExtendedTimer

Files at this revision

API Documentation at this revision

Comitter:
mlochridge22
Date:
Thu Mar 21 17:51:23 2019 +0000
Commit message:
finished rough draft

Changed in this revision

BNO055.cpp Show annotated file Show diff for this revision Revisions of this file
BNO055.h Show annotated file Show diff for this revision Revisions of this file
ExtendedTimer.lib Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BNO055.cpp	Thu Mar 21 17:51:23 2019 +0000
@@ -0,0 +1,109 @@
+/* Embedded Systems Final Project: 9-Axis Absolute Orientation imu (BNO055) ndof.cpp 
+*/
+
+#include "BNO055.h"
+#include "mbed.h"
+
+#define REG_ADDR_OPR_MODE   0x3D //adresses for registers which are needed for this code
+#define REG_ADDR_UNIT_SEL   0x3B //address of unit selection
+
+BNO055::BNO055(I2C i2c, int addr) : _i2c(i2c) { // for user friendly sensor-address setup and view 
+    _addr = addr;
+}
+
+char BNO055::checkID(){//will check if compiler is talking to imu
+    char buff[1];
+    buff[0] = 0x00;
+    _i2c.write(_addr, buff, 1);
+    _i2c.read(_addr, buff, 1);
+    return buff[0];
+}
+
+void BNO055::setMode(int modeCode){//sets up the registers
+    char buff[2];
+    char code;
+
+    buff[0] = REG_ADDR_OPR_MODE;
+    _i2c.write(_addr, buff, 1);
+    _i2c.read(_addr, buff, 1);
+    code = buff[0];
+
+    if(modeCode == BNO055_MODE_NDOF){
+        code = code & 0xF0;
+        code = code | 0x0C;
+    }
+    buff[0] = REG_ADDR_OPR_MODE;
+    buff[1] = code;
+    _i2c.write(_addr, buff, 2);
+    wait_ms(7);
+
+}
+
+int BNO055::setAngleUnits(int unitsCode){//will set whether it displays information in degrees or radians
+    _units = unitsCode;
+    char buff[3];
+    char code;
+    buff[0] = REG_ADDR_UNIT_SEL;
+    _i2c.write(_addr, buff, 1);
+    _i2c.read(_addr, buff, 1);
+    code = buff[0];
+
+    if(unitsCode == BNO055_ANGLE_UNITS_DEGREE){//depending on which unit you want to use the right registers and bits will be selected
+        code = code & (0xFF - 0x02 - 0x04);
+    } else if(unitsCode == BNO055_ANGLE_UNITS_RADIAN){
+        code = code & (0xFF - 0x02 - 0x04);
+        code = code | 0x04;
+        code = code | 0x02;
+    }
+    buff[0] = REG_ADDR_UNIT_SEL;
+    buff[1] = code;
+    _i2c.write(_addr, buff, 2);
+    return unitsCode;//returns int for main to be able to manipulate
+}
+
+
+
+float BNO055::readHeading(){//calls register address for heading of imu
+    char buff[2];
+    int16_t rawHeading;
+    buff[0] = 0x1A;
+    _i2c.write(_addr, buff, 1, true);
+    _i2c.read(_addr, buff, 2);
+    rawHeading = (buff[1]<<8) | buff[0];
+    float euler;
+    switch (_units) {//uses class int to determine which case to return - depended on what programmer enters in main
+        case BNO055_ANGLE_UNITS_DEGREE: euler = rawHeading/16.0; break;   //1 deg = 16LSB  from data sheet
+        case BNO055_ANGLE_UNITS_RADIAN: euler = rawHeading/900.0; break;  //1 rad = 900LSB  
+    }
+    return euler;
+    }//rest of the function follow this same structure
+
+float BNO055::readRoll(){//calls register for roll of imu
+    char buff[2];
+    int16_t rawRoll;
+    buff[0] = 0x1C;
+    _i2c.write(_addr, buff, 1, true);
+    _i2c.read(_addr, buff , 2);
+    rawRoll = (buff[1]<< 8) | buff[0];
+    float euler;
+    switch(_units){
+        case BNO055_ANGLE_UNITS_DEGREE: euler = rawRoll/16.0; break;     
+        case BNO055_ANGLE_UNITS_RADIAN: euler = rawRoll/900.0; break;   
+    }
+    return euler;
+}
+
+float BNO055::readPitch(){
+    char buff[2];
+    int16_t rawPitch;
+    buff[0] = 0x1E;
+    _i2c.write(_addr, buff, 1, true);
+    _i2c.read(_addr, buff, 2);
+    rawPitch = (buff[1]<< 8) | buff[0];
+    float euler;
+    switch(_units){
+        case BNO055_ANGLE_UNITS_DEGREE: euler = -rawPitch/16.0; break;     
+        case BNO055_ANGLE_UNITS_RADIAN: euler = -rawPitch/900.0; break;  
+    }
+    return euler;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BNO055.h	Thu Mar 21 17:51:23 2019 +0000
@@ -0,0 +1,30 @@
+/* Embedded Systems Final Project: 9-Axis Absolute Orientation imu (BNO055) ndof.h
+*/ 
+
+#ifndef BNO055_H
+#define BNO055_H
+
+#include "mbed.h"
+
+#define BNO055_MODE_NDOF   0 //used to call the 9 axis orientation of imu
+
+#define BNO055_ANGLE_UNITS_DEGREE  0  
+#define BNO055_ANGLE_UNITS_RADIAN  1
+
+
+class BNO055 {//class for imu
+    public: 
+        BNO055(I2C i2c, int addr);//constructor
+        float readHeading(void);//class methods
+        float readRoll(void);
+        float readPitch(void);
+        char checkID(void); //Check for proper communication with sensor
+        void setMode(int modeCode); //Set program to NDOF forge mode
+        int setAngleUnits(int unitsCode); //Select Euler Angles as units
+    private:
+        I2C _i2c;
+        int _addr; 
+        int _units;
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ExtendedTimer.lib	Thu Mar 21 17:51:23 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/Whitworth-EN173-Resources/code/ExtendedTimer/#7a6067de3bff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Thu Mar 21 17:51:23 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/Whitworth-EN173-Resources/code/SDFileSystem/#ee903cb278a6
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Mar 21 17:51:23 2019 +0000
@@ -0,0 +1,94 @@
+#include "mbed.h"
+#include "BNO055.h"
+#include "ExtendedTimer.h"
+#include "SDFileSystem.h"
+
+Serial pc(USBTX,USBRX);
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+I2C i2c(p28,p27);
+BNO055 imu(i2c, 0x28 << 1);//calls class of imu, moves bytes for address
+
+InterruptIn pulseIn1(p21);
+InterruptIn pulseIn2(p22);
+
+ExtendedTimer t;
+Ticker fileTime;
+Ticker readTime;
+
+SDFileSystem fs(p5, p6, p7, p8, "fs");
+
+int coincCounts;
+bool timeToRead;
+bool timeToSave;
+bool print;
+
+void interrupt() {
+    if (!pulseIn1 && !pulseIn2) {
+        coincCounts++;
+    }
+}
+void triggerCollection() {
+    timeToRead = true;  // flag to collect data
+    led3 = 1;
+}
+void triggerSave() {
+    timeToSave = true;      // flag to save data
+    led4 = 1;
+}
+
+int main() {
+    coincCounts = 0;
+    led1 = 0;
+    led2 = 0;
+    led3 = 0;
+    led4 = 0;
+    bool mountFailure = fs.mount();
+    if (mountFailure != 0) {
+        pc.printf("Failed to mount the SD card.\r\n");
+        led1 = 1;
+        return -1;  // ends program with error status
+    }
+    FILE* fp = fopen("/fs/log.txt","a");
+    if (fp == NULL) {
+        pc.printf("Failed to open the file.\r\n");
+        fs.unmount();
+        led1 = 1;
+        return -1;
+    }
+    fprintf(fp,"------------------\r\n");
+    fprintf(fp,"Time\tFace\tRoll\tPitch\tCoincidence\r\n");
+    fileTime.attach(&triggerSave,300);
+    readTime.attach(&triggerCollection,59);
+    if(imu.checkID() == 0xA0){//calling imu making sure compiler is communicating with it
+        imu.setMode(BNO055_MODE_NDOF);
+        imu.setAngleUnits(BNO055_ANGLE_UNITS_DEGREE);//allows programmer to change between using degrees or radians
+    }
+    pulseIn1.fall(&interrupt);
+    pulseIn2.fall(&interrupt);
+    led2 = 1;
+    t.start();
+    while(t <= 12000){
+        if (timeToRead == true) {
+            fprintf(fp, "%5.3f \t %.2f \t %.2f \t %.2f  \t %d \r\n", t.read()/60, imu.readHeading(), imu.readRoll(), imu.readPitch(), coincCounts); 
+            pc.printf("Reading...\r\n");
+            timeToRead = false;
+            coincCounts = 0;
+            led3 = 0;
+        }
+        if (timeToSave == true) {
+            fclose(fp);
+            fp = fopen("/fs/log.txt","a");
+            pc.printf("Saved!\r\n");
+            timeToSave = false;
+            led4 = 0;
+        }
+    } 
+    fclose(fp);     //close file
+    fs.unmount();   //unmount SD
+    pc.printf("Safe to remove SD");
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Mar 21 17:51:23 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/3a7713b1edbc
\ No newline at end of file