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.
Main.cpp@0:1a972ed770da, 2020-03-09 (annotated)
- Committer:
- oehlemar
- Date:
- Mon Mar 09 16:23:04 2020 +0000
- Revision:
- 0:1a972ed770da
LAB2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
oehlemar | 0:1a972ed770da | 1 | /* |
oehlemar | 0:1a972ed770da | 2 | * Main.cpp |
oehlemar | 0:1a972ed770da | 3 | * Copyright (c) 2020, ZHAW |
oehlemar | 0:1a972ed770da | 4 | * All rights reserved. |
oehlemar | 0:1a972ed770da | 5 | */ |
oehlemar | 0:1a972ed770da | 6 | |
oehlemar | 0:1a972ed770da | 7 | #include <mbed.h> |
oehlemar | 0:1a972ed770da | 8 | #include "IRSensor.h" |
oehlemar | 0:1a972ed770da | 9 | #include "EncoderCounter.h" |
oehlemar | 0:1a972ed770da | 10 | #include "Controller.h" |
oehlemar | 0:1a972ed770da | 11 | #include "StateMachine.h" |
oehlemar | 0:1a972ed770da | 12 | |
oehlemar | 0:1a972ed770da | 13 | int main() { |
oehlemar | 0:1a972ed770da | 14 | |
oehlemar | 0:1a972ed770da | 15 | // initialise digital inputs and outputs |
oehlemar | 0:1a972ed770da | 16 | |
oehlemar | 0:1a972ed770da | 17 | DigitalIn button(USER_BUTTON); |
oehlemar | 0:1a972ed770da | 18 | |
oehlemar | 0:1a972ed770da | 19 | DigitalOut ledGreen(LED1); |
oehlemar | 0:1a972ed770da | 20 | DigitalOut ledBlue(LED2); |
oehlemar | 0:1a972ed770da | 21 | DigitalOut ledRed(LED3); |
oehlemar | 0:1a972ed770da | 22 | |
oehlemar | 0:1a972ed770da | 23 | DigitalOut led0(PD_4); |
oehlemar | 0:1a972ed770da | 24 | DigitalOut led1(PD_3); |
oehlemar | 0:1a972ed770da | 25 | DigitalOut led2(PD_6); |
oehlemar | 0:1a972ed770da | 26 | DigitalOut led3(PD_2); |
oehlemar | 0:1a972ed770da | 27 | DigitalOut led4(PD_7); |
oehlemar | 0:1a972ed770da | 28 | DigitalOut led5(PD_5); |
oehlemar | 0:1a972ed770da | 29 | |
oehlemar | 0:1a972ed770da | 30 | // create distance sensor objects |
oehlemar | 0:1a972ed770da | 31 | |
oehlemar | 0:1a972ed770da | 32 | AnalogIn distance(PA_0); |
oehlemar | 0:1a972ed770da | 33 | DigitalOut enable(PG_1); |
oehlemar | 0:1a972ed770da | 34 | DigitalOut bit0(PF_0); |
oehlemar | 0:1a972ed770da | 35 | DigitalOut bit1(PF_1); |
oehlemar | 0:1a972ed770da | 36 | DigitalOut bit2(PF_2); |
oehlemar | 0:1a972ed770da | 37 | |
oehlemar | 0:1a972ed770da | 38 | enable = 1; |
oehlemar | 0:1a972ed770da | 39 | |
oehlemar | 0:1a972ed770da | 40 | IRSensor irSensor0(distance, bit0, bit1, bit2, 0); |
oehlemar | 0:1a972ed770da | 41 | IRSensor irSensor1(distance, bit0, bit1, bit2, 1); |
oehlemar | 0:1a972ed770da | 42 | IRSensor irSensor2(distance, bit0, bit1, bit2, 2); |
oehlemar | 0:1a972ed770da | 43 | IRSensor irSensor3(distance, bit0, bit1, bit2, 3); |
oehlemar | 0:1a972ed770da | 44 | IRSensor irSensor4(distance, bit0, bit1, bit2, 4); |
oehlemar | 0:1a972ed770da | 45 | IRSensor irSensor5(distance, bit0, bit1, bit2, 5); |
oehlemar | 0:1a972ed770da | 46 | |
oehlemar | 0:1a972ed770da | 47 | // create motor controller objects |
oehlemar | 0:1a972ed770da | 48 | |
oehlemar | 0:1a972ed770da | 49 | DigitalOut enableMotorDriver(PG_0); |
oehlemar | 0:1a972ed770da | 50 | DigitalIn motorDriverFault(PD_1); |
oehlemar | 0:1a972ed770da | 51 | DigitalIn motorDriverWarning(PD_0); |
oehlemar | 0:1a972ed770da | 52 | |
oehlemar | 0:1a972ed770da | 53 | PwmOut pwmLeft(PF_9); |
oehlemar | 0:1a972ed770da | 54 | PwmOut pwmRight(PF_8); |
oehlemar | 0:1a972ed770da | 55 | |
oehlemar | 0:1a972ed770da | 56 | // create encoder counter objects |
oehlemar | 0:1a972ed770da | 57 | |
oehlemar | 0:1a972ed770da | 58 | EncoderCounter counterLeft(PD_12, PD_13); |
oehlemar | 0:1a972ed770da | 59 | EncoderCounter counterRight(PB_4, PC_7); |
oehlemar | 0:1a972ed770da | 60 | |
oehlemar | 0:1a972ed770da | 61 | // create robot controller objects |
oehlemar | 0:1a972ed770da | 62 | |
oehlemar | 0:1a972ed770da | 63 | Controller controller(pwmLeft, pwmRight, counterLeft, counterRight); |
oehlemar | 0:1a972ed770da | 64 | StateMachine stateMachine(controller, enableMotorDriver, led0, led1, led2, led3, led4, led5, button, irSensor0, irSensor1, irSensor2, irSensor3, irSensor4, irSensor5); |
oehlemar | 0:1a972ed770da | 65 | |
oehlemar | 0:1a972ed770da | 66 | // enter main loop |
oehlemar | 0:1a972ed770da | 67 | |
oehlemar | 0:1a972ed770da | 68 | while (true) { |
oehlemar | 0:1a972ed770da | 69 | |
oehlemar | 0:1a972ed770da | 70 | if (stateMachine.getState() == StateMachine::MOVE_FORWARD) { |
oehlemar | 0:1a972ed770da | 71 | |
oehlemar | 0:1a972ed770da | 72 | ledGreen = 1; |
oehlemar | 0:1a972ed770da | 73 | ledBlue = 0; |
oehlemar | 0:1a972ed770da | 74 | ledRed = 0; |
oehlemar | 0:1a972ed770da | 75 | |
oehlemar | 0:1a972ed770da | 76 | } else if ((stateMachine.getState() == StateMachine::TURN_LEFT) || (stateMachine.getState() == StateMachine::TURN_RIGHT)) { |
oehlemar | 0:1a972ed770da | 77 | |
oehlemar | 0:1a972ed770da | 78 | ledGreen = 1; |
oehlemar | 0:1a972ed770da | 79 | ledBlue = 1; |
oehlemar | 0:1a972ed770da | 80 | ledRed = 0; |
oehlemar | 0:1a972ed770da | 81 | |
oehlemar | 0:1a972ed770da | 82 | } else { |
oehlemar | 0:1a972ed770da | 83 | |
oehlemar | 0:1a972ed770da | 84 | ledGreen = 0; |
oehlemar | 0:1a972ed770da | 85 | ledBlue = 0; |
oehlemar | 0:1a972ed770da | 86 | ledRed = 1; |
oehlemar | 0:1a972ed770da | 87 | } |
oehlemar | 0:1a972ed770da | 88 | |
oehlemar | 0:1a972ed770da | 89 | wait(0.1); |
oehlemar | 0:1a972ed770da | 90 | } |
oehlemar | 0:1a972ed770da | 91 | } |
oehlemar | 0:1a972ed770da | 92 |