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
main.cpp
- Committer:
- rjs718
- Date:
- 2019-03-07
- Revision:
- 4:07e5212862ab
- Parent:
- 3:4d7ff98b0e96
- Child:
- 5:bb8e6f2cfb3a
File content as of revision 4:07e5212862ab:
#include "mbed.h"
#include "Terminal.h"
#include <cmath>
#include <algorithm>
#define TI 0.001 //1kHz Sample time
#define KP 27.67804f //Proportional gain
#define KI 489.8934f //Integral gain
#define LOWER_BOUNDARY 0.0f //Lower boundary
#define UPPER_BOUNDARY 1.0f //upper boundary
#define LOWERDC_BOUNDARY 0.0f //Lower boundary
#define UPPERDC_BOUNDARY 0.6f //upper boundary
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(LED3);
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*err + KI*errorArea;
control_out = min(max(LOWERDC_BOUNDARY, errorArea),UPPERDC_BOUNDARY);
PWM_out.write(control_out);
}
int main() {
PWM_out.period(.005);
Time_Wizard.attach(&Adjusting, TI);
while(1) {
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("\ntachADC/Duty: \r\n %f" ,Tach_in.read() );
wait(.05);
}
}