Rachel Ireland-Jones / Mbed OS FinalYear0
Committer:
Mikebob
Date:
Tue Nov 19 15:21:36 2019 +0000
Revision:
12:3abb17739c2e
Parent:
11:475b412bbc3c
Child:
13:29a994566dc5
needs comments;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mikebob 7:9c77eca4158a 1 /*
Mikebob 12:3abb17739c2e 2 Our version
Mikebob 7:9c77eca4158a 3 */
Mikebob 7:9c77eca4158a 4 #include "mbed.h"
Mikebob 7:9c77eca4158a 5
Mikebob 7:9c77eca4158a 6 //Status LED
Mikebob 7:9c77eca4158a 7 DigitalOut led(LED1);
Mikebob 7:9c77eca4158a 8
Mikebob 7:9c77eca4158a 9 //Motor PWM (speed)
Mikebob 7:9c77eca4158a 10 PwmOut PWMA(PA_8);
Mikebob 7:9c77eca4158a 11 PwmOut PWMB(PB_4);
Mikebob 7:9c77eca4158a 12
Mikebob 7:9c77eca4158a 13 //Motor Direction
Mikebob 7:9c77eca4158a 14 DigitalOut DIRA(PA_9);
Mikebob 7:9c77eca4158a 15 DigitalOut DIRB(PB_10);
Mikebob 7:9c77eca4158a 16
Mikebob 7:9c77eca4158a 17 //Hall-Effect Sensor Inputs
Mikebob 7:9c77eca4158a 18 DigitalIn HEA1(PB_2);
Mikebob 7:9c77eca4158a 19 DigitalIn HEA2(PB_1);
Mikebob 7:9c77eca4158a 20 DigitalIn HEB1(PB_15);
Mikebob 7:9c77eca4158a 21 DigitalIn HEB2(PB_14);
jaffacat 5:fdc550ee3b6e 22
Mikebob 7:9c77eca4158a 23
Mikebob 7:9c77eca4158a 24 //On board switch
Mikebob 7:9c77eca4158a 25 DigitalIn SW1(USER_BUTTON);
Mikebob 7:9c77eca4158a 26
Mikebob 7:9c77eca4158a 27 //Use the serial object so we can use higher speeds
Mikebob 7:9c77eca4158a 28 Serial terminal(USBTX, USBRX);
Mikebob 7:9c77eca4158a 29
Mikebob 7:9c77eca4158a 30 //Timer used for measuring speeds
Mikebob 7:9c77eca4158a 31 Timer timer;
Mikebob 7:9c77eca4158a 32 Timer timer1;
Mikebob 7:9c77eca4158a 33
Mikebob 7:9c77eca4158a 34 //Enumerated types
Mikebob 7:9c77eca4158a 35 enum DIRECTION {FORWARD=0, REVERSE};
Mikebob 7:9c77eca4158a 36 enum PULSE {NOPULSE=0, PULSE};
Mikebob 7:9c77eca4158a 37 enum SWITCHSTATE {PRESSED=0, RELEASED};
Mikebob 7:9c77eca4158a 38
Mikebob 7:9c77eca4158a 39 //Debug GPIO
Mikebob 7:9c77eca4158a 40 DigitalOut probe(D10);
Mikebob 7:9c77eca4158a 41
Mikebob 7:9c77eca4158a 42 //Duty cycles
Mikebob 7:9c77eca4158a 43 float dutyA = 1.0f; //100%
Mikebob 7:9c77eca4158a 44 float dutyB = 1.0f; //100%
Mikebob 7:9c77eca4158a 45 //Array of sensor data
Mikebob 7:9c77eca4158a 46 int tA1[2];
Mikebob 7:9c77eca4158a 47 int tA2[2];
Mikebob 7:9c77eca4158a 48 float dis;
Mikebob 8:f2b6ef8c5eb8 49 float trav =0;
jaffacat 5:fdc550ee3b6e 50
Mikebob 7:9c77eca4158a 51 void time()
Mikebob 7:9c77eca4158a 52 {
Mikebob 7:9c77eca4158a 53 //Reset timer and Start
Mikebob 7:9c77eca4158a 54 timer.reset();
Mikebob 7:9c77eca4158a 55 timer.start();
Mikebob 7:9c77eca4158a 56 //*********************************************************************
Mikebob 7:9c77eca4158a 57 //FIRST TIME - SYNCHRONISE (YOU SHOULD NOT NEED THIS ONCE IT's RUNNING)
Mikebob 7:9c77eca4158a 58 //*********************************************************************
Mikebob 7:9c77eca4158a 59
Mikebob 7:9c77eca4158a 60 //Wait for rising edge of A1 and log time
Mikebob 7:9c77eca4158a 61 while (HEA1 == NOPULSE);
Mikebob 7:9c77eca4158a 62 //Wait for rising edge of A2 and log time (30 degrees?)
Mikebob 7:9c77eca4158a 63 while (HEA2 == NOPULSE);
Mikebob 7:9c77eca4158a 64 //Wait for falling edge of A1
Mikebob 7:9c77eca4158a 65 while (HEA1 == PULSE);
Mikebob 7:9c77eca4158a 66 //Wait for falling edge of A2
Mikebob 7:9c77eca4158a 67 while (HEA2 == PULSE);
Mikebob 7:9c77eca4158a 68
Mikebob 7:9c77eca4158a 69 //**********************
Mikebob 7:9c77eca4158a 70 //TIME THE FULL SEQUENCE
Mikebob 7:9c77eca4158a 71 //**********************
Mikebob 7:9c77eca4158a 72
Mikebob 7:9c77eca4158a 73 //Wait for rising edge of A1 and log time
Mikebob 7:9c77eca4158a 74 while (HEA1 == NOPULSE);
Mikebob 7:9c77eca4158a 75 tA1[0] = timer.read_us();
Mikebob 7:9c77eca4158a 76
Mikebob 7:9c77eca4158a 77 //Wait for rising edge of A2 and log time (30 degrees?)
Mikebob 7:9c77eca4158a 78 while (HEA2 == NOPULSE);
Mikebob 7:9c77eca4158a 79 tA2[0] = timer.read_us();
Mikebob 7:9c77eca4158a 80
Mikebob 7:9c77eca4158a 81 //Wait for falling edge of A1
Mikebob 7:9c77eca4158a 82 while (HEA1 == PULSE);
Mikebob 7:9c77eca4158a 83 tA1[1] = timer.read_us();
Mikebob 7:9c77eca4158a 84
Mikebob 7:9c77eca4158a 85 //Wait for falling edge of A2
Mikebob 7:9c77eca4158a 86 while (HEA2 == PULSE);
Mikebob 7:9c77eca4158a 87 tA2[1] = timer.read_us();
Mikebob 7:9c77eca4158a 88
Mikebob 7:9c77eca4158a 89
Mikebob 7:9c77eca4158a 90 terminal.printf("tA1(0) = %d\n", tA1[0]);
Mikebob 7:9c77eca4158a 91 terminal.printf("tA1(1) = %d\n", tA1[1]);
Mikebob 7:9c77eca4158a 92 terminal.printf("tA2(0) = %d\n", tA2[0]);
Mikebob 7:9c77eca4158a 93 terminal.printf("tA2(1) = %d\n", tA2[1]);
Mikebob 7:9c77eca4158a 94
Mikebob 7:9c77eca4158a 95 //Calculate the frequency of rotation
Mikebob 7:9c77eca4158a 96 float TA1 = 2.0f * (tA1[1]-tA1[0]);
Mikebob 7:9c77eca4158a 97 float TA2 = 2.0f * (tA2[1]-tA2[0]);
Mikebob 7:9c77eca4158a 98 float TA = (TA1 + TA2) * 0.5f;
jaffacat 5:fdc550ee3b6e 99
Mikebob 7:9c77eca4158a 100 dis = timer1.read_us();
Mikebob 7:9c77eca4158a 101 float mm = ((TA*3)*20.8)/175.9;
Mikebob 10:1637fce7ef1e 102 trav = dis/mm;
Mikebob 7:9c77eca4158a 103 float fA = 1.0f/ (TA *(float)3.0E-6);
Mikebob 8:f2b6ef8c5eb8 104 terminal.printf("Average A2 Shaft: %6.2fHz \t Wheel: %6.2f \t trav: %6.2f\n", fA, fA/20.8f, trav);
Mikebob 7:9c77eca4158a 105 }
Mikebob 7:9c77eca4158a 106
Mikebob 10:1637fce7ef1e 107 void reset()
Mikebob 10:1637fce7ef1e 108 {
Mikebob 10:1637fce7ef1e 109 timer1.reset();
Mikebob 10:1637fce7ef1e 110 time();
Mikebob 10:1637fce7ef1e 111 }
Mikebob 10:1637fce7ef1e 112
Mikebob 7:9c77eca4158a 113 int main()
Mikebob 7:9c77eca4158a 114 {
jaffacat 5:fdc550ee3b6e 115
Mikebob 7:9c77eca4158a 116 //Configure the terminal to high speed
Mikebob 7:9c77eca4158a 117 terminal.baud(115200);
Mikebob 7:9c77eca4158a 118
Mikebob 7:9c77eca4158a 119 //Set initial motor direction
Mikebob 7:9c77eca4158a 120 DIRA = FORWARD;
Mikebob 7:9c77eca4158a 121 DIRB = FORWARD;
Mikebob 7:9c77eca4158a 122
Mikebob 7:9c77eca4158a 123 //Set motor period to 100Hz
Mikebob 7:9c77eca4158a 124 PWMA.period_ms(10);
Mikebob 7:9c77eca4158a 125 PWMB.period_ms(10);
Mikebob 7:9c77eca4158a 126
Mikebob 7:9c77eca4158a 127 //Set initial motor speed to stop
Mikebob 7:9c77eca4158a 128 PWMA.write(0.0f); //0% duty cycle
Mikebob 7:9c77eca4158a 129 PWMB.write(0.0f); //0% duty cycle
Mikebob 7:9c77eca4158a 130
Mikebob 7:9c77eca4158a 131 //Wait for USER button (blue pull-down switch) to start
Mikebob 7:9c77eca4158a 132 terminal.puts("Press USER button to start");
Mikebob 7:9c77eca4158a 133 led = 0;
Mikebob 7:9c77eca4158a 134 while (SW1 == RELEASED);
Mikebob 7:9c77eca4158a 135 led = 1;
Mikebob 7:9c77eca4158a 136
Mikebob 7:9c77eca4158a 137 //Set initial motor speed to stop
Mikebob 7:9c77eca4158a 138 PWMA.write(0.0f); //Set duty cycle (%)
Mikebob 7:9c77eca4158a 139 PWMB.write(0.0f); //Set duty cycle (%)
Mikebob 7:9c77eca4158a 140
Mikebob 7:9c77eca4158a 141 //Wait - give time to start running
Mikebob 7:9c77eca4158a 142 wait(1.0);
Mikebob 10:1637fce7ef1e 143 timer1.reset();
Mikebob 10:1637fce7ef1e 144 timer1.start();
Mikebob 7:9c77eca4158a 145 //Main polling loop
Mikebob 7:9c77eca4158a 146 while(1)
Mikebob 7:9c77eca4158a 147 {
Mikebob 8:f2b6ef8c5eb8 148 while(trav <= 1250)
Mikebob 7:9c77eca4158a 149 {
Mikebob 7:9c77eca4158a 150 PWMA.write(dutyA); //Set duty cycle y
Mikebob 7:9c77eca4158a 151 PWMB.write(dutyB);
Mikebob 7:9c77eca4158a 152 time();
Mikebob 10:1637fce7ef1e 153 }
Mikebob 10:1637fce7ef1e 154 reset();
Mikebob 9:45ae94cf5e23 155 while(trav <= 330)
Mikebob 7:9c77eca4158a 156 {
Mikebob 8:f2b6ef8c5eb8 157 PWMA.write(dutyA);
Mikebob 8:f2b6ef8c5eb8 158 PWMB.write(0.0f);
Mikebob 7:9c77eca4158a 159 time();
Mikebob 7:9c77eca4158a 160 }
Mikebob 10:1637fce7ef1e 161 reset();
Mikebob 9:45ae94cf5e23 162 while(trav <= 1457)
Mikebob 7:9c77eca4158a 163 {
Mikebob 7:9c77eca4158a 164 PWMA.write(dutyA);
Mikebob 7:9c77eca4158a 165 PWMB.write(dutyB);
Mikebob 7:9c77eca4158a 166 time();
Mikebob 7:9c77eca4158a 167 }
Mikebob 10:1637fce7ef1e 168 reset();
Mikebob 9:45ae94cf5e23 169 while(trav <= 268)
Mikebob 7:9c77eca4158a 170 {
Mikebob 8:f2b6ef8c5eb8 171 PWMA.write(dutyA);
Mikebob 8:f2b6ef8c5eb8 172 PWMB.write(0.0f);
Mikebob 7:9c77eca4158a 173 time();
Mikebob 7:9c77eca4158a 174 }
Mikebob 10:1637fce7ef1e 175 reset();
Mikebob 9:45ae94cf5e23 176 while(trav <= 750)
Mikebob 7:9c77eca4158a 177 {
Mikebob 7:9c77eca4158a 178 PWMA.write(dutyA);
Mikebob 7:9c77eca4158a 179 PWMB.write(dutyB);
Mikebob 7:9c77eca4158a 180 time();
Mikebob 7:9c77eca4158a 181 }
Mikebob 10:1637fce7ef1e 182 reset();
Mikebob 9:45ae94cf5e23 183 while(trav <= 200)
Mikebob 7:9c77eca4158a 184 {
Mikebob 8:f2b6ef8c5eb8 185 PWMA.write(dutyA);
Mikebob 8:f2b6ef8c5eb8 186 PWMB.write(0.0f);
Mikebob 7:9c77eca4158a 187 time();
Mikebob 7:9c77eca4158a 188 }
Mikebob 7:9c77eca4158a 189 timer.stop();
Mikebob 7:9c77eca4158a 190 break;
Mikebob 7:9c77eca4158a 191 }
Mikebob 11:475b412bbc3c 192 PWMA.write(0.0f);
Mikebob 11:475b412bbc3c 193 PWMB.write(0.0f);
Mikebob 7:9c77eca4158a 194 }
Mikebob 7:9c77eca4158a 195