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.
Dependencies: mbed MotorLab3Code
Diff: main.cpp
- Revision:
- 3:4d7ff98b0e96
- Parent:
- 2:060a86df835f
- Child:
- 4:07e5212862ab
diff -r 060a86df835f -r 4d7ff98b0e96 main.cpp
--- a/main.cpp Wed Mar 06 20:21:46 2019 +0000
+++ b/main.cpp Thu Mar 07 00:16:47 2019 +0000
@@ -1,28 +1,48 @@
#include "mbed.h"
-#include "Terminal.h" //header file for terminal screen
+#include "Terminal.h"
+#include <cmath>
+#include <algorithm>
+#define TI 0.0001 //1kHz Sample time
+#define KP 1.0f //Proportional gain
+#define KI 1.0f //Integral gain
+#define LOWER_BOUNDARY 0.0f //Lower boundary
+#define UPPER_BOUNDARY 1.0f //upper boundary
+#define LOWERDC_BOUNDARY 0.4f1 //Lower boundary
+#define UPPERDC_BOUNDARY 1.0f //upper boundary
-Terminal term(USBTX, USBRX);
+volatile float INTEGRAL_ERROR = 0.0;
+
+float errorArea;
+
+Terminal term(USBTX, USBRX);
AnalogIn AI_in(PTB0);
PwmOut PWM_out(PTE21);
-
-
+AnalogIn Tach_in(PTB2); //0-3.3 feedback
+Ticker Time_Wizard;
+DigitalOut Adjustingled
-
-
+void Adjusting(){
+ float feedback = Tach_in.read();
+ float vref = AI_in.read();
+ float err = vref - feedback;
+ errorArea += err * TI;
+ errorArea = min(max(LOWER_BOUNDARY, errorArea),UPPER_BOUNDARY);
+ float control_out = KP*error + KI*errorArea;
+ control_out = min(max(LOWERDC_BOUNDARY, control_out),UPPERDC_BOUNDARY);
+ PWM_out.write(control_out);
+
+ }
int main() {
PWM_out.period(.005);
+ Time_Wizard.attach(&Adjusting, TI);
while(1) {
- if( AI_in.read() < .40){
- PWM_out.write(.40);
- }else{
- PWM_out.write(AI_in.read());
- }
-
+
term.cls();
term.printf("\nVrefV:\r\n %f", 3.3*AI_in.read(), AI_in.read());
term.printf("\nVrefADC/Duty: \r\n %f" ,AI_in.read() );
+ term.printf("\nVrefADC/Duty: \r\n %f" ,Tach_in.read() );
wait(.05);
}