Updated OCE 360 Assignment 4
Dependencies: 4DGL-uLCD-SE MMA8452Q mbed physics_ball
Diff: main.cpp
- Revision:
- 2:8085bd89612b
- Parent:
- 0:2067ba6c5e74
- Child:
- 4:241bf0f0e626
--- a/main.cpp Tue Oct 31 14:54:06 2017 +0000 +++ b/main.cpp Fri Nov 17 04:10:17 2017 +0000 @@ -1,31 +1,150 @@ //Kaitlyn Barros //OCE 360 Fall 2017 -//Last Revised: Oct 30 2017 +//Last Revised: Nov 16 2017 -//Software Exercise: +//Software Exercise 4: //1. Update the software system by creating a new library, which includes // a class that defines the behavior of a bouncing ball on the LCD screen. //2. Demonstrate the system with two or more bouncing balls on the screen. //3. Show that the system resets the ball locations when you hold the // accelerometer upside down. -#include "mbed.h" -#include "physics_ball.h" -#include "uLCD_4DGL.h" -#include "MMA8452Q.h" +//Software Exercise 5: +//1. Update the ball postion using the accelerometer's data at 50Hz. Update the LCD display w/ ball postions at 11HZ. +//2. Use a buttom interrupt to turn data recording on/off. Use an onboard LED to indicate recording. + +#include "mbed.h" //Mbed Header +#include "physics_ball.h" // Physics Ball Behavior Header +#include "uLCD_4DGL.h" // LCD Header +#include "MMA8452Q.h" // Accelerometer Header +#include "SDFileSystem.h" // SD Card Header + +AnalogIn ain(p15); // Analog Input (pin 15) For Temp Sensor -#define DEFAULT 64 -#define STEP 0.1 +Serial pc(USBTX, USBRX); // USB Serial (tx, rx) + +SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD card (SPI pins) + +DigitalOut led1(LED1); //Onboard LED 1 +DigitalOut led2(LED2); //Onboard LED 2 + +DigitalIn switchinput(p12); //Digital switch set in Pin 12 + +// Call out classes (For Ball 1 & 2) +physics_ball ball1; +physics_ball ball2; + // Graphic LCD - TX, RX, and RES pins uLCD_4DGL uLCD(p9,p10,p11); // Accelerometer - SDA, SCL, and I2C address MMA8452Q accel(p28, p27, 0x1D); + +// Call Out Variables +int status = 0; //"Logging" Status, i.e. 1 = Logging 0 - Not Logging +float voltage_in; //Temp Sensor +float degrees_c; //Temp Sensor +int i; //Temp Sensor +int c; //Temp Sensor + +// Timestamp Timer +Timer timer; + +// Call out Tickers +Ticker BallUpdate; +Ticker LCDUpdate; +Ticker Record; + +//Ticker Function 1: Ball_Update_Pos +void BUP(){ + //Update Ball Postion + ball1.update(accel); + ball2.update(accel); + } + +//Ticker Function 2: LCD_Update_Pos +void LCDUP(){ + // Draw new circle + uLCD.filled_circle(ball1.posx, ball1.posy, ball1.radius, BLUE); + uLCD.filled_circle(ball2.posx, ball2.posy, ball2.radius, RED); + led1 = 1; + // Erase old circle (For Ball 1 & 2) + uLCD.filled_circle(ball1.old_posx, ball1.old_posy, ball1.radius, BLACK); + uLCD.filled_circle(ball2.old_posx, ball2.old_posy, ball2.radius, BLACK); + led1= 0; + + } + +// Ticker Function 3: Record +void REC (){ + +//If the switch is pressed and the logging status is not logging, set the status to logging and turn the indicating LED on + if ((switchinput == 1) && (status == 0)) { + status = 1; + led2 = 1; + } + +//If the switch is pressed and the logging status is logging, set the status to not logging and turn the indicating LED off + if ((switchinput == 1) && (status == 1)) { + status = 0; + led2 = 0; + } + + if (status == 1){ + FILE *file; //file descriptor + + // Start our timer + timer.start(); + + // Open file for writing, if there is not a file with such a name, create one + file = fopen("/sd/BallData.txt", "w"); + if ( file == NULL ) { + error("ERROR: Could not open file for writing!\n\r"); + } + + // Tell the user we need to wait while we collect some data + pc.printf("\nCollecting data (Do not remove SD Card!) ...\n\r"); + + // Collect Temperature + voltage_in = ain * 3.3; + degrees_c = (voltage_in - 0.5) * 100.0; + fprintf(file, "%2.2fs: %3.1f deg C\n\r", timer.read(), degrees_c); + + // Collect Ball Data + + + if (status == 0){ + // Close file and re-open it for reading + fclose(file); + file = fopen("/sd/BallData.txt", "r"); + if ( file == NULL ) { + error("ERROR: Could not open file for reading!\n\r"); + } + + // Print results to console + pc.printf("Temperature data:\n\r"); + while(1) { + c = fgetc(file); + if ( c == EOF ) { + break; + } + pc.putc(c); + } + + // Close the file and finish + fclose(file); + pc.printf("Done! Safe to remove SD card\n\r"); + } + } + } + int main() { - + // pc.printf("I Print"); //Test: Make sure I can print to PC + // led1 = 1; //Test: Make sure led can turn on + // Initialize uLCD uLCD.baudrate(115200); uLCD.background_color(BLACK); @@ -33,53 +152,18 @@ // Initialize accelerometer accel.init(); - - // Call out classes (For Ball 1 & 2) - physics_ball ball1; - physics_ball ball2; - - //Intial Ball Conditions - ball1.posx = DEFAULT; - ball1.posy = DEFAULT; - - ball2.posx = DEFAULT /2; - ball2.posy = DEFAULT /2; - + + //Intial Ball Conditions & Reset Conditiosn ball1.define_space(ball1.width, ball1.height); ball2.define_space(ball2.width, ball2.height); ball1.set_param(ball1.radius, BLUE); - ball2.set_param(ball2.radius, RED); + ball2.set_param(ball2.radius, RED); - // Make a ball "fall" in direction of accelerometer - while (1) { - - // Draw a circle (For Ball 1 & 2) - uLCD.filled_circle(ball1.posx, ball1.posy, ball1.radius, BLUE); - uLCD.filled_circle(ball2.posx, ball2.posy, ball2.radius, RED); + //Tickers + Record.attach(&REC, 0.1); + BallUpdate.attach(&BUP, 0.02); //50 Hz + LCDUpdate.attach(&LCDUP, 0.0909);//11 Hz - // Wait before erasing old circle - wait(0.02); // In seconds - - // Erase old circle (For Ball 1 & 2) - uLCD.filled_circle(ball1.posx, ball1.posy, ball1.radius, BLACK); - uLCD.filled_circle(ball2.posx, ball2.posy, ball2.radius, BLACK); - - //Update Postions - ball1.update(STEP, accel); - ball2.update(STEP, accel); - - // Reset when sensor is flipped upside down (For Ball 1 & 2) - if (accel.readZ() < 0){ - uLCD.locate(5,6); - uLCD.color(RED); - uLCD.printf("GAME RESET"); - wait(2); - uLCD.cls(); - - ball1.set_state(DEFAULT, DEFAULT, 25, 25); - ball2.set_state(DEFAULT /2, DEFAULT /2, 25, 25); - wait (1); //Wait 1 second - } - } - } \ No newline at end of file + while (1) { } + } \ No newline at end of file