Rachel Ireland-Jones / Mbed OS FinalYear0
Committer:
Mikebob
Date:
Tue Nov 19 15:15:50 2019 +0000
Revision:
11:475b412bbc3c
Parent:
10:1637fce7ef1e
Child:
12:3abb17739c2e
practically working;

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 8:f2b6ef8c5eb8 72 float trav =0;
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 //FIRST TIME - SYNCHRONISE (YOU SHOULD NOT NEED THIS ONCE IT's RUNNING)
Mikebob 7:9c77eca4158a 81 //*********************************************************************
Mikebob 7:9c77eca4158a 82
Mikebob 7:9c77eca4158a 83 //Wait for rising edge of A1 and log time
Mikebob 7:9c77eca4158a 84 while (HEA1 == NOPULSE);
Mikebob 7:9c77eca4158a 85 //Wait for rising edge of A2 and log time (30 degrees?)
Mikebob 7:9c77eca4158a 86 while (HEA2 == NOPULSE);
Mikebob 7:9c77eca4158a 87 //Wait for falling edge of A1
Mikebob 7:9c77eca4158a 88 while (HEA1 == PULSE);
Mikebob 7:9c77eca4158a 89 //Wait for falling edge of A2
Mikebob 7:9c77eca4158a 90 while (HEA2 == PULSE);
Mikebob 7:9c77eca4158a 91
Mikebob 7:9c77eca4158a 92 //**********************
Mikebob 7:9c77eca4158a 93 //TIME THE FULL SEQUENCE
Mikebob 7:9c77eca4158a 94 //**********************
Mikebob 7:9c77eca4158a 95
Mikebob 7:9c77eca4158a 96 //Wait for rising edge of A1 and log time
Mikebob 7:9c77eca4158a 97 while (HEA1 == NOPULSE);
Mikebob 7:9c77eca4158a 98 tA1[0] = timer.read_us();
Mikebob 7:9c77eca4158a 99
Mikebob 7:9c77eca4158a 100 //Wait for rising edge of A2 and log time (30 degrees?)
Mikebob 7:9c77eca4158a 101 while (HEA2 == NOPULSE);
Mikebob 7:9c77eca4158a 102 tA2[0] = timer.read_us();
Mikebob 7:9c77eca4158a 103
Mikebob 7:9c77eca4158a 104 //Wait for falling edge of A1
Mikebob 7:9c77eca4158a 105 while (HEA1 == PULSE);
Mikebob 7:9c77eca4158a 106 tA1[1] = timer.read_us();
Mikebob 7:9c77eca4158a 107
Mikebob 7:9c77eca4158a 108 //Wait for falling edge of A2
Mikebob 7:9c77eca4158a 109 while (HEA2 == PULSE);
Mikebob 7:9c77eca4158a 110 tA2[1] = timer.read_us();
Mikebob 7:9c77eca4158a 111
Mikebob 7:9c77eca4158a 112
Mikebob 7:9c77eca4158a 113 terminal.printf("tA1(0) = %d\n", tA1[0]);
Mikebob 7:9c77eca4158a 114 terminal.printf("tA1(1) = %d\n", tA1[1]);
Mikebob 7:9c77eca4158a 115 terminal.printf("tA2(0) = %d\n", tA2[0]);
Mikebob 7:9c77eca4158a 116 terminal.printf("tA2(1) = %d\n", tA2[1]);
Mikebob 7:9c77eca4158a 117
Mikebob 7:9c77eca4158a 118 //Calculate the frequency of rotation
Mikebob 7:9c77eca4158a 119 float TA1 = 2.0f * (tA1[1]-tA1[0]);
Mikebob 7:9c77eca4158a 120 float TA2 = 2.0f * (tA2[1]-tA2[0]);
Mikebob 7:9c77eca4158a 121 float TA = (TA1 + TA2) * 0.5f;
jaffacat 5:fdc550ee3b6e 122
Mikebob 7:9c77eca4158a 123 dis = timer1.read_us();
Mikebob 7:9c77eca4158a 124 float mm = ((TA*3)*20.8)/175.9;
Mikebob 10:1637fce7ef1e 125 trav = dis/mm;
Mikebob 7:9c77eca4158a 126 float fA = 1.0f/ (TA *(float)3.0E-6);
Mikebob 8:f2b6ef8c5eb8 127 terminal.printf("Average A2 Shaft: %6.2fHz \t Wheel: %6.2f \t trav: %6.2f\n", fA, fA/20.8f, trav);
Mikebob 7:9c77eca4158a 128 }
Mikebob 7:9c77eca4158a 129
Mikebob 10:1637fce7ef1e 130 void reset()
Mikebob 10:1637fce7ef1e 131 {
Mikebob 10:1637fce7ef1e 132 timer1.reset();
Mikebob 10:1637fce7ef1e 133 time();
Mikebob 10:1637fce7ef1e 134 }
Mikebob 10:1637fce7ef1e 135
Mikebob 7:9c77eca4158a 136 int main()
Mikebob 7:9c77eca4158a 137 {
jaffacat 5:fdc550ee3b6e 138
Mikebob 7:9c77eca4158a 139 //Configure the terminal to high speed
Mikebob 7:9c77eca4158a 140 terminal.baud(115200);
Mikebob 7:9c77eca4158a 141
Mikebob 7:9c77eca4158a 142 //Set initial motor direction
Mikebob 7:9c77eca4158a 143 DIRA = FORWARD;
Mikebob 7:9c77eca4158a 144 DIRB = FORWARD;
Mikebob 7:9c77eca4158a 145
Mikebob 7:9c77eca4158a 146 //Set motor period to 100Hz
Mikebob 7:9c77eca4158a 147 PWMA.period_ms(10);
Mikebob 7:9c77eca4158a 148 PWMB.period_ms(10);
Mikebob 7:9c77eca4158a 149
Mikebob 7:9c77eca4158a 150 //Set initial motor speed to stop
Mikebob 7:9c77eca4158a 151 PWMA.write(0.0f); //0% duty cycle
Mikebob 7:9c77eca4158a 152 PWMB.write(0.0f); //0% duty cycle
Mikebob 7:9c77eca4158a 153
Mikebob 7:9c77eca4158a 154 //Wait for USER button (blue pull-down switch) to start
Mikebob 7:9c77eca4158a 155 terminal.puts("Press USER button to start");
Mikebob 7:9c77eca4158a 156 led = 0;
Mikebob 7:9c77eca4158a 157 while (SW1 == RELEASED);
Mikebob 7:9c77eca4158a 158 led = 1;
Mikebob 7:9c77eca4158a 159
Mikebob 7:9c77eca4158a 160 //Set initial motor speed to stop
Mikebob 7:9c77eca4158a 161 PWMA.write(0.0f); //Set duty cycle (%)
Mikebob 7:9c77eca4158a 162 PWMB.write(0.0f); //Set duty cycle (%)
Mikebob 7:9c77eca4158a 163
Mikebob 7:9c77eca4158a 164 //Wait - give time to start running
Mikebob 7:9c77eca4158a 165 wait(1.0);
Mikebob 10:1637fce7ef1e 166 timer1.reset();
Mikebob 10:1637fce7ef1e 167 timer1.start();
Mikebob 7:9c77eca4158a 168 //Main polling loop
Mikebob 7:9c77eca4158a 169 while(1)
Mikebob 7:9c77eca4158a 170 {
Mikebob 8:f2b6ef8c5eb8 171 while(trav <= 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 10:1637fce7ef1e 176 }
Mikebob 10:1637fce7ef1e 177 reset();
Mikebob 9:45ae94cf5e23 178 while(trav <= 330)
Mikebob 7:9c77eca4158a 179 {
Mikebob 8:f2b6ef8c5eb8 180 PWMA.write(dutyA);
Mikebob 8:f2b6ef8c5eb8 181 PWMB.write(0.0f);
Mikebob 7:9c77eca4158a 182 time();
Mikebob 7:9c77eca4158a 183 }
Mikebob 10:1637fce7ef1e 184 reset();
Mikebob 9:45ae94cf5e23 185 while(trav <= 1457)
Mikebob 7:9c77eca4158a 186 {
Mikebob 7:9c77eca4158a 187 PWMA.write(dutyA);
Mikebob 7:9c77eca4158a 188 PWMB.write(dutyB);
Mikebob 7:9c77eca4158a 189 time();
Mikebob 7:9c77eca4158a 190 }
Mikebob 10:1637fce7ef1e 191 reset();
Mikebob 9:45ae94cf5e23 192 while(trav <= 268)
Mikebob 7:9c77eca4158a 193 {
Mikebob 8:f2b6ef8c5eb8 194 PWMA.write(dutyA);
Mikebob 8:f2b6ef8c5eb8 195 PWMB.write(0.0f);
Mikebob 7:9c77eca4158a 196 time();
Mikebob 7:9c77eca4158a 197 }
Mikebob 10:1637fce7ef1e 198 reset();
Mikebob 9:45ae94cf5e23 199 while(trav <= 750)
Mikebob 7:9c77eca4158a 200 {
Mikebob 7:9c77eca4158a 201 PWMA.write(dutyA);
Mikebob 7:9c77eca4158a 202 PWMB.write(dutyB);
Mikebob 7:9c77eca4158a 203 time();
Mikebob 7:9c77eca4158a 204 }
Mikebob 10:1637fce7ef1e 205 reset();
Mikebob 9:45ae94cf5e23 206 while(trav <= 200)
Mikebob 7:9c77eca4158a 207 {
Mikebob 8:f2b6ef8c5eb8 208 PWMA.write(dutyA);
Mikebob 8:f2b6ef8c5eb8 209 PWMB.write(0.0f);
Mikebob 7:9c77eca4158a 210 time();
Mikebob 7:9c77eca4158a 211 }
Mikebob 7:9c77eca4158a 212 timer.stop();
Mikebob 7:9c77eca4158a 213 break;
Mikebob 7:9c77eca4158a 214 }
Mikebob 11:475b412bbc3c 215 PWMA.write(0.0f);
Mikebob 11:475b412bbc3c 216 PWMB.write(0.0f);
Mikebob 7:9c77eca4158a 217 }
Mikebob 7:9c77eca4158a 218