motor controller with P velocity control
Dependencies: HIDScope PID QEI mbed EMG
Fork of PID_VelocityExample by
Diff: main.cpp
- Revision:
- 2:b2ccd9f044bb
- Parent:
- 1:ac598811dd00
- Child:
- 3:4c93be3a9010
diff -r ac598811dd00 -r b2ccd9f044bb main.cpp --- a/main.cpp Sat Nov 27 11:37:20 2010 +0000 +++ b/main.cpp Thu Sep 24 11:20:14 2015 +0000 @@ -15,20 +15,21 @@ //****************************************************************************/ // Globals //****************************************************************************/ +Serial pc(USBTX, USBRX); //-------- // Motors //-------- //Left motor. -PwmOut leftMotor(p23); -DigitalOut leftBrake(p19); -DigitalOut leftDirection(p28); -QEI leftQei(p29, p30, NC, 624); +PwmOut leftMotor(D5); +DigitalOut leftBrake(D3); +DigitalOut leftDirection(D4); +QEI leftQei(D12, D13, NC, 624); PID leftController(Kc, Ti, Td, RATE); //------- // Files //------- -LocalFileSystem local("local"); -FILE* fp; +//LocalFileSystem local("local"); +//FILE* fp; //-------- // Timers //-------- @@ -76,29 +77,33 @@ initializePidControllers(); //Open results file. - fp = fopen("/local/pidtest.csv", "w"); + //fp = fopen("/local/pidtest.csv", "w"); endTimer.start(); //Set velocity set point. leftController.setSetPoint(goal); - + leftMotor.period_ms(0.1); + //Run for 3 seconds. while (endTimer.read() < 3.0){ leftPulses = leftQei.getPulses(); leftVelocity = (leftPulses - leftPrevPulses) / RATE; leftPrevPulses = leftPulses; + leftController.setProcessValue(leftVelocity); leftPwmDuty = leftController.compute(); leftMotor = leftPwmDuty; - fprintf(fp, "%f,%f\n", leftVelocity, goal); + pc.printf("leftpusles: %d, lefVelocity: %f, leftPwmDuty: %f \n\r",leftPulses,leftVelocity,leftPwmDuty); + + //fprintf(fp, "%f,%f\n", leftVelocity, goal); wait(RATE); } //Stop motors. - leftMotor = 1.0; + leftMotor = 0; //Close results file. - fclose(fp); + //fclose(fp); }