Agra-GPS / FreePilot_V2-3

Dependencies:   FreePilot PinDetect mbed-src

Fork of FreePilot_V2-2 by Agra-GPS

Committer:
maximbolduc
Date:
Sat Mar 14 15:44:33 2015 +0000
Revision:
50:07dfcda65732
Parent:
49:c43fa54dd990
Child:
51:f874574674c0
fixed checksum and refractoring bug

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maximbolduc 26:dc00998140af 1 #include "mbed.h"
maximbolduc 26:dc00998140af 2 #include "PinDetect.h"
maximbolduc 26:dc00998140af 3 #include "Point.h"
maximbolduc 26:dc00998140af 4 #include <vector>
maximbolduc 26:dc00998140af 5 #include "Line.h"
maximbolduc 26:dc00998140af 6 #include "stringUtils.h"
jhedmonton 28:5905886c76ee 7 #include "base.h"
jhedmonton 28:5905886c76ee 8 #include "Config.h"
maximbolduc 30:3afafa1ef16b 9 #include "imu_functions.h"
maximbolduc 32:c57bc701d65c 10 #include "atoh.h"
maximbolduc 47:d3123bb4f673 11 #include "string_utilities.h"
maximbolduc 34:c2bc9f9be7ff 12 #include "checksum.h"
maximbolduc 34:c2bc9f9be7ff 13 #include <string.h>
maximbolduc 47:d3123bb4f673 14 #include "gps.h"
maximbolduc 47:d3123bb4f673 15 #include "autosteer.h"
maximbolduc 35:f9caeb8ca31e 16
maximbolduc 30:3afafa1ef16b 17 char *version="FreePilot V2.11 Jtan 20, 2015\r\n";
jhedmonton 27:9ac59b261d87 18 long lastsend_version=0;
jhedmonton 27:9ac59b261d87 19 Timer vTimer; //this timer is int based! Max is 30 minutes
maximbolduc 35:f9caeb8ca31e 20
maximbolduc 50:07dfcda65732 21 //int checksumm;
maximbolduc 26:dc00998140af 22 double distance_from_line;
maximbolduc 26:dc00998140af 23 double cm_per_deg_lon;
maximbolduc 26:dc00998140af 24 double cm_per_deg_lat;
maximbolduc 26:dc00998140af 25 //all timing objects
maximbolduc 26:dc00998140af 26 Timer gps_connecting;
maximbolduc 26:dc00998140af 27 Timer autosteer_time;
maximbolduc 36:8e84b5ade03e 28 Timer autosteer_timeout; //timeout work near as timer, but they don't give timing. they just trigger an interrupt once the time we assigned it is passed.
maximbolduc 26:dc00998140af 29 Ticker accelerometerTicker;
maximbolduc 26:dc00998140af 30 Ticker gyroscopeTicker;
maximbolduc 26:dc00998140af 31 Ticker filterTicker;
maximbolduc 26:dc00998140af 32 Ticker angle_print;
maximbolduc 35:f9caeb8ca31e 33
jhedmonton 27:9ac59b261d87 34 //Motor
jhedmonton 27:9ac59b261d87 35 PinDetect motor_switch(p16); //pinDetect is close to digitalIn, althought, it can detect holds and detect the debounce.
jhedmonton 27:9ac59b261d87 36 DigitalOut enable_motor(p7);
maximbolduc 35:f9caeb8ca31e 37
maximbolduc 37:ac60a8a0ae8a 38 PwmOut pwm1(p21);
maximbolduc 37:ac60a8a0ae8a 39 PwmOut pwm2(p22);
maximbolduc 35:f9caeb8ca31e 40
jhedmonton 27:9ac59b261d87 41 //equipment switches
jhedmonton 27:9ac59b261d87 42 PinDetect boom1(p20,PullUp); //pinDetect is close to digitalIn, althought, it can detect holds and detect the debounce.
jhedmonton 27:9ac59b261d87 43 PinDetect boom2(p19); //pinDetect is close to digitalIn, althought, it can detect holds and detect the debounce.
jhedmonton 27:9ac59b261d87 44 PinDetect boom3(p18); //pinDetect is close to digitalIn, althought, it can detect holds and detect the debounce.
jhedmonton 27:9ac59b261d87 45 PinDetect boom4(p17); //pinDetect is close to digitalIn, althought, it can detect holds and detect the debounce.
maximbolduc 35:f9caeb8ca31e 46
jhedmonton 27:9ac59b261d87 47 char boom18; //1 byte
jhedmonton 27:9ac59b261d87 48 char lastboom18; //1 byte
maximbolduc 35:f9caeb8ca31e 49 char boomstate[8]= {'$','F','B','S',0,13,10,0 };
maximbolduc 35:f9caeb8ca31e 50
maximbolduc 35:f9caeb8ca31e 51 double filterg = 100;
maximbolduc 48:5d9c63364c94 52
maximbolduc 26:dc00998140af 53 Point looked_ahead;
maximbolduc 26:dc00998140af 54 Point line_start;
maximbolduc 26:dc00998140af 55 Point line_end;
maximbolduc 26:dc00998140af 56 Point tilt_compensated_position;
maximbolduc 26:dc00998140af 57 Point yaw_compensated_position;
maximbolduc 35:f9caeb8ca31e 58
maximbolduc 32:c57bc701d65c 59 extern int gyro_pos;
maximbolduc 26:dc00998140af 60 double distance_to_line;
maximbolduc 35:f9caeb8ca31e 61
maximbolduc 26:dc00998140af 62 //FreePilot variables
maximbolduc 26:dc00998140af 63 int timer_enabled;
jhedmonton 28:5905886c76ee 64 double motorspeed;
maximbolduc 26:dc00998140af 65 int enable_time;
maximbolduc 26:dc00998140af 66 char* motor_enable_state = 0;
jhedmonton 28:5905886c76ee 67 int motor_enable = 0;
jhedmonton 28:5905886c76ee 68 int lastmotor_enable = 1;
maximbolduc 26:dc00998140af 69 double pwm1_speed;
maximbolduc 26:dc00998140af 70 double pwm2_speed;
jhedmonton 28:5905886c76ee 71 long lastsend_motorstate=0;
jhedmonton 28:5905886c76ee 72 Timer motTimer; //this timer is int based! Max is 30 minutes
jhedmonton 28:5905886c76ee 73 Timer btTimer; //measure time for Bluetooth communication
jhedmonton 28:5905886c76ee 74 long lastgetBT=0;
maximbolduc 35:f9caeb8ca31e 75
maximbolduc 50:07dfcda65732 76 //int msg2_changed = 1;
maximbolduc 50:07dfcda65732 77 //char* buffer;
maximbolduc 50:07dfcda65732 78 //double meter_lat = 0;
maximbolduc 50:07dfcda65732 79 //double meter_lon = 0;
maximbolduc 35:f9caeb8ca31e 80
maximbolduc 26:dc00998140af 81 char msg[256]; //GPS line buffer
maximbolduc 26:dc00998140af 82 char msg2[256];//PC line buffer
maximbolduc 45:ecd8c2e27948 83 char* result;
maximbolduc 26:dc00998140af 84 int printing;
maximbolduc 26:dc00998140af 85 float longitude;
maximbolduc 26:dc00998140af 86 float latitude;
maximbolduc 26:dc00998140af 87 char ns, ew;
maximbolduc 26:dc00998140af 88 int lock;
maximbolduc 50:07dfcda65732 89 //int flag_gga;
maximbolduc 50:07dfcda65732 90 //int reading;
maximbolduc 48:5d9c63364c94 91
maximbolduc 26:dc00998140af 92 int connect_time = 10000; //variable to change the time that the serial output all the strings in order to verify if the command was right.
maximbolduc 26:dc00998140af 93 int connecting = 0; //are we still in phase of connecting? based on the connect_time value.
maximbolduc 35:f9caeb8ca31e 94
maximbolduc 26:dc00998140af 95 int angle_send = 0;
maximbolduc 26:dc00998140af 96 int correct_rmc = 1;
maximbolduc 50:07dfcda65732 97 //double m_lat = 0;
maximbolduc 50:07dfcda65732 98 //double m_lon = 0;
maximbolduc 26:dc00998140af 99 char* degminsec;
maximbolduc 26:dc00998140af 100 double m_per_deg_lon;
maximbolduc 26:dc00998140af 101 double m_per_deg_lat;
maximbolduc 26:dc00998140af 102 double look_ahead_lon;
maximbolduc 26:dc00998140af 103 double look_ahead_lat;
maximbolduc 26:dc00998140af 104 int active_AB = 0;
maximbolduc 50:07dfcda65732 105 //double compensation_vector;
maximbolduc 26:dc00998140af 106 char output[256];
maximbolduc 35:f9caeb8ca31e 107
maximbolduc 26:dc00998140af 108 double yaw;
maximbolduc 26:dc00998140af 109 double pitch;
maximbolduc 26:dc00998140af 110 double roll;
maximbolduc 30:3afafa1ef16b 111 double a_x;
maximbolduc 30:3afafa1ef16b 112 double a_y;
maximbolduc 30:3afafa1ef16b 113 double a_z;
maximbolduc 30:3afafa1ef16b 114 double w_x;
maximbolduc 30:3afafa1ef16b 115 double w_y;
maximbolduc 30:3afafa1ef16b 116 double w_z;
maximbolduc 35:f9caeb8ca31e 117
maximbolduc 26:dc00998140af 118 int readings[3];
maximbolduc 26:dc00998140af 119 double Freepilot_bearing;
maximbolduc 36:8e84b5ade03e 120 int time_till_stop = 200;
maximbolduc 50:07dfcda65732 121 //volatile bool newline_detected = false;
maximbolduc 35:f9caeb8ca31e 122
maximbolduc 35:f9caeb8ca31e 123 void autosteer_done()
maximbolduc 35:f9caeb8ca31e 124 {
maximbolduc 35:f9caeb8ca31e 125 enable_motor = 0;
maximbolduc 35:f9caeb8ca31e 126 }
maximbolduc 35:f9caeb8ca31e 127
maximbolduc 26:dc00998140af 128 Point compensation;
maximbolduc 35:f9caeb8ca31e 129
maximbolduc 26:dc00998140af 130 void yaw_compensate()
maximbolduc 26:dc00998140af 131 {
maximbolduc 33:3e71c418e90d 132 yaw = get_yaw();
maximbolduc 26:dc00998140af 133 }
maximbolduc 34:c2bc9f9be7ff 134
maximbolduc 35:f9caeb8ca31e 135 void pitch_and_roll(double real_bearing)
maximbolduc 34:c2bc9f9be7ff 136 {
maximbolduc 34:c2bc9f9be7ff 137 pitch = get_pitch();
maximbolduc 34:c2bc9f9be7ff 138 roll = get_roll();
maximbolduc 34:c2bc9f9be7ff 139 compensation.SetX(antennaheight * tan(roll) * sin(real_bearing/ 57.295779513)-antennaheight * tan(pitch) * cos(real_bearing/ 57.295779513));
maximbolduc 34:c2bc9f9be7ff 140 compensation.SetY(antennaheight * tan(roll) * cos(real_bearing/ 57.295779513)-antennaheight * tan(pitch) * sin(real_bearing/ 57.295779513));
maximbolduc 34:c2bc9f9be7ff 141 }
maximbolduc 33:3e71c418e90d 142
maximbolduc 50:07dfcda65732 143
maximbolduc 26:dc00998140af 144 void process_GPSHEIGHT(char* height_string)
maximbolduc 26:dc00998140af 145 {
maximbolduc 26:dc00998140af 146 char *token;
maximbolduc 26:dc00998140af 147 int token_counter = 0;
maximbolduc 26:dc00998140af 148 char *height = (char *)NULL;
maximbolduc 26:dc00998140af 149 token = strtok(height_string, ",");
maximbolduc 35:f9caeb8ca31e 150 while (token) {
maximbolduc 35:f9caeb8ca31e 151 switch (token_counter) {
maximbolduc 26:dc00998140af 152 case 1:
maximbolduc 26:dc00998140af 153 height = token;
maximbolduc 26:dc00998140af 154 break;
maximbolduc 26:dc00998140af 155 }
maximbolduc 26:dc00998140af 156 token = strtok((char *)NULL, ",");
maximbolduc 26:dc00998140af 157 token_counter++;
maximbolduc 26:dc00998140af 158 }
maximbolduc 35:f9caeb8ca31e 159 if ( height ) {
jhedmonton 28:5905886c76ee 160 antennaheight = atof(height);
maximbolduc 42:854d8cc26bbb 161 // Config_Save();
maximbolduc 26:dc00998140af 162 }
maximbolduc 26:dc00998140af 163 }
maximbolduc 26:dc00998140af 164
maximbolduc 35:f9caeb8ca31e 165 //sets pwm1 and pwm2 and enable_motor
maximbolduc 50:07dfcda65732 166 void process_ASTEER(char* asteer)
maximbolduc 46:d7d6dc429153 167 {
maximbolduc 50:07dfcda65732 168 char *token;
maximbolduc 50:07dfcda65732 169 int token_counter = 0;
maximbolduc 50:07dfcda65732 170 char *asteer_speed = (char *)NULL;
maximbolduc 50:07dfcda65732 171 char *asteer_time = (char *)NULL;
maximbolduc 50:07dfcda65732 172 token = strtok(asteer, ",");
maximbolduc 50:07dfcda65732 173 while (token) {
maximbolduc 50:07dfcda65732 174 switch (token_counter) {
maximbolduc 50:07dfcda65732 175 case 1:
maximbolduc 50:07dfcda65732 176 asteer_speed = token;
maximbolduc 50:07dfcda65732 177 break;
maximbolduc 50:07dfcda65732 178 case 2:
maximbolduc 50:07dfcda65732 179 asteer_time = token;
maximbolduc 50:07dfcda65732 180 break;
maximbolduc 35:f9caeb8ca31e 181 }
maximbolduc 50:07dfcda65732 182 token = strtok((char *)NULL, ",");
maximbolduc 50:07dfcda65732 183 token_counter++;
maximbolduc 50:07dfcda65732 184 }
maximbolduc 50:07dfcda65732 185 if ( asteer_speed && asteer_time ) {
maximbolduc 50:07dfcda65732 186 motorspeed = atof(asteer_speed);
maximbolduc 50:07dfcda65732 187 enable_time = atof(asteer_time);
maximbolduc 50:07dfcda65732 188 autosteer_timeout.reset();
maximbolduc 50:07dfcda65732 189 time_till_stop = atoi(asteer_time);
maximbolduc 50:07dfcda65732 190 if ( motor_enable == 0 ) {
maximbolduc 50:07dfcda65732 191 } else {
maximbolduc 50:07dfcda65732 192 if ( motorspeed > 127.0 ) {
maximbolduc 50:07dfcda65732 193 pwm2_speed = 0.0;
maximbolduc 50:07dfcda65732 194 pwm1_speed = ((double)motorspeed - (double)127.0) / 128.0;
maximbolduc 37:ac60a8a0ae8a 195
maximbolduc 50:07dfcda65732 196 } else if ( motorspeed < 127.0 ) {
maximbolduc 50:07dfcda65732 197 pwm1_speed = 0.0;
maximbolduc 50:07dfcda65732 198 pwm2_speed = ( ((double)127-(double)motorspeed) / 128.0 );
maximbolduc 50:07dfcda65732 199 } else {
maximbolduc 50:07dfcda65732 200 pwm1_speed = 0;
maximbolduc 50:07dfcda65732 201 pwm2_speed = 0;
maximbolduc 50:07dfcda65732 202 enable_motor = 0;
maximbolduc 37:ac60a8a0ae8a 203 }
maximbolduc 50:07dfcda65732 204 // if(Authenticated)
maximbolduc 50:07dfcda65732 205 // {
maximbolduc 50:07dfcda65732 206 pwm1 = pwm1_speed;
maximbolduc 50:07dfcda65732 207 pwm2 = pwm2_speed;
maximbolduc 50:07dfcda65732 208 enable_motor = 1;
maximbolduc 50:07dfcda65732 209 //}
maximbolduc 35:f9caeb8ca31e 210 }
maximbolduc 35:f9caeb8ca31e 211 }
maximbolduc 50:07dfcda65732 212
maximbolduc 41:a26acd346c2f 213 }
maximbolduc 41:a26acd346c2f 214
maximbolduc 48:5d9c63364c94 215 void action_on_rmc()
maximbolduc 26:dc00998140af 216 {
maximbolduc 39:6767d4c840f9 217 double angle = ((int)((track-90 )* -1 + 360) + 180) % 360 - 180;
maximbolduc 39:6767d4c840f9 218 double diff_angle = Freepilot_bearing - angle;
maximbolduc 39:6767d4c840f9 219 diff_angle = ((int)diff_angle + 180) % 360 - 180;
maximbolduc 39:6767d4c840f9 220
maximbolduc 39:6767d4c840f9 221 // pc.printf("%f %f %f\r\n",diff_angle, Freepilot_bearing, (track - 90) * -1);
maximbolduc 39:6767d4c840f9 222 if ( abs(diff_angle) > 90 ) {
maximbolduc 39:6767d4c840f9 223 if ( (abs(360 - diff_angle)) > 90 ) {
maximbolduc 39:6767d4c840f9 224 Point temp = line_end;
maximbolduc 39:6767d4c840f9 225 line_end = line_start;
maximbolduc 39:6767d4c840f9 226 line_start = temp;
maximbolduc 39:6767d4c840f9 227 Freepilot_bearing = ((int) Freepilot_bearing + 360) % 360 - 180;
maximbolduc 39:6767d4c840f9 228 }
maximbolduc 39:6767d4c840f9 229 }
maximbolduc 48:5d9c63364c94 230 cm_per_deg_lat = 11054000;
maximbolduc 48:5d9c63364c94 231 cm_per_deg_lon = 11132000 * cos(decimal_latitude);
maximbolduc 48:5d9c63364c94 232
maximbolduc 48:5d9c63364c94 233 pitch_and_roll((track-90)*-1);// as to be real bearing
maximbolduc 35:f9caeb8ca31e 234
maximbolduc 48:5d9c63364c94 235 compensation.SetY(compensation.GetY() / cm_per_deg_lon);
maximbolduc 48:5d9c63364c94 236 compensation.SetX(compensation.GetX() / cm_per_deg_lat);
maximbolduc 35:f9caeb8ca31e 237
maximbolduc 48:5d9c63364c94 238 position = point_add(position,compensation);
maximbolduc 35:f9caeb8ca31e 239
maximbolduc 48:5d9c63364c94 240 double lookaheaddistance = lookaheadtime * speed_m_s;
maximbolduc 48:5d9c63364c94 241 get_latlon_byangle(position.GetX(),position.GetY(),lookaheaddistance,(track-90)*-1,look_ahead_lon,look_ahead_lat);
maximbolduc 48:5d9c63364c94 242 looked_ahead.SetX(look_ahead_lat);
maximbolduc 48:5d9c63364c94 243 looked_ahead.SetY(look_ahead_lon);
maximbolduc 48:5d9c63364c94 244 double filtering = sqrt(111111.0*111111.0 + 111111.0 * cos(decimal_latitude/57.295779513)*111111.0 * cos(decimal_latitude/57.295779513));
maximbolduc 48:5d9c63364c94 245 distance_to_line = dist_Point_to_Line( looked_ahead,line_start,line_end) * isLeft( line_start,line_end,looked_ahead) * filtering;/////////////////////////////////////////////////
maximbolduc 35:f9caeb8ca31e 246
maximbolduc 48:5d9c63364c94 247 SetDigitalFilter(phaseadv,tcenter, 0 );
maximbolduc 48:5d9c63364c94 248 string steering = ControlSteerFilter(225, distance_to_line, speed_km , filterg, 20.0, 75.0, scale);
maximbolduc 35:f9caeb8ca31e 249
maximbolduc 48:5d9c63364c94 250 char command[128];
maximbolduc 48:5d9c63364c94 251 sprintf(command,"%s\r\n\0",steering.c_str()); //(int)((((val/2)-127)/scale)+127),500);
maximbolduc 50:07dfcda65732 252 // pc.puts(command);
maximbolduc 41:a26acd346c2f 253
maximbolduc 50:07dfcda65732 254 process_ASTEER(command);
maximbolduc 48:5d9c63364c94 255
maximbolduc 34:c2bc9f9be7ff 256 string rmc = "";
maximbolduc 48:5d9c63364c94 257
maximbolduc 48:5d9c63364c94 258 rmc = "$GPRMC,";
maximbolduc 50:07dfcda65732 259 rmc += not_equal(string(time_s));
maximbolduc 50:07dfcda65732 260 rmc += not_equal(string(stat));
maximbolduc 50:07dfcda65732 261 rmc += string(To_DMS(position.GetX())) + "," + not_equal(string(lat_dir));
maximbolduc 50:07dfcda65732 262 rmc += (string(To_DMS_lon(position.GetY())) + "," + not_equal(string(lon_dir)));
maximbolduc 50:07dfcda65732 263 rmc += not_equal(string(vel));
maximbolduc 50:07dfcda65732 264 rmc += not_equal(string(trk));
maximbolduc 50:07dfcda65732 265 rmc += not_equal(string(date));
maximbolduc 50:07dfcda65732 266 rmc += not_equal(string(magv));
maximbolduc 50:07dfcda65732 267 rmc += not_equal(string(magd)) + "W";
maximbolduc 34:c2bc9f9be7ff 268
maximbolduc 34:c2bc9f9be7ff 269 char test[256];
maximbolduc 38:b5352d6f8166 270 sprintf(test,"%s*\0",rmc.c_str());
maximbolduc 38:b5352d6f8166 271 sprintf(output,"%s*%02X\r\n\0",rmc.c_str(),getCheckSum(test));
maximbolduc 50:07dfcda65732 272 pc.puts("\r\n");
maximbolduc 50:07dfcda65732 273 pc.puts(output);
maximbolduc 50:07dfcda65732 274 pc.puts("\r\n");
maximbolduc 50:07dfcda65732 275 bluetooth.puts("\r\n");
maximbolduc 34:c2bc9f9be7ff 276 bluetooth.puts(output);
maximbolduc 26:dc00998140af 277 }
maximbolduc 35:f9caeb8ca31e 278
maximbolduc 26:dc00998140af 279 void process_FGPSAB(char* ab)
maximbolduc 26:dc00998140af 280 {
maximbolduc 33:3e71c418e90d 281 char *token;
maximbolduc 33:3e71c418e90d 282 int token_counter = 0;
maximbolduc 33:3e71c418e90d 283 char *line_lat = (char *)NULL;
maximbolduc 33:3e71c418e90d 284 char *line_lon = (char *)NULL;
maximbolduc 33:3e71c418e90d 285 char *line_lat1 = (char *)NULL;
maximbolduc 33:3e71c418e90d 286 char *line_lon1 = (char *)NULL;
maximbolduc 38:b5352d6f8166 287 //char *bearing = (char *)NULL;
maximbolduc 38:b5352d6f8166 288 string bearing = "";
maximbolduc 33:3e71c418e90d 289 token = strtok(ab, ",");
maximbolduc 35:f9caeb8ca31e 290 while (token) {
maximbolduc 35:f9caeb8ca31e 291 switch (token_counter) {
maximbolduc 33:3e71c418e90d 292 case 1:
maximbolduc 33:3e71c418e90d 293 line_lat = token;
maximbolduc 33:3e71c418e90d 294 break;
maximbolduc 33:3e71c418e90d 295 case 2:
maximbolduc 33:3e71c418e90d 296 line_lon = token;
maximbolduc 33:3e71c418e90d 297 break;
maximbolduc 33:3e71c418e90d 298 case 3:
maximbolduc 33:3e71c418e90d 299 line_lat1 = token;
maximbolduc 33:3e71c418e90d 300 break;
maximbolduc 33:3e71c418e90d 301 case 4:
maximbolduc 33:3e71c418e90d 302 line_lon1 = token;
maximbolduc 33:3e71c418e90d 303 break;
maximbolduc 33:3e71c418e90d 304 case 5:
maximbolduc 45:ecd8c2e27948 305 bearing = token;
maximbolduc 33:3e71c418e90d 306 break;
maximbolduc 26:dc00998140af 307 }
maximbolduc 33:3e71c418e90d 308 token = strtok((char *)NULL, ",");
maximbolduc 33:3e71c418e90d 309 token_counter++;
maximbolduc 33:3e71c418e90d 310 }
maximbolduc 39:6767d4c840f9 311 double Freepilot_lon = atof(line_lon);
maximbolduc 39:6767d4c840f9 312 double Freepilot_lat = atof(line_lat);
maximbolduc 39:6767d4c840f9 313 double Freepilot_lon1 = atof(line_lon1);
maximbolduc 39:6767d4c840f9 314 double Freepilot_lat1 = atof(line_lat1);
maximbolduc 39:6767d4c840f9 315 Freepilot_bearing = atof(bearing.c_str()) + 360;
maximbolduc 39:6767d4c840f9 316 Freepilot_bearing = ((int)Freepilot_bearing+ 180) % 360 - 180;
maximbolduc 33:3e71c418e90d 317 line_start.SetX(Freepilot_lat);
maximbolduc 33:3e71c418e90d 318 line_start.SetY(Freepilot_lon);
maximbolduc 33:3e71c418e90d 319 line_end.SetX(Freepilot_lat1);
maximbolduc 33:3e71c418e90d 320 line_end.SetY(Freepilot_lon1);
maximbolduc 39:6767d4c840f9 321
maximbolduc 33:3e71c418e90d 322 active_AB = 1;
maximbolduc 26:dc00998140af 323 }
maximbolduc 35:f9caeb8ca31e 324
maximbolduc 26:dc00998140af 325 void process_FGPSAUTO(char* FGPSAUTO)
maximbolduc 26:dc00998140af 326 {
maximbolduc 26:dc00998140af 327 char *token;
maximbolduc 26:dc00998140af 328 int token_counter = 0;
maximbolduc 26:dc00998140af 329 char *ahead = (char *)NULL;
maximbolduc 26:dc00998140af 330 char *center = (char *)NULL;
maximbolduc 26:dc00998140af 331 char *phase = (char *)NULL;
maximbolduc 26:dc00998140af 332 char *scl = (char *)NULL;
maximbolduc 26:dc00998140af 333 char *avg = (char *)NULL;
maximbolduc 26:dc00998140af 334 char *_kp = (char *)NULL;
maximbolduc 26:dc00998140af 335 char *_ki = (char *)NULL;
maximbolduc 35:f9caeb8ca31e 336 char *fg = (char *)NULL;
maximbolduc 26:dc00998140af 337 char *_kd = (char *)NULL;
maximbolduc 39:6767d4c840f9 338
maximbolduc 26:dc00998140af 339 token = strtok(FGPSAUTO, ",");
maximbolduc 35:f9caeb8ca31e 340 while (token) {
maximbolduc 35:f9caeb8ca31e 341 switch (token_counter) {
maximbolduc 26:dc00998140af 342 case 1:
maximbolduc 26:dc00998140af 343 phase = token;
maximbolduc 26:dc00998140af 344 break;
maximbolduc 26:dc00998140af 345 case 2:
maximbolduc 26:dc00998140af 346 center = token;
maximbolduc 26:dc00998140af 347 break;
maximbolduc 34:c2bc9f9be7ff 348 case 3:
maximbolduc 34:c2bc9f9be7ff 349 fg = token;
maximbolduc 34:c2bc9f9be7ff 350 break;
maximbolduc 26:dc00998140af 351 case 4:
maximbolduc 26:dc00998140af 352 scl = token;
maximbolduc 26:dc00998140af 353 break;
maximbolduc 26:dc00998140af 354 case 5:
maximbolduc 26:dc00998140af 355 ahead = token;
maximbolduc 26:dc00998140af 356 break;
maximbolduc 26:dc00998140af 357 case 6:
maximbolduc 26:dc00998140af 358 avg = token;
maximbolduc 26:dc00998140af 359 break;
maximbolduc 26:dc00998140af 360 case 7:
maximbolduc 26:dc00998140af 361 _kp = token;
maximbolduc 26:dc00998140af 362 break;
maximbolduc 26:dc00998140af 363 case 8:
maximbolduc 26:dc00998140af 364 _ki = token;
maximbolduc 26:dc00998140af 365 break;
maximbolduc 26:dc00998140af 366 case 9:
maximbolduc 26:dc00998140af 367 _kd = token;
maximbolduc 26:dc00998140af 368 break;
maximbolduc 26:dc00998140af 369 }
maximbolduc 26:dc00998140af 370 token = strtok((char *)NULL, ",");
maximbolduc 26:dc00998140af 371 token_counter++;
maximbolduc 26:dc00998140af 372 }
maximbolduc 35:f9caeb8ca31e 373 if ( _kp && _ki && _kd ) {
maximbolduc 26:dc00998140af 374 kp = atof(_kp);
maximbolduc 26:dc00998140af 375 ki = atof(_ki);
maximbolduc 26:dc00998140af 376 kd = atof(_kd);
maximbolduc 26:dc00998140af 377 }
maximbolduc 35:f9caeb8ca31e 378 if ( phase && center && scl && avg && ahead ) {
jhedmonton 28:5905886c76ee 379 lookaheadtime = atof(ahead);
maximbolduc 41:a26acd346c2f 380 scale = atof(scl);
maximbolduc 26:dc00998140af 381 phaseadv = atof(phase);
jhedmonton 28:5905886c76ee 382 avgpos = atof(avg);
jhedmonton 28:5905886c76ee 383 tcenter = atof(center);
maximbolduc 34:c2bc9f9be7ff 384 filterg = atof(fg);
maximbolduc 38:b5352d6f8166 385
maximbolduc 41:a26acd346c2f 386 // scale = scale * -1;
maximbolduc 41:a26acd346c2f 387 SetDigitalFilter(phaseadv,tcenter, 0 );
maximbolduc 26:dc00998140af 388 }
maximbolduc 26:dc00998140af 389 }
maximbolduc 35:f9caeb8ca31e 390
maximbolduc 26:dc00998140af 391 void pc_analyse(char* pc_string)
maximbolduc 26:dc00998140af 392 {
maximbolduc 50:07dfcda65732 393 // pc.puts(pc_string);
maximbolduc 35:f9caeb8ca31e 394 if (!strncmp(pc_string, "$ASTEER", 7)) {
maximbolduc 39:6767d4c840f9 395 //stop sending when already exists
maximbolduc 50:07dfcda65732 396 // process_ASTEER(pc_string);
maximbolduc 35:f9caeb8ca31e 397 } else if (!strncmp(pc_string, "$BANY",5)) {
jhedmonton 29:23ccb2a50b6f 398 _ID = Config_GetID();
maximbolduc 42:854d8cc26bbb 399 // Config_Save();
maximbolduc 50:07dfcda65732 400 } else if (!strncmp(pc_string, "$FGPS-BAUD",10)) {
maximbolduc 50:07dfcda65732 401 pc.puts("\r\n");
maximbolduc 50:07dfcda65732 402 pc.puts(pc_string);
maximbolduc 50:07dfcda65732 403 pc.puts("\r\n");
maximbolduc 45:ecd8c2e27948 404
maximbolduc 45:ecd8c2e27948 405 process_GPSBAUD(pc_string);
maximbolduc 42:854d8cc26bbb 406 // Config_Save();
maximbolduc 35:f9caeb8ca31e 407 } else if (!strncmp(pc_string, "$FGPSAUTO",9)) {
maximbolduc 34:c2bc9f9be7ff 408 process_FGPSAUTO(pc_string);
maximbolduc 34:c2bc9f9be7ff 409 sprintf(output,"%s\r\n",pc_string);
maximbolduc 34:c2bc9f9be7ff 410 bluetooth.puts(output);
maximbolduc 42:854d8cc26bbb 411 // Config_Save();
maximbolduc 35:f9caeb8ca31e 412 } else if (!strncmp(pc_string, "$FGPS,",6)) {
maximbolduc 50:07dfcda65732 413 pc.puts(pc_string);
maximbolduc 35:f9caeb8ca31e 414 int i=5;
maximbolduc 35:f9caeb8ca31e 415 char c=pc_string[i];
maximbolduc 35:f9caeb8ca31e 416 while (c!=0) {
maximbolduc 35:f9caeb8ca31e 417 i++;
maximbolduc 35:f9caeb8ca31e 418 if (i>255) break; //protect msg buffer!
maximbolduc 35:f9caeb8ca31e 419 c=pc_string[i];
maximbolduc 35:f9caeb8ca31e 420 gps.putc(c);
maximbolduc 50:07dfcda65732 421 // pc.putc(c);
maximbolduc 35:f9caeb8ca31e 422 }
maximbolduc 36:8e84b5ade03e 423 } else if (!strncmp(pc_string, "$GPSHEIGHT",10)) {
maximbolduc 26:dc00998140af 424 process_GPSHEIGHT(pc_string);
maximbolduc 26:dc00998140af 425 sprintf(output,"%s\r\n",pc_string);
maximbolduc 26:dc00998140af 426 bluetooth.puts(output);
maximbolduc 42:854d8cc26bbb 427 // Config_Save();
maximbolduc 37:ac60a8a0ae8a 428 } else if (!strncmp(pc_string, "$FGPSAB",7)) {
maximbolduc 26:dc00998140af 429 process_FGPSAB(pc_string);
maximbolduc 35:f9caeb8ca31e 430 } else if (!strncmp(pc_string, "$CALIBRATEACCEL",15)) {
maximbolduc 32:c57bc701d65c 431 calibrateGyroscope();
maximbolduc 30:3afafa1ef16b 432 calibrateAccelerometer();
maximbolduc 42:854d8cc26bbb 433 // Config_Save();
maximbolduc 35:f9caeb8ca31e 434 } else if (!strncmp(pc_string, "$POSITION",9)) {
maximbolduc 32:c57bc701d65c 435 char* pointer;
maximbolduc 32:c57bc701d65c 436 char* Data[5];
maximbolduc 32:c57bc701d65c 437 int index = 0;
maximbolduc 32:c57bc701d65c 438 //Split data at commas
maximbolduc 32:c57bc701d65c 439 pointer = strtok(pc_string, ",");
maximbolduc 32:c57bc701d65c 440 if(pointer == NULL)
maximbolduc 32:c57bc701d65c 441 Data[0] = pc_string;
maximbolduc 35:f9caeb8ca31e 442 while(pointer != NULL) {
maximbolduc 32:c57bc701d65c 443 Data[index] = pointer;
maximbolduc 32:c57bc701d65c 444 pointer = strtok(NULL, ",");
maximbolduc 32:c57bc701d65c 445 index++;
maximbolduc 32:c57bc701d65c 446 }
maximbolduc 32:c57bc701d65c 447 gyro_pos = atoi(Data[1]);
maximbolduc 42:854d8cc26bbb 448 // Config_Save();
maximbolduc 35:f9caeb8ca31e 449 } else {
maximbolduc 50:07dfcda65732 450 pc.puts("\r\n");
maximbolduc 50:07dfcda65732 451 pc.puts(pc_string);
maximbolduc 50:07dfcda65732 452 pc.puts("\r\n");
maximbolduc 50:07dfcda65732 453
maximbolduc 26:dc00998140af 454 }
maximbolduc 26:dc00998140af 455 }
maximbolduc 35:f9caeb8ca31e 456
maximbolduc 26:dc00998140af 457 void gps_analyse(char* gps_string)
maximbolduc 26:dc00998140af 458 {
maximbolduc 50:07dfcda65732 459 string temp(gps_string);
maximbolduc 50:07dfcda65732 460 while ( temp.find ("\r\n") != string::npos ) {
maximbolduc 50:07dfcda65732 461 temp.erase ( temp.find ("\r\n"), 2 );
maximbolduc 50:07dfcda65732 462 }
maximbolduc 50:07dfcda65732 463 sprintf(gps_string,"%s",temp.c_str());
maximbolduc 50:07dfcda65732 464
maximbolduc 35:f9caeb8ca31e 465 if (!strncmp(gps_string, "$GPRMC", 6)) {
maximbolduc 50:07dfcda65732 466
maximbolduc 48:5d9c63364c94 467 if (nmea_rmc(gps_string)) {
maximbolduc 50:07dfcda65732 468 action_on_rmc();
maximbolduc 48:5d9c63364c94 469 } //analyse and decompose the rmc string
maximbolduc 50:07dfcda65732 470 else {
maximbolduc 50:07dfcda65732 471 char test[256];
maximbolduc 50:07dfcda65732 472 sprintf(test,"%s*",temp.c_str());
maximbolduc 50:07dfcda65732 473 sprintf(output,"%s*%02X\r\n\0",temp.c_str(),getCheckSum(test));
maximbolduc 50:07dfcda65732 474 bluetooth.puts(output);
maximbolduc 50:07dfcda65732 475 pc.puts(output);
maximbolduc 50:07dfcda65732 476 }
maximbolduc 36:8e84b5ade03e 477 } else {
maximbolduc 50:07dfcda65732 478 char test[256];
maximbolduc 50:07dfcda65732 479 sprintf(test,"%s*\0",gps_string);
maximbolduc 50:07dfcda65732 480 sprintf(output,"%s*%02X\r\n\0",gps_string,getCheckSum(test));
maximbolduc 50:07dfcda65732 481 bluetooth.puts(output);
maximbolduc 50:07dfcda65732 482 pc.puts(output);
maximbolduc 33:3e71c418e90d 483 }
maximbolduc 26:dc00998140af 484 }
maximbolduc 35:f9caeb8ca31e 485
maximbolduc 50:07dfcda65732 486 static int i2 = 0;
maximbolduc 50:07dfcda65732 487 static bool end2 = false;
maximbolduc 50:07dfcda65732 488 static bool start2 = false;
maximbolduc 35:f9caeb8ca31e 489
jhedmonton 27:9ac59b261d87 490 bool getline2()
maximbolduc 26:dc00998140af 491 {
jhedmonton 27:9ac59b261d87 492 int gotstring=false;
maximbolduc 35:f9caeb8ca31e 493 while (1) {
maximbolduc 35:f9caeb8ca31e 494 if( !bluetooth.readable() ) {
jhedmonton 27:9ac59b261d87 495 break;
jhedmonton 27:9ac59b261d87 496 }
jhedmonton 27:9ac59b261d87 497 char c = bluetooth.getc();
maximbolduc 35:f9caeb8ca31e 498 if (c == 36 ) {
maximbolduc 33:3e71c418e90d 499 start2=true;
maximbolduc 33:3e71c418e90d 500 end2 = false;
maximbolduc 33:3e71c418e90d 501 i2 = 0;
maximbolduc 33:3e71c418e90d 502 }
maximbolduc 35:f9caeb8ca31e 503 if ((start2) && (c == 10)) {
maximbolduc 33:3e71c418e90d 504 end2=true;
jhedmonton 29:23ccb2a50b6f 505 start2 = false;
jhedmonton 29:23ccb2a50b6f 506 }
maximbolduc 35:f9caeb8ca31e 507 if (start2) {
jhedmonton 27:9ac59b261d87 508 msg2[i2]=c;
jhedmonton 27:9ac59b261d87 509 i2++;
jhedmonton 27:9ac59b261d87 510 if (i2>255) break; //protect msg buffer!
jhedmonton 27:9ac59b261d87 511 }
maximbolduc 35:f9caeb8ca31e 512 if (end2) {
maximbolduc 33:3e71c418e90d 513 msg2[i2]=c;
jhedmonton 27:9ac59b261d87 514 msg2[i2+1] = 0;
jhedmonton 27:9ac59b261d87 515 start2 = false;
jhedmonton 27:9ac59b261d87 516 gotstring = true;
jhedmonton 27:9ac59b261d87 517 end2=false;
jhedmonton 27:9ac59b261d87 518 i2=0;
jhedmonton 27:9ac59b261d87 519 break;
maximbolduc 26:dc00998140af 520 }
maximbolduc 26:dc00998140af 521 }
jhedmonton 27:9ac59b261d87 522 return gotstring;
maximbolduc 26:dc00998140af 523 }
maximbolduc 35:f9caeb8ca31e 524
maximbolduc 50:07dfcda65732 525 static int i=0;
maximbolduc 50:07dfcda65732 526 static bool start=false;
maximbolduc 50:07dfcda65732 527 static bool end=false;
maximbolduc 35:f9caeb8ca31e 528
jhedmonton 27:9ac59b261d87 529 bool getline(bool forward)
maximbolduc 26:dc00998140af 530 {
maximbolduc 35:f9caeb8ca31e 531 while (1) {
maximbolduc 50:07dfcda65732 532 // gps.putc('\0');
maximbolduc 35:f9caeb8ca31e 533 if( !gps.readable() ) {
jhedmonton 27:9ac59b261d87 534 break;
jhedmonton 27:9ac59b261d87 535 }
jhedmonton 28:5905886c76ee 536 char c = gps.getc();
maximbolduc 35:f9caeb8ca31e 537 if (forward) { //simply forward all to Bluetooth
maximbolduc 50:07dfcda65732 538 pc.putc(c);
jhedmonton 27:9ac59b261d87 539 }
maximbolduc 35:f9caeb8ca31e 540 if (c == 36 ) {
maximbolduc 35:f9caeb8ca31e 541 start=true;
maximbolduc 35:f9caeb8ca31e 542 end = false;
maximbolduc 35:f9caeb8ca31e 543 i = 0;
maximbolduc 35:f9caeb8ca31e 544 }
maximbolduc 35:f9caeb8ca31e 545 if ((start) && (c == 10)) {
maximbolduc 35:f9caeb8ca31e 546 end=true;
jhedmonton 29:23ccb2a50b6f 547 start = false;
jhedmonton 29:23ccb2a50b6f 548 }
maximbolduc 35:f9caeb8ca31e 549 if (start) {
jhedmonton 27:9ac59b261d87 550 msg[i]=c;
jhedmonton 27:9ac59b261d87 551 i++;
jhedmonton 27:9ac59b261d87 552 if (i>255) break; //protect msg buffer!
jhedmonton 27:9ac59b261d87 553 }
maximbolduc 35:f9caeb8ca31e 554 if (end) {
maximbolduc 35:f9caeb8ca31e 555 msg[i]=c;
maximbolduc 26:dc00998140af 556 msg[i+1] = 0;
jhedmonton 29:23ccb2a50b6f 557 i=0;
jhedmonton 27:9ac59b261d87 558 start = false;
jhedmonton 27:9ac59b261d87 559 end = true;
jhedmonton 27:9ac59b261d87 560 break;
maximbolduc 26:dc00998140af 561 }
maximbolduc 26:dc00998140af 562 }
jhedmonton 27:9ac59b261d87 563 return end;
maximbolduc 26:dc00998140af 564 }
maximbolduc 35:f9caeb8ca31e 565
maximbolduc 26:dc00998140af 566 void keyPressedHeld( void )
maximbolduc 26:dc00998140af 567 {
maximbolduc 36:8e84b5ade03e 568 motor_enable_state = "$ENABLE,0\r\n";
maximbolduc 37:ac60a8a0ae8a 569 motor_enable = 0;
maximbolduc 37:ac60a8a0ae8a 570 pwm1 = 0.0;
maximbolduc 37:ac60a8a0ae8a 571 pwm2 = 0.0;
jhedmonton 28:5905886c76ee 572 ledGREEN=1; //show green for being ready to steer
maximbolduc 26:dc00998140af 573 }
maximbolduc 35:f9caeb8ca31e 574
maximbolduc 26:dc00998140af 575 void keyReleasedHeld( void )
maximbolduc 26:dc00998140af 576 {
maximbolduc 34:c2bc9f9be7ff 577 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
maximbolduc 34:c2bc9f9be7ff 578 motor_enable_state = "$ENABLE,1\r\n";
maximbolduc 37:ac60a8a0ae8a 579 motor_enable = 1;
maximbolduc 37:ac60a8a0ae8a 580 pwm1 = 0.0;
maximbolduc 37:ac60a8a0ae8a 581 pwm2 = 0.0;
maximbolduc 35:f9caeb8ca31e 582 ledGREEN=0;
jhedmonton 27:9ac59b261d87 583 }
maximbolduc 35:f9caeb8ca31e 584
jhedmonton 27:9ac59b261d87 585 void boom1PressedHeld( void )
jhedmonton 27:9ac59b261d87 586 {
maximbolduc 35:f9caeb8ca31e 587 // ledGREEN=1;
maximbolduc 35:f9caeb8ca31e 588 boom18=boom18 & 0xFE;
jhedmonton 27:9ac59b261d87 589 }
maximbolduc 35:f9caeb8ca31e 590
jhedmonton 27:9ac59b261d87 591 void boom1ReleasedHeld( void )
jhedmonton 27:9ac59b261d87 592 {
maximbolduc 35:f9caeb8ca31e 593 //ledGREEN=0;
maximbolduc 35:f9caeb8ca31e 594 boom18=boom18 | 0x01;
jhedmonton 27:9ac59b261d87 595 }
maximbolduc 35:f9caeb8ca31e 596
jhedmonton 27:9ac59b261d87 597 void boom2PressedHeld( void )
maximbolduc 35:f9caeb8ca31e 598 {
maximbolduc 35:f9caeb8ca31e 599 boom18=boom18 & 0xFD;
maximbolduc 26:dc00998140af 600 }
maximbolduc 35:f9caeb8ca31e 601
jhedmonton 27:9ac59b261d87 602 void boom2ReleasedHeld( void )
jhedmonton 27:9ac59b261d87 603 {
maximbolduc 35:f9caeb8ca31e 604 boom18=boom18 | 0x02;
jhedmonton 27:9ac59b261d87 605 }
jhedmonton 27:9ac59b261d87 606 void boom3PressedHeld( void )
jhedmonton 27:9ac59b261d87 607 {
maximbolduc 35:f9caeb8ca31e 608 boom18=boom18 & 0xFB;
jhedmonton 27:9ac59b261d87 609 }
maximbolduc 35:f9caeb8ca31e 610
jhedmonton 27:9ac59b261d87 611 void boom3ReleasedHeld( void )
jhedmonton 27:9ac59b261d87 612 {
maximbolduc 35:f9caeb8ca31e 613 boom18=boom18 | 0x04;
jhedmonton 27:9ac59b261d87 614 }
maximbolduc 35:f9caeb8ca31e 615
jhedmonton 27:9ac59b261d87 616 void boom4PressedHeld( void )
jhedmonton 27:9ac59b261d87 617 {
maximbolduc 32:c57bc701d65c 618 boom18=boom18 & 0xF7;
jhedmonton 27:9ac59b261d87 619 }
maximbolduc 35:f9caeb8ca31e 620
jhedmonton 27:9ac59b261d87 621 void boom4ReleasedHeld( void )
jhedmonton 27:9ac59b261d87 622 {
maximbolduc 32:c57bc701d65c 623 boom18=boom18 | 0x08;
jhedmonton 27:9ac59b261d87 624 }
maximbolduc 35:f9caeb8ca31e 625
maximbolduc 26:dc00998140af 626 void toprint()
maximbolduc 26:dc00998140af 627 {
maximbolduc 26:dc00998140af 628 angle_send = 1;
maximbolduc 26:dc00998140af 629 }
maximbolduc 35:f9caeb8ca31e 630
maximbolduc 38:b5352d6f8166 631 double last_yaw = 0;
maximbolduc 38:b5352d6f8166 632 int counter = 0;
maximbolduc 38:b5352d6f8166 633 bool bear = false;
maximbolduc 38:b5352d6f8166 634 double lastroll = 0;
maximbolduc 38:b5352d6f8166 635 double lastpitch = 0;
maximbolduc 38:b5352d6f8166 636
maximbolduc 26:dc00998140af 637 int main()
maximbolduc 26:dc00998140af 638 {
jhedmonton 27:9ac59b261d87 639 bluetooth.baud(115200);
jhedmonton 28:5905886c76ee 640 gps.baud(38400);
maximbolduc 30:3afafa1ef16b 641 pc.baud(38400);
maximbolduc 35:f9caeb8ca31e 642
jhedmonton 27:9ac59b261d87 643 //JH prepare and send version info
jhedmonton 27:9ac59b261d87 644 vTimer.start();
jhedmonton 27:9ac59b261d87 645 vTimer.reset();
maximbolduc 38:b5352d6f8166 646
jhedmonton 28:5905886c76ee 647 motTimer.start();
jhedmonton 28:5905886c76ee 648 motTimer.reset();
jhedmonton 29:23ccb2a50b6f 649 lastsend_motorstate=motTimer.read_ms()-6500; //should trigger in 5s
maximbolduc 34:c2bc9f9be7ff 650 motor_enable_state = "$ENABLE,1\r\n";
maximbolduc 36:8e84b5ade03e 651
jhedmonton 28:5905886c76ee 652 btTimer.start();
jhedmonton 28:5905886c76ee 653 btTimer.reset();
maximbolduc 38:b5352d6f8166 654 lastgetBT= btTimer.read_ms();
maximbolduc 35:f9caeb8ca31e 655
jhedmonton 27:9ac59b261d87 656 bluetooth.puts(version);
maximbolduc 38:b5352d6f8166 657
jhedmonton 29:23ccb2a50b6f 658 lastsend_version=vTimer.read_ms()-18000;
maximbolduc 38:b5352d6f8166 659 // pc.puts("test\r\n");
maximbolduc 38:b5352d6f8166 660 /* Config_Startup();
maximbolduc 38:b5352d6f8166 661 _ID = Config_GetID();
maximbolduc 38:b5352d6f8166 662 Config_Save();
maximbolduc 38:b5352d6f8166 663 */
jhedmonton 27:9ac59b261d87 664 boom1.attach_asserted_held( &boom1PressedHeld );
jhedmonton 27:9ac59b261d87 665 boom1.attach_deasserted_held( &boom1ReleasedHeld );
jhedmonton 27:9ac59b261d87 666 boom1.setSampleFrequency(); //default = 20 ms
jhedmonton 27:9ac59b261d87 667 boom1.setSamplesTillAssert(5);
jhedmonton 27:9ac59b261d87 668 boom1.setSamplesTillHeld(5);
jhedmonton 27:9ac59b261d87 669 boom2.attach_asserted_held( &boom2PressedHeld );
jhedmonton 27:9ac59b261d87 670 boom2.attach_deasserted_held( &boom2ReleasedHeld );
jhedmonton 27:9ac59b261d87 671 boom2.setSamplesTillAssert(5);
jhedmonton 27:9ac59b261d87 672 boom2.setSamplesTillHeld(5);
jhedmonton 27:9ac59b261d87 673 boom2.setSampleFrequency();
jhedmonton 27:9ac59b261d87 674 boom3.attach_asserted_held( &boom3PressedHeld );
jhedmonton 27:9ac59b261d87 675 boom3.attach_deasserted_held( &boom3ReleasedHeld );
jhedmonton 27:9ac59b261d87 676 boom3.setSamplesTillAssert(5);
jhedmonton 27:9ac59b261d87 677 boom3.setSamplesTillHeld(5);
jhedmonton 27:9ac59b261d87 678 boom3.setSampleFrequency();
jhedmonton 27:9ac59b261d87 679 boom4.attach_asserted_held( &boom4PressedHeld );
jhedmonton 27:9ac59b261d87 680 boom4.attach_deasserted_held( &boom4ReleasedHeld );
jhedmonton 27:9ac59b261d87 681 boom4.setSamplesTillAssert(5);
jhedmonton 27:9ac59b261d87 682 boom4.setSamplesTillHeld(5);
jhedmonton 27:9ac59b261d87 683 boom4.setSampleFrequency();
maximbolduc 47:d3123bb4f673 684
maximbolduc 44:e9d5cd98273d 685 motor_switch.setSamplesTillAssert(5);
maximbolduc 44:e9d5cd98273d 686 motor_switch.setSamplesTillHeld(5);
maximbolduc 44:e9d5cd98273d 687 motor_switch.setSampleFrequency();
maximbolduc 26:dc00998140af 688 motor_switch.attach_asserted_held( &keyPressedHeld );
maximbolduc 26:dc00998140af 689 motor_switch.attach_deasserted_held( &keyReleasedHeld );
maximbolduc 35:f9caeb8ca31e 690
maximbolduc 36:8e84b5ade03e 691 initializeAccelerometer();
maximbolduc 36:8e84b5ade03e 692 calibrateAccelerometer();
maximbolduc 36:8e84b5ade03e 693 initializeGyroscope();
maximbolduc 36:8e84b5ade03e 694 calibrateGyroscope();
maximbolduc 36:8e84b5ade03e 695
maximbolduc 30:3afafa1ef16b 696 accelerometerTicker.attach(&sampleAccelerometer, 0.005);
maximbolduc 30:3afafa1ef16b 697 gyroscopeTicker.attach(&sampleGyroscope, 0.005);
maximbolduc 30:3afafa1ef16b 698 filterTicker.attach(&filter, FILTER_RATE);
maximbolduc 30:3afafa1ef16b 699 angle_print.attach(&toprint,0.2);
maximbolduc 26:dc00998140af 700 activate_antenna();
maximbolduc 36:8e84b5ade03e 701 autosteer_timeout.start();
maximbolduc 38:b5352d6f8166 702
maximbolduc 44:e9d5cd98273d 703 SetDigitalFilter(phaseadv,tcenter, 0); //for FGPS guidance
maximbolduc 38:b5352d6f8166 704
maximbolduc 35:f9caeb8ca31e 705 while(1) {
jhedmonton 27:9ac59b261d87 706 //JH send version information every 10 seconds to keep Bluetooth alive
maximbolduc 35:f9caeb8ca31e 707 if ((vTimer.read_ms()-lastsend_version)>25000) {
maximbolduc 50:07dfcda65732 708 // pc.puts(version);
jhedmonton 27:9ac59b261d87 709 bluetooth.puts(version);
jhedmonton 27:9ac59b261d87 710 vTimer.reset();
jhedmonton 27:9ac59b261d87 711 lastsend_version=vTimer.read_ms();
maximbolduc 35:f9caeb8ca31e 712 }
maximbolduc 35:f9caeb8ca31e 713
maximbolduc 36:8e84b5ade03e 714 if ( autosteer_timeout.read_ms() > 5000 || autosteer_timeout.read_ms() > time_till_stop ) {
maximbolduc 36:8e84b5ade03e 715 autosteer_timeout.reset();
maximbolduc 36:8e84b5ade03e 716 enable_motor = 0;
maximbolduc 36:8e84b5ade03e 717 }
maximbolduc 35:f9caeb8ca31e 718 if ( antenna_active == 1 && gps.readable()) {
maximbolduc 35:f9caeb8ca31e 719 if (getline(false)) {
maximbolduc 45:ecd8c2e27948 720 if ( validate_checksum(msg,result)) {
maximbolduc 50:07dfcda65732 721 // pc.puts(msg);
maximbolduc 45:ecd8c2e27948 722 gps_analyse(result);
maximbolduc 35:f9caeb8ca31e 723 } else {
maximbolduc 50:07dfcda65732 724 pc.puts(result);
maximbolduc 32:c57bc701d65c 725 }
maximbolduc 50:07dfcda65732 726 }//
maximbolduc 26:dc00998140af 727 }
maximbolduc 35:f9caeb8ca31e 728 if ( bluetooth.readable()) {
maximbolduc 35:f9caeb8ca31e 729 if (getline2()) {
maximbolduc 45:ecd8c2e27948 730 if ( validate_checksum(msg2,result)) {
maximbolduc 45:ecd8c2e27948 731 btTimer.reset();
maximbolduc 45:ecd8c2e27948 732 lastgetBT= btTimer.read_ms();
maximbolduc 50:07dfcda65732 733
maximbolduc 50:07dfcda65732 734 pc.puts(msg2);
maximbolduc 45:ecd8c2e27948 735 pc_analyse(result);
maximbolduc 45:ecd8c2e27948 736 }
maximbolduc 26:dc00998140af 737 }
maximbolduc 26:dc00998140af 738 }
maximbolduc 35:f9caeb8ca31e 739 if ( btTimer.read_ms()-lastgetBT>1000) {
maximbolduc 35:f9caeb8ca31e 740 //we did not get any commands over BT
maximbolduc 35:f9caeb8ca31e 741 ledRED=1; //turn red
maximbolduc 35:f9caeb8ca31e 742 } else ledRED=0;
maximbolduc 35:f9caeb8ca31e 743
maximbolduc 35:f9caeb8ca31e 744 if ( ((motTimer.read_ms()-lastsend_motorstate)>8000) || (motor_enable!=lastmotor_enable)) {
maximbolduc 26:dc00998140af 745 bluetooth.puts(motor_enable_state);
maximbolduc 38:b5352d6f8166 746 // pc.puts(motor_enable_state);
jhedmonton 28:5905886c76ee 747 motTimer.reset();
jhedmonton 28:5905886c76ee 748 lastsend_motorstate=motTimer.read_ms();
jhedmonton 28:5905886c76ee 749 lastmotor_enable=motor_enable;
maximbolduc 26:dc00998140af 750 }
maximbolduc 35:f9caeb8ca31e 751 if (boom18!=lastboom18) {
maximbolduc 35:f9caeb8ca31e 752 boomstate[4]=boom18 | 0x80; //
maximbolduc 35:f9caeb8ca31e 753 bluetooth.puts(boomstate);
maximbolduc 38:b5352d6f8166 754 // pc.puts(boomstate);
maximbolduc 35:f9caeb8ca31e 755 lastboom18=boom18;
jhedmonton 27:9ac59b261d87 756 }
maximbolduc 44:e9d5cd98273d 757 if ( print_euler == 1 && angle_send == 1 ) {
maximbolduc 44:e9d5cd98273d 758 //&& reading == 0){
maximbolduc 43:e3f064f5eecd 759 lastpitch = 0.9*lastpitch + 0.1 * (toDegrees(get_pitch()*3.5));
maximbolduc 43:e3f064f5eecd 760 lastroll = 0.9 * lastroll + 0.1 * toDegrees(get_roll()*3.5);
maximbolduc 38:b5352d6f8166 761
maximbolduc 38:b5352d6f8166 762 double dps = - last_yaw;
maximbolduc 38:b5352d6f8166 763 last_yaw =toDegrees( imuFilter.getYaw()) * -1;
maximbolduc 38:b5352d6f8166 764 dps = (dps + last_yaw) * 5; // update every 200 ms, so for dps need *5
maximbolduc 38:b5352d6f8166 765
maximbolduc 38:b5352d6f8166 766 sprintf(output,"$EULER,%f,%f,%f\r\n",lastroll,lastpitch,dps);
maximbolduc 30:3afafa1ef16b 767 bluetooth.puts(output);
maximbolduc 26:dc00998140af 768 angle_send = 0;
maximbolduc 38:b5352d6f8166 769 counter++;
maximbolduc 38:b5352d6f8166 770 if ( bear == false && counter > 3 ) { //reinitialise the gyro after 600ms for the reference to be changed.
maximbolduc 38:b5352d6f8166 771 imuFilter.reset();
maximbolduc 38:b5352d6f8166 772 bear = true;
maximbolduc 38:b5352d6f8166 773 }
jhedmonton 27:9ac59b261d87 774 }
maximbolduc 26:dc00998140af 775 }
maximbolduc 50:07dfcda65732 776 }