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 21:32:00 2020 +0000
Parent:
18:0e0edd4f9e4d
Child:
26:2063ee8419cc
Commit message:
trying to make a servo move back and forth. tried to use SongPlayer class to control servos.

Changed in this revision

SongPlayer.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
motor_ctl.h Show annotated file Show diff for this revision Revisions of this file
motor_ctl_t.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SongPlayer.h	Mon Apr 13 21:32:00 2020 +0000
@@ -0,0 +1,41 @@
+#include "mbed.h"
+// new class to play a note on Speaker based on PwmOut class
+class SongPlayer
+{
+public:
+    SongPlayer(PinName pin) : _pin(pin) {
+// _pin(pin) means pass pin to the constructor
+    }
+// class method to play a note based on PwmOut class
+    void PlaySong(float frequency[], float duration[], float volume=1.0) {
+        vol = volume;
+        notecount = 0;
+        _pin.period(1.0/frequency[notecount]);
+        _pin = volume/2.0;
+        noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]);
+        // setup timer to interrupt for next note to play
+        frequencyptr = frequency;
+        durationptr = duration;
+        //returns after first note starts to play
+    }
+    void nextnote();
+private:
+    Timeout noteduration;
+    PwmOut _pin;
+    int notecount;
+    float vol;
+    float * frequencyptr;
+    float * durationptr;
+};
+//Interrupt Routine to play next note
+void SongPlayer::nextnote()
+{
+    _pin = 0.0;
+    notecount++; //setup next note in song
+    if (durationptr[notecount]!=0.0) {
+        _pin.period(1.0/frequencyptr[notecount]);
+        noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]);
+        _pin = vol/2.0;
+    } else
+        _pin = 0.0; //turn off on last note
+}
\ No newline at end of file
--- a/main.cpp	Mon Apr 13 02:43:08 2020 +0000
+++ b/main.cpp	Mon Apr 13 21:32:00 2020 +0000
@@ -1,8 +1,9 @@
 #include "mbed.h"
 #include <iostream>
+#include <vector>
 #include "speaker_out.h"
-#include "uLCD_4DGL.h"
 #include "motor_ctl.h"
+
 DigitalOut myled1(LED1);
 DigitalOut myled2(LED2);
 DigitalOut myled3(LED3);
@@ -17,6 +18,10 @@
 
 enum sm_state {sRESET, sSTOP, sRECORD, sPLAY, sERASE};
 
+//SongPlayer music; 
+//Puppet puppet;
+vector<float> song; //stores all notes recorded by user
+
 void reset(){ 
     /* reset state:
         Initial state upon powering up the device
@@ -28,6 +33,9 @@
         6. LED goes from green to red
         NOTE: ONLY exits to stop or erase state 
     */
+    //puppet.reset();
+    //music.reset();
+    //led = red; //use rgb red for this
 }
 void stop(){
     /* stop state:
@@ -36,6 +44,8 @@
         2. Stop recording
         3. Mute all audio
     */
+    //puppet.stop();
+    //music.stop(); 
 }
 void record(){
     /* record state:
@@ -46,6 +56,7 @@
         4. Append to list of frequencies
         5. 
     */
+    //music.record(); //need to play as recording happens
 }
 void play(){
     /* play state:
@@ -55,11 +66,14 @@
             - moving servo motors accordingly
             - playing corresponding sounds
     */
+    //puppet.dance();
+    //music.play();
 }
 void erase(){
     /* erase state:
         erases entire audio recording
     */
+    //song.erase();
 }
 
 void state_machine_mgr(){
@@ -156,8 +170,6 @@
 }
 
 int main() {
-    myled1 = 1; 
-    //hardware_init();
     //state_machine_mgr();
-    move_motors();
+    puppet_move();
 }
--- a/motor_ctl.h	Mon Apr 13 02:43:08 2020 +0000
+++ b/motor_ctl.h	Mon Apr 13 21:32:00 2020 +0000
@@ -1,5 +1,3 @@
-// Move servos in playback mode
-
 #include "mbed.h"
 #include "Servo.h"
 #include "rtos.h"
@@ -14,68 +12,85 @@
 Thread t_r_leg;
 Thread t_l_leg;
 
-DigitalOut led(LED4); 
+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 short move_map[] {0x149, 0x14A, 0x14C, 0x151, 0x152, 0x154, 0x291, 0x292, 0x294, 0x2A1, 0x2A2, 0x2A4};
+unsigned short move_map1[] = {0x149, 0x14A, 0x14C, 0x151, 0x152, 0x154, 0x291, 0x292, 0x294, 0x2A1, 0x2A2, 0x2A4};
 unsigned short ra_mask = 0x0C0;
 unsigned short la_mask = 0x300;
 unsigned short rl_mask = 0x03C;
 unsigned short ll_mask = 0x007; 
 
-//for testing run through move map
+unsigned short move_map[] = {1,2,4,1,2,4,1,2,4,1,2,4,1,2,4,1,2,4};
 
-void ra_dance(unsigned short move) {    
-    if (move & ra_mask == 0x040){
-        r_arm.write(.50);
-    } else {
-        r_arm.write(.25);
+void ra_dance() {
+    //r_arm = 0;
+    //wait(1);
+    for(int p=0; p<12; ++p) {
+        unsigned short pos = move_map[p];
+        switch(pos){
+            case 1: r_arm.write(1.0);
+            case 2: r_arm.write(0.5);
+            case 4: r_arm.write(1.0);
+        }
+        wait(0.5);
+        r_arm = 0.5;
     }
     
+    r_arm = 1;
+    wait(0.5);
+    r_arm = 0.5;
+    wait(0.5);
+    r_arm = 1; 
+    r_arm.write(.50);
+    r_arm.write()
+
 }
 
-void la_dance(unsigned short move) {    
-     if (move & ra_mask == 0x100){
-        l_arm.write(.50);
-    } else {
-        l_arm.write(.25);
+void la_dance() {
+    l_arm = 0;
+    wait(1);
+    for(float p=0; p<1.0; p += 0.1) {
+        l_arm = p;
+        wait(0.2);
     }
-    
+    l_arm.write(.50);
+
 }
 
-void rl_dance() {    
-    r_leg = 0; 
+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; 
+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); 
-    }
+void puppet_move(){
+    //while(1){
+    //t_r_arm.start(ra_dance);
+    //wait(1);
+    //t_l_arm.start(la_dance);
+    //wait(1);
+    //t_r_leg.start(rl_dance);
+    //wait(1);
+    //t_l_leg.start(ll_dance);
+    //wait(1);
+  // }
+  ra_dance();
 }
 
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/motor_ctl_t.h	Mon Apr 13 21:32:00 2020 +0000
@@ -0,0 +1,113 @@
+// 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); 
+
+int i = 0; 
+int j = 0; 
+
+//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 short move_map[] {0x149, 0x14A, 0x14C, 0x151, 0x152, 0x154, 0x291, 0x292, 0x294, 0x2A1, 0x2A2, 0x2A4};
+unsigned short ra_mask = 0x0C0;
+unsigned short la_mask = 0x300;
+unsigned short rl_mask = 0x03C;
+unsigned short ll_mask = 0x007; 
+
+//for testing run through move map
+
+// new class to play a note on Speaker based on PwmOut class
+class Puppet
+{
+public:
+    Puppet(){ //do I need pins if I keep the global var?
+    }
+    void dance(unsigned short[]);
+    void ra_dance(unsigned short);
+    void la_dance(unsigned short);
+    void nextmove();
+private:
+    Timeout moveduration;
+    int movecount;
+    float vol;
+    unsigned short * movementptr;
+};
+//Interrupt Routine to play next note
+void Puppet::nextmove()
+{
+    //_pin = 0.0; //need to set the servo to start up here?
+    r_arm.write(0);
+    l_arm.write(0);
+    movecount++; //setup next note in song
+    if (movementptr[movecount++] == 0.0) movecount = 0;
+    ra_dance(movementptr[movecount]);
+    la_dance(movementptr[movecount]);
+    moveduration.attach(this,&Puppet::nextmove, 0.2);
+
+}
+
+
+void Puppet::ra_dance(unsigned short move) {    
+    /*
+    if (move & ra_mask == 0x040){
+        r_arm.write(1);
+    } else {
+        r_arm.write(.2);
+    }*/
+    if(i > 1) i = 0;
+    r_arm.write(i); 
+    i += 0.1; 
+    
+}
+
+void Puppet::la_dance(unsigned short move) {    
+    /* if (move & ra_mask == 0x100){
+        l_arm.write(1);
+    } else {
+        l_arm.write(.25);
+    }*/
+    if(j > 1) j = 0;
+    l_arm.write(i); 
+    j += 0.1; 
+}
+
+void Puppet::dance(unsigned short movement[]) {
+    movecount = 0;
+    movementptr = movement; 
+    ra_dance(movement[movecount]);
+    la_dance(movement[movecount]);
+    moveduration.attach(this,&Puppet::nextmove, 0.2);
+    // setup timer to interrupt for next note to play
+    
+    //returns after first note starts to play
+}
+
+void puppet_move(){
+    Puppet pupper;
+// Start song and return once playing starts
+    pupper.dance(move_map);
+    // loops forever while song continues to play to end using interrupts
+    while(1) {
+        led = !led;
+        wait(.1);
+    }
+}
+
+
+