ECE3872 HW/SW Project Code

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

Committer:
trmontgomery
Date:
Sat Apr 04 23:51:19 2020 +0000
Revision:
19:f76e4ffddbe1
Parent:
4:1790aa9234a3
Child:
20:8c3644bf5d28
refactoring and servo testing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nnguyen99 3:7486869c8027 1 #include "mbed.h"
trmontgomery 4:1790aa9234a3 2 #include "audio_out.h"
trmontgomery 19:f76e4ffddbe1 3 #include "motor_ctl.h"
nnguyen99 3:7486869c8027 4
nnguyen99 3:7486869c8027 5 DigitalOut myled(LED1);
nnguyen99 3:7486869c8027 6 // States
nnguyen99 3:7486869c8027 7 #define RESET 1
nnguyen99 3:7486869c8027 8 #define STOP 2
nnguyen99 3:7486869c8027 9 #define RECORD 3
nnguyen99 3:7486869c8027 10 #define PLAY 4
nnguyen99 3:7486869c8027 11 #define ERASE 5
nnguyen99 3:7486869c8027 12 // State Machine control global variables
nnguyen99 3:7486869c8027 13 bool X; //reset
nnguyen99 3:7486869c8027 14 bool P; //play
nnguyen99 3:7486869c8027 15 bool S; //stop
nnguyen99 3:7486869c8027 16 bool R; //record
nnguyen99 3:7486869c8027 17 bool E; //erase
nnguyen99 3:7486869c8027 18
nnguyen99 3:7486869c8027 19 enum sm_state {sRESET,sSTOP,sRECORD,sPLAY,sERASE};
nnguyen99 3:7486869c8027 20
nnguyen99 3:7486869c8027 21
nnguyen99 3:7486869c8027 22 void reset(){
nnguyen99 3:7486869c8027 23 /* reset state:
nnguyen99 3:7486869c8027 24 Initial state upon powering up the device
nnguyen99 3:7486869c8027 25 1. Cease all motion
nnguyen99 3:7486869c8027 26 2. Reset all motors to initial position
nnguyen99 3:7486869c8027 27 3. Mutes all audio
nnguyen99 3:7486869c8027 28 4. Goes to erase state if reset button is held down for at least 3 seconds
nnguyen99 3:7486869c8027 29 5. Goes to stop state according to rotary switch input
nnguyen99 3:7486869c8027 30 6. LED goes from green to red
nnguyen99 3:7486869c8027 31 NOTE: ONLY exits to stop or erase state
nnguyen99 3:7486869c8027 32 */
nnguyen99 3:7486869c8027 33 }
nnguyen99 3:7486869c8027 34 void stop(){
nnguyen99 3:7486869c8027 35 /* stop state:
nnguyen99 3:7486869c8027 36 Initiated by rotary switch
nnguyen99 3:7486869c8027 37 1. Cease all motion
nnguyen99 3:7486869c8027 38 2. Stop recording
nnguyen99 3:7486869c8027 39 3. Mute all audio
nnguyen99 3:7486869c8027 40 */
nnguyen99 3:7486869c8027 41 }
nnguyen99 3:7486869c8027 42 void record(){
nnguyen99 3:7486869c8027 43 /* record state:
nnguyen99 3:7486869c8027 44 Initiated by rotary switch
nnguyen99 3:7486869c8027 45 1. Cease all motion
nnguyen99 3:7486869c8027 46 2. Begin recording ToF inputs (distance and time)
nnguyen99 3:7486869c8027 47 3. Convert distances to corresponding audio frequencies
nnguyen99 3:7486869c8027 48 4. Append to list of frequencies
nnguyen99 3:7486869c8027 49 5.
nnguyen99 3:7486869c8027 50 */
nnguyen99 3:7486869c8027 51 }
nnguyen99 3:7486869c8027 52 void play(){
nnguyen99 3:7486869c8027 53 /* play state:
nnguyen99 3:7486869c8027 54 Initiated by rotary switch
nnguyen99 3:7486869c8027 55 1. wait a few seconds
nnguyen99 3:7486869c8027 56 2. begin reading list of frequencies while concurrently:
nnguyen99 3:7486869c8027 57 - moving servo motors accordingly
nnguyen99 3:7486869c8027 58 - playing corresponding sounds
nnguyen99 3:7486869c8027 59 */
nnguyen99 3:7486869c8027 60 }
nnguyen99 3:7486869c8027 61 void erase(){
nnguyen99 3:7486869c8027 62 /* erase state:
nnguyen99 3:7486869c8027 63 erases entire audio recording
nnguyen99 3:7486869c8027 64 */
nnguyen99 3:7486869c8027 65 }
nnguyen99 3:7486869c8027 66
trmontgomery 4:1790aa9234a3 67 void state_machine_mgr(){
nnguyen99 3:7486869c8027 68 sm_state curr_state = sRESET;
nnguyen99 3:7486869c8027 69 while(1) {
nnguyen99 3:7486869c8027 70 switch(curr_state){
nnguyen99 3:7486869c8027 71 case sRESET:
nnguyen99 3:7486869c8027 72 if(S) curr_state = sSTOP;
nnguyen99 3:7486869c8027 73 break;
nnguyen99 3:7486869c8027 74 case sSTOP:
nnguyen99 3:7486869c8027 75 if(X){
nnguyen99 3:7486869c8027 76 curr_state = sRESET;
nnguyen99 3:7486869c8027 77 }else if(S&R){
nnguyen99 3:7486869c8027 78 curr_state = sRECORD;
nnguyen99 3:7486869c8027 79 }else if(S&P){
nnguyen99 3:7486869c8027 80 curr_state = sPLAY;
nnguyen99 3:7486869c8027 81 }
nnguyen99 3:7486869c8027 82 break;
nnguyen99 3:7486869c8027 83 case sRECORD:
nnguyen99 3:7486869c8027 84 if(X){
nnguyen99 3:7486869c8027 85 curr_state = sRESET;
nnguyen99 3:7486869c8027 86 }else if(R&S){
nnguyen99 3:7486869c8027 87 curr_state = sSTOP;
nnguyen99 3:7486869c8027 88 }else if(R&P){
nnguyen99 3:7486869c8027 89 curr_state = sPLAY;
nnguyen99 3:7486869c8027 90 }
nnguyen99 3:7486869c8027 91 break;
nnguyen99 3:7486869c8027 92 case sPLAY:
nnguyen99 3:7486869c8027 93 if(X){
nnguyen99 3:7486869c8027 94 curr_state = sRESET;
nnguyen99 3:7486869c8027 95 }else if(P&S){
nnguyen99 3:7486869c8027 96 curr_state = sSTOP;
nnguyen99 3:7486869c8027 97 }else if(P&R){
nnguyen99 3:7486869c8027 98 curr_state = sPLAY;
nnguyen99 3:7486869c8027 99 }
nnguyen99 3:7486869c8027 100 break;
nnguyen99 3:7486869c8027 101 case sERASE:
nnguyen99 3:7486869c8027 102 if(X){
nnguyen99 3:7486869c8027 103 curr_state = sRESET;
nnguyen99 3:7486869c8027 104 }
nnguyen99 3:7486869c8027 105 }
nnguyen99 3:7486869c8027 106 }
nnguyen99 3:7486869c8027 107 }
trmontgomery 4:1790aa9234a3 108
trmontgomery 4:1790aa9234a3 109 int main() {
trmontgomery 19:f76e4ffddbe1 110 //state_machine_mgr();
trmontgomery 19:f76e4ffddbe1 111 //motor_ctl();
trmontgomery 19:f76e4ffddbe1 112 record_and_play();
trmontgomery 4:1790aa9234a3 113 }