motor controller with P velocity control

Dependencies:   HIDScope PID QEI mbed EMG

Fork of PID_VelocityExample by Aaron Berk

Committer:
ewoud
Date:
Thu Sep 24 11:20:14 2015 +0000
Revision:
2:b2ccd9f044bb
Parent:
1:ac598811dd00
Child:
3:4c93be3a9010
made working for own setup

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aberk 0:9bca35ae9c6b 1 //****************************************************************************/
aberk 0:9bca35ae9c6b 2 // Includes
aberk 0:9bca35ae9c6b 3 //****************************************************************************/
aberk 0:9bca35ae9c6b 4 #include "PID.h"
aberk 0:9bca35ae9c6b 5 #include "QEI.h"
aberk 0:9bca35ae9c6b 6
aberk 0:9bca35ae9c6b 7 //****************************************************************************/
aberk 0:9bca35ae9c6b 8 // Defines
aberk 0:9bca35ae9c6b 9 //****************************************************************************/
aberk 0:9bca35ae9c6b 10 #define RATE 0.01
aberk 0:9bca35ae9c6b 11 #define Kc -2.6
aberk 0:9bca35ae9c6b 12 #define Ti 0.0
aberk 0:9bca35ae9c6b 13 #define Td 0.0
aberk 0:9bca35ae9c6b 14
aberk 0:9bca35ae9c6b 15 //****************************************************************************/
aberk 0:9bca35ae9c6b 16 // Globals
aberk 0:9bca35ae9c6b 17 //****************************************************************************/
ewoud 2:b2ccd9f044bb 18 Serial pc(USBTX, USBRX);
aberk 0:9bca35ae9c6b 19 //--------
aberk 0:9bca35ae9c6b 20 // Motors
aberk 0:9bca35ae9c6b 21 //--------
aberk 0:9bca35ae9c6b 22 //Left motor.
ewoud 2:b2ccd9f044bb 23 PwmOut leftMotor(D5);
ewoud 2:b2ccd9f044bb 24 DigitalOut leftBrake(D3);
ewoud 2:b2ccd9f044bb 25 DigitalOut leftDirection(D4);
ewoud 2:b2ccd9f044bb 26 QEI leftQei(D12, D13, NC, 624);
aberk 0:9bca35ae9c6b 27 PID leftController(Kc, Ti, Td, RATE);
aberk 0:9bca35ae9c6b 28 //-------
aberk 0:9bca35ae9c6b 29 // Files
aberk 0:9bca35ae9c6b 30 //-------
ewoud 2:b2ccd9f044bb 31 //LocalFileSystem local("local");
ewoud 2:b2ccd9f044bb 32 //FILE* fp;
aberk 0:9bca35ae9c6b 33 //--------
aberk 0:9bca35ae9c6b 34 // Timers
aberk 0:9bca35ae9c6b 35 //--------
aberk 0:9bca35ae9c6b 36 Timer endTimer;
aberk 0:9bca35ae9c6b 37 //--------------------
aberk 0:9bca35ae9c6b 38 // Working variables.
aberk 0:9bca35ae9c6b 39 //--------------------
aberk 0:9bca35ae9c6b 40 volatile int leftPulses = 0;
aberk 0:9bca35ae9c6b 41 volatile int leftPrevPulses = 0;
aberk 0:9bca35ae9c6b 42 volatile float leftPwmDuty = 1.0;
aberk 0:9bca35ae9c6b 43 volatile float leftVelocity = 0.0;
aberk 0:9bca35ae9c6b 44 //Velocity to reach.
aberk 0:9bca35ae9c6b 45 int goal = 3000;
aberk 0:9bca35ae9c6b 46
aberk 0:9bca35ae9c6b 47 //****************************************************************************/
aberk 0:9bca35ae9c6b 48 // Prototypes
aberk 0:9bca35ae9c6b 49 //****************************************************************************/
aberk 0:9bca35ae9c6b 50 //Set motors to go "forward", brake off, not moving.
aberk 0:9bca35ae9c6b 51 void initializeMotors(void);
aberk 0:9bca35ae9c6b 52 //Set up PID controllers with appropriate limits and biases.
aberk 0:9bca35ae9c6b 53 void initializePidControllers(void);
aberk 0:9bca35ae9c6b 54
aberk 0:9bca35ae9c6b 55 void initializeMotors(void){
aberk 0:9bca35ae9c6b 56
aberk 0:9bca35ae9c6b 57 leftMotor.period_us(50);
aberk 0:9bca35ae9c6b 58 leftMotor = 1.0;
aberk 0:9bca35ae9c6b 59 leftBrake = 0.0;
aberk 0:9bca35ae9c6b 60 leftDirection = 0;
aberk 0:9bca35ae9c6b 61
aberk 0:9bca35ae9c6b 62 }
aberk 0:9bca35ae9c6b 63
aberk 0:9bca35ae9c6b 64 void initializePidControllers(void){
aberk 0:9bca35ae9c6b 65
aberk 0:9bca35ae9c6b 66 leftController.setInputLimits(0.0, 10500.0);
aberk 0:9bca35ae9c6b 67 leftController.setOutputLimits(0.0, 1.0);
aberk 0:9bca35ae9c6b 68 leftController.setBias(1.0);
aberk 0:9bca35ae9c6b 69 leftController.setMode(AUTO_MODE);
aberk 0:9bca35ae9c6b 70
aberk 0:9bca35ae9c6b 71 }
aberk 0:9bca35ae9c6b 72
aberk 0:9bca35ae9c6b 73 int main() {
aberk 0:9bca35ae9c6b 74
aberk 0:9bca35ae9c6b 75 //Initialization.
aberk 0:9bca35ae9c6b 76 initializeMotors();
aberk 0:9bca35ae9c6b 77 initializePidControllers();
aberk 0:9bca35ae9c6b 78
aberk 0:9bca35ae9c6b 79 //Open results file.
ewoud 2:b2ccd9f044bb 80 //fp = fopen("/local/pidtest.csv", "w");
aberk 0:9bca35ae9c6b 81
aberk 0:9bca35ae9c6b 82 endTimer.start();
aberk 0:9bca35ae9c6b 83
aberk 0:9bca35ae9c6b 84 //Set velocity set point.
aberk 0:9bca35ae9c6b 85 leftController.setSetPoint(goal);
ewoud 2:b2ccd9f044bb 86 leftMotor.period_ms(0.1);
ewoud 2:b2ccd9f044bb 87
aberk 0:9bca35ae9c6b 88 //Run for 3 seconds.
aberk 0:9bca35ae9c6b 89 while (endTimer.read() < 3.0){
aberk 0:9bca35ae9c6b 90 leftPulses = leftQei.getPulses();
aberk 0:9bca35ae9c6b 91 leftVelocity = (leftPulses - leftPrevPulses) / RATE;
aberk 0:9bca35ae9c6b 92 leftPrevPulses = leftPulses;
ewoud 2:b2ccd9f044bb 93
aberk 0:9bca35ae9c6b 94 leftController.setProcessValue(leftVelocity);
aberk 1:ac598811dd00 95 leftPwmDuty = leftController.compute();
aberk 0:9bca35ae9c6b 96 leftMotor = leftPwmDuty;
ewoud 2:b2ccd9f044bb 97 pc.printf("leftpusles: %d, lefVelocity: %f, leftPwmDuty: %f \n\r",leftPulses,leftVelocity,leftPwmDuty);
ewoud 2:b2ccd9f044bb 98
ewoud 2:b2ccd9f044bb 99 //fprintf(fp, "%f,%f\n", leftVelocity, goal);
aberk 0:9bca35ae9c6b 100 wait(RATE);
aberk 0:9bca35ae9c6b 101 }
aberk 0:9bca35ae9c6b 102
aberk 0:9bca35ae9c6b 103 //Stop motors.
ewoud 2:b2ccd9f044bb 104 leftMotor = 0;
aberk 0:9bca35ae9c6b 105
aberk 0:9bca35ae9c6b 106 //Close results file.
ewoud 2:b2ccd9f044bb 107 //fclose(fp);
aberk 0:9bca35ae9c6b 108
aberk 0:9bca35ae9c6b 109 }