IMU is outputting garbage value to the terminal

Dependencies:   mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem

Files at this revision

API Documentation at this revision

Comitter:
Tito96
Date:
Fri Apr 23 01:03:49 2021 +0000
Parent:
1:618aa2c4ca6a
Commit message:
one player with IMU

Changed in this revision

LSM9DS1_Library.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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LSM9DS1_Library.lib	Fri Apr 23 01:03:49 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/ECE-4180-Two-Player-Space-Invaders-Team/code/LSM9DS1_Library/#c980d44f7bbe
--- a/main.cpp	Tue Mar 15 19:46:47 2016 +0000
+++ b/main.cpp	Fri Apr 23 01:03:49 2021 +0000
@@ -7,71 +7,23 @@
 #include "globals.h"
 #include "rtos.h"
 #include <string>
+#include "LSM9DS1.h"
 
 /* ==== Navigation Switch ===== */
-class Nav_Switch
-{
-public:
-    Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire);
-    int read();
-//boolean functions to test each switch
-    bool up();
-    bool down();
-    bool left();
-    bool right();
-    bool fire();
-//automatic read on RHS
-    operator int ();
-//index to any switch array style
-    bool operator[](int index) {
-        return _pins[index];
-    };
-private:
-    BusIn _pins;
- 
-};
-Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire):
-    _pins(up, down, left, right, fire)
-{
-    _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise
-    wait(0.001); //delays just a bit for pullups to pull inputs high
-}
-inline bool Nav_Switch::up()
-{
-    return !(_pins[0]);
-}
-inline bool Nav_Switch::down()
-{
-    return !(_pins[1]);
-}
-inline bool Nav_Switch::left()
-{
-    return !(_pins[2]);
-}
-inline bool Nav_Switch::right()
-{
-    return !(_pins[3]);
-}
-inline bool Nav_Switch::fire()
-{
-    return !(_pins[4]);
-}
-inline int Nav_Switch::read()
-{
-    return _pins.read();
-}
-inline Nav_Switch::operator int ()
-{
-    return _pins.read();
-}
+
 
 // Platform initialization
+
 uLCD_4DGL uLCD(p28,p27,p29); // LCD (serial tx, serial rx, reset pin;)
 AnalogOut DACout(p18);      // speaker
 wave_player waver(&DACout); // wav player
 SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD card and filesystem (mosi, miso, sck, cs)
-Nav_Switch myNav(p13, p10, p11, p9, p12); //pin order on Sparkfun breakout
-DigitalIn pb(p15); // push button for player misisle fire
+//Nav_Switch myNav(p16, p10, p11, p9, p12); //pin order on Sparkfun breakout
+DigitalIn pb(p15); // push button for player misisle fir
+Serial pc(USBTX, USBRX);
+PwmOut red(p21); // added to dim and brighten red LED -- Brice
+PwmOut green(p22); // added to dim and brighten green LED -- Brice
+PwmOut blue(p23); // added to dim and brighten blue LED -- Brice
 
 // Initialize all global enemy objects
 enemy_t enemy_1;
@@ -98,10 +50,16 @@
 int firing_col = 0;
 int hit_player = 0;
 bool lose = false;
-int lives = 3;
+int lives1 = 3;
+int lives2 = 2;
+int lives3 = 1;
+//int max_lives = 3;
 bool game_menu = false;
 bool begin_game = false;
 bool gameover = false;
+int level = 1;
+ double inX,inY,inZ;
+  int pos = 0;
 
 // Initialize global player object
 player_t player;
@@ -392,7 +350,7 @@
     enemy_14.enemy_blk_y += enemy_14.enemy_height+4;
     enemy_15.enemy_blk_y += enemy_15.enemy_height+4;
 }
-
+/*
 // thread that plays sounds during game
 void playstart(void const *args)//Th
 {   //Depending on the state of the game,
@@ -442,18 +400,128 @@
         }
     }
 }
+*/
+
+void ledEffects(void const *args)//Th
+{   //Depending on the state of the game,
+    //generate different patterns/colors of lighting
+    while(1) {
+        // gradually increases and decreases each color independently. (A chill build up effect?)
+        while(game_menu)
+        {
+            red = 0.0;
+            green = 0.0;
+            blue = 0.0;
+            for (float i = 0.0; i < 0.5; i = i + 0.05) {
+                red = i;
+                Thread::wait(10);
+            }
+            red = 0.5;
+            Thread::wait(300);
+            for (float i = 0.5; i > 0.0; i = i - 0.05) {
+                red = i;
+                Thread::wait(10);
+            }
+            red = 0.0;
+            for (float i = 0.0; i < 0.25; i = i + 0.05) {
+                green = i;
+                Thread::wait(10);
+            }
+            green = 0.25;
+            Thread::wait(300);
+            for (float i = 0.25; i > 0.0; i = i - 0.05) {
+                green = i;
+                Thread::wait(10);
+            }
+            green = 0.0;
+            for (float i = 0.0; i < 0.5; i = i + 0.05) {
+                blue = i;
+                Thread::wait(10);
+            }
+            blue = 0.5;
+            Thread::wait(300);
+            for (float i = 0.5; i > 0.0; i = i - 0.05) {
+                blue = i;
+                Thread::wait(10);
+            }
+            blue = 0.0;
+        }
+        // Checks in game sound conditions
+        while(begin_game) // added OR begin_game2 so that music/sounds play during one-player or two-player 
+        {
+            // play firing sound when the player fires
+            if(!pb && missile.status == PLAYER_MISSILE_INACTIVE) {
+                red = 0.0;
+                green = 0.0;
+                blue = 0.0;
+                red = 0.5;
+                Thread::wait(200);
+                green = 0.15;
+                Thread::wait(200);
+                red = 0.0;
+                green = 0.0;
+            }
+            
+            // if player hit, play hit sound
+            if (hit_player)
+            {
+                red = 0.0;
+                green = 0.0;
+                blue = 0.0;
+                red = 0.5;
+                Thread::wait(60);
+                red = 0.0;
+                Thread::wait(60);
+                red = 0.5;
+                Thread::wait(60);
+                red = 0.0;
+                Thread::wait(60);
+                red = 0.5;
+                Thread::wait(60);
+                red = 0.0;
+            }
+            Thread::wait(500);
+        }
+        
+        // players gameover voice if player loses
+        while(gameover)
+        {
+            for (float i = 0.0; i < 0.25; i = i + 0.05) {
+                red = i;
+                Thread::wait(10);
+            }
+            red = 0.25;
+            Thread::wait(300);
+            for (float i = 0.25; i > 0.0; i = i - 0.05) {
+                red = i;
+                Thread::wait(10);
+            }
+            red = 0.0;
+            Thread::wait(500);
+        }
+    }
+}
+
+
+
 
 int main() {
-     
+      LSM9DS1 lol(p9, p10, 0xD6, 0x3C);
      // Initialization of Setup variables
      int blk_x, blk_y;
      pb.mode(PullUp);
-     
-     Thread thread(playstart); // intializes the thread to play sound
-     
+     lol.begin();
+    if (!lol.begin()) {
+        pc.printf("Failed to communicate with LSM9DS1.\n");
+    }
+    lol.calibrate();
+    // Thread thread(playstart); // intializes the thread to play sound
+        Thread thread2(ledEffects);
      uLCD.baudrate(500000); // set to 500000 to increase smooth gameplay
      
+     
      // Initialization of Game Menu variables
+     
     const int title_x_pos = 2; // initial x-pos of title
     const int title_y_pos = 3; // initial y-pos of title
     int start_label_x_pos = 7; // start label x-pos
@@ -466,29 +534,86 @@
     int win_y_pos = 5; // congratulations label y-position
     int startover_x_pos = 3; // startover label x-position
     int startover_y_pos = 8; // startover label y-position
+    int level_cursor_y_pos_end = 11; // BOTTOM CURSOR POS -- Added by Brice for more menu options
+    int level_cursor_y_pos_start = 7; //--Added by Brice
     
     // intialize temporary score and current score
     int temp = 0;
     int score = 0;
-     
+
+        uLCD.cls();
     // Begin game loop
     while(1)
     {
-        // initialize all starting conditions for the beginning of the game
         game_menu = true; // defaults to display menu screen
         ENEMY_MOVE = true; // defaults to move enemy
         DIRECTION = 1; // default to move right
         hit_player = 0; // default to not player hit
         MOVE_DOWN = 0; // default to not move down
         lose = false; // default to not lose
-        lives = 3; // defaults to 3 lives
+         lives1 = 3; // defaults to 3 lives
+         lives2 = 2;
+         lives3 = 1;
         score = 0; // default to score of 0
 
         uLCD.cls();
+        while(!lol.accelAvailable()) {
+            pos = 0;
+            }
+        lol.readAccel();
+                      
+        inX = lol.calcAccel(lol.ax);
+        inY = lol.calcAccel(lol.ay); 
+        inZ = lol.calcAccel(lol.az);
+        
+        // pc.printf("gyro: %d %d %d\n\r", lol.gx, imu.gy, imu.gz);
+        pc.printf("accel: %d %d %d\n\r", inX, inY, inZ);
+        //pc.printf("mag: %d %d %d\n\n\r", imu.mx, imu.my, imu.mz);
+        if (inY >= .5)
+        {
+            pc.printf("up\n\r");
+            pos = 1;
+          //  wireless.sendDirection(DIR_LEFT);
+        }
+        else if( inY <= -.5)
+        {
+            pc.printf("down\n\r");
+            pos = 2;
+            //wireless.sendDirection(DIR_RIGHT);
+        }
+        else if (inX >= .5)
+        {
+            pc.printf("right\n\r");
+            pos = 3;
+           // wireless.sendDirection(DIR_UP);
+        }
+        else if (inX <= -.5)
+        {
+            pc.printf("left\n\r");
+            pos = 4;
+            //wireless.sendDirection(DIR_DOWN);
+        }
+        else
+        {
+            pc.printf("error");
+            pos = 0;
+           // wireless.sendDirection(DIR_NONE);
+        }
+        wait(0.5);
         
         // Implementation of Game Menu
+         // Implementation of Game Menu
         while(game_menu) 
         {
+             // Brice added this in order to move the cursor through the menu options
+            uLCD.locate(level_cursor_x_pos, level_cursor_y_pos);
+            uLCD.printf("  ");
+            if (pos == 2 && level_cursor_y_pos < level_cursor_y_pos_end) {
+                level_cursor_y_pos += 2;
+            } else if (pos == 1 && level_cursor_y_pos > level_cursor_y_pos_start) {
+                level_cursor_y_pos -= 2;
+            }
+        
             uLCD.locate(level_cursor_x_pos,level_cursor_y_pos); // draws cursor next to "START" label
             uLCD.printf("->");
             
@@ -496,16 +621,34 @@
             uLCD.printf("SPACE INVADERS"); // Title
             
             uLCD.locate(start_label_x_pos,start_label_y_pos); // "START" label position
-            uLCD.printf("START");
+            uLCD.printf("LEVEL 1");
+            
+            uLCD.locate(start_label_x_pos,start_label_y_pos + 2); // "START" label position
+            uLCD.printf("LEVEL 2");
+            
+            uLCD.locate(start_label_x_pos,start_label_y_pos + 4); // "START" label position
+            uLCD.printf("LEVEL 3");
 
             // if pushbutton is pressed, game menu is exited and game begins
             if(!pb) 
             { 
                 game_menu = false;
+                 if (level_cursor_y_pos == start_label_y_pos) {
+                    level = 1;
+                } else if (level_cursor_y_pos == start_label_y_pos + 2) {
+                    level = 2;
+                   // pc.printf("num players: 2");
+                } else if (level_cursor_y_pos == start_label_y_pos + 4){
+                    level = 3;
+                }
+           
+          
                 wait(0.5);
+              
             }
         }
         
+        
         begin_game = true; // defaults begin_game to true
         
         uLCD.cls();
@@ -523,11 +666,22 @@
         int e_blk_x = 0;
         int e_blk_y = 2;
         enemy_missile_init(&enemy_missile, e_blk_x, e_blk_y, WHITE);
-        
+       
         // prints lives
+        if (level == 1){
         uLCD.locate(0,0);
-        uLCD.printf("Lives:%i", lives);
+        uLCD.printf("Lives:%i", 3);
+        }
+    
+        else if (level == 2){
+        uLCD.locate(0,0);
+        uLCD.printf("Lives:%i", 2);
+        }
 
+        else if (level == 3){
+        uLCD.locate(0,0);
+        uLCD.printf("Lives:%i", 1);
+        }
         // prints score
         uLCD.locate(9,0);
         uLCD.printf("Score:%i", score);
@@ -587,13 +741,13 @@
             update_enemy_missile_pos(&enemy_missile); // updates enemy missile position
 
             // Player Movement checked with navigation switch
-            if (myNav.left() && ((player.player_blk_x-3) > 0)) 
+            if (pos == 4 && ((player.player_blk_x-3) > 0)) 
             {
                 player_erase(&player);
                 player.player_blk_x -= 3;
                 player_show(&player);
             } 
-            else if (myNav.right() && ((player.player_blk_x+3) < (128-player.player_width))) 
+            else if (pos == 3 && ((player.player_blk_x+3) < (128-player.player_width))) 
             {
                 player_erase(&player);
                 player.player_blk_x += 3;
@@ -649,10 +803,12 @@
             }
             
             // checks if player was hit
+            // checks if player was hit
             if (hit_player)
             {
-                // updates lives
-                lives -= 1;
+                if (level == 1){
+                //lives = max_lives;
+                lives1 -= 1;
                 wait(0.5);
                 hit_player = 0;
                 player_show(&player);
@@ -660,11 +816,37 @@
                 
                 // prints updated lives number
                 uLCD.locate(0,0);
-                uLCD.printf("Lives:%i", lives);
-            }   
-            
+                uLCD.printf("Lives:%i", lives1);
+                }
+                else if (level == 2){
+                  // lives = max_lives - 1;
+                lives2 -= 1;
+                wait(0.5);
+                hit_player = 0;
+                player_show(&player);
+                player.status = PLAYER_ALIVE;
+                
+                // prints updated lives number
+                uLCD.locate(0,0);
+                uLCD.printf("Lives:%i", lives2);
+                } 
+                    
+                else if (level == 3){
+                //lives = max_lives - 2;
+                lives3 -= 1;
+                wait(0.5);
+                hit_player = 0;
+                player_show(&player);
+                player.status = PLAYER_ALIVE;
+                
+                // prints updated lives number
+                uLCD.locate(0,0);
+                uLCD.printf("Lives:%i", lives3);
+                }
+                
+                }
             // if player loses all lives or enemy reaches the player
-            if (lose || lives == 0)
+            if (lose || lives1 == 0 || lives2 == 0 || lives3 == 0)
             {    
                 begin_game = false; // set to false to end game
                 uLCD.cls();