sats_management.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2014 Swift Navigation Inc.
00003  * Contact: Ian Horn <ian@swift-nav.com>
00004  *
00005  * This source is subject to the license found in the file 'LICENSE' which must
00006  * be be distributed together with this source. All other rights reserved.
00007  *
00008  * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
00009  * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
00010  * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
00011  */
00012 
00013 #include <string.h>
00014 #include <stdlib.h>
00015 #include <stdio.h>
00016 #include "single_diff.h"
00017 #include "sats_management.h"
00018 #include "linear_algebra.h"
00019 
00020 #define DEBUG_SATS_MAN 0
00021 
00022 u8 choose_reference_sat(u8 num_sats, sdiff_t *sats)
00023 {
00024   double best_snr=sats[0].snr;
00025   u8 best_prn=sats[0].prn;
00026   for (u8 i=1; i<num_sats; i++) {
00027     if (sats[i].snr > best_snr) {
00028       best_snr = sats[i].snr;
00029       best_prn = sats[i].prn;
00030     }
00031   }
00032   return best_prn;
00033 }
00034 
00035 // bool contains(u8 num_sats, u8 ref_prn, sdiff_t *sdiffs) 
00036 // {
00037 //   //todo make and use assumptions about the ordering of the prns
00038 //   for (u8 i=0; i<num_sats; i++) {
00039 //     if (ref_prn == sdiffs[i].prn) {
00040 //       return true;
00041 //     }
00042 //   }
00043 //   return false;
00044 // }
00045 
00046 //assumes both sets are ordered
00047 u8 intersect_sats(u8 num_sats1, u8 num_sdiffs, u8 *sats1, sdiff_t *sdiffs,
00048                   sdiff_t *intersection_sats)
00049 {
00050   u8 i, j, n = 0;
00051   if(DEBUG_SATS_MAN) {
00052     printf("<INTERSECT_SATS>\n");
00053     printf("sdiff prns= {");
00054     for (u8 k=0; k< num_sdiffs; k++) {
00055       printf("%u, ", sdiffs[k].prn);
00056     }
00057     printf("}\n");
00058     printf("sats= {");
00059     for (u8 k=0; k< num_sats1; k++) {
00060       printf("%u, ", sats1[k]);
00061     }
00062     printf("}\n");
00063     printf("(n, i, prn1[i],\t j, prn2[j])\n");
00064   }
00065   /* Loop over sats1 and sdffs and check if a PRN is present in both. */
00066   for (i=0, j=0; i<num_sats1 && j<num_sdiffs; i++, j++) {
00067     if (sats1[i] < sdiffs[j].prn) {
00068       if (DEBUG_SATS_MAN) {
00069         printf("(%u, %u, prn1=%u,\t %u, prn2=%u)\t\t prn1 < prn2  i++\n", n, i, sats1[i], j, sdiffs[j].prn);
00070       }
00071       j--;
00072     }
00073     else if (sats1[i] > sdiffs[j].prn) {
00074       if (DEBUG_SATS_MAN) {
00075         printf("(%u, %u, prn1=%u,\t %u, prn2=%u)\t\t prn1 > prn2  j++\n", n, i, sats1[i], j, sdiffs[j].prn);
00076       }
00077       i--;
00078     }
00079     else {
00080       if (DEBUG_SATS_MAN) {
00081         printf("(%u, %u, prn1=%u,\t %u, prn2=%u)\t\t prn1 = prn2  i,j,n++\n", n, i, sats1[i], j, sdiffs[j].prn);
00082       }
00083       memcpy(&intersection_sats[n], &sdiffs[j], sizeof(sdiff_t));
00084       n++;
00085     }
00086   }
00087   if (DEBUG_SATS_MAN) {
00088     printf("intersection_sats= {");
00089     for (u8 k=0; k< n; k++) {
00090       printf("%u, ", intersection_sats[k].prn);
00091     }
00092     printf("}\n</INTERSECT_SATS>\n");
00093   }
00094   return n;
00095 }
00096 
00097 // s8 check_and_rebase_reference_sat(kf_t *kf, u8 num_sats, sdiff_t *sdiffs, kf_state_t *state)
00098 // {
00099 //   bool still_has_ref = contains(num_sats, sdiffs);
00100 //   if (still_has_ref == false) {
00101 //     sdiff_t common_sats[num_sats];
00102 //     u8 intersection_size = intersect_sats(num_sats, sdiffs, common_sats);
00103 //     if (intersection_size>=4) {
00104 //       u8 new_ref_prn = choose_reference_sat(intersection_size, common_sats);
00105 //       printf("%i\n", new_ref_prn);
00106 //       printf("%i\n", state->state_dim);
00107 //     }
00108 //   }
00109 //   return 1;
00110 // }
00111 
00112 // u8 update_filter(kf_t *kf, u8 num_sats, sdiff_t *sdiffs, kf_state_t *state)
00113 // {
00114 //   //TODO add update_E logic
00115 
00116 //   u8 control_logic = check_and_rebase_reference_sat(kf, num_sats, sdiffs, state);
00117 // }
00118 
00121 void set_reference_sat_of_prns(u8 ref_prn, u8 num_sats, u8 *prns)
00122 {
00123   u8 old_ref = prns[0];
00124   u8 j;
00125   if (old_ref != ref_prn) {
00126     j = 1;
00127     u8 old_prns[num_sats];
00128     memcpy(old_prns, prns, num_sats * sizeof(u8));
00129     u8 set_old_yet = 0;
00130     prns[0] = ref_prn;
00131     for (u8 i=1; i<num_sats; i++) {
00132       if (old_prns[i] != ref_prn) {
00133         if (old_prns[i]>old_ref && set_old_yet == 0) {
00134           prns[j] = old_ref;
00135           j++;
00136           i--;
00137           set_old_yet = 1;
00138         }
00139         else {
00140           prns[j] = old_prns[i];
00141           j++;
00142         }
00143       }
00144       else if (i == num_sats-1) {
00145         prns[j] = old_ref;
00146       }
00147     }
00148   }
00149 }
00150 
00151 
00154 void set_reference_sat(u8 ref_prn, sats_management_t *sats_management,
00155                           u8 num_sdiffs, sdiff_t *sdiffs, sdiff_t *sdiffs_with_ref_first)
00156 {  
00157   u8 old_ref = sats_management->prns[0];
00158   if (DEBUG_SATS_MAN) {
00159     printf("<SET_REFERENCE_SAT>\n");
00160     printf("ref_prn = %u\n", ref_prn);
00161     printf("old_ref = %u\n", old_ref);
00162   }
00163   u8 j;
00164   if (old_ref != ref_prn) {
00165     j = 1;
00166     u8 old_prns[sats_management->num_sats];
00167     memcpy(old_prns, sats_management->prns, sats_management->num_sats * sizeof(u8));
00168     u8 set_old_yet = 0;
00169     sats_management->prns[0] = ref_prn;
00170     for (u8 i=1; i<sats_management->num_sats; i++) {
00171       if (old_prns[i] != ref_prn) {
00172         if (old_prns[i]>old_ref && set_old_yet == 0) {
00173           sats_management->prns[j] = old_ref;
00174           j++;
00175           i--;
00176           set_old_yet = 1;
00177         }
00178         else {
00179           sats_management->prns[j] = old_prns[i];
00180           j++;
00181         }
00182       }
00183       else if (i == sats_management->num_sats-1) {
00184         sats_management->prns[j] = old_ref;
00185       }
00186     }
00187   }
00188   j=1;
00189   for (u8 i=0; i<num_sdiffs; i++) {
00190     if (sdiffs[i].prn != ref_prn) {
00191       if (DEBUG_SATS_MAN) {
00192         printf("prn[%u] = %u\n", j, sdiffs[i].prn);
00193       }
00194       memcpy(&sdiffs_with_ref_first[j], &sdiffs[i], sizeof(sdiff_t));
00195       j++;
00196     } else {
00197       if (DEBUG_SATS_MAN) {
00198         printf("prn[0] = %u\n", sdiffs[i].prn);
00199       }
00200       memcpy(&sdiffs_with_ref_first[0], &sdiffs[i], sizeof(sdiff_t));
00201     }
00202   }
00203   if (DEBUG_SATS_MAN) {
00204     printf("</SET_REFERENCE_SAT>\n");
00205   }
00206 }
00207 
00208 void set_reference_sat_and_prns(u8 ref_prn, sats_management_t *sats_management,
00209                                 u8 num_sdiffs, sdiff_t *sdiffs, sdiff_t *sdiffs_with_ref_first)
00210 {
00211   sats_management->num_sats = num_sdiffs;
00212   sats_management->prns[0] = ref_prn;
00213   u8 j=1;
00214   for (u8 i=0; i<num_sdiffs; i++) {
00215     if (sdiffs[i].prn != ref_prn) {
00216       sats_management->prns[j] = sdiffs[i].prn;
00217       if (sdiffs_with_ref_first) {
00218         memcpy(&sdiffs_with_ref_first[j], &sdiffs[i], sizeof(sdiff_t));
00219       }
00220       j++;
00221     } else {
00222       if (sdiffs_with_ref_first) {
00223         memcpy(&sdiffs_with_ref_first[0], &sdiffs[i], sizeof(sdiff_t));
00224       }
00225     }
00226   }
00227 }
00228 
00229 void init_sats_management(sats_management_t *sats_management,
00230                           u8 num_sdiffs, sdiff_t *sdiffs, sdiff_t *sdiffs_with_ref_first)
00231 {
00232   if (DEBUG_SATS_MAN) {
00233     printf("<INIT_SATS_MANAGEMENT>\n");
00234   }
00235   if (num_sdiffs == 0) {
00236     sats_management->num_sats = 0;
00237     if (DEBUG_SATS_MAN) {
00238       printf("</INIT_SATS_MANAGEMENT>\n");
00239     }
00240     return;
00241   }
00242 
00243   u8 ref_prn = choose_reference_sat(num_sdiffs, sdiffs);
00244   set_reference_sat_and_prns(ref_prn, sats_management,
00245                              num_sdiffs, sdiffs, sdiffs_with_ref_first);
00246   if (DEBUG_SATS_MAN) {
00247       printf("</INIT_SATS_MANAGEMENT>\n");
00248     }
00249 }
00250 
00252 void print_sats_management(sats_management_t *sats_management)
00253 {
00254   printf("sats_management->num_sats=%u\n", sats_management->num_sats);
00255   for (u8 i=0; i<sats_management->num_sats; i++) {
00256     printf("sats_management->prns[%u]= %u\n", i, sats_management->prns[i]);
00257   }
00258 }
00260 void print_sats_management_short(sats_management_t *sats_man) {
00261   printf("sats_management sats: ");
00262   for (u8 i=0; i<sats_man->num_sats; i++) {
00263     printf("%d,", sats_man->prns[i]);
00264   }
00265   printf("\n");
00266 }
00267 
00268 
00271 s8 rebase_sats_management(sats_management_t *sats_management,
00272                           u8 num_sdiffs, sdiff_t *sdiffs, sdiff_t *sdiffs_with_ref_first)
00273 {
00274   s8 return_code;
00275   u8 ref_prn;
00276   if (DEBUG_SATS_MAN) {
00277         printf("<REBASE_SATS_MANAGEMENT>\n");
00278   }
00279 
00280   if (sats_management->num_sats <= 1) {
00281     // Need to init first.
00282     init_sats_management(sats_management, num_sdiffs, sdiffs, 0);
00283   }
00284 
00285   // Check if old reference is in sdiffs
00286   if (bsearch(&(sats_management->prns[0]), sdiffs, num_sdiffs, sizeof(sdiff_t), &sdiff_search_prn)) {
00287     ref_prn = sats_management->prns[0];
00288     return_code = OLD_REF;
00289   }
00290   else {
00291     sdiff_t intersection_sats[num_sdiffs];
00292     u8 num_intersection = intersect_sats(sats_management->num_sats, num_sdiffs,
00293                                          &(sats_management->prns[1]), sdiffs, intersection_sats);
00294     if (num_intersection < INTERSECTION_SATS_THRESHOLD_SIZE) {
00295       if (DEBUG_SATS_MAN) {
00296         printf("</REBASE_SATS_MANAGEMENT>\n");
00297       }
00298       return NEW_REF_START_OVER;
00299     }
00300     else {
00301       if (DEBUG_SATS_MAN) {
00302         printf("sdiff prns= {");
00303         for (u8 yo_mama=0; yo_mama< num_sdiffs; yo_mama++) {
00304           printf("%u, ", sdiffs[yo_mama].prn);
00305         }
00306         printf("}\n");
00307         printf("sats_man_prns= {");
00308         for (u8 so_fetch=0; so_fetch < sats_management->num_sats; so_fetch++) {
00309           printf("%u, ", sats_management->prns[so_fetch]);
00310         }
00311         printf("}\n");
00312         
00313         printf("num intersect_sats= %u\nintersection= {", num_intersection);
00314         for (u8 bork=0; bork<num_intersection; bork++) {
00315           printf("%u, ", intersection_sats[bork].prn);
00316         }
00317         printf("}\n");
00318       }
00319       ref_prn = choose_reference_sat(num_intersection, intersection_sats);
00320       return_code = NEW_REF;
00321     }
00322   }
00323   set_reference_sat(ref_prn, sats_management,
00324                     num_sdiffs, sdiffs, sdiffs_with_ref_first);
00325   if (DEBUG_SATS_MAN) {
00326     printf("</REBASE_SATS_MANAGEMENT>\n");
00327   }
00328   return return_code;
00329 }
00330 
00331 void update_sats_sats_management(sats_management_t *sats_management, u8 num_non_ref_sdiffs, sdiff_t *non_ref_sdiffs)
00332 {
00333   sats_management->num_sats = num_non_ref_sdiffs + 1;
00334   for (u8 i=1; i<num_non_ref_sdiffs+1; i++) {
00335     sats_management->prns[i] = non_ref_sdiffs[i-1].prn;
00336   }
00337 }
00338 
00339 
00340 
00341 s8 match_sdiffs_to_sats_man(sats_management_t *sats, u8 num_sdiffs, 
00342                             sdiff_t *sdiffs, sdiff_t *sdiffs_with_ref_first)
00343 {
00344   u8 j = 1;
00345   u8 ref_prn = sats->prns[0];
00346   for (u8 i=0; i<num_sdiffs && j<sats->num_sats; i++) {
00347     if (sdiffs[i].prn == ref_prn) {
00348       memcpy(sdiffs_with_ref_first, &sdiffs[i], sizeof(sdiff_t));
00349     }
00350     else if (sdiffs[i].prn == sats->prns[j]) {
00351       memcpy(&sdiffs_with_ref_first[j], &sdiffs[i], sizeof(sdiff_t));
00352       j++;
00353     }
00354     else if (sdiffs[i].prn > sats->prns[j]) {
00355       return -1;
00356     }
00357   }
00358   return 0;
00359 }
00360 
00361 
00362 


swiftnav
Author(s):
autogenerated on Sat Jun 8 2019 18:55:56