Rachel Ireland-Jones / Mbed OS FinalYear0
Committer:
Mikebob
Date:
Tue Nov 19 12:17:42 2019 +0000
Revision:
8:f2b6ef8c5eb8
Parent:
7:9c77eca4158a
Child:
9:45ae94cf5e23
v2

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 8:f2b6ef8c5eb8 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 7:9c77eca4158a 130 int main()
Mikebob 7:9c77eca4158a 131 {
Mikebob 7:9c77eca4158a 132 timer1.reset();
jaffacat 5:fdc550ee3b6e 133
Mikebob 7:9c77eca4158a 134 //Configure the terminal to high speed
Mikebob 7:9c77eca4158a 135 terminal.baud(115200);
Mikebob 7:9c77eca4158a 136
Mikebob 7:9c77eca4158a 137 //Set initial motor direction
Mikebob 7:9c77eca4158a 138 DIRA = FORWARD;
Mikebob 7:9c77eca4158a 139 DIRB = FORWARD;
Mikebob 7:9c77eca4158a 140
Mikebob 7:9c77eca4158a 141 //Set motor period to 100Hz
Mikebob 7:9c77eca4158a 142 PWMA.period_ms(10);
Mikebob 7:9c77eca4158a 143 PWMB.period_ms(10);
Mikebob 7:9c77eca4158a 144
Mikebob 7:9c77eca4158a 145 //Set initial motor speed to stop
Mikebob 7:9c77eca4158a 146 PWMA.write(0.0f); //0% duty cycle
Mikebob 7:9c77eca4158a 147 PWMB.write(0.0f); //0% duty cycle
Mikebob 7:9c77eca4158a 148
Mikebob 7:9c77eca4158a 149 //Wait for USER button (blue pull-down switch) to start
Mikebob 7:9c77eca4158a 150 terminal.puts("Press USER button to start");
Mikebob 7:9c77eca4158a 151 led = 0;
Mikebob 7:9c77eca4158a 152 while (SW1 == RELEASED);
Mikebob 7:9c77eca4158a 153 led = 1;
Mikebob 7:9c77eca4158a 154
Mikebob 7:9c77eca4158a 155 //Set initial motor speed to stop
Mikebob 7:9c77eca4158a 156 PWMA.write(0.0f); //Set duty cycle (%)
Mikebob 7:9c77eca4158a 157 PWMB.write(0.0f); //Set duty cycle (%)
Mikebob 7:9c77eca4158a 158
Mikebob 7:9c77eca4158a 159 //Wait - give time to start running
Mikebob 7:9c77eca4158a 160 wait(1.0);
jaffacat 5:fdc550ee3b6e 161
Mikebob 7:9c77eca4158a 162 //Main polling loop
Mikebob 7:9c77eca4158a 163 while(1)
Mikebob 7:9c77eca4158a 164 {
Mikebob 8:f2b6ef8c5eb8 165 timer1.start();
Mikebob 8:f2b6ef8c5eb8 166 while(trav <= 1250)
Mikebob 7:9c77eca4158a 167 {
Mikebob 7:9c77eca4158a 168 PWMA.write(dutyA); //Set duty cycle y
Mikebob 7:9c77eca4158a 169 PWMB.write(dutyB);
Mikebob 7:9c77eca4158a 170 time();
Mikebob 7:9c77eca4158a 171 }
Mikebob 8:f2b6ef8c5eb8 172 while(trav <= 1331)
Mikebob 7:9c77eca4158a 173 {
Mikebob 8:f2b6ef8c5eb8 174 PWMA.write(dutyA);
Mikebob 8:f2b6ef8c5eb8 175 PWMB.write(0.0f);
Mikebob 7:9c77eca4158a 176 time();
Mikebob 7:9c77eca4158a 177 }
Mikebob 8:f2b6ef8c5eb8 178 while(trav <= 2788)
Mikebob 7:9c77eca4158a 179 {
Mikebob 7:9c77eca4158a 180 PWMA.write(dutyA);
Mikebob 7:9c77eca4158a 181 PWMB.write(dutyB);
Mikebob 7:9c77eca4158a 182 time();
Mikebob 7:9c77eca4158a 183 }
Mikebob 8:f2b6ef8c5eb8 184 while(trav <= 3544)
Mikebob 7:9c77eca4158a 185 {
Mikebob 8:f2b6ef8c5eb8 186 PWMA.write(dutyA);
Mikebob 8:f2b6ef8c5eb8 187 PWMB.write(0.0f);
Mikebob 7:9c77eca4158a 188 time();
Mikebob 7:9c77eca4158a 189 }
Mikebob 8:f2b6ef8c5eb8 190 while(trav <= 4294)
Mikebob 7:9c77eca4158a 191 {
Mikebob 7:9c77eca4158a 192 PWMA.write(dutyA);
Mikebob 7:9c77eca4158a 193 PWMB.write(dutyB);
Mikebob 7:9c77eca4158a 194 time();
Mikebob 7:9c77eca4158a 195 }
Mikebob 8:f2b6ef8c5eb8 196 while(trav <= 4963)
Mikebob 7:9c77eca4158a 197 {
Mikebob 8:f2b6ef8c5eb8 198 PWMA.write(dutyA);
Mikebob 8:f2b6ef8c5eb8 199 PWMB.write(0.0f);
Mikebob 7:9c77eca4158a 200 time();
Mikebob 7:9c77eca4158a 201 }
Mikebob 7:9c77eca4158a 202
Mikebob 7:9c77eca4158a 203 PWMA.write(0.0f);
Mikebob 7:9c77eca4158a 204 PWMB.write(0.0f);
Mikebob 7:9c77eca4158a 205 timer.stop();
Mikebob 7:9c77eca4158a 206 break;
Mikebob 7:9c77eca4158a 207 }
Mikebob 7:9c77eca4158a 208 wait(500);
Mikebob 7:9c77eca4158a 209 }
Mikebob 7:9c77eca4158a 210