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.
Dependencies: mbed 3875_Individualproject
main/main.h@23:71e84953b3f3, 2020-04-08 (annotated)
- Committer:
- jamesheavey
- Date:
- Wed Apr 08 21:57:10 2020 +0000
- Revision:
- 23:71e84953b3f3
- Parent:
- 22:02dda79d50b4
- Child:
- 24:adb946be4ce5
checkpoint;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jamesheavey | 0:df5216b20861 | 1 | #ifndef MAIN_H |
jamesheavey | 0:df5216b20861 | 2 | #define MAIN_H |
jamesheavey | 0:df5216b20861 | 3 | |
jamesheavey | 0:df5216b20861 | 4 | #include "m3pi.h" |
jamesheavey | 0:df5216b20861 | 5 | #include "mbed.h" |
jamesheavey | 23:71e84953b3f3 | 6 | #include <cmath> |
jamesheavey | 23:71e84953b3f3 | 7 | |
jamesheavey | 23:71e84953b3f3 | 8 | using namespace std; |
jamesheavey | 20:5cf6a378801d | 9 | |
jamesheavey | 20:5cf6a378801d | 10 | // Constants |
jamesheavey | 20:5cf6a378801d | 11 | #define A 0.5 // 20 |
jamesheavey | 20:5cf6a378801d | 12 | #define B 1/10000 // 10000 |
jamesheavey | 20:5cf6a378801d | 13 | #define C 1.5 // 2/3 |
jamesheavey | 20:5cf6a378801d | 14 | |
jamesheavey | 20:5cf6a378801d | 15 | #define SENS_THRESH 500 // >500 = black |
jamesheavey | 20:5cf6a378801d | 16 | #define TURN_SPEED 0.2 |
jamesheavey | 0:df5216b20861 | 17 | |
jamesheavey | 0:df5216b20861 | 18 | // API |
jamesheavey | 0:df5216b20861 | 19 | extern m3pi robot; |
jamesheavey | 0:df5216b20861 | 20 | |
jamesheavey | 0:df5216b20861 | 21 | // LEDs |
jamesheavey | 0:df5216b20861 | 22 | extern BusOut leds; |
jamesheavey | 0:df5216b20861 | 23 | |
jamesheavey | 0:df5216b20861 | 24 | // Buttons |
jamesheavey | 0:df5216b20861 | 25 | extern DigitalIn button_A; |
jamesheavey | 0:df5216b20861 | 26 | extern DigitalIn button_B; |
jamesheavey | 0:df5216b20861 | 27 | extern DigitalIn button_X; |
jamesheavey | 0:df5216b20861 | 28 | extern DigitalIn button_Y; |
jamesheavey | 0:df5216b20861 | 29 | extern DigitalIn button_enter; |
jamesheavey | 0:df5216b20861 | 30 | extern DigitalIn button_back; |
jamesheavey | 0:df5216b20861 | 31 | |
jamesheavey | 0:df5216b20861 | 32 | // Potentiometers |
jamesheavey | 0:df5216b20861 | 33 | extern AnalogIn pot_P; |
jamesheavey | 0:df5216b20861 | 34 | extern AnalogIn pot_I; |
jamesheavey | 0:df5216b20861 | 35 | extern AnalogIn pot_D; |
jamesheavey | 0:df5216b20861 | 36 | extern AnalogIn pot_S; |
jamesheavey | 0:df5216b20861 | 37 | |
jamesheavey | 18:991658b628fc | 38 | // Sensors |
jamesheavey | 18:991658b628fc | 39 | extern DigitalInOut QTRA; //connected to digital P26 |
jamesheavey | 18:991658b628fc | 40 | extern DigitalInOut QTRB; //connected to digital P25 |
jamesheavey | 18:991658b628fc | 41 | |
jamesheavey | 20:5cf6a378801d | 42 | // Timers |
jamesheavey | 21:54ea75f7984f | 43 | Timer t_L; // left encoder |
jamesheavey | 21:54ea75f7984f | 44 | Timer t_R; // right encoder |
jamesheavey | 21:54ea75f7984f | 45 | Timer t_coord; // coordinate timer |
jamesheavey | 0:df5216b20861 | 46 | |
jamesheavey | 0:df5216b20861 | 47 | // Prototypes |
jamesheavey | 21:54ea75f7984f | 48 | |
jamesheavey | 20:5cf6a378801d | 49 | void read_encoders(); |
jamesheavey | 20:5cf6a378801d | 50 | void init(); |
jamesheavey | 20:5cf6a378801d | 51 | void calibrate(); |
jamesheavey | 20:5cf6a378801d | 52 | void follow_line(); |
jamesheavey | 20:5cf6a378801d | 53 | bool junction_detect(); |
jamesheavey | 20:5cf6a378801d | 54 | char junction_logic(); |
jamesheavey | 20:5cf6a378801d | 55 | void turn_select( char turn ); |
jamesheavey | 20:5cf6a378801d | 56 | void left(); |
jamesheavey | 20:5cf6a378801d | 57 | void right(); |
jamesheavey | 20:5cf6a378801d | 58 | void back(); |
jamesheavey | 20:5cf6a378801d | 59 | void goal(); |
jamesheavey | 20:5cf6a378801d | 60 | void simplify(); |
jamesheavey | 20:5cf6a378801d | 61 | void invert_path(); |
jamesheavey | 20:5cf6a378801d | 62 | void non_looped(); |
jamesheavey | 20:5cf6a378801d | 63 | void looped(); |
jamesheavey | 21:54ea75f7984f | 64 | void node_logic(); |
jamesheavey | 0:df5216b20861 | 65 | |
jamesheavey | 20:5cf6a378801d | 66 | // Global Variables |
jamesheavey | 20:5cf6a378801d | 67 | char path[100]; |
jamesheavey | 20:5cf6a378801d | 68 | char inv_path[100]; |
jamesheavey | 20:5cf6a378801d | 69 | int path_length = 0; |
jamesheavey | 20:5cf6a378801d | 70 | unsigned int *sensor; |
jamesheavey | 20:5cf6a378801d | 71 | float speed; |
jamesheavey | 20:5cf6a378801d | 72 | float proportional = 0.0; |
jamesheavey | 20:5cf6a378801d | 73 | float prev_proportional = 0.0; |
jamesheavey | 20:5cf6a378801d | 74 | float integral = 0.0; |
jamesheavey | 20:5cf6a378801d | 75 | float derivative = 0.0; |
jamesheavey | 20:5cf6a378801d | 76 | int encoder[2]; |
jamesheavey | 20:5cf6a378801d | 77 | int dist_est_1 = 0; |
jamesheavey | 20:5cf6a378801d | 78 | int dist_est_2 = 0; |
jamesheavey | 0:df5216b20861 | 79 | |
jamesheavey | 23:71e84953b3f3 | 80 | bool first = true; |
jamesheavey | 21:54ea75f7984f | 81 | bool t_restart = true; |
jamesheavey | 23:71e84953b3f3 | 82 | char dir; |
jamesheavey | 21:54ea75f7984f | 83 | int curr_coords[2]; |
jamesheavey | 23:71e84953b3f3 | 84 | int total_points; |
jamesheavey | 22:02dda79d50b4 | 85 | unsigned int looped_path[100] |
jamesheavey | 22:02dda79d50b4 | 86 | unsigned int point[100]; |
jamesheavey | 22:02dda79d50b4 | 87 | unsigned int type[100]; |
jamesheavey | 22:02dda79d50b4 | 88 | unsigned int explored[100]; |
jamesheavey | 21:54ea75f7984f | 89 | int coords_x[100]; |
jamesheavey | 21:54ea75f7984f | 90 | int coords_y[100]; |
jamesheavey | 22:02dda79d50b4 | 91 | char turn_priority[4] = { 'S' , 'E' , 'N', 'W' }; // reversed |
jamesheavey | 0:df5216b20861 | 92 | #endif |