Students must find QEI library on mbed.org and import library into program. Hint: use QEI by Aaron Berk!

Dependencies:   mbed

Committer:
ddamato31
Date:
Sun Jul 03 09:55:18 2011 +0000
Revision:
0:a8a0b59c162b
DC Motor Speed Control (No QEI Library)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ddamato31 0:a8a0b59c162b 1 #include "mbed.h"
ddamato31 0:a8a0b59c162b 2 #include "QEI.h"
ddamato31 0:a8a0b59c162b 3
ddamato31 0:a8a0b59c162b 4 // Initialize motor PWM signal period and pulsewidth
ddamato31 0:a8a0b59c162b 5 PwmOut speed(p22);
ddamato31 0:a8a0b59c162b 6 DigitalIn SetSpeed(p20);
ddamato31 0:a8a0b59c162b 7 Timer t;
ddamato31 0:a8a0b59c162b 8 QEI encoder (p29, p30, NC, 18);
ddamato31 0:a8a0b59c162b 9 Serial pc(USBTX, USBRX); // tx, rx
ddamato31 0:a8a0b59c162b 10
ddamato31 0:a8a0b59c162b 11 int main() {
ddamato31 0:a8a0b59c162b 12
ddamato31 0:a8a0b59c162b 13 // Initialize motor PWM signal period and pulsewidth
ddamato31 0:a8a0b59c162b 14 speed.period_us(100000); //Experimented to find these numbers
ddamato31 0:a8a0b59c162b 15 speed.pulsewidth_us(10000);
ddamato31 0:a8a0b59c162b 16
ddamato31 0:a8a0b59c162b 17 // Start motor forward at 25% speed
ddamato31 0:a8a0b59c162b 18 int s; s = 25000;
ddamato31 0:a8a0b59c162b 19 speed.pulsewidth_us(s);
ddamato31 0:a8a0b59c162b 20
ddamato31 0:a8a0b59c162b 21 // Aquire set point from user
ddamato31 0:a8a0b59c162b 22 int DutyCycle;
ddamato31 0:a8a0b59c162b 23 while ( (DutyCycle < 25) || (DutyCycle > 100) ){ // Experimented to determine this range
ddamato31 0:a8a0b59c162b 24 pc.printf("Enter a desired duty cycle between 25%% and 90%% as an interger.\n\n");
ddamato31 0:a8a0b59c162b 25 pc.scanf("%i", &DutyCycle);
ddamato31 0:a8a0b59c162b 26 }
ddamato31 0:a8a0b59c162b 27
ddamato31 0:a8a0b59c162b 28 pc.printf("Duty cycle set to %i%%\n\n", DutyCycle);
ddamato31 0:a8a0b59c162b 29 s = (DutyCycle * 1000);
ddamato31 0:a8a0b59c162b 30 speed.pulsewidth_us(s);
ddamato31 0:a8a0b59c162b 31 wait(1);
ddamato31 0:a8a0b59c162b 32
ddamato31 0:a8a0b59c162b 33 // Motor RPM average initialization and index
ddamato31 0:a8a0b59c162b 34 float rpm[5] = {0.0, 0.0, 0.0, 0.0, 0.0};
ddamato31 0:a8a0b59c162b 35 int i; i = 0;
ddamato31 0:a8a0b59c162b 36
ddamato31 0:a8a0b59c162b 37 // Start program loop
ddamato31 0:a8a0b59c162b 38 while(1) {
ddamato31 0:a8a0b59c162b 39
ddamato31 0:a8a0b59c162b 40 // Index and reset Motor Average Counter
ddamato31 0:a8a0b59c162b 41 if(i > 4)
ddamato31 0:a8a0b59c162b 42 i = 0;
ddamato31 0:a8a0b59c162b 43
ddamato31 0:a8a0b59c162b 44 // Revolutions per minute
ddamato31 0:a8a0b59c162b 45 t.start();
ddamato31 0:a8a0b59c162b 46 encoder.reset(); // requires 1_us to execute
ddamato31 0:a8a0b59c162b 47 wait_ms(100); // requires 9.062_ms to execute
ddamato31 0:a8a0b59c162b 48 int pulse10ms = encoder.getPulses(); // requires 3.152 ms to execute for small numbers of pulses (i.e. < 10,000
ddamato31 0:a8a0b59c162b 49 t.stop();
ddamato31 0:a8a0b59c162b 50 pc.printf("Pulses Count is %i in %f ms\n", pulse10ms, t.read()*1000); // requires 13.542 ms to excute
ddamato31 0:a8a0b59c162b 51 rpm[i] = (60*pulse10ms)/(2*18*t.read());
ddamato31 0:a8a0b59c162b 52 float avgrpm = (rpm[0] + rpm[1] + rpm[2] + rpm[3] + rpm[4]) / 5 ;
ddamato31 0:a8a0b59c162b 53 pc.printf("Motor RPM %i= %f\n AVGRPM = %f\n\n", i, rpm[i], avgrpm);
ddamato31 0:a8a0b59c162b 54 ++i;
ddamato31 0:a8a0b59c162b 55 t.reset();
ddamato31 0:a8a0b59c162b 56 wait(1);
ddamato31 0:a8a0b59c162b 57
ddamato31 0:a8a0b59c162b 58 /* Speed Control of Motor ****(CHANGE TO LINE COMMENT TO ADD SPEED CONTROL)******
ddamato31 0:a8a0b59c162b 59
ddamato31 0:a8a0b59c162b 60 // Build DC Motor Table and enter values into variables on the next line
ddamato31 0:a8a0b59c162b 61
ddamato31 0:a8a0b59c162b 62 int rpm20 = , rpm100 = , rpm60 = , rpm70 = , dcY = , rpmLimit = 250;
ddamato31 0:a8a0b59c162b 63 int SetSpeed;
ddamato31 0:a8a0b59c162b 64 static int j = 1;
ddamato31 0:a8a0b59c162b 65
ddamato31 0:a8a0b59c162b 66 if(j > 0){
ddamato31 0:a8a0b59c162b 67 while ( (SetSpeed < rpm60) || (SetSpeed > rpm70) ){
ddamato31 0:a8a0b59c162b 68 pc.printf("Enter a desired RPM between %i and %i as an interger.\n", rpm60, rpm70);
ddamato31 0:a8a0b59c162b 69 pc.scanf("%i", &SetSpeed);
ddamato31 0:a8a0b59c162b 70 pc.printf("RPM Set Point= %i rpm \n\n", SetSpeed);
ddamato31 0:a8a0b59c162b 71 DutyCycle = (80 * SetSpeed / (rpm100 - rpm20)) - dcY; // y = m*x + b (linear motor curve
ddamato31 0:a8a0b59c162b 72 pc.printf("Approx Duty Cycle = %i us \n", DutyCycle);
ddamato31 0:a8a0b59c162b 73 s = (DutyCycle * 1000);
ddamato31 0:a8a0b59c162b 74 pc.printf("Inital Pulse Width = %i us \n\n", s);
ddamato31 0:a8a0b59c162b 75 wait(2);
ddamato31 0:a8a0b59c162b 76 }
ddamato31 0:a8a0b59c162b 77 }
ddamato31 0:a8a0b59c162b 78 j = 0;
ddamato31 0:a8a0b59c162b 79 if ( avgrpm < (SetSpeed - rpmLimit)) {
ddamato31 0:a8a0b59c162b 80 DutyCycle = (DutyCycle + 1);
ddamato31 0:a8a0b59c162b 81 s = (DutyCycle * 1000);
ddamato31 0:a8a0b59c162b 82 pc.printf("Increase Duty Cycle= %i%%\n", DutyCycle);
ddamato31 0:a8a0b59c162b 83 pc.printf("Pulse Width = %i us \n\n", s);
ddamato31 0:a8a0b59c162b 84 speed.pulsewidth_us(s);
ddamato31 0:a8a0b59c162b 85 }
ddamato31 0:a8a0b59c162b 86 else if ( avgrpm > (SetSpeed + rpmLimit)){
ddamato31 0:a8a0b59c162b 87 DutyCycle = (DutyCycle - 1);
ddamato31 0:a8a0b59c162b 88 s = (DutyCycle*1000);
ddamato31 0:a8a0b59c162b 89 pc.printf("Decrease Duty Cycle= %i%%\n", DutyCycle);
ddamato31 0:a8a0b59c162b 90 pc.printf("Pulse Width = %i us \n\n", s);
ddamato31 0:a8a0b59c162b 91 speed.pulsewidth_us(s);
ddamato31 0:a8a0b59c162b 92 }
ddamato31 0:a8a0b59c162b 93 else if ( (avgrpm > (SetSpeed - rpmLimit)) || (avgrpm < (SetSpeed + rpmLimit)) ) {
ddamato31 0:a8a0b59c162b 94 s = DutyCycle * 1000;
ddamato31 0:a8a0b59c162b 95 pc.printf("Stable Duty Cycle= %i%%\n", DutyCycle);
ddamato31 0:a8a0b59c162b 96 pc.printf("Pulse Width = %i us \n\n", s);
ddamato31 0:a8a0b59c162b 97 }
ddamato31 0:a8a0b59c162b 98 else
ddamato31 0:a8a0b59c162b 99
ddamato31 0:a8a0b59c162b 100 return 0;
ddamato31 0:a8a0b59c162b 101
ddamato31 0:a8a0b59c162b 102 */ //****(CHANGE TO LINE COMMENT TO ADD SPEED CONTROL)
ddamato31 0:a8a0b59c162b 103
ddamato31 0:a8a0b59c162b 104 }
ddamato31 0:a8a0b59c162b 105 }