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 12:46:27 2015 +0000
Revision:
4:be465e9a12cb
Parent:
3:4c93be3a9010
Child:
5:8ae6d935a16a
working with better values

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