N-sim
Emulation and simulation of
Wireless Sensor Networks



   Home


   Project Page


   Download


   CVS



   Installation


   Configuration


   Plug-ins




 Hosted by
SourceForge.net Logo

/home/brennan/n-sim/OrbisQuartus/shared/xml_cfg_test.cpp

Go to the documentation of this file.
00001 
00014 /*
00015  * Copyright 2007. Los Alamos National Security, LLC. This material
00016  * was produced under U.S. Government contract DE-AC52-06NA25396 for
00017  * Los Alamos National Laboratory (LANL), which is operated by Los
00018  * Alamos National Security, LLC, for the Department of Energy. The
00019  * U.S. Government has rights to use, reproduce, and distribute this
00020  * software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY,
00021  * LLC, MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL
00022  * LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified to
00023  * produce derivative works, such modified software should be clearly
00024  * marked, so as not to confuse it with the version available from LANL.
00025  *
00026  * Additionally, this program is free software; you can redistribute
00027  * it and/or modify it under the terms of the GNU General Public
00028  * License as published by the Free Software Foundation; version 2 of
00029  * the License. Accordingly, this program is distributed in the hope
00030  * it will be useful, but WITHOUT ANY WARRANTY; without even the
00031  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00032  * PURPOSE. See the GNU General Public License for more details.
00033  */
00034 
00035 
00036 #include "test.h"
00037 #include "configuration.h"
00038 
00039 
00040 #define ACCURACY     0.0000000001
00041 #define PERCENT_ERR  0.001    // i.e. one tenth of one percent (1m per km)
00042 #define MAX_SCALE    13       
00043 
00044 
00045 int main(int argc, char *argv[])
00046 {
00047         int i, j, error = 0;
00048         int max_scale = MAX_SCALE;
00049         double lat, lon, x, y, new_lat, new_lon;
00050         Configuration conf;
00051 
00052         if (argc > 1)
00053                 max_scale = atoi(argv[1]);
00054         
00055         printf("\n");
00056         /* load/unload config */
00057         if ((error = conf.open("test.cfg")) < 0) {
00058                 printf("  Failed to load test configuration.\n");
00059                 goto end;
00060         }
00061         if ((error = conf.save("result.cfg")) < 0) {
00062                 printf("  Failed to save test configuration.\n");
00063                 goto end;
00064         }
00065 
00066         printf("  WARNING: test-xml is incomplete\n");
00067         /* sensors (accuracy) */
00069 /*
00070         Signal null_signal;
00071         AVL_node *list;
00072         for (list = conf.nodes.find_min(); list != NULL; 
00073              list = list->next()) {
00074                 Sensor_node *node = (Sensor_node *) list->element;
00075 // !! FIXME: Sensor_device *gps_sensor = node->???
00076                 char *gga = gps_sensor->nmea_gga(node, null_signal);
00077                 char *gll = gps_sensor->nmea_gll(node, null_signal);
00078                 printf("  lat %lf, lon %lf\n", node->coords.lat, node->coords.lon);
00079                 printf("  %s%s\n", gga, gll);
00080                 free(gga);
00081                 free(gll);
00082         }
00083 */
00084 
00085         /* sources (accuracy) */
00087 
00088 
00089         /* obstructions (accuracy) */
00091 
00092         /* coordinate -> meter -> coordinate conversion */
00093         lat = conf.get_origin().lat;
00094         lon = conf.get_origin().lon;
00095         for (i = 0; i <= 7; i++) {
00096                 conf.get_origin().coords2meters(lat, lon, &x, &y);
00097                 conf.get_origin().meters2coords(x, y, &new_lat, &new_lon);
00098 
00099                 double orig_dist =  
00100                   conf.get_origin().coord_distance(conf.get_origin().lat,
00101                                                    conf.get_origin().lon,
00102                                                    lat, lon);
00103                 double meter_dist = sqrtl(powl(x, 2) + powl(y, 2));
00104                 double percent_error = fabs(orig_dist - meter_dist) / 
00105                         orig_dist;
00106 
00107                 if (new_lat - lat > ACCURACY || new_lon - lon >ACCURACY) {
00108                         error = -1;
00109                         printf("  Conversion error: latitude %lf vs %lf,\n\t\t"
00110                                "longitude %lf vs %lf\n", new_lat, lat, 
00111                                new_lon, lon);
00112                 }
00113                 if (percent_error > PERCENT_ERR) {
00114                         printf("  Conversion error %lf %%.\n", 
00115                                percent_error * 100);
00116                         if (orig_dist > 1000.0)
00117                             printf("    (Rule of thumb: use coordinates [not "
00118                                    "meters] for \n\tdistances greater than "
00119                                    "a couple of kilometers.)\n");
00120                         else
00121                             error = -1;
00122                 }
00123 
00124                 // origin: 35.08735, -106.65065
00125                 if (i == 0) {
00126                         lat = 35.08735;
00127                         lon = -106.65055;
00128                 } else if (i == 1) {
00129                         lat = 35.08725;
00130                         lon = -106.65065;
00131                 } else if (i == 2) {
00132                         lat = 35.08725;
00133                         lon = -106.65055;
00134                 } else if (i == 3) {
00135                         lat = 35.08805;
00136                         lon = -106.65135;
00137                 } else if (i == 4) {
00138                         lat = 35.08705;
00139                         lon = -106.65185;
00140                 } else if (i == 5) {
00141                         lat = 35.08825;
00142                         lon = -106.64965;
00143                 } else if (i == 6) {
00144                         lat = 35.86548;
00145                         lon = -106.31967;
00146                 }
00147         }
00148         if (error >= 0)
00149                 printf("  Sucessfully converted coordinates to meters and back.\n");
00150 
00151         /* scaling (progressively larger configs) */
00152         for (i = 2; i <= max_scale; i++) {
00153                 int num_nodes = (int) pow(2.0, (double) i);
00154                 FILE *file;
00155                 Configuration conf2("WGS 84");
00156 
00157                 if ((file = fopen("test_tmp.cfg", "w")) == NULL) {
00158                     perror("test_tmp.cfg");
00159                     return -errno;
00160                 }
00161 
00162                 fprintf(file, "<?xml version=\"1.0\"?>\n");
00163                 fprintf(file, "<sensor_net>\n");
00164                 fprintf(file, "  <topology>\n");
00165                 fprintf(file, "    <name>Test %d</name>\n", i);
00166                 fprintf(file, "    <viewpoint>\n");
00167                 fprintf(file, "      <latitude>35.871334</latitude>\n");
00168                 fprintf(file, "      <longitude>-106.326067</longitude>\n");
00169                 fprintf(file, "      <range>500</range>\n");
00170                 fprintf(file, "      <tilt>0</tilt>\n");
00171                 fprintf(file, "      <heading>0</heading>\n");
00172                 fprintf(file, "    </viewpoint>\n");
00173                 for (j = 0; j < num_nodes; j++) {
00174                         fprintf(file, "    <node id=\"%d\">\n", j);
00175                         fprintf(file, "      <random size=\"500\"/>\n");
00176                         fprintf(file, "    </node>\n");
00177                 }
00178                 fprintf(file, "  </topology>\n");
00179                 fprintf(file, "  <network_data>\n");
00180                 fprintf(file, "  </network_data>\n");
00181                 fprintf(file, "</sensor_net>\n");
00182                 fclose(file);
00183 
00184                 //printf("  Loading %d-node configuration\n", num_nodes);
00185 
00186                 if ((error = conf2.open("test_tmp.cfg")) < 0) {
00187                         printf("  Failed to load %d-node config.\n", 
00188                                num_nodes);
00189                         goto end;
00190                 }
00191         }
00192         printf("  Sucessfully loaded up to a %d-node configuration.\n", 
00193                    (int) pow(2.0, (double) i - 1));
00194 
00195  end:
00196         return error;
00197 }


© 2007, Los Alamos National Security, LLC.