Guides the user to their classes

Dependencies:   4DGL-uLCD-SE Course SDFileSystem mbed PinDetect LSM9DS1_Library_cal MBed_Adafruit-GPS-Library

main.cpp

Committer:
kkizirian
Date:
2016-12-08
Revision:
4:93a4b415fe6c
Parent:
3:0ca91f8afec5
Child:
5:430f44669f94

File content as of revision 4:93a4b415fe6c:

// Class Scheduler
#include "mbed.h"
#include "uLCD_4DGL.h"
#include <string>
#include <vector>
#include "Course.h"
#include "SDFileSystem.h"
#include "LSM9DS1.h"
#include "PinDetect.h"
#define PI 3.14159
#define DECLINATION -4.94 // Declination (degrees) in Atlanta,GA.

SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
Serial pc(USBTX, USBRX);
uLCD_4DGL uLCD(p13,p14,p15); // serial tx, serial rx, reset pin;
PinDetect left(p20);
PinDetect right(p18);

int update;
vector<Course> courseVec;
void readClassFile(vector<Course>& cVec);
void displayCourseVec();
float calculateHeading(float mx, float my), computeAngleToDestination(float diffLat, float diffLong);
int xEnd, yEnd = 0.0;
bool screen_refreshed = false;
int volatile current_screen = 0;
bool volatile screen_change = false;

void left_callback(void)
{
    if (courseVec.size() != 0) {
        current_screen = (3 + current_screen - 1)%3;
        screen_change = true;
    } else
        current_screen = 0;
}

void right_callback(void)
{
    if (courseVec.size() != 0) {
        current_screen = (3 + current_screen + 1)%3;
        screen_change = true;
    } else
        current_screen = 0;
}

int main()
{
    LSM9DS1 IMU(p9, p10, 0xD6, 0x3C);
    float h = 0.0;
    float angleToDest = 0.0;
    IMU.begin();
    if (!IMU.begin()) {
        pc.printf("Failed to communicate with LSM9DS1.\n");
    }
    uLCD.cls();
    uLCD.printf("Place IMU flat");
    IMU.calibrate(1);
    uLCD.cls();
    uLCD.printf("Rotate IMU 360\ndegrees in \nhorizontal plane");
    IMU.calibrateMag(0);

    left.mode(PullUp);
    right.mode(PullUp);
    left.attach_deasserted(&left_callback);
    right.attach_deasserted(&right_callback);
    left.setSampleFrequency();
    right.setSampleFrequency();

    readClassFile(courseVec);
    
    Course tempCourse("CLH", 7, 0, "AM");
    courseVec.insert(courseVec.begin(), tempCourse);
    
    float myLat = 33.775991;
    float myLong = -84.397128;

    float destinationLat = courseVec[0].getLat();
    float destinationLong = courseVec[0].getLong();
    
    screen_change = true;
    pc.baud(9600);

    while(1) {
        switch(current_screen) {
            case 0:
                wait(.2);
                if (screen_change) {
                    displayCourseVec();
                    screen_change = false;
                }
                break;
            case 1:
                wait(.1);
                if (screen_change) {
                    uLCD.printf("Compass screen");
                    screen_change = false;
                }

                IMU.readMag();
                h = calculateHeading(IMU.calcMag(IMU.mx), IMU.calcMag(IMU.my));
                pc.printf("Heading: %f\n", h);
                destinationLat = courseVec[0].getLat();
                destinationLong = courseVec[0].getLong();
                float diffLat = destinationLat - myLat;
                float diffLong = destinationLong - myLong;
                angleToDest = computeAngleToDestination(diffLat, diffLong);
                h = angleToDest - h;
                h = h - 90;
                if (h < 0)
                    h = h + 360;
                if (h > 360)
                    h = h - 360;
                xEnd = 0;
                yEnd = 0;
                float rads = 0.0;
                //uLCD.cls();
                if (h < 90) {
                    rads = h * PI / 180;
                    xEnd = floor(63 * sin(rads) + .5);
                    yEnd = floor(63 * cos(rads) + .5);
                    xEnd = 63 + xEnd;
                    yEnd = 63 + yEnd;
                } else if (90 < h < 180) {
                    h = h - 90;
                    rads = h * PI / 180;
                    xEnd = floor(63 * cos(rads) + .5);
                    yEnd = floor(63 * sin(rads) + .5);
                    xEnd = 63 + xEnd;
                    yEnd = 63 - yEnd;
                } else if (180 < h < 270) {
                    h = h - 180;
                    rads = h * PI / 180;
                    xEnd = floor(63 * sin(rads) + .5);
                    yEnd = floor(63 * cos(rads) + .5);
                    xEnd = 63 - xEnd;
                    yEnd = 63 - yEnd;
                } else if (270 < h) {
                    h = h - 270;
                    rads = h * PI / 180;
                    xEnd = floor(63 * cos(rads) + .5);
                    yEnd = floor(63 * sin(rads) + .5);
                    xEnd = 63 - xEnd;
                    yEnd = 63 + yEnd;
                }
                uLCD.cls();
                uLCD.line(63, 63, xEnd, yEnd, WHITE);
                wait(.1);
                break;
            case 2:
                wait(.1);
                if (screen_change) {
                    uLCD.cls();
                    uLCD.printf("Distance screen");
                    screen_change = false;
                }
                break;
        }
    }
}

void readClassFile(vector<Course>& cVec)
{
    cVec.clear();

    FILE *readFp = fopen("/sd/classdir/classes.txt", "r");
    char line[15];
    char buildingBuf[4];
    char hourBuf[3];
    int hour;
    char minuteBuf[3];
    int minute;
    char ampmBuf[3];
    uLCD.cls();
    uLCD.locate(0, 1);
    uLCD.printf("Reading class file...");

    memset(buildingBuf, 0, sizeof(buildingBuf));
    memset(hourBuf, 0, sizeof(hourBuf));
    memset(minuteBuf, 0, sizeof(minuteBuf));
    memset(ampmBuf, 0, sizeof(ampmBuf));
    memset(line, 0, sizeof(line));

    if (readFp == NULL)
        return;
    else {
        while (!feof(readFp)) {
            fgets(line, 15, readFp);
            if(line[8] == NULL)
                continue;
            memcpy(buildingBuf, line, 3);
            memcpy(hourBuf, &line[4], 2);
            memcpy(minuteBuf, &line[7], 2);
            memcpy(ampmBuf, &line[10], 2);

            string building = buildingBuf;
            hour = atoi(hourBuf);
            minute = atoi(minuteBuf);
            string ampm = ampmBuf;

            Course temp(building, hour, minute, ampm);
            cVec.push_back(temp);
        }
    }
    fclose(readFp);
    return;
}

void displayCourseVec()
{
    if (courseVec.size() == 0) {
        uLCD.cls();
        uLCD.locate(0,0);
        uLCD.printf("No classes input!");
        uLCD.locate(0,1);
    } else {
        uLCD.cls();
        uLCD.locate(0,1);
        for (int i = 0; i < courseVec.size(); i++) {
            uLCD.locate(0, i);
            uLCD.printf("%s", courseVec[i].getDisplayString());
        }
    }
}

float calculateHeading(float mx, float my)
{
    float heading = 0.0;
    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;

    //pc.printf("Magnetic Heading: %f degress\n\r",heading);
    return heading;
}

float computeAngleToDestination(float diffLat, float diffLong)
{
    float angle = 0.0;

    if (diffLat > 0) {
        if (diffLong > 0) {
            // in quadrant 1
            angle = 180*atan2(diffLat,diffLong)/PI;
        } else {
            // in quadrant 2
            angle = 180*atan2(diffLat,-1 * diffLong)/PI;
            angle = 180 - angle;
        }
    } else {
        if (diffLong > 0) {
            // in quadrant 4
            angle = 180*atan2(-1*diffLat, diffLong)/PI;
            angle = 360 - angle;
        } else {
            // in quadrant 3
            angle = 180*atan2(-1*diffLat, -1*diffLong)/PI;
            angle = 180 + angle;
        }
    }

    //pc.printf("Angle to Destination: %f degress\n\r",angle);
    return angle;
}