Code to let Gr20's BioRobotics2017 robot come to live.
Dependencies: FastPWM MODSERIAL QEI mbed
main.cpp@7:b9a209f889f5, 2017-11-13 (annotated)
- Committer:
- megrootens
- Date:
- Mon Nov 13 10:39:55 2017 +0000
- Revision:
- 7:b9a209f889f5
- Parent:
- 6:cddc73091ab5
- Child:
- 9:4bf2034d37a3
Moved implementation to separate .cpp files, by using static const variables for constants in namespaces and extern declarations for non-const vars.; Code compiles & runs on a board. **Not yet tested on the actual robot!**
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
megrootens | 6:cddc73091ab5 | 1 | /** |
megrootens | 7:b9a209f889f5 | 2 | * Code to bring BioRobotics2017 group 20's robot to life. |
megrootens | 7:b9a209f889f5 | 3 | * |
megrootens | 7:b9a209f889f5 | 4 | * M.E. Grootens, UTwente, v0.2 11-12-2017 |
megrootens | 7:b9a209f889f5 | 5 | * |
megrootens | 7:b9a209f889f5 | 6 | * DISCLAIMER: |
megrootens | 7:b9a209f889f5 | 7 | * Just a quick-and-dirty working-example. I do NOT claim this to be an |
megrootens | 7:b9a209f889f5 | 8 | * example of good coding practice... as it is not. |
megrootens | 7:b9a209f889f5 | 9 | * Motors and Controllers are implemented as C++ classes. Reference, UI and |
megrootens | 7:b9a209f889f5 | 10 | * Robot are kept in different namespaces, so as to avoid too many global |
megrootens | 7:b9a209f889f5 | 11 | * variables. |
megrootens | 7:b9a209f889f5 | 12 | * |
megrootens | 7:b9a209f889f5 | 13 | * THE PROGRAM: |
megrootens | 7:b9a209f889f5 | 14 | * The robot starts to calibrate when SW3 is pressed and will then cycle |
megrootens | 7:b9a209f889f5 | 15 | * through the following states: |
megrootens | 7:b9a209f889f5 | 16 | * [OFF->CALIBRATION->HOMING->DEMO->OFF->CALIBRATION->etc.] |
megrootens | 7:b9a209f889f5 | 17 | * The robot will move continuously; MANUAL mode is not implemented. |
megrootens | 7:b9a209f889f5 | 18 | * |
megrootens | 7:b9a209f889f5 | 19 | * Implementation can be found in main.cpp |
megrootens | 2:df0c6af898ac | 20 | */ |
megrootens | 7:b9a209f889f5 | 21 | #include "mbed.h" |
megrootens | 0:caa8ee3bd882 | 22 | |
megrootens | 7:b9a209f889f5 | 23 | #include "tools.h" |
megrootens | 7:b9a209f889f5 | 24 | #include "ui.h" |
megrootens | 7:b9a209f889f5 | 25 | #include "robot.h" |
megrootens | 7:b9a209f889f5 | 26 | #include "ref.h" |
megrootens | 0:caa8ee3bd882 | 27 | |
megrootens | 0:caa8ee3bd882 | 28 | |
megrootens | 0:caa8ee3bd882 | 29 | |
megrootens | 6:cddc73091ab5 | 30 | /** |
megrootens | 7:b9a209f889f5 | 31 | * Main |
megrootens | 2:df0c6af898ac | 32 | */ |
megrootens | 7:b9a209f889f5 | 33 | int main() |
megrootens | 0:caa8ee3bd882 | 34 | { |
megrootens | 7:b9a209f889f5 | 35 | // Start with only red LED |
megrootens | 7:b9a209f889f5 | 36 | ui::rgb_led[0] = LED_ON; |
megrootens | 7:b9a209f889f5 | 37 | ui::rgb_led[1] = !LED_ON; |
megrootens | 7:b9a209f889f5 | 38 | ui::rgb_led[2] = !LED_ON; |
megrootens | 0:caa8ee3bd882 | 39 | |
megrootens | 7:b9a209f889f5 | 40 | // Setup serial comm |
megrootens | 7:b9a209f889f5 | 41 | ui::serial.baud(115200); |
megrootens | 7:b9a209f889f5 | 42 | ui::serial.printf("\r\n\r\n! STARTING MAIN LOOP.\r\n"); |
megrootens | 7:b9a209f889f5 | 43 | ui::serial.printf(" - Initializing...\r\n"); |
megrootens | 0:caa8ee3bd882 | 44 | |
megrootens | 7:b9a209f889f5 | 45 | // Ignore user interface while setup, ensure robot is off |
megrootens | 7:b9a209f889f5 | 46 | ui::SwitchState(ui::IGNORE); |
megrootens | 7:b9a209f889f5 | 47 | robot::SwitchState(robot::OFF); |
megrootens | 0:caa8ee3bd882 | 48 | |
megrootens | 7:b9a209f889f5 | 49 | // Tickers: status, control and reference signal |
megrootens | 7:b9a209f889f5 | 50 | Ticker tick_status; |
megrootens | 7:b9a209f889f5 | 51 | Ticker tick_control_loop; |
megrootens | 7:b9a209f889f5 | 52 | Ticker tick_reference; |
megrootens | 2:df0c6af898ac | 53 | |
megrootens | 7:b9a209f889f5 | 54 | tick_status.attach( &ui::StatusDisplay, ui::kSampleTime); |
megrootens | 7:b9a209f889f5 | 55 | tick_control_loop.attach(&robot::ControlLoop, robot::kSampleTime); |
megrootens | 7:b9a209f889f5 | 56 | tick_reference.attach( &ref::UpdateReference, ref::kSampleTime); |
megrootens | 2:df0c6af898ac | 57 | |
megrootens | 7:b9a209f889f5 | 58 | // User input interrupt switches, pass to ui |
megrootens | 7:b9a209f889f5 | 59 | InterruptIn sw2(SW2); |
megrootens | 7:b9a209f889f5 | 60 | InterruptIn sw3(SW3); |
megrootens | 2:df0c6af898ac | 61 | |
megrootens | 7:b9a209f889f5 | 62 | sw2.rise(ui::InterruptSwitch2); |
megrootens | 7:b9a209f889f5 | 63 | sw3.rise(ui::InterruptSwitch3); |
megrootens | 2:df0c6af898ac | 64 | |
megrootens | 7:b9a209f889f5 | 65 | ui::serial.printf(" - Robot ready.\r\n"); |
megrootens | 7:b9a209f889f5 | 66 | ui::SwitchState(ui::STATE_SWITCHING); |
megrootens | 2:df0c6af898ac | 67 | |
megrootens | 7:b9a209f889f5 | 68 | // inf loop |
megrootens | 7:b9a209f889f5 | 69 | while (true); |
megrootens | 7:b9a209f889f5 | 70 | } |