James Heavey / Mbed 2 deprecated 3875_DISSERTATION

Dependencies:   mbed 3875_Individualproject

Committer:
jamesheavey
Date:
Mon Jun 01 20:34:11 2020 +0000
Revision:
68:14f2a5315d88
Parent:
67:f307ff6ec083
music

Who changed what in which revision?

UserRevisionLine numberNew 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 29:ecf497c3fdc0 6 #include <stdlib.h>
jamesheavey 20:5cf6a378801d 7
jamesheavey 20:5cf6a378801d 8 // Constants
jamesheavey 20:5cf6a378801d 9 #define A 0.5 // 20
jamesheavey 20:5cf6a378801d 10 #define B 1/10000 // 10000
jamesheavey 20:5cf6a378801d 11 #define C 1.5 // 2/3
jamesheavey 20:5cf6a378801d 12
jamesheavey 20:5cf6a378801d 13 #define SENS_THRESH 500 // >500 = black
jamesheavey 20:5cf6a378801d 14 #define TURN_SPEED 0.2
jamesheavey 0:df5216b20861 15
jamesheavey 0:df5216b20861 16 // API
jamesheavey 0:df5216b20861 17 extern m3pi robot;
jamesheavey 0:df5216b20861 18
jamesheavey 0:df5216b20861 19 // LEDs
jamesheavey 0:df5216b20861 20 extern BusOut leds;
jamesheavey 0:df5216b20861 21
jamesheavey 0:df5216b20861 22 // Buttons
jamesheavey 0:df5216b20861 23 extern DigitalIn button_A;
jamesheavey 0:df5216b20861 24 extern DigitalIn button_B;
jamesheavey 0:df5216b20861 25 extern DigitalIn button_X;
jamesheavey 0:df5216b20861 26 extern DigitalIn button_Y;
jamesheavey 0:df5216b20861 27 extern DigitalIn button_enter;
jamesheavey 0:df5216b20861 28 extern DigitalIn button_back;
jamesheavey 0:df5216b20861 29
jamesheavey 0:df5216b20861 30 // Potentiometers
jamesheavey 0:df5216b20861 31 extern AnalogIn pot_P;
jamesheavey 0:df5216b20861 32 extern AnalogIn pot_I;
jamesheavey 0:df5216b20861 33 extern AnalogIn pot_D;
jamesheavey 0:df5216b20861 34 extern AnalogIn pot_S;
jamesheavey 0:df5216b20861 35
jamesheavey 18:991658b628fc 36 // Sensors
jamesheavey 18:991658b628fc 37 extern DigitalInOut QTRA; //connected to digital P26
jamesheavey 18:991658b628fc 38 extern DigitalInOut QTRB; //connected to digital P25
jamesheavey 18:991658b628fc 39
jamesheavey 20:5cf6a378801d 40 // Timers
jamesheavey 21:54ea75f7984f 41 Timer t_L; // left encoder
jamesheavey 21:54ea75f7984f 42 Timer t_R; // right encoder
jamesheavey 21:54ea75f7984f 43 Timer t_coord; // coordinate timer
jamesheavey 0:df5216b20861 44
jamesheavey 0:df5216b20861 45 // Prototypes
jamesheavey 21:54ea75f7984f 46
jamesheavey 20:5cf6a378801d 47 void read_encoders();
jamesheavey 20:5cf6a378801d 48 void init();
jamesheavey 20:5cf6a378801d 49 void calibrate();
jamesheavey 20:5cf6a378801d 50 void follow_line();
jamesheavey 20:5cf6a378801d 51 bool junction_detect();
jamesheavey 20:5cf6a378801d 52 char junction_logic();
jamesheavey 20:5cf6a378801d 53 void turn_select( char turn );
jamesheavey 20:5cf6a378801d 54 void left();
jamesheavey 20:5cf6a378801d 55 void right();
jamesheavey 20:5cf6a378801d 56 void back();
jamesheavey 20:5cf6a378801d 57 void goal();
jamesheavey 29:ecf497c3fdc0 58 void looped_goal();
jamesheavey 20:5cf6a378801d 59 void simplify();
jamesheavey 20:5cf6a378801d 60 void invert_path();
jamesheavey 20:5cf6a378801d 61 void non_looped();
jamesheavey 20:5cf6a378801d 62 void looped();
jamesheavey 21:54ea75f7984f 63 void node_logic();
jamesheavey 24:adb946be4ce5 64 bool coord_check();
jamesheavey 25:7523239a2fc1 65 void update_index();
jamesheavey 28:63ff8290964a 66 void choose_turn();
jamesheavey 28:63ff8290964a 67 void back_track();
jamesheavey 29:ecf497c3fdc0 68 int path_to_point_index( int path_point );
jamesheavey 37:2967f3f9c936 69 void print_data( char *word );
jamesheavey 55:18ea9529afb2 70 bool dead_end_removal(int index1, int index2);
jamesheavey 58:cb32aa0f4116 71 void looped_goal_simplification();
jamesheavey 58:cb32aa0f4116 72 void goal_path_explored( bool inverse );
jamesheavey 0:df5216b20861 73
jamesheavey 20:5cf6a378801d 74 // Global Variables
jamesheavey 20:5cf6a378801d 75 char path[100];
jamesheavey 20:5cf6a378801d 76 char inv_path[100];
jamesheavey 20:5cf6a378801d 77 int path_length = 0;
jamesheavey 20:5cf6a378801d 78 unsigned int *sensor;
jamesheavey 20:5cf6a378801d 79 float speed;
jamesheavey 20:5cf6a378801d 80 float proportional = 0.0;
jamesheavey 20:5cf6a378801d 81 float prev_proportional = 0.0;
jamesheavey 20:5cf6a378801d 82 float integral = 0.0;
jamesheavey 20:5cf6a378801d 83 float derivative = 0.0;
jamesheavey 20:5cf6a378801d 84 int encoder[2];
jamesheavey 20:5cf6a378801d 85 int dist_est_1 = 0;
jamesheavey 20:5cf6a378801d 86 int dist_est_2 = 0;
jamesheavey 0:df5216b20861 87
jamesheavey 41:2b6b73dd897c 88 bool loop_check;
jamesheavey 60:ea69bd1a9889 89 //bool completed = false;
jamesheavey 60:ea69bd1a9889 90 bool _switch = false;
jamesheavey 38:b5b06625d06e 91 int dist = 0;
jamesheavey 23:71e84953b3f3 92 bool first = true;
jamesheavey 65:2103a02d72bc 93 bool first_g = true;
jamesheavey 27:0a3f028f9365 94 bool goal_node = false;
jamesheavey 21:54ea75f7984f 95 bool t_restart = true;
jamesheavey 23:71e84953b3f3 96 char dir;
jamesheavey 25:7523239a2fc1 97 int curr_index;
jamesheavey 21:54ea75f7984f 98 int curr_coords[2];
jamesheavey 23:71e84953b3f3 99 int total_points;
jamesheavey 24:adb946be4ce5 100 unsigned int looped_path[100];
jamesheavey 27:0a3f028f9365 101 unsigned int point[100]; // use a struct
jamesheavey 22:02dda79d50b4 102 unsigned int type[100];
jamesheavey 22:02dda79d50b4 103 unsigned int explored[100];
jamesheavey 21:54ea75f7984f 104 int coords_x[100];
jamesheavey 21:54ea75f7984f 105 int coords_y[100];
jamesheavey 48:76cf4521d342 106 int shortest[100];
jamesheavey 55:18ea9529afb2 107 int short_length;
jamesheavey 58:cb32aa0f4116 108 int goal_path1[100];
jamesheavey 58:cb32aa0f4116 109 int goal_length1;
jamesheavey 58:cb32aa0f4116 110 int goal_path2[100];
jamesheavey 58:cb32aa0f4116 111 int goal_length2;
jamesheavey 64:a58d8538132a 112 int goal_path3[100];
jamesheavey 64:a58d8538132a 113 int goal_path4[100];
jamesheavey 65:2103a02d72bc 114 int temp_path[100];
jamesheavey 65:2103a02d72bc 115 int temp_length;
jamesheavey 68:14f2a5315d88 116 char* start = "L16 cdegreg4";
jamesheavey 68:14f2a5315d88 117 bool goal_sound = false;
jamesheavey 68:14f2a5315d88 118 char* victory = "e e e eee d e g g ggg";
jamesheavey 68:14f2a5315d88 119 char* oops =
jamesheavey 68:14f2a5315d88 120 "v12 L16 o4 frc32<b32c32c#8cr8.erf";
jamesheavey 68:14f2a5315d88 121 char* doh =
jamesheavey 68:14f2a5315d88 122 "v12 L16 o4 c8.e8f#8ag8.e8c8<a<f#<f#<f#<g";
jamesheavey 68:14f2a5315d88 123 char* win =
jamesheavey 68:14f2a5315d88 124 "v12 L16 o5 eererce8g8r8<g8r8"
jamesheavey 68:14f2a5315d88 125 "c8.<gr8<e8.<a8<b8<b-<a8<g.e.g.a8fgre8cd<b8."
jamesheavey 68:14f2a5315d88 126 "c8.<gr8<e8.<a8<b8<b-<a8<g.e.g.a8fgre8cd<b8."
jamesheavey 68:14f2a5315d88 127 "r8gf#fd#8er<g#<acr<acd"
jamesheavey 68:14f2a5315d88 128 "r8gf#fd#8er>c8>c>c4"
jamesheavey 68:14f2a5315d88 129 "r8gf#fd#8er<g#<acr<acd"
jamesheavey 68:14f2a5315d88 130 "r8e-8rd8.c4";
jamesheavey 68:14f2a5315d88 131 // "O6 T40 L16 d#<b<f#<d#<f#<bd#f#"
jamesheavey 68:14f2a5315d88 132 // "T80 c#<b-<f#<c#<f#<b-c#8"
jamesheavey 68:14f2a5315d88 133 // "T180 d#b<f#d#f#>bd#f#c#b-<f#c#f#>b-c#8 c>c#<c#>c#<b>c#<c#>c#c>c#<c#>c#<b>c#<c#>c#"
jamesheavey 68:14f2a5315d88 134 // "c>c#<c#>c#<b->c#<c#>c#c>c#<c#>c#<b->c#<c#>c#"
jamesheavey 68:14f2a5315d88 135 // "c>c#<c#>c#f>c#<c#>c#c>c#<c#>c#f>c#<c#>c#"
jamesheavey 68:14f2a5315d88 136 // "c>c#<c#>c#f#>c#<c#>c#c>c#<c#>c#f#>c#<c#>c#d#bb-bd#bf#d#c#b-ab-c#b-f#d#";
jamesheavey 68:14f2a5315d88 137 char* bumblebee =
jamesheavey 68:14f2a5315d88 138 "! T144 L16 O6"
jamesheavey 68:14f2a5315d88 139 "ag#gf#gf#fe fee-dc#cO5bb-"
jamesheavey 68:14f2a5315d88 140 "ag#gf#gf#fe fee-dc#co4bb-"
jamesheavey 68:14f2a5315d88 141 "ag#gf#gf#fe ag#gf#gf#fe"
jamesheavey 68:14f2a5315d88 142 "ag#gf#fb-ag# ag#gf#ff#g#"
jamesheavey 68:14f2a5315d88 143 "ag#gf#fb-ag# ag#gf#ff#g#"
jamesheavey 68:14f2a5315d88 144 "ag#gf#gf#fe ff#gg#ab-ag#"
jamesheavey 68:14f2a5315d88 145 "ag#gf#gf#fe ff#gg#abO5cc#"
jamesheavey 68:14f2a5315d88 146 "dc#cO4bb-O5e-dc# dc#cO4bb-bO5cc#"
jamesheavey 68:14f2a5315d88 147 "dc#cO4bb-O5e-dc# dc#cO4bb-bO5cc#"
jamesheavey 68:14f2a5315d88 148 "dc#cO4b>cbb-a b-bO5cc#de-dc"
jamesheavey 68:14f2a5315d88 149 "dc#cO4b>cbb-a b-bO5cc#de-dc"
jamesheavey 68:14f2a5315d88 150 "dO4dMSdddddd e-de-O5e-e-de->e-"
jamesheavey 68:14f2a5315d88 151 "dddddddd e-de->e-e-de-O6e-"
jamesheavey 68:14f2a5315d88 152 "MLdO5e-dc#de-dc# de-dc#de-dc#"
jamesheavey 68:14f2a5315d88 153 "de-eff#fee- de-eff#fee-"
jamesheavey 68:14f2a5315d88 154 "dO4gMSgggggg a-ga->a-a-ga->a-"
jamesheavey 68:14f2a5315d88 155 "gO5ggggggg a-ga->a-a-ga-O6a-"
jamesheavey 68:14f2a5315d88 156 "MLfO5a-gf#ga-af# ga-gf#ga-gf#"
jamesheavey 68:14f2a5315d88 157 "ga-ab-bb-aa- ga-ab-bb-aa-"
jamesheavey 68:14f2a5315d88 158 "gf#fee-a-gf# gf#fee-eff#"
jamesheavey 68:14f2a5315d88 159 "gf#fefee-d e-eff#ff#gg#"
jamesheavey 68:14f2a5315d88 160 "ag#gf#gf#fe fee-dc#cO4bb-"
jamesheavey 68:14f2a5315d88 161 "ab-ag#ab-ag# ab-ag#ab-ag#"
jamesheavey 68:14f2a5315d88 162 "ab-ag#ab-ag# ab-ag#ab-ag#"
jamesheavey 68:14f2a5315d88 163 "ag#gf#gf#fe fee-dc#cO3bb-"
jamesheavey 68:14f2a5315d88 164 "ab-ag#ab-ag# ab-ag#ab-ag#"
jamesheavey 68:14f2a5315d88 165 "ab-ag#ab-ag# ab-ag#ab-ag#"
jamesheavey 68:14f2a5315d88 166 "ab-bO4cc#dd#e ff#gg#ab-bO5c"
jamesheavey 68:14f2a5315d88 167 "c#dd#eff#gg# ab-ag#ab-ag#"
jamesheavey 68:14f2a5315d88 168 "ag#gf#fb-ag# ag#gf#ff#g#"
jamesheavey 68:14f2a5315d88 169 "ag#gf#fb-ag# ag#gf#ff#g#"
jamesheavey 68:14f2a5315d88 170 "ag#gf#gf#fe ff#gg#ab-ag#"
jamesheavey 68:14f2a5315d88 171 "ag#gf#gf#fe ff#gg#abO5cc#"
jamesheavey 68:14f2a5315d88 172 "dc#cO4bb-O5e-dc# dc#cO4bb-bO5cc#"
jamesheavey 68:14f2a5315d88 173 "dc#cO4bb-O5e-dc# dc#cO4bb-bO5cc#"
jamesheavey 68:14f2a5315d88 174 "dc#cO4b>cbb-a b-bO5cc#de-dc"
jamesheavey 68:14f2a5315d88 175 "dc#cO4b>cbb-a b-bO5cc#de-dc"
jamesheavey 68:14f2a5315d88 176 "dO4dMSdddddd e-de-O5e-e-de->e-"
jamesheavey 68:14f2a5315d88 177 "dddddddd e-de->e-e-de-O6e-"
jamesheavey 68:14f2a5315d88 178 "MLdO5e-dc#de-dc# de-dc#de-dc#"
jamesheavey 68:14f2a5315d88 179 "de-eff#fee- de-eff#fee-"
jamesheavey 68:14f2a5315d88 180 "dO4gMSgggggg a-ga->a-a-ga->a-"
jamesheavey 68:14f2a5315d88 181 "gO5ggggggg a-ga->a-a-ga-O6a-"
jamesheavey 68:14f2a5315d88 182 "MLfO5a-gf#ga-af# ga-gf#ga-gf#"
jamesheavey 68:14f2a5315d88 183 "ga-ab-bb-aa- ga-ab-bb-aa-"
jamesheavey 68:14f2a5315d88 184 "gf#fee-a-gf# gf#fee-eff#"
jamesheavey 68:14f2a5315d88 185 "gf#fefee-d e-eff#ff#gg#"
jamesheavey 68:14f2a5315d88 186 "ag#gf#gf#fe fee-dc#cO4bb-"
jamesheavey 68:14f2a5315d88 187 "ab-ag#ab-ag# ab-ag#ab-ag#"
jamesheavey 68:14f2a5315d88 188 "ab-ag#ab-ag# ab-ag#ab-ag#"
jamesheavey 68:14f2a5315d88 189 "ag#gf#gf#fe fee-dc#cO3bb-"
jamesheavey 68:14f2a5315d88 190 "ab-ag#ab-ag# ab-ag#ab-ag#"
jamesheavey 68:14f2a5315d88 191 "ab-ag#ab-ag# ab-ag#ab-ag#"
jamesheavey 68:14f2a5315d88 192 "ab-bO4cc#dd#e ff#gg#ab-bO5c"
jamesheavey 68:14f2a5315d88 193 "c#dd#eff#gg# ab-ag#ab-ag#"
jamesheavey 68:14f2a5315d88 194 "ag#gf#fb-ag# ag#gf#ff#gg#"
jamesheavey 68:14f2a5315d88 195 "ag#gf#fb-ag# ag#gf#ff#gg#"
jamesheavey 68:14f2a5315d88 196 "ag#gf#gf#fe ff#gg#ab-ag#"
jamesheavey 68:14f2a5315d88 197 "ag#gf#gf#fe ff#gg#abO6cc#"
jamesheavey 68:14f2a5315d88 198 "dc#c<b<b-e-dc# dc#cO5bb-bO6cc#"
jamesheavey 68:14f2a5315d88 199 "dc#c<b<b-e-dc# dc#cO5bb-bO6cc#"
jamesheavey 68:14f2a5315d88 200 "dc#c<bcO5bb-a b-bO6cc#de-dc#"
jamesheavey 68:14f2a5315d88 201 "dc#cO5bb-bO6cc# defgab-ag#"
jamesheavey 68:14f2a5315d88 202 "ag#gf#fb-ag# ag#gf#ff#gg#"
jamesheavey 68:14f2a5315d88 203 "ag#gf#fb-ag# ag#gf#ff#gg#"
jamesheavey 68:14f2a5315d88 204 "a8c#de-eff# gf#fefee-d"
jamesheavey 68:14f2a5315d88 205 "c#dd#eff#gg# ab-ag#ab-ag#"
jamesheavey 68:14f2a5315d88 206 "a8O5c#de-eff# gf#fefee-d"
jamesheavey 68:14f2a5315d88 207 "c#dd#eff#gg# ab-ag#abO6cc#"
jamesheavey 68:14f2a5315d88 208 "dc#cO5bcbb-a b-ag#gf#fee-"
jamesheavey 68:14f2a5315d88 209 "dc#cO4b>cbb-a b-ag#gf#fee-"
jamesheavey 68:14f2a5315d88 210 "de-dc#e-de->e- de-dedfdg"
jamesheavey 68:14f2a5315d88 211 "ab-ag#b>abO5b aR<ab<bO6cO4bO6c#"
jamesheavey 68:14f2a5315d88 212 "L8dRL16O3ab-bO4c c#de-eff#gg#"
jamesheavey 68:14f2a5315d88 213 "ab-bO5cc#de-e ff#gg#abO6cc#"
jamesheavey 68:14f2a5315d88 214 "L2d O7d L4O5dR";
jamesheavey 0:df5216b20861 215 #endif