Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 7:9c77eca4158a
- Parent:
- 6:c23ecdd1a581
- Child:
- 8:f2b6ef8c5eb8
--- a/main.cpp Tue Nov 12 12:40:11 2019 +0000 +++ b/main.cpp Thu Nov 14 16:55:17 2019 +0000 @@ -1,83 +1,215 @@ -/* - Version 7 – Mike edit -*/ - -#include "mbed.h" - -//Motor PWM (speed) -PwmOut PWMA(PA_8); -PwmOut PWMB(PB_4); - -//Motor Direction -DigitalOut DIRA(PA_9); -DigitalOut DIRB(PB_10); - -//On board switch -DigitalIn SW1(USER_BUTTON); - -//Use the serial object so we can use higher speeds -Serial terminal(USBTX, USBRX); +/* + +** ELEC143 Coursework Sample Code ** + +Author: Dr Nicholas Outram +Date: 09-10-2015 +Version: 1.0 + +This sample code demonstrates the following: + +- How to control the motor speed and direction +- How to measure motor speed using software polling (assuming a forward direction) + +Notes: + +1. This code is very monolithic. I have made no attempt to factor this into meaningful and +reuseable functions. I will be looking to see you factor your code into different functions, +and maybe even into seperate C (or CPP) files. + +2. This code is only lightly commented. The commenting is sufficient to +explain the purpose of each step in the code. Do not assume this is always sufficient. + +You may adapt this code for your own purposes. However, you may be questioned about any of the +code you use. Your answers will affect your grade to some extent. + +*/ +#include "mbed.h" + +//Status LED +DigitalOut led(LED1); + +//Motor PWM (speed) +PwmOut PWMA(PA_8); +PwmOut PWMB(PB_4); + +//Motor Direction +DigitalOut DIRA(PA_9); +DigitalOut DIRB(PB_10); + +//Hall-Effect Sensor Inputs +DigitalIn HEA1(PB_2); +DigitalIn HEA2(PB_1); +DigitalIn HEB1(PB_15); +DigitalIn HEB2(PB_14); -//Timer used for measuring speeds -Timer timer; - -//Enumerated types -enum DIRECTION {FORWARD=0, REVERSE}; -enum PULSE {NOPULSE=0, PULSE}; -enum SWITCHSTATE {PRESSED=0, RELEASED}; + +//On board switch +DigitalIn SW1(USER_BUTTON); + +//Use the serial object so we can use higher speeds +Serial terminal(USBTX, USBRX); + +//Timer used for measuring speeds +Timer timer; +Timer timer1; + +//Enumerated types +enum DIRECTION {FORWARD=0, REVERSE}; +enum PULSE {NOPULSE=0, PULSE}; +enum SWITCHSTATE {PRESSED=0, RELEASED}; + +//Debug GPIO +DigitalOut probe(D10); + +//Duty cycles +float dutyA = 1.0f; //100% +float dutyB = 1.0f; //100% +//Array of sensor data +int tA1[2]; +int tA2[2]; +float dis; +float distance; -//Debug GPIO -DigitalOut probe(D10); +void time() + { + //Reset timer and Start + timer.reset(); + timer.start(); + + //********************************************************************* + //FIRST TIME - SYNCHRONISE (YOU SHOULD NOT NEED THIS ONCE IT's RUNNING) + //********************************************************************* + + //Wait for rising edge of A1 and log time + while (HEA1 == NOPULSE); + //Wait for rising edge of A2 and log time (30 degrees?) + while (HEA2 == NOPULSE); + //Wait for falling edge of A1 + while (HEA1 == PULSE); + //Wait for falling edge of A2 + while (HEA2 == PULSE); + + //********************** + //TIME THE FULL SEQUENCE + //********************** + + //Wait for rising edge of A1 and log time + while (HEA1 == NOPULSE); + tA1[0] = timer.read_us(); + + //Wait for rising edge of A2 and log time (30 degrees?) + while (HEA2 == NOPULSE); + tA2[0] = timer.read_us(); + + //Wait for falling edge of A1 + while (HEA1 == PULSE); + tA1[1] = timer.read_us(); + + //Wait for falling edge of A2 + while (HEA2 == PULSE); + tA2[1] = timer.read_us(); + + + terminal.printf("tA1(0) = %d\n", tA1[0]); + terminal.printf("tA1(1) = %d\n", tA1[1]); + terminal.printf("tA2(0) = %d\n", tA2[0]); + terminal.printf("tA2(1) = %d\n", tA2[1]); + + //Calculate the frequency of rotation + float TA1 = 2.0f * (tA1[1]-tA1[0]); + float TA2 = 2.0f * (tA2[1]-tA2[0]); + float TA = (TA1 + TA2) * 0.5f; -//Duty cycles -float dutyA = 1.0f; //100% -float dutyB = 1.0f; //100% - -int main() -{ - //Configure the terminal to high speed - terminal.baud(115200); + dis = timer1.read_us(); + float mm = ((TA*3)*20.8)/175.9; + distance = dis/(mm); + float fA = 1.0f/ (TA *(float)3.0E-6); + terminal.printf("Average A2 Shaft: %6.2fHz \t Wheel: %6.2f \t distance: %6.2f\n", fA, fA/20.8f, distance); + } + +int main() +{ + timer1.reset(); - //Set initial motor direction - DIRA = FORWARD; - DIRB = FORWARD; - - //Set motor period to 100Hz - PWMA.period_ms(10); - PWMB.period_ms(10); + //Configure the terminal to high speed + terminal.baud(115200); + + //Set initial motor direction + DIRA = FORWARD; + DIRB = FORWARD; + + //Set motor period to 100Hz + PWMA.period_ms(10); + PWMB.period_ms(10); + + //Set initial motor speed to stop + PWMA.write(0.0f); //0% duty cycle + PWMB.write(0.0f); //0% duty cycle + + //Wait for USER button (blue pull-down switch) to start + terminal.puts("Press USER button to start"); + led = 0; + while (SW1 == RELEASED); + led = 1; + + //Set initial motor speed to stop + PWMA.write(0.0f); //Set duty cycle (%) + PWMB.write(0.0f); //Set duty cycle (%) + + //Wait - give time to start running + wait(1.0); + + - //Set initial motor speed to stop - PWMA.write(0.0f); //0% duty cycle - PWMB.write(0.0f); //0% duty cycle - //Wait for USER button (blue pull-down switch) to start - terminal.puts("Press USER button to start"); - while (SW1 == RELEASED); - //Set initial motor speed to stop { - for(float ramp = 0.0f; ramp <= 1.0f ; ramp += 0.2) - { - PWMA.write(ramp); //Set duty cycle y - PWMB.write(ramp); //Set duty cycle y - wait(1); - } + //Main polling loop + while(1) + { - PWMA.write(dutyA); //Set duty cycle y - PWMB.write(dutyB); //Set duty cycle y - wait(1.1); - PWMB.write(0.2f); //turn 31deg - wait(1.3); - PWMA.write(dutyA); //Set duty cycle hyp - PWMB.write(dutyB); //Set duty cycle hyp - wait(4.4); - PWMB.write(0.2f); //turn 59deg - wait(1.1); - PWMA.write(dutyA); //Set duty cycle x - PWMB.write(dutyB); //Set duty cycle x - wait(2.4); - PWMB.write(0.2f); //turn 90deg - wait(0.7); - - PWMA.write(0.0f); - PWMB.write(0.0f); -} \ No newline at end of file + while(distance <= 1250) + { + PWMA.write(dutyA); //Set duty cycle y + PWMB.write(dutyB); + time(); + } + while(distance <= 1331) + { + PWMA.write(0.0F); + PWMB.write(dutyB); + time(); + } + while(distance <= 2788) + { + PWMA.write(dutyA); + PWMB.write(dutyB); + time(); + } + while(distance <= 3544) + { + PWMA.write(0.0F); + PWMB.write(dutyB); + time(); + } + while(distance <= 4294) + { + PWMA.write(dutyA); + PWMB.write(dutyB); + time(); + } + while(distance <= 4963) + { + PWMA.write(0); + PWMB.write(dutyB); + time(); + } + + PWMA.write(0.0f); + PWMB.write(0.0f); + timer.stop(); + break; + } + wait(500); +} + \ No newline at end of file