Agra-GPS / FreePilot_V2-3

Dependencies:   FreePilot PinDetect mbed-src

Fork of FreePilot_V2-2 by Agra-GPS

Committer:
maximbolduc
Date:
Fri Mar 27 22:44:29 2015 +0000
Revision:
55:8561b35130fc
Parent:
54:2405c62c1049
Child:
56:456d454d9ced
added dynamic bias

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