ECE 4180 Final Project, curiosity rover

Dependencies:   mbed wave_player Servo Motor SDFileSystem LSM9DS1_Library_cal HC_SR04_Ultrasonic_Library

Files at this revision

API Documentation at this revision

Comitter:
nivedharavi
Date:
Fri Dec 06 19:54:32 2019 +0000
Parent:
0:7bbc230e00d6
Commit message:
ECE 4180 Final Project Minesweeper / Rover

Changed in this revision

HC_SR04_Ultrasonic_Library.lib Show annotated file Show diff for this revision Revisions of this file
LSM9DS1_Library_cal.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
main.cpp 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/HC_SR04_Ultrasonic_Library.lib	Fri Dec 06 19:54:32 2019 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/ejteb/code/HC_SR04_Ultrasonic_Library/#e0f9c9fb4cf3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LSM9DS1_Library_cal.lib	Fri Dec 06 19:54:32 2019 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/4180_1/code/LSM9DS1_Library_cal/#36abf8e18ade
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Fri Dec 06 19:54:32 2019 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/SDFileSystem/#c8f66dc765d4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.lib	Fri Dec 06 19:54:32 2019 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/Servo/#36b69a7ced07
--- a/main.cpp	Tue Nov 23 16:19:19 2010 +0000
+++ b/main.cpp	Fri Dec 06 19:54:32 2019 +0000
@@ -2,12 +2,186 @@
 
 #include "mbed.h"
 #include "Motor.h"
+#include "LSM9DS1.h"
+#include "math.h"
+#define PI 3.14159
+#include "ultrasonic.h"
+#include "Servo.h"
+#include "SDFileSystem.h"
+#include "wave_player.h"
 
-Motor m(p23, p6, p5); // pwm, fwd, rev
+Servo myservo(p23);
 
+Motor m1(p22, p17, p15); // pwmA, fwd, rev, can brake
+Motor m2(p21,p20,p19); //pwmB, fwd, rev,brake
+Serial bluemod(p13,p14);
+DigitalOut myLed(LED1);
+Serial pc(USBTX, USBRX); // usb serial for debugging
+SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
+AnalogOut DACout(p18);
+wave_player waver(&DACout);
+// SDO_XM and SDO_G are pulled up, so our addresses are:
+#define LSM9DS0_XM_ADDR  0x1D 
+#define LSM9DS0_G_ADDR   0x6B // Would be 0x6A if SDO_G is LOW
+float mx;
+float my;
+float heading_val = 0.0;
+#define DECLINATION -4.94 // Declination (degrees) in Atlanta,GA.
+LSM9DS1 imu2(p28, p27, 0xD6, 0x3C);
+float p = 0;
+float calcHeading()
+{
+    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);
+    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;
+    return heading;
+}
+
+
+float updateHeading(){
+        imu2.readMag();
+        mx=imu2.mx;
+        my=imu2.my;
+        heading_val = calcHeading();
+        pc.printf("%2f\r\n",abs(heading_val));
+        return abs(heading_val);
+  
+  }
+
+void playSounds(){
+    FILE *wave_file;
+    wave_file=fopen("/sd/blaster-firing.wav","r");
+    waver.play(wave_file);
+    fclose(wave_file);    
+    
+}
+    
 int main() {
-    for (float s= -1.0; s < 1.0 ; s += 0.01) {
-       m.speed(s); 
-       wait(0.02);
+    imu2.begin();
+    if (!imu2.begin()) {
+        pc.printf("Failed to communicate with LSM9DS1.\n");
     }
-}
+    imu2.calibrate(1);
+    wait(2);
+    m1.speed(0.25);
+    m2.speed(-0.25);
+    imu2.calibrateMag(0);
+    m1.speed(0.0);
+    m2.speed(0.0);
+    
+    
+    while(1){ 
+        myLed = 0;
+        wait(0.2);
+        imu2.readMag();
+        mx=imu2.mx;
+        my=imu2.my;
+        heading_val = calcHeading();
+        heading_val = updateHeading();
+        myservo.calibrate();
+        if(bluemod.readable()){
+                    float speed;
+                    myLed = 1;
+                    char bnum=0;
+                    char bhit = 0;
+                     if (bluemod.getc()=='!') {
+                        if (bluemod.getc()=='B') { //button data
+                            bnum = bluemod.getc(); //button number
+                            bhit  = bluemod.getc();
+                            switch(bnum){
+                                case '1': // move forward
+                                    if(bhit=='1'){
+                                        speed=0.3;
+                                        playSounds();
+                                        }
+                                    break;
+                                case '2': // break
+                                    if(bhit=='1'){
+                                            speed=0;
+                                        }
+                                    break;
+                                case '3':  //reverse
+                                    if(bhit=='1'){
+                                            speed=-0.3;
+                                        }
+                                    break;
+                                case '4': //raise flag
+                                    if(bhit=='1'){
+                                        playSounds(); //play sound before raising flag
+                                        myLed=1;
+                                        myservo.write(0);
+                                        wait(1);
+                                        myLed =0;
+                                        myservo.write(.05);
+                                        wait(2);
+                                        myLed = 1;
+                                        m1.speed(0);
+                                        m2.speed(0);
+                                        playSounds();  //play sound after flag is raised
+                                        }
+                                    break;
+                                case '5':  //north
+                                    if(bhit=='1'){
+                                        while(heading_val>5.0 && heading_val<355.0){
+                                            heading_val = updateHeading();
+                                            m1.speed(0.3);
+                                            m2.speed(-0.3);
+                                            }
+                                        playSounds();
+                                        m1.speed(0);
+                                        m2.speed(0);
+                                        }
+                                    break;
+                                case '6': //south: 175->185
+                                    if(bhit=='1'){
+                                       while(!(heading_val<185.0 && heading_val>175.0)){
+                                            heading_val = updateHeading();
+                                            m1.speed(0.3);
+                                            m2.speed(-0.3);
+                                            }
+                                        playSounds();
+                                        m1.speed(0);
+                                        m2.speed(0);
+                                        }
+                                    break;
+                                case '8': //east
+                                    if(bhit=='1'){
+                                       while(!(heading_val<95.0 && heading_val>85.0)){
+                                            heading_val = updateHeading();
+                                            m1.speed(0.3);
+                                            m2.speed(-0.3);
+                                            }
+                                        playSounds();
+                                        m1.speed(0);
+                                        m2.speed(0);
+                                        }
+                                    break;
+                                 case '7': //west
+                                    if(bhit=='1'){
+                                       while(!(heading_val<275.0 && heading_val>265.0)){
+                                            heading_val = updateHeading();
+                                            m1.speed(0.3);
+                                            m2.speed(-0.3);
+                                            }
+                                        playSounds();//play sound when direction is found
+                                        m1.speed(0);
+                                        m2.speed(0);
+                                        }
+                                    break;
+                            default: break;    
+                            }//close case
+                        } //close B
+                } //!
+        m1.speed(speed);
+        m2.speed(speed);
+        }//close readable
+    }  // close while
+}//main
+        
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wave_player.lib	Fri Dec 06 19:54:32 2019 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/sravet/code/wave_player/#acc3e18e77ad