ld_slip.c
Go to the documentation of this file.
1 #include <gsl/gsl_rng.h>
2 #include <gsl/gsl_randist.h>
3 #include <gsl/gsl_math.h>
4 
5 #include <math.h>
6 #include <options/options.h>
7 
8 #include "../csm/csm_all.h"
9 
10 struct ld_noise_params {
11  int seed;
12 
13  double sigma_xy;
15 
16  const char* file_input;
17  const char* file_output;
18 
19  int debug;
20 };
21 
22 const char * banner =
23  "A simple program for adding slip to odometry \n\n"
24  "The 'odometry' field is set to 'true_pose' + noise.\n"
25  "If 'true_pose' is not available, then the 'odometry' \n"
26  "field is set to 'odometry' + noise.\n\n"
27  "Note: this program does *not* simulate the effect of \n"
28  "slip or odometry error in a realistic way; each scan \n"
29  "in the file is considered separately, the error does \n"
30  "not depend on the traveled distance, etc.\n\n";
31 
32 int main(int argc, const char ** argv) {
33  sm_set_program_name(argv[0]);
34 
35  struct ld_noise_params p;
36 
38 
39  struct option* ops = options_allocate(10);
40  options_double(ops, "sigma_theta_deg", &p.sigma_theta_deg, 0.0,
41  "Std deviation of gaussian noise for theta (deg) (disabled if 0)");
42  options_double(ops, "sigma_xy", &p.sigma_xy, 0.0,
43  "Std deviation of gaussian noise for x,y (disabled if 0)");
44  options_int(ops, "seed", &p.seed, 0,
45  "Seed for random number generator (if 0, use GSL_RNG_SEED env. variable).");
46  options_string(ops, "in", &p.file_input, "stdin", "Input file ");
47  options_string(ops, "out", &p.file_output, "stdout", "Output file ");
48 
49  options_int(ops, "debug", &p.debug, 0, "Shows debug information");
50 
51 
52  if(!options_parse_args(ops, argc, argv)) {
53  options_print_help(ops, stderr);
54  return -1;
55  }
56 
58 
59  gsl_rng_env_setup();
60  gsl_rng * rng = gsl_rng_alloc (gsl_rng_ranlxs0);
61  if(p.seed != 0)
62  gsl_rng_set(rng, (unsigned int) p.seed);
63 
64 
65  FILE * in = open_file_for_reading(p.file_input);
66  if(!in) return -3;
67 
68  FILE * out = open_file_for_writing(p.file_output);
69  if(!out) return -2;
70 
71  LDP ld; int count=0;
72  while( (ld = ld_read_smart(in))) {
73  count++;
74  if(!ld_valid_fields(ld)) {
75  sm_error("Invalid laser data (#%d in file)\n", count);
76  continue;
77  }
78 
79  if(!any_nan(ld->true_pose, 3))
80  copy_d( (const double*) ld->true_pose, 3, ld->odometry);
81 
82  double e[3] = {0,0,0};
83 
84  if(p.sigma_xy > 0) {
85  e[0] = gsl_ran_gaussian(rng, p.sigma_xy);
86  e[1] = gsl_ran_gaussian(rng, p.sigma_xy);
87  }
88 
89  if(p.sigma_theta_deg > 0) {
90  e[2] = gsl_ran_gaussian(rng, deg2rad(p.sigma_theta_deg));
91  }
92 
93  ld->odometry[0] += e[0];
94  ld->odometry[1] += e[1];
95  ld->odometry[2] += e[2];
96 
97  sm_debug("Adding noise %s.\n", friendly_pose(e));
98 
99  ld_write_as_json(ld, out);
100 
101  ld_free(ld);
102  }
103 
104  return 0;
105 }
void options_banner(const char *message)
Definition: options.c:33
#define deg2rad(x)
Definition: gpc_test.c:22
double true_pose[3]
Definition: laser_data.h:38
void sm_set_program_name(const char *name)
Definition: logging.c:21
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
int main(int argc, const char **argv)
Definition: ld_slip.c:32
double sigma_xy
Definition: ld_slip.c:13
const char * friendly_pose(const double *pose)
Definition: math_utils.c:266
void options_double(struct option *, const char *name, double *p, double def_value, const char *desc)
int ld_valid_fields(LDP ld)
Definition: laser_data.c:179
struct option * ops
Definition: rb_sm.c:31
FILE * open_file_for_writing(const char *filename)
Definition: utils.c:25
const char * banner
Definition: ld_slip.c:22
LDP ld_read_smart(FILE *)
const char * file_input
Definition: ld_noise.c:14
const char * file_output
Definition: ld_noise.c:15
Definition: options.h:49
void ld_free(LDP ld)
Definition: laser_data.c:87
struct option * options_allocate(int n)
void options_int(struct option *, const char *name, int *p, int def_value, const char *desc)
double sigma_theta_deg
Definition: ld_slip.c:14
void sm_debug_write(int flag)
Definition: logging.c:16
FILE * open_file_for_reading(const char *filename)
Definition: utils.c:19
void options_print_help(struct option *options, FILE *f)
Definition: options.c:398
void sm_debug(const char *msg,...)
Definition: logging.c:88
int any_nan(const double *d, int n)
Definition: math_utils.c:64
void options_string(struct option *, const char *name, const char **p, const char *def_balue, const char *desc)
gsl_rng * rng
Definition: ld_resample.c:18
void sm_error(const char *msg,...)
Definition: logging.c:49
int options_parse_args(struct option *ops, int argc, const char *argv[])
Definition: options.c:66


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