David Salmon
/
ES_CW2_Starter_MDMA
ES2017 coursework 2
Fork of ES_CW2_Starter by
Diff: main.cpp
- Revision:
- 12:8ea29b18d289
- Parent:
- 11:1f596bf4182b
- Child:
- 13:87ab3b008803
- Child:
- 14:155e9a9147d4
diff -r 1f596bf4182b -r 8ea29b18d289 main.cpp --- a/main.cpp Thu Mar 09 12:17:32 2017 +0000 +++ b/main.cpp Thu Mar 09 13:02:44 2017 +0000 @@ -224,12 +224,16 @@ void VPID() { + controller.setMode(1); while(1) { controller.setSetPoint(revsec); // printf("revsec: %2.3f\r\n", revsec); controller.setProcessValue(measuredRevs); speedControl = controller.compute(); // printf("speed setpoint: %2.3f\r\n", speedControl); + if(speedControl<0) speedControl = -speedControl; + else if (speedControl==0) speedControl = 1; + spinWait = (1/speedControl)/6; Thread::wait(PIDrate); } } @@ -311,6 +315,7 @@ int units = 0, tens = 0, decimals = 0; char ch; testpin=0; + int vartens = 0, varunits = 0, vardecs = 0; speedTimer.reset(); speedTimer.start(); @@ -399,10 +404,10 @@ //Calculate the number of revolutions per second required revsec = float(tens)*10 + float(units) + float(decimals)/10; //Calculate the required wait period - spinWait = (1/revsec)/6; + //Print values for verification - pc.printf("Rev/S: %2.4f, Wait: %2.4f\n\r", revsec, spinWait); + pc.printf("Rev/S: %2.4f\n\r", revsec); //Run the function to start rotating at a fixed speed fixedSpeed(); @@ -417,6 +422,34 @@ case 't': // pc.printf("%d\n\r", pos); break; + + case 'K': + vartens = command[1] - '0'; + varunits = command[2] - '0'; + vardecs = command[4] - '0'; + Kc = float(vartens)*10 + float(varunits) + float(vardecs)/10; + printf("Kc: %2.1f\r\n", Kc); + controller.setTunings(Kc, Ti, Td); +// controller.setMode(1); + break; + case 'i': + vartens = command[1] - '0'; + varunits = command[2] - '0'; + vardecs = command[4] - '0'; + Ti = float(vartens)*10 + float(varunits) + float(vardecs)/10; + printf("Ti: %2.1f\r\n", Ti); + controller.setTunings(Kc, Ti, Td); +// controller.setMode(1); + break; + case 'd': + vartens = command[1] - '0'; + varunits = command[2] - '0'; + vardecs = command[4] - '0'; + Td = float(vartens)*10 + float(varunits) + float(vardecs)/10; + printf("Td: %2.1f\r\n", Td); + controller.setTunings(Kc, Ti, Td); +// controller.setMode(1); + break; default: //Set speed variables to zero to stop motor spinning //Print error message