4180 Lab 2

Dependencies:   mbed wave_player Servo 4DGL-uLCD-SE Motor SDFileSystem LSM9DS1_Library_cal PinDetect X_NUCLEO_53L0A1

Files at this revision

API Documentation at this revision

Comitter:
emilywilson
Date:
Mon Feb 03 13:22:28 2020 +0000
Parent:
0:90df82d4732d
Child:
2:de355b6fbd87
Commit message:
parts 6-14 and extra credit

Changed in this revision

Motor.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
Servo.lib Show annotated file Show diff for this revision Revisions of this file
X_NUCLEO_53L0A1.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
part10.h Show annotated file Show diff for this revision Revisions of this file
part13.h Show annotated file Show diff for this revision Revisions of this file
part14.h Show annotated file Show diff for this revision Revisions of this file
part3.h Show annotated file Show diff for this revision Revisions of this file
part6.h Show annotated file Show diff for this revision Revisions of this file
part9.h Show annotated file Show diff for this revision Revisions of this file
sensor_EC.h Show annotated file Show diff for this revision Revisions of this file
wave_player.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Motor.lib	Mon Feb 03 13:22:28 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/simon/code/Motor/#f265e441bcd9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Mon Feb 03 13:22:28 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/SDFileSystem/#8db0d3b02cec
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.lib	Mon Feb 03 13:22:28 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/simon/code/Servo/#36b69a7ced07
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/X_NUCLEO_53L0A1.lib	Mon Feb 03 13:22:28 2020 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/ST/code/X_NUCLEO_53L0A1/#27d3d95c8593
--- a/main.cpp	Sun Feb 02 21:36:14 2020 +0000
+++ b/main.cpp	Mon Feb 03 13:22:28 2020 +0000
@@ -1,8 +1,27 @@
 #include "part1.h"
 //#include "part3.h"
+//#include "part6.h"
+//#include "part9.h"
+//#include "part10.h"
+//#include "part13.h"
+//#include "part14.h"
+//#include "sensor_EC.h"
 
 int main() {
     run_part1();
     
 //    run_part3();
+//    run_part3_EC();
+
+//    run_part6();
+
+//    run_part9();
+    
+//    run_part10();
+    
+//    run_part13();
+
+//    run_part14();
+    
+//    run_sensor_EC();
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/part10.h	Mon Feb 03 13:22:28 2020 +0000
@@ -0,0 +1,15 @@
+#include "mbed.h"
+#include "Motor.h"
+
+AnalogIn pot(p9);
+Motor motor(p10, p11, p12);
+
+int run_part10() {
+    while(1) {
+        float ctrl = (pot * 2) - 1.0;
+        
+        motor.speed(ctrl);
+        
+        wait(0.1);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/part13.h	Mon Feb 03 13:22:28 2020 +0000
@@ -0,0 +1,24 @@
+#include "mbed.h"
+#include "SDFileSystem.h"
+
+SDFileSystem sd(p5, p6, p7, p8, "sd");
+
+int run_part13() {
+    mkdir("/sd/mydir", 0777);
+    
+    FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
+    if(fp == NULL) {
+        error("Could not open file for write\n");
+    }
+    fprintf(fp, "Hello fun SD file world!");
+    fclose(fp);
+    
+    char mystring[100];
+    FILE *fp2 = fopen("/sd/mydir/sdtest.txt", "r");
+    if (fgets(mystring, 100, fp) != NULL) {
+        printf(mystring);
+    }
+    fclose(fp);
+    
+    return 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/part14.h	Mon Feb 03 13:22:28 2020 +0000
@@ -0,0 +1,15 @@
+#include "mbed.h"
+#include "SDFileSystem.h"
+#include "wave_player.h"
+
+SDFileSystem sd(p5, p6, p7, p8, "sd");
+AnalogOut DACOut(p18);
+
+wave_player player(&DACOut);
+
+int run_part14() {
+    FILE *wave_file = fopen("/sd/sample.wav", "r");
+    player.play(wave_file);
+    fclose(wave_file);
+    return 0;
+}
\ No newline at end of file
--- a/part3.h	Sun Feb 02 21:36:14 2020 +0000
+++ b/part3.h	Mon Feb 03 13:22:28 2020 +0000
@@ -1,14 +1,13 @@
 #include "mbed.h"
+#include "LSM9DS1.h"
 #include "uLCD_4DGL.h"
+#include <stdio.h>
 
 uLCD_4DGL uLCD(p27,p28,p30);
 LSM9DS1 lol(p9, p10, 0xD6, 0x3C);
 
 int run_part3() {
     lol.begin();
-    if (!lol.begin()) {
-        pc.printf("Failed to communicate with LSM9DS1.\n");
-    }
     
     lol.calibrate();
     int outer_radius = 15;
@@ -20,13 +19,13 @@
     int curr_x = center;
     int curr_y = center;
     
+    uLCD.background_color(BLACK);
+    
     while(1) {
         lol.readTemp();
         //lol.readMag();
 //        lol.readGyro();
         
-        pc.printf("accel: %d %d %d\n\r", lol.ax, lol.ay, lol.az);
-        
         uLCD.filled_circle(curr_x, curr_y, inner_radius, BLACK);
         uLCD.circle(center, center, outer_radius, WHITE);
         
@@ -37,5 +36,70 @@
         curr_y -= y_diff;
         
         uLCD.filled_circle(curr_x, curr_y, inner_radius, WHITE);
+        wait(0.001);
+    }
+}
+
+class Line {
+    public:
+    int start_x;
+    int start_y;
+    int end_x;
+    int end_y;
+};
+
+int run_part3_EC() {
+    lol.begin();
+    lol.calibrate();
+    
+    uLCD.background_color(BLACK);
+    
+    int center = (int)floor(127.0 / 2.0);
+    int radius = 10;
+    int line_length = 15;
+    
+    int prev_x = 0;
+    int prev_y = 0;
+    
+    Line curr_line;
+    curr_line.start_x = center;
+    curr_line.start_y = center - (radius/2);
+    curr_line.end_x = center;
+    curr_line.end_y = center - (radius / 2) + line_length;
+    
+    while (1) {
+        uLCD.circle(center, center, radius, BLACK);
+        uLCD.line(curr_line.start_x, curr_line.start_y, curr_line.end_x, curr_line.end_y, WHITE);
+        
+        uLCD.circle(center, center, radius, WHITE);
+        
+        int y_diff = lol.my - prev_y;
+        int x_diff = lol.mx - prev_x;
+        
+//        uLCD.drawLine();
+        
+        lol.readMag();
+        
+        char buffer[50];
+        sprintf(buffer, "Heading: %d, %d, %d", lol.mx, lol.my, lol.mz);
+        
+        uLCD.text_string(buffer, 100, 10, 12, WHITE);
+        
+        wait(0.001);
+    }
+}
+
+int run_time_EC() {
+    set_time(1580736096);
+    
+    int center = (int)floor(127.0 / 2.0);
+    
+    while (1) {
+        time_t seconds = time(NULL);
+        
+        char buffer[50];
+        sprintf(buffer, "Time: %s", ctime(&seconds));
+        
+        uLCD.text_string(buffer, center, center, 24, RED);
     }
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/part6.h	Mon Feb 03 13:22:28 2020 +0000
@@ -0,0 +1,12 @@
+#include "mbed.h"
+
+DigitalOut relay(p8);
+
+int run_part6() {
+    while (1) {
+        relay = 1;
+        wait(2);
+        relay = 0;
+        wait(2);
+    }   
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/part9.h	Mon Feb 03 13:22:28 2020 +0000
@@ -0,0 +1,12 @@
+#include "mbed.h"
+#include "Servo.h"
+
+Servo servo(p9);
+AnalogIn pot(p9);
+
+int run_part9() {
+    while (1) {
+        servo = pot;
+        wait(0.2);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sensor_EC.h	Mon Feb 03 13:22:28 2020 +0000
@@ -0,0 +1,49 @@
+#include "mbed.h"
+#include "Servo.h"
+#include "XNucleo53L0A1.h"
+#include <stdio.h>
+Serial pc1(USBTX,USBRX);
+DigitalOut shdn(p26);
+// This VL53L0X board test application performs a range measurement in polling mode
+// Use 3.3(Vout) for Vin, p28 for SDA, p27 for SCL, P26 for shdn on mbed LPC1768
+
+//I2C sensor pins
+#define VL53L0_I2C_SDA   p28
+#define VL53L0_I2C_SCL   p27
+
+static XNucleo53L0A1 *board=NULL;
+Servo servo(p9);
+
+int min_dist = 66536;
+int angle = 0;
+
+int run_sensor_EC() {
+    
+    int status;
+    uint32_t distance;
+    DevI2C *device_i2c = new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
+    /* creates the 53L0A1 expansion board singleton obj */
+    board = XNucleo53L0A1::instance(device_i2c, A2, D8, D2);
+    shdn = 0; //must reset sensor for an mbed reset to work
+    wait(0.1);
+    shdn = 1;
+    wait(0.1);
+    /* init the 53L0A1 board with default values */
+    status = board->init_board();
+    while (status) {
+        pc1.printf("Failed to init board! \r\n");
+        status = board->init_board();
+    }
+    
+    for (float i = 0.0; i < 1.0; i += 0.1) {
+        status = board->sensor_centre->get_distance(&distance);
+        if (status == VL53L0X_ERROR_NONE) {
+            pc1.printf("D=%ld mm\r\n", distance);
+        }
+        if (distance < min_dist) {
+            min_dist = distance;
+            angle = i;
+        }
+    }
+    return 0;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wave_player.lib	Mon Feb 03 13:22:28 2020 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/sravet/code/wave_player/#acc3e18e77ad