map1.cpp
Go to the documentation of this file.
1 #include <string.h>
2 #include "pan.h"
3 #include <options/options.h>
4 
5 struct myparams {
6  const char * file_in;
7  const char * file_out;
8  const char * file_out_stats;
9  const char * file_jj;
10  int format;
11 
12  /* which algorithm to run */
13  int algo;
14 } p;
15 
16 extern "C" void sm_options(struct sm_params*p, struct option*ops);
17 
18 
19 int main(int argc, const char*argv[]) {
20  sm_set_program_name(argv[0]);
21 
22  struct sm_params params;
23  struct sm_result result;
24 
25  struct option* ops = options_allocate(100);
26  options_string(ops, "in", &p.file_in, "stdin", "Input file ");
27  options_string(ops, "out", &p.file_out, "stdout", "Output file ");
28  options_string(ops, "out_stats", &p.file_out_stats, "", "Output file (stats) ");
29  options_string(ops, "file_jj", &p.file_jj, "",
30  "File for journaling -- if left empty, journal not open.");
31  options_int(ops, "algo", &p.algo, 0, "Which algorithm to use (0:icp 1:gpm-stripped) ");
32  p.format = 0;
33 /* options_int(ops, "format", &p.format, 0,
34  "Output format (0: log in JSON format, 1: log in Carmen format (not implemented))");*/
35 
36  sm_options(&params, ops);
37  if(!options_parse_args(ops, argc, argv)) {
38  fprintf(stderr, "\n\nUsage:\n");
39  options_print_help(ops, stderr);
40  return -1;
41  }
42 
43  /* Open input and output files */
44 
46  if(!file_in) return -1;
48  if(!file_out) return -1;
49 
50  if(strcmp(p.file_jj, "")) {
51  FILE * jj = open_file_for_writing(p.file_jj);
52  if(!jj) return -1;
53  jj_set_stream(jj);
54  }
55 
56  FILE * file_out_stats = 0;
57  if(strcmp(p.file_out_stats, "")) {
58  file_out_stats = open_file_for_writing(p.file_out_stats);
59  if(!file_out_stats) return -1;
60  }
61 
62  /* Read first scan */
63  LDP laser_ref;
64  if(!(laser_ref = ld_read_smart(file_in))) {
65  sm_error("Could not read first scan.\n");
66  return -1;
67  }
68  if(!ld_valid_fields(laser_ref)) {
69  sm_error("Invalid laser data in first scan.\n");
70  return -2;
71  }
72 
73 
74  /* For the first scan, set estimate = odometry */
75  copy_d(laser_ref->odometry, 3, laser_ref->estimate);
76 
77  ld_write_as_json(laser_ref, file_out);
78  int count=-1;
79  LDP laser_sens;
80  while( (laser_sens = ld_read_smart(file_in)) ) {
81  count++;
82  if(!ld_valid_fields(laser_sens)) {
83  sm_error("Invalid laser data in (#%d in file).\n", count);
84  return -(count+2);
85  }
86 
87  params.laser_ref = laser_ref;
88  params.laser_sens = laser_sens;
89 
90  /* Set first guess as the difference in odometry */
91  double odometry[3];
92  pose_diff_d(laser_sens->odometry, laser_ref->odometry, odometry);
93  double ominus_laser[3], temp[3];
94  ominus_d(params.laser, ominus_laser);
95  oplus_d(ominus_laser, odometry, temp);
96  oplus_d(temp, params.laser, params.first_guess);
97 
98  /* Do the actual work */
99  switch(p.algo) {
100  case(0):
101  sm_icp(&params, &result); break;
102  case(1):
103  sm_gpm(&params, &result); break;
104  default:
105  sm_error("Unknown algorithm to run: %d.\n",p.algo);
106  return -1;
107  }
108 
109  /* Add the result to the previous estimate */
110  oplus_d(laser_ref->estimate, result.x, laser_sens->estimate);
111 
112  /* Write the corrected log */
113  ld_write_as_json(laser_sens, file_out);
114 
115  /* Write the statistics (if required) */
116  if(file_out_stats) {
117  JO jo = result_to_json(&params, &result);
118  fputs(jo_to_string(jo), file_out_stats);
119  fputs("\n", file_out_stats);
120  jo_free(jo);
121  }
122 
123  ld_free(laser_ref); laser_ref = laser_sens;
124  }
125  ld_free(laser_ref);
126 
127  return 0;
128 }
int format
Definition: map1.cpp:10
const char * file_out
Definition: map1.cpp:7
void sm_set_program_name(const char *name)
Definition: logging.c:21
int main(int argc, const char *argv[])
Definition: map1.cpp:19
void sm_icp(struct sm_params *input, struct sm_result *output)
Definition: icp.c:29
#define jo_to_string
void ld_write_as_json(LDP ld, FILE *stream)
double odometry[3]
Definition: laser_data.h:39
void copy_d(const double *from, int n, double *to)
Definition: math_utils.c:83
const char * file_out_stats
Definition: map1.cpp:8
const char * file_in
Definition: map1.cpp:6
int ld_valid_fields(LDP ld)
Definition: laser_data.c:179
struct option * ops
Definition: rb_sm.c:31
struct myparams p
FILE * open_file_for_writing(const char *filename)
Definition: utils.c:25
LDP ld_read_smart(FILE *)
int algo
Definition: map1.cpp:13
Definition: options.h:49
void ld_free(LDP ld)
Definition: laser_data.c:87
JO result_to_json(struct sm_params *p, struct sm_result *r)
struct option * options_allocate(int n)
const char * file_jj
Definition: map1.cpp:9
void options_int(struct option *, const char *name, int *p, int def_value, const char *desc)
LDP laser_ref
Definition: algos.h:14
LDP laser_sens
Definition: algos.h:16
FILE * open_file_for_reading(const char *filename)
Definition: utils.c:19
double first_guess[3]
Definition: algos.h:19
void oplus_d(const double x1[3], const double x2[3], double res[3])
Definition: math_utils.c:96
double laser[3]
Definition: algos.h:121
void options_print_help(struct option *options, FILE *f)
Definition: options.c:398
void ominus_d(const double x[3], double res[3])
Definition: math_utils.c:87
double estimate[3]
Definition: laser_data.h:40
double x[3]
Definition: algos.h:143
Definition: map1.cpp:5
void options_string(struct option *, const char *name, const char **p, const char *def_balue, const char *desc)
void pose_diff_d(const double pose2[3], const double pose1[3], double res[3])
Definition: math_utils.c:115
void sm_error(const char *msg,...)
Definition: logging.c:49
void sm_gpm(struct sm_params *input, struct sm_result *output)
Definition: gpm.c:11
void jj_set_stream(FILE *f)
Definition: json_journal.c:109
int options_parse_args(struct option *ops, int argc, const char *argv[])
Definition: options.c:66
void sm_options(struct sm_params *p, struct option *ops)
Definition: sm_options.c:6
#define jo_free


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23