ECE3872 HW/SW Project Code

Dependencies:   mbed Servo mbed-rtos 4DGL-uLCD-SE PinDetect X_NUCLEO_53L0A1

Files at this revision

API Documentation at this revision

Comitter:
trmontgomery
Date:
Mon Apr 13 02:16:45 2020 +0000
Parent:
14:7e81f2cd4b4b
Child:
17:11c46079d674
Commit message:
map servo movement to freqs

Changed in this revision

Servo.lib Show annotated file Show diff for this revision Revisions of this file
Speakerout2.h Show diff for this revision Revisions of this file
audio_out.h 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
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
motor_ctl.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.lib	Mon Apr 13 02:16:45 2020 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/Servo/#36b69a7ced07
--- a/Speakerout2.h	Sat Apr 11 20:00:59 2020 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,75 +0,0 @@
-#include "mbed.h"
-#include "Speaker.h"
-
-Serial pc(USBTX,USBRX);
-Speaker mySpeaker(p26);
-
-char note;
-float freq [30];
-float freq2;
-
-float *record(float freq1[30]) {
-    for(int i = 0; i<30; i++){
-        pc.printf("Please enter note\n");
-        note = pc.getc();
-        switch (note){
-            case 'a':
-                freq2 = 523.25;
-                break;
-            
-            case 'b':
-                freq2 = 554.37;
-                break;
-            
-            case 'c':
-                freq2 = 587.33;
-                break;
-            
-            case 'd':
-                freq2 = 622.25;
-                break;
-            
-            case 'e':
-                freq2 = 659.26;
-                break;
-            
-            case 'f':
-                freq2 = 698.46;
-                break;
-            
-            case 'g':
-                freq2 = 739.99;
-                break;
-            
-            case 'h':
-                freq2 = 783.99;
-                break;
-            
-            case 'i':
-                freq2 = 830.61;
-                break;
-            
-            case 'j':
-                freq2 = 880.00;
-                break;
-            
-            case 'k':
-                freq2 = 932.33;
-                break;
-            
-            case 'l':
-                freq2 = 987.77;
-                break;
-            }
-            freq1[i] = freq2;
-        }
-    return freq1;
-    }
-
-
-int main() {
-    record(freq);
-    for (int i = 0; i<30; i++){
-        mySpeaker.PlayNote(freq[i],1.0,0.1);
-    }
-}
--- a/audio_out.h	Sat Apr 11 20:00:59 2020 +0000
+++ b/audio_out.h	Mon Apr 13 02:16:45 2020 +0000
@@ -1,4 +1,4 @@
-#include "mbed.h"
+/*#include "mbed.h"
 #include "XNucleo53L0A1.h"
 #include <stdio.h>
 #include "Speaker.h"
@@ -21,13 +21,13 @@
     int status;
     uint32_t distance;
     DevI2C *device_i2c = new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
-    /* creates the 53L0A1 expansion board singleton obj */
+    /* 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 */
+    /* init the 53L0A1 board with default values 
     status = board->init_board();
     while (status) {
         status = board->init_board();
@@ -94,4 +94,5 @@
         state = false;
         }
     }
-}
\ No newline at end of file
+}
+*/
\ No newline at end of file
--- a/main.cpp	Sat Apr 11 20:00:59 2020 +0000
+++ b/main.cpp	Mon Apr 13 02:16:45 2020 +0000
@@ -3,15 +3,19 @@
 #include "audio_out.h"
 #include "PinDetect.h"
 #include "uLCD_4DGL.h"
-DigitalOut myled(LED1);
-//Serial mac(USBTX,USBRX);
+#include "motor_ctl.h"
+DigitalOut myled1(LED1);
+DigitalOut myled2(LED2);
+DigitalOut myled3(LED3);
+DigitalOut myled4(LED4);
 
+// Push buttons init
+PinDetect pb1(p16); 
+PinDetect pb2(p17);  
+PinDetect pb3(p18); 
+PinDetect pb4(p19); 
 // States 
-#define sRESET 1
-#define sSTOP 2
-#define sRECORD 3
-#define sPLAY 4
-#define sERASE 5
+
 //  State Machine control global variables
 bool X;     //reset
 bool P;     //play
@@ -19,6 +23,7 @@
 bool R;     //record
 bool E;     //erase
 
+enum sm_state {sRESET, sSTOP, sRECORD, sPLAY, sERASE};
 
 
 void reset(){ 
@@ -68,100 +73,91 @@
 
 
 
+void pb1_hit_callback (void) 
+{
+    myled1 = !myled1; 
+    //guLCD.printf("REEST");
+}
+
+void pb2_hit_callback (void)
+{
+    myled2 = !myled2; 
+    //guLCD.printf("STOP");
+}
+
+void pb3_hit_callback (void) 
+{
+    myled3 = !myled3; 
+    //guLCD.printf("RECORD");
+}
+
+void pb4_hit_callback (void) 
+{
+    myled4 = !myled4; 
+    //guLCD.printf("PLAY");
+}
+void hardware_init(){ 
+    // Push buttons init
+    pb1.mode(PullUp); 
+    pb2.mode(PullUp); 
+    pb3.mode(PullUp);
+    pb4.mode(PullUp);
+    pb1.attach_deasserted(&pb1_hit_callback);
+    pb2.attach_deasserted(&pb2_hit_callback); 
+    pb3.attach_deasserted(&pb3_hit_callback);
+    pb4.attach_deasserted(&pb4_hit_callback);
+    pb1.setSampleFrequency(); 
+    pb2.setSampleFrequency(); 
+    pb3.setSampleFrequency();
+    pb4.setSampleFrequency();
+}
 
 void state_machine_mgr(){
-    char curr_state =  1;
-    char key_input;
-    X = 1;
-    S = 0;
-    R = 0;
-    P = 0;
-    E = 0;
+    sm_state curr_state =  sRESET;
     while(1) {
-        if (pc.readable()) {                // if they have pressed a key
-            key_input = pc.getc();
-            if(key_input == '1') X=1;   //toggle reset
-            if(key_input == '2') S=1;   //toggle stop
-            if(key_input == '3') R=1;   //toogle record
-            if(key_input == '4') P=1;   //toggle play
-            if(key_input == '5') E=1;   //toggle erase
-        }
-        //pc.printf("X: %d\n",X);
-//        pc.printf("S: %d\n",S);
-//        pc.printf("R: %d\n",R);
-//        pc.printf("P: %d\n",P);
-//        pc.printf("E: %d\n",E);
-//        pc.printf("Current State %d\n",curr_state);
         switch(curr_state){
-            case 1:
-                pc.printf("RESET\n");
-                P = 0;
-                R = 0;
-                if(E){
-                    curr_state = 5;
-                    X = 0;
-                }else if(S){
-                    curr_state = 2;
-                    X = 0;
+            case sRESET:
+                if(S) curr_state = sSTOP;
+                break;
+            case sSTOP:
+                if(X){
+                    curr_state = sRESET;
+                }else if(S&R){
+                    curr_state = sRECORD;
+                }else if(S&P){
+                    curr_state = sPLAY;
                 }
-                reset();
                 break;
-            case 2:
-                pc.printf("STOP\n");
-                E = 0;
+            case sRECORD:
                 if(X){
-                    curr_state = 1;
-                    S = 0;
-                }else if(R){
-                    curr_state = 3;
-                    S = 0;
-                }else if(P){
-                    curr_state = 4;
-                    S = 0;
+                    curr_state = sRESET;
+                }else if(R&S){
+                    curr_state = sSTOP;
+                }else if(R&P){
+                    curr_state = sPLAY;
                 }
-                stop();
-                break;
-            case 3:
-                pc.printf("RECORD\n");
-                E = 0;
-                P = 0;
-                if(X){
-                    curr_state = 1;
-                    R = 0;
-                }else if(S){
-                    curr_state = 2;
-                    R = 0;
-                }
-                record();
                 break;
-            case 4:
-                pc.printf("PLAY\n");
-                E = 0;
-                R = 0;
+            case sPLAY:
                 if(X){
-                    curr_state = 1;
-                    P = 0;
-                }else if(S){
-                    curr_state = 2;
-                    P = 0;
+                    curr_state = sRESET;
+                }else if(P&S){
+                    curr_state = sSTOP;
+                }else if(P&R){
+                    curr_state = sPLAY;
                 }
-                play();
                 break;
-            case 5:
-                pc.printf("ERASE\n");
-                S = 0;
-                P = 0;
-                R = 0;
+            case sERASE:
                 if(X){
-                    curr_state = 1;
-                    E = 0;
+                    curr_state = sRESET;
                 }
-                erase();
-                break;
         }
     }
 }
 
+
 int main() {
-    state_machine_mgr();
+    myled1 = 1; 
+    //hardware_init();
+    //state_machine_mgr();
+    move_motors();
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Mon Apr 13 02:16:45 2020 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/mbed-rtos/#58563e6cba1e
--- a/mbed.bld	Sat Apr 11 20:00:59 2020 +0000
+++ b/mbed.bld	Mon Apr 13 02:16:45 2020 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file
+https://os.mbed.com/users/mbed_official/code/mbed/builds/0ab6a29f35bf
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/motor_ctl.h	Mon Apr 13 02:16:45 2020 +0000
@@ -0,0 +1,81 @@
+// Move servos in playback mode
+
+#include "mbed.h"
+#include "Servo.h"
+//#include "rtos.h"
+
+Servo r_arm(p24);
+Servo l_arm(p23);
+Servo r_leg(p22);
+Servo l_leg(p21);
+
+Thread t_r_arm;
+Thread t_l_arm;
+Thread t_r_leg;
+Thread t_l_leg;
+
+DigitalOut led(LED4); 
+
+//left sensor move whole left side
+//right sensor moves whole right side //not gonna think about this right now. Dicuss ideas with team. 
+//and then the distance can just control the height
+//movement pattern can be random.. right now every other note recorded will be distributed to one side and the rest will go to the other
+
+unsigned char move_map {0x149, 0x14A, 0x14C, 0x151, 0x152, 0x154, 0x291, 0x292, 0x294, 0x2A1, 0x2A2, 0x2A4};
+unsigned char ra_mask = 0x0C0;
+unsigned char la_mask = 0x300;
+unsigned char rl_mask = 0x03C;
+unsigned char ll_mask = 0x007; 
+
+//for testing run through move map
+
+void ra_dance(unsigned char move) {    
+    if (move & ra_mask == 0x040){
+        r_arm.write(.50);
+    } else {
+        r_arm.write(.25);
+    }
+    
+}
+
+void la_dance() {    
+     if (move & ra_mask == 0x100){
+        l_arm.write(.50);
+    } else {
+        l_arm.write(.25);
+    }
+    
+}
+
+void rl_dance() {    
+    r_leg = 0; 
+    wait(1);
+    for(float p=0; p<1.0; p += 0.1) {
+        r_leg = p;
+        wait(0.2);
+    }
+    r_leg.write(.50);
+    
+}
+
+void ll_dance() {    
+    l_leg = 0; 
+    wait(1);
+    for(float p=0; p<1.0; p += 0.1) {
+        l_leg = p;
+        wait(0.2);
+    }
+    l_leg.write(.50);
+    
+}
+
+
+void move_motors(){
+    for (int i = 0; i < 12; ++i){
+        ra_dance(move_map[i]);
+        la_dance(move_map[i]);
+        wait(0.2); 
+    }
+}
+
+