Rachel Ireland-Jones / Mbed OS FinalYear0
Committer:
Mikebob
Date:
Thu Nov 14 16:55:17 2019 +0000
Revision:
7:9c77eca4158a
Parent:
6:c23ecdd1a581
Child:
8:f2b6ef8c5eb8
distance

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mikebob 7:9c77eca4158a 1 /*
Mikebob 7:9c77eca4158a 2
Mikebob 7:9c77eca4158a 3 ** ELEC143 Coursework Sample Code **
Mikebob 7:9c77eca4158a 4
Mikebob 7:9c77eca4158a 5 Author: Dr Nicholas Outram
Mikebob 7:9c77eca4158a 6 Date: 09-10-2015
Mikebob 7:9c77eca4158a 7 Version: 1.0
Mikebob 7:9c77eca4158a 8
Mikebob 7:9c77eca4158a 9 This sample code demonstrates the following:
Mikebob 7:9c77eca4158a 10
Mikebob 7:9c77eca4158a 11 - How to control the motor speed and direction
Mikebob 7:9c77eca4158a 12 - How to measure motor speed using software polling (assuming a forward direction)
Mikebob 7:9c77eca4158a 13
Mikebob 7:9c77eca4158a 14 Notes:
Mikebob 7:9c77eca4158a 15
Mikebob 7:9c77eca4158a 16 1. This code is very monolithic. I have made no attempt to factor this into meaningful and
Mikebob 7:9c77eca4158a 17 reuseable functions. I will be looking to see you factor your code into different functions,
Mikebob 7:9c77eca4158a 18 and maybe even into seperate C (or CPP) files.
Mikebob 7:9c77eca4158a 19
Mikebob 7:9c77eca4158a 20 2. This code is only lightly commented. The commenting is sufficient to
Mikebob 7:9c77eca4158a 21 explain the purpose of each step in the code. Do not assume this is always sufficient.
Mikebob 7:9c77eca4158a 22
Mikebob 7:9c77eca4158a 23 You may adapt this code for your own purposes. However, you may be questioned about any of the
Mikebob 7:9c77eca4158a 24 code you use. Your answers will affect your grade to some extent.
Mikebob 7:9c77eca4158a 25
Mikebob 7:9c77eca4158a 26 */
Mikebob 7:9c77eca4158a 27 #include "mbed.h"
Mikebob 7:9c77eca4158a 28
Mikebob 7:9c77eca4158a 29 //Status LED
Mikebob 7:9c77eca4158a 30 DigitalOut led(LED1);
Mikebob 7:9c77eca4158a 31
Mikebob 7:9c77eca4158a 32 //Motor PWM (speed)
Mikebob 7:9c77eca4158a 33 PwmOut PWMA(PA_8);
Mikebob 7:9c77eca4158a 34 PwmOut PWMB(PB_4);
Mikebob 7:9c77eca4158a 35
Mikebob 7:9c77eca4158a 36 //Motor Direction
Mikebob 7:9c77eca4158a 37 DigitalOut DIRA(PA_9);
Mikebob 7:9c77eca4158a 38 DigitalOut DIRB(PB_10);
Mikebob 7:9c77eca4158a 39
Mikebob 7:9c77eca4158a 40 //Hall-Effect Sensor Inputs
Mikebob 7:9c77eca4158a 41 DigitalIn HEA1(PB_2);
Mikebob 7:9c77eca4158a 42 DigitalIn HEA2(PB_1);
Mikebob 7:9c77eca4158a 43 DigitalIn HEB1(PB_15);
Mikebob 7:9c77eca4158a 44 DigitalIn HEB2(PB_14);
jaffacat 5:fdc550ee3b6e 45
Mikebob 7:9c77eca4158a 46
Mikebob 7:9c77eca4158a 47 //On board switch
Mikebob 7:9c77eca4158a 48 DigitalIn SW1(USER_BUTTON);
Mikebob 7:9c77eca4158a 49
Mikebob 7:9c77eca4158a 50 //Use the serial object so we can use higher speeds
Mikebob 7:9c77eca4158a 51 Serial terminal(USBTX, USBRX);
Mikebob 7:9c77eca4158a 52
Mikebob 7:9c77eca4158a 53 //Timer used for measuring speeds
Mikebob 7:9c77eca4158a 54 Timer timer;
Mikebob 7:9c77eca4158a 55 Timer timer1;
Mikebob 7:9c77eca4158a 56
Mikebob 7:9c77eca4158a 57 //Enumerated types
Mikebob 7:9c77eca4158a 58 enum DIRECTION {FORWARD=0, REVERSE};
Mikebob 7:9c77eca4158a 59 enum PULSE {NOPULSE=0, PULSE};
Mikebob 7:9c77eca4158a 60 enum SWITCHSTATE {PRESSED=0, RELEASED};
Mikebob 7:9c77eca4158a 61
Mikebob 7:9c77eca4158a 62 //Debug GPIO
Mikebob 7:9c77eca4158a 63 DigitalOut probe(D10);
Mikebob 7:9c77eca4158a 64
Mikebob 7:9c77eca4158a 65 //Duty cycles
Mikebob 7:9c77eca4158a 66 float dutyA = 1.0f; //100%
Mikebob 7:9c77eca4158a 67 float dutyB = 1.0f; //100%
Mikebob 7:9c77eca4158a 68 //Array of sensor data
Mikebob 7:9c77eca4158a 69 int tA1[2];
Mikebob 7:9c77eca4158a 70 int tA2[2];
Mikebob 7:9c77eca4158a 71 float dis;
Mikebob 7:9c77eca4158a 72 float distance;
jaffacat 5:fdc550ee3b6e 73
Mikebob 7:9c77eca4158a 74 void time()
Mikebob 7:9c77eca4158a 75 {
Mikebob 7:9c77eca4158a 76 //Reset timer and Start
Mikebob 7:9c77eca4158a 77 timer.reset();
Mikebob 7:9c77eca4158a 78 timer.start();
Mikebob 7:9c77eca4158a 79
Mikebob 7:9c77eca4158a 80 //*********************************************************************
Mikebob 7:9c77eca4158a 81 //FIRST TIME - SYNCHRONISE (YOU SHOULD NOT NEED THIS ONCE IT's RUNNING)
Mikebob 7:9c77eca4158a 82 //*********************************************************************
Mikebob 7:9c77eca4158a 83
Mikebob 7:9c77eca4158a 84 //Wait for rising edge of A1 and log time
Mikebob 7:9c77eca4158a 85 while (HEA1 == NOPULSE);
Mikebob 7:9c77eca4158a 86 //Wait for rising edge of A2 and log time (30 degrees?)
Mikebob 7:9c77eca4158a 87 while (HEA2 == NOPULSE);
Mikebob 7:9c77eca4158a 88 //Wait for falling edge of A1
Mikebob 7:9c77eca4158a 89 while (HEA1 == PULSE);
Mikebob 7:9c77eca4158a 90 //Wait for falling edge of A2
Mikebob 7:9c77eca4158a 91 while (HEA2 == PULSE);
Mikebob 7:9c77eca4158a 92
Mikebob 7:9c77eca4158a 93 //**********************
Mikebob 7:9c77eca4158a 94 //TIME THE FULL SEQUENCE
Mikebob 7:9c77eca4158a 95 //**********************
Mikebob 7:9c77eca4158a 96
Mikebob 7:9c77eca4158a 97 //Wait for rising edge of A1 and log time
Mikebob 7:9c77eca4158a 98 while (HEA1 == NOPULSE);
Mikebob 7:9c77eca4158a 99 tA1[0] = timer.read_us();
Mikebob 7:9c77eca4158a 100
Mikebob 7:9c77eca4158a 101 //Wait for rising edge of A2 and log time (30 degrees?)
Mikebob 7:9c77eca4158a 102 while (HEA2 == NOPULSE);
Mikebob 7:9c77eca4158a 103 tA2[0] = timer.read_us();
Mikebob 7:9c77eca4158a 104
Mikebob 7:9c77eca4158a 105 //Wait for falling edge of A1
Mikebob 7:9c77eca4158a 106 while (HEA1 == PULSE);
Mikebob 7:9c77eca4158a 107 tA1[1] = timer.read_us();
Mikebob 7:9c77eca4158a 108
Mikebob 7:9c77eca4158a 109 //Wait for falling edge of A2
Mikebob 7:9c77eca4158a 110 while (HEA2 == PULSE);
Mikebob 7:9c77eca4158a 111 tA2[1] = timer.read_us();
Mikebob 7:9c77eca4158a 112
Mikebob 7:9c77eca4158a 113
Mikebob 7:9c77eca4158a 114 terminal.printf("tA1(0) = %d\n", tA1[0]);
Mikebob 7:9c77eca4158a 115 terminal.printf("tA1(1) = %d\n", tA1[1]);
Mikebob 7:9c77eca4158a 116 terminal.printf("tA2(0) = %d\n", tA2[0]);
Mikebob 7:9c77eca4158a 117 terminal.printf("tA2(1) = %d\n", tA2[1]);
Mikebob 7:9c77eca4158a 118
Mikebob 7:9c77eca4158a 119 //Calculate the frequency of rotation
Mikebob 7:9c77eca4158a 120 float TA1 = 2.0f * (tA1[1]-tA1[0]);
Mikebob 7:9c77eca4158a 121 float TA2 = 2.0f * (tA2[1]-tA2[0]);
Mikebob 7:9c77eca4158a 122 float TA = (TA1 + TA2) * 0.5f;
jaffacat 5:fdc550ee3b6e 123
Mikebob 7:9c77eca4158a 124 dis = timer1.read_us();
Mikebob 7:9c77eca4158a 125 float mm = ((TA*3)*20.8)/175.9;
Mikebob 7:9c77eca4158a 126 distance = dis/(mm);
Mikebob 7:9c77eca4158a 127 float fA = 1.0f/ (TA *(float)3.0E-6);
Mikebob 7:9c77eca4158a 128 terminal.printf("Average A2 Shaft: %6.2fHz \t Wheel: %6.2f \t distance: %6.2f\n", fA, fA/20.8f, distance);
Mikebob 7:9c77eca4158a 129 }
Mikebob 7:9c77eca4158a 130
Mikebob 7:9c77eca4158a 131 int main()
Mikebob 7:9c77eca4158a 132 {
Mikebob 7:9c77eca4158a 133 timer1.reset();
jaffacat 5:fdc550ee3b6e 134
Mikebob 7:9c77eca4158a 135 //Configure the terminal to high speed
Mikebob 7:9c77eca4158a 136 terminal.baud(115200);
Mikebob 7:9c77eca4158a 137
Mikebob 7:9c77eca4158a 138 //Set initial motor direction
Mikebob 7:9c77eca4158a 139 DIRA = FORWARD;
Mikebob 7:9c77eca4158a 140 DIRB = FORWARD;
Mikebob 7:9c77eca4158a 141
Mikebob 7:9c77eca4158a 142 //Set motor period to 100Hz
Mikebob 7:9c77eca4158a 143 PWMA.period_ms(10);
Mikebob 7:9c77eca4158a 144 PWMB.period_ms(10);
Mikebob 7:9c77eca4158a 145
Mikebob 7:9c77eca4158a 146 //Set initial motor speed to stop
Mikebob 7:9c77eca4158a 147 PWMA.write(0.0f); //0% duty cycle
Mikebob 7:9c77eca4158a 148 PWMB.write(0.0f); //0% duty cycle
Mikebob 7:9c77eca4158a 149
Mikebob 7:9c77eca4158a 150 //Wait for USER button (blue pull-down switch) to start
Mikebob 7:9c77eca4158a 151 terminal.puts("Press USER button to start");
Mikebob 7:9c77eca4158a 152 led = 0;
Mikebob 7:9c77eca4158a 153 while (SW1 == RELEASED);
Mikebob 7:9c77eca4158a 154 led = 1;
Mikebob 7:9c77eca4158a 155
Mikebob 7:9c77eca4158a 156 //Set initial motor speed to stop
Mikebob 7:9c77eca4158a 157 PWMA.write(0.0f); //Set duty cycle (%)
Mikebob 7:9c77eca4158a 158 PWMB.write(0.0f); //Set duty cycle (%)
Mikebob 7:9c77eca4158a 159
Mikebob 7:9c77eca4158a 160 //Wait - give time to start running
Mikebob 7:9c77eca4158a 161 wait(1.0);
Mikebob 7:9c77eca4158a 162
Mikebob 7:9c77eca4158a 163
jaffacat 5:fdc550ee3b6e 164
jaffacat 5:fdc550ee3b6e 165
jaffacat 5:fdc550ee3b6e 166
Mikebob 7:9c77eca4158a 167 //Main polling loop
Mikebob 7:9c77eca4158a 168 while(1)
Mikebob 7:9c77eca4158a 169 {
jaffacat 5:fdc550ee3b6e 170
Mikebob 7:9c77eca4158a 171 while(distance <= 1250)
Mikebob 7:9c77eca4158a 172 {
Mikebob 7:9c77eca4158a 173 PWMA.write(dutyA); //Set duty cycle y
Mikebob 7:9c77eca4158a 174 PWMB.write(dutyB);
Mikebob 7:9c77eca4158a 175 time();
Mikebob 7:9c77eca4158a 176 }
Mikebob 7:9c77eca4158a 177 while(distance <= 1331)
Mikebob 7:9c77eca4158a 178 {
Mikebob 7:9c77eca4158a 179 PWMA.write(0.0F);
Mikebob 7:9c77eca4158a 180 PWMB.write(dutyB);
Mikebob 7:9c77eca4158a 181 time();
Mikebob 7:9c77eca4158a 182 }
Mikebob 7:9c77eca4158a 183 while(distance <= 2788)
Mikebob 7:9c77eca4158a 184 {
Mikebob 7:9c77eca4158a 185 PWMA.write(dutyA);
Mikebob 7:9c77eca4158a 186 PWMB.write(dutyB);
Mikebob 7:9c77eca4158a 187 time();
Mikebob 7:9c77eca4158a 188 }
Mikebob 7:9c77eca4158a 189 while(distance <= 3544)
Mikebob 7:9c77eca4158a 190 {
Mikebob 7:9c77eca4158a 191 PWMA.write(0.0F);
Mikebob 7:9c77eca4158a 192 PWMB.write(dutyB);
Mikebob 7:9c77eca4158a 193 time();
Mikebob 7:9c77eca4158a 194 }
Mikebob 7:9c77eca4158a 195 while(distance <= 4294)
Mikebob 7:9c77eca4158a 196 {
Mikebob 7:9c77eca4158a 197 PWMA.write(dutyA);
Mikebob 7:9c77eca4158a 198 PWMB.write(dutyB);
Mikebob 7:9c77eca4158a 199 time();
Mikebob 7:9c77eca4158a 200 }
Mikebob 7:9c77eca4158a 201 while(distance <= 4963)
Mikebob 7:9c77eca4158a 202 {
Mikebob 7:9c77eca4158a 203 PWMA.write(0);
Mikebob 7:9c77eca4158a 204 PWMB.write(dutyB);
Mikebob 7:9c77eca4158a 205 time();
Mikebob 7:9c77eca4158a 206 }
Mikebob 7:9c77eca4158a 207
Mikebob 7:9c77eca4158a 208 PWMA.write(0.0f);
Mikebob 7:9c77eca4158a 209 PWMB.write(0.0f);
Mikebob 7:9c77eca4158a 210 timer.stop();
Mikebob 7:9c77eca4158a 211 break;
Mikebob 7:9c77eca4158a 212 }
Mikebob 7:9c77eca4158a 213 wait(500);
Mikebob 7:9c77eca4158a 214 }
Mikebob 7:9c77eca4158a 215