Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 21:828582e4d4ef
- Parent:
- 20:d796667e0c4d
- Child:
- 22:997c013e0f13
diff -r d796667e0c4d -r 828582e4d4ef main.cpp --- a/main.cpp Fri Mar 24 20:15:40 2017 +0000 +++ b/main.cpp Sun Mar 26 15:42:10 2017 +0000 @@ -9,6 +9,7 @@ #include "stdlib.h" #include "math.h" #include <limits> +//#include <vector> /*_________________________________PIN SETUP__________________________________*/ @@ -47,6 +48,8 @@ PwmOut L3L(L3Lpin); PwmOut L3H(L3Hpin); +PwmOut testOut(A5); + //Status LED //DigitalOut led1(LED1); DigitalOut led2(LED2); @@ -73,10 +76,11 @@ typedef struct{ char note; uint8_t sharp; + uint8_t oct; float dur; }note_t; -const float octaveMap[] = {27.50, 30.87, 16.35, 18.35, 20.60, 21.83, 24.50}; + /*________________________Motor Drive States__________________________________*/ @@ -115,7 +119,7 @@ int8_t driveto = 0; //Holds value of new motor drive state //Angular Velocity Variables -float PWM_freq = 0.001f; //500Hz (> Motor LP cut-off frequency = 10Hz) +float PWM_freq = 0.00001f; //100kHz (> Motor LP cut-off frequency = 10Hz) float dutyout = 1.0f; //Initialized at 50% duty cycle float dutyout_max = 1.0f; //Maximum Duty Cycle will enable maximum speed @@ -134,7 +138,7 @@ float partial_rev = 0.0f; float rev_target = 0.0f; //Target Rotations -uint8_t revstates_max = 0; +float revstates_max = 0; //Debug Variables bool flag = false; @@ -169,6 +173,8 @@ if (driveOut & 0x10) L3L.write(dutyout); L3L.period(PWM_freq); if (driveOut & 0x20) L3H = 0; + testOut.write(0.5f); + testOut.period(PWM_freq); } //Convert photointerrupter inputs to a rotor state @@ -176,20 +182,25 @@ return stateMap[InterruptI1.read() + 2*InterruptI2.read() + 4*InterruptI3.read()]; } -float returnNote(note_t n) +const float octaveMap[] = {27.50, 30.87, 16.35, 18.35, 20.60, 21.83, 24.50}; + +float returnNote(const note_t &n) { - unsigned octMult = 4; - //for (uint8_t i=0; i<n.oct; ++i){ - //octMult = octMult << 1; - //} - return 1.0f/( octMult*octaveMap[n.note - 'A']); + + unsigned octMult = 1; + for (uint8_t i=0; i<n.oct; ++i){ + octMult = octMult << 1; + } + return 1.0f/( octMult*octaveMap[n.note - 'A'] ); } void playMelody(note_t* song){ for (int i=0; i<songLen; ++i){ + //pc.printf("Playing note %c with duration %f and period %f\r\n", song[i].note, song[i].dur, returnNote(song[i])); PWM_freq = returnNote(song[i]); wait(song[i].dur); } + PWM_freq = 0.00001f; delete[] song; } @@ -234,8 +245,8 @@ rev_target = rev; vel_target = vel; - pc.printf("rev %f\r\n", rev); - pc.printf("vel %f\r\n", vel); + //pc.printf("rev %f\r\n", rev); + //pc.printf("vel %f\r\n", vel); //Reverses motor direction if forwards rotation requested if (rev_target < 0.0f || vel_target < 0.0f){ @@ -249,16 +260,16 @@ direction = 1; } - pc.printf("vel_target %f\r\n", vel_target); - pc.printf("dir %d\r\n", direction); + //pc.printf("vel_target %f\r\n", vel_target); + //pc.printf("dir %d\r\n", direction); - pc.printf("Waiting for stabilize... %d\r\n", direction); + pc.printf("Waiting for stabilize...\r\n"); dutyout = 0.0f; velocity_pid.reset(); dist_pid.reset(); wait(3); motorHome(); - pc.printf("Restarting... %d\r\n", direction); + pc.printf("Restarting...\r\n"); velocity_pid.reset(); //velocity_pid.setInputLimits(0.0, 50); @@ -400,16 +411,15 @@ t_loc=input.find('T'); r_loc=input.find('R'); v_loc=input.find('V'); - pc.printf("Location of T is %d\r\n",t_loc); - pc.printf("Location of R is %d\r\n",r_loc); - pc.printf("Location of V is %d\r\n",v_loc); + //pc.printf("Location of T is %d\r\n",t_loc); + //pc.printf("Location of R is %d\r\n",r_loc); + //pc.printf("Location of V is %d\r\n",v_loc); if(t_loc==0 && input.length()>1){ //if melody marker present and there is something after it - printf("Note sequence detected\r\n"); + //printf("Note sequence detected\r\n"); //note_t* song = (note_t*)malloc(sizeof(note_t)*input.length()); - note_t* song = new note_t[input.length()]; - songLen = input.length(); - uint8_t noteIter = 0; + note_t* song = new note_t[input.length()/2]; // since each note is at least 2 characters long, max length is string/2 + songLen = 0; for(int i=1;i<input.length();i++){ if(accent_marker==false && dur_marker==false && note_marker==false && @@ -426,40 +436,34 @@ if(note_marker==true && accent_marker==true && dur_marker == true){ note=input.substr(i-2,2); - printf("The note is %s\r\n",note.c_str()); + //printf("The note is %s\r\n",note.c_str()); duration=atof(input.substr(i,1).c_str()); - printf("Duration is %d\r\n",duration); + //printf("Duration is %d\r\n",duration); note_marker=false; dur_marker=false; accent_marker=false; - - song[noteIter].note = note[0]; - song[noteIter].sharp = 1; - song[noteIter].dur = duration; - noteIter++; + note_t newNote = {note[0], 1, 5, duration}; + song[songLen++] = newNote; } else if(note_marker==true && dur_marker==true && accent_marker==false){ note=input.substr(i-1,1); - printf("The note is %s\r\n",note.c_str()); + //printf("The note is %s\r\n",note.c_str()); duration=atof(input.substr(i,1).c_str()); - printf("Duration is %d\r\n",duration); + //printf("Duration is %d\r\n",duration); note_marker=false; dur_marker=false; accent_marker=false; - - song[noteIter].note = note[0]; - song[noteIter].sharp = 0; - song[noteIter].dur = duration; - noteIter++; + note_t newNote = {note[0], 1, 5, duration}; + song[songLen++] = newNote; } } melody_thread.start(callback(playMelody, song)); } else if(t_loc==-1){ //if no melody marker present - pc.printf("Note sequence NOT detected\r\n"); + //pc.printf("Note sequence NOT detected\r\n"); if(r_loc==0 && v_loc==-1 && input.length()>1){ //check if first letter is R - pc.printf("Checking for sole R input type...\r\n"); + //pc.printf("Checking for sole R input type...\r\n"); for(int j=1; j<input.length();j++){ if(!isdigit(input[j]) && input[j]!='-' && input[j]!='.'){ @@ -477,7 +481,7 @@ } } else if(r_loc==0 && v_loc!=-1 && v_loc < input.length()-1){ //check if first letter is R and V is also present - pc.printf("Checking for combined R and V input type...\r\n"); + //pc.printf("Checking for combined R and V input type...\r\n"); for(int j=1; j<v_loc;j++){ if(!isdigit(input[j]) && input[j]!='-' && input[j]!='.'){ @@ -505,7 +509,7 @@ } } else if(v_loc==0 && input.length()>1){ //check if first letter is V - pc.printf("Checking for sole V input type...\r\n"); + //pc.printf("Checking for sole V input type...\r\n"); for(int j=1; j<input.length();j++){ if(!isdigit(input[j]) && input[j]!='-' && input[j]!='.'){ v_val=false; @@ -536,7 +540,7 @@ //Run the motor synchronisation: orState is subtracted from future rotor state inputs orState = motorHome(); - pc.printf("Synchronization Complete: Rotor and Motor aligned with Offset: %d \r\n",orState); + //pc.printf("Synchronization Complete: Rotor and Motor aligned with Offset: %d\r\n", orState); //Interrupts (Optical Disk State Change): Drives to next state, Measures whole revolution count, Measures angular velocity over a whole revolution InterruptI1.rise(&changestate_isr);