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:
- 17:209ac0b10ba1
- Parent:
- 16:d426b65b4ace
- Child:
- 18:19d35daed140
--- a/main.cpp Fri Mar 24 15:11:39 2017 +0000
+++ b/main.cpp Fri Mar 24 16:29:01 2017 +0000
@@ -116,6 +116,7 @@
float drive_vel = 0.0f;
float vel_target = 0.0f; //Target Speed
+
float vel_max = 100; //Maximum Speed at 3.0V achievable is ~60 rps
//Position Variables
@@ -126,8 +127,8 @@
float partial_rev = 0.0f;
float rev_target = 0.0f; //Target Rotations
-uint32_t revstates_max = 0xFFFFFFFF;
-
+uint8_t revstates_max = 0;
+
//Debug Variables
bool flag = false;
float test_time = 0.0f;
@@ -221,14 +222,14 @@
motorOut(driveto);
while(!completed){
-
- //pc.printf("dutyout: %f \r\n", dutyout);
+ pc.printf("dutyout: %f \r\n", dutyout);
//pc.printf("Error: %f \r\n", (rev_target - total_rev));
- pc.printf("DutyA: %f \r\n", a);
- pc.printf("DutyB: %f \r\n", b);
+ //pc.printf("DutyA: %f \r\n", a);
+ //pc.printf("DutyB: %f \r\n", b);
//pc.printf("\n");
}
+ pc.printf("Thread finished \r\n");
}
@@ -268,8 +269,8 @@
// Measure number of revolutions
count++;
- //Turn-off when target reached
- if(total_rev >= rev_target){
+ //Turn-off when target reached (if target is 0, never end)
+ if( rev_target && total_rev >= rev_target){
completed = 1;
dutyout = 0;
motorOut(0);
@@ -285,9 +286,6 @@
}
void pid_isr(){
-
- TIME = 1;
-
//117 Pulses per revolution
pulse_count++;
@@ -317,10 +315,7 @@
else{
velocity_pid.setProcessValue(partial_vel);
dutyout = velocity_pid.compute();
- }
-
- TIME = 0;
-
+ }
}
/*__________________________Main Function_____________________________________*/
@@ -337,14 +332,22 @@
int v_loc=0;
char buf[80];
+ bool note_marker;
+ bool dur_marker;
+ bool accent_marker;
+ string note="";
+ int duration=0;
string input;
-
+ float2 cmd_set;
while(1){
r=0;
v=0;
r_val=true;
v_val=true;
+ note_marker=false;
+ dur_marker=false;
+ accent_marker=false;
pc.printf("Please enter something\r\n");
pc.scanf("%s",&buf);
input=buf;
@@ -357,10 +360,43 @@
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){ //if melody marker present
- pc.printf("Note sequence detected\r\n");
- }
-
+ if(t_loc==0 && input.length()>1){ //if melody marker present and there is something after it
+ printf("Note sequence detected\r\n");
+
+ for(int i=1;i<input.length();i++){
+
+ if(accent_marker==false && dur_marker==false && note_marker==false &&
+ (input[i]=='A' || input[i]=='B' || input[i]=='C' || input[i]=='D' || input[i]=='E' || input[i]=='F' || input[i]=='G')){
+ note_marker=true;
+ }
+ else if(note_marker==true && (input[i]=='^' || input[i]=='#')){
+ accent_marker=true;
+ }
+ else if((note_marker==true && isdigit(input[i]) && accent_marker==false) ||
+ (note_marker==true && isdigit(input[i]) && accent_marker==true)){
+ dur_marker=true;
+ }
+
+ 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());
+ duration=atof(input.substr(i,1).c_str());
+ printf("Duration is %d\r\n",duration);
+ note_marker=false;
+ dur_marker=false;
+ accent_marker=false;
+ }
+ 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());
+ duration=atof(input.substr(i,1).c_str());
+ printf("Duration is %d\r\n",duration);
+ note_marker=false;
+ dur_marker=false;
+ accent_marker=false;
+ }
+ }
+ }
else if(t_loc==-1){ //if no melody marker present
pc.printf("Note sequence NOT detected\r\n");
@@ -375,7 +411,16 @@
if(r_val==true){
r=atof(input.substr(1).c_str());
+ cmd_set = (float2){r,vel_max};
+ if (pid_thread.get_state() == Thread::Running){
+ pc.printf("Thread running...Terminating \r\n");
+ pid_thread.terminate();
+ dutyout = 0.0;
+ }
pc.printf("Spin for %.3f number of rotations\r\n",r);
+ dutyout = 1.0;
+ pid_thread.start(callback(position_control, &cmd_set));
+ pid_thread.join();
}
else{
pc.printf("Invalid input\r\n");
@@ -402,7 +447,17 @@
v=abs(v);
}
+ cmd_set = (float2){r,v};
+ if (pid_thread.get_state() == Thread::Running){
+ pc.printf("Thread running...Terminating \r\n");
+ pid_thread.terminate();
+ dutyout = 0.0;
+ }
pc.printf("Spin for %.3f number of rotations at %.3f speed \r\n",r,v);
+ dutyout = 1.0;
+ pid_thread.start(callback(position_control, &cmd_set));
+ pid_thread.join();
+ pc.printf("Thread done. \r\n");
}
else{
pc.printf("Invalid input\r\n");
@@ -417,7 +472,17 @@
}
if(v_val==true){
v=atof(input.substr(1).c_str());
+
+ cmd_set = (float2){revstates_max,v};
+ if (pid_thread.get_state() == Thread::Running){
+ pc.printf("Thread running...Terminating \r\n");
+ pid_thread.terminate();
+ dutyout = 0.0;
+ }
pc.printf("Spin at %.3f speed\r\n",v);
+ dutyout = 1.0;
+ pid_thread.start(callback(position_control, &cmd_set));
+ pid_thread.join();
}
else{
pc.printf("Invalid input\r\n");
@@ -453,9 +518,9 @@
InterruptCHA.rise(&pid_isr);
//Initial Target Settings
- float rotation_set = 100.00;
- float velocity_set = 10.00;
- float2 cmd_set = {rotation_set,velocity_set};
+ //float rotation_set = 100.00;
+ //float velocity_set = 10.00;
+
// Melody in a Thread
// PID in Thread
@@ -463,9 +528,10 @@
//float rotation_set = revstates_max;
//float velocity_set = vel_max;
- pid_thread.start(callback(position_control, &cmd_set));
- //serial_com();
+ //pid_thread.join();
+ dutyout = 1.0;
+ serial_com();
}
