Dependencies:   mbed QEI

Committer:
crazycliffy
Date:
Sat Aug 20 17:53:11 2011 +0000
Revision:
0:6fee21a0a314

        

Who changed what in which revision?

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