config_util.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
33 #include <math.h>
34 #include <assert.h>
35 #include "config_util.h"
36 #include "math_util.h"
37 #include "rotations.h"
38 #include "small_linalg.h"
39 
40 static int
41 _config_util_get_quat (Config *cfg, const char *name, double quat[4])
42 {
43  char key[256];
44  sprintf(key, "calibration.%s.orientation", name);
45  if (config_has_key(cfg, key)) {
46  int sz = config_get_double_array(cfg, key, quat, 4);
47  assert(sz==4);
48  return 0;
49  }
50 
51  sprintf(key, "calibration.%s.rpy", name);
52  if (config_has_key(cfg, key)) {
53  double rpy[3];
54  int sz = config_get_double_array(cfg, key, rpy, 3);
55  assert(sz == 3);
56  int i;
57  for (i = 0; i < 3; i++)
58  rpy[i] = to_radians(rpy[i]);
59  rot_roll_pitch_yaw_to_quat(rpy, quat);
60  return 0;
61  }
62 
63  sprintf(key, "calibration.%s.angleaxis", name);
64  if (config_has_key(cfg, key)) {
65  double aa[4];
66  int sz = config_get_double_array(cfg, key, aa, 4);
67  assert(sz==4);
68 
69  double theta = aa[0];
70  double s = sin(theta/2);
71 
72  quat[0] = cos(theta/2);
73  int i;
74  for (i = 1; i < 4; i++)
75  quat[i] = aa[i] * s;
76  return 0;
77  }
78  return -1;
79 }
80 
81 int config_util_get_quat(Config *cfg, const char *name, double quat[4])
82 {
83  int result = _config_util_get_quat (cfg, name, quat);
84  return result;
85 }
86 
87 int config_util_get_pos(Config *cfg, const char *name, double pos[3])
88 {
89  char key[256];
90 
91  sprintf(key, "calibration.%s.position", name);
92  if (config_has_key(cfg, key)) {
93  int sz = config_get_double_array(cfg, key, pos, 3);
94  assert(sz==3);
95  return 0;
96  }
97  return -1;
98 }
99 
100 int config_util_get_matrix(Config *cfg, const char *name, double m[16])
101 {
102  double quat[4];
103  double pos[3];
104 
105  if (config_util_get_quat(cfg, name, quat))
106  return -1;
107 
108  if (config_util_get_pos(cfg, name, pos))
109  return -1;
110 
111  rot_quat_pos_to_matrix(quat, pos, m);
112 
113  return 0;
114 }
115 
116 // compute the sensor-to-local rigid body transformation matrix for a
117 // specific sensor, given a vehicle pose.
118 int
120  const char *name, double m[16],
121  lcmtypes_pose_t *p)
122 {
123  double body_to_local[16];
124 
125  rot_quat_pos_to_matrix(p->orientation, p->pos, body_to_local);
126 
127  double sensor_to_calibration[16];
128 
129  if (config_util_get_matrix(config, name, sensor_to_calibration))
130  return -1;
131 
132  char key[128];
133  sprintf(key,"calibration.%s.relative_to", name);
134 
135  char *calib_frame = config_get_str_or_fail(config, key);
136  if (!strcmp(calib_frame, "body")) {
137 
138  matrix_multiply_4x4_4x4(body_to_local, sensor_to_calibration, m);
139 
140  } else {
141  double calibration_to_body[16];
142 
143  if (config_util_get_matrix(config, calib_frame, calibration_to_body))
144  return -1;
145 
146  double sensor_to_body[16];
147  matrix_multiply_4x4_4x4(calibration_to_body, sensor_to_calibration,
148  sensor_to_body);
149  matrix_multiply_4x4_4x4(body_to_local, sensor_to_body, m);
150  }
151 
152  return 0;
153 }
convenience functions for small linear algebra operations
int config_util_get_matrix(Config *cfg, const char *name, double m[16])
int config_util_get_pos(Config *cfg, const char *name, double pos[3])
Definition: config_util.cpp:87
double orientation[4]
char * config_get_str_or_fail(Config *conf, const char *key)
Definition: config.cpp:822
#define to_radians(x)
Definition: math_util.h:45
int config_has_key(Config *conf, const char *key)
Definition: config.cpp:720
static int _config_util_get_quat(Config *cfg, const char *name, double quat[4])
Definition: config_util.cpp:41
int rot_quat_pos_to_matrix(const double quat[4], const double pose[3], double m[16])
Definition: rotations.cpp:260
int config_util_get_quat(Config *cfg, const char *name, double quat[4])
Definition: config_util.cpp:81
header file for rotations.cpp Transformation utilities
int config_get_double_array(Config *conf, const char *key, double *vals, int len)
Definition: config.cpp:920
header file for config_util.cpp Check the LCM instructions for more info
void rot_roll_pitch_yaw_to_quat(const double rpy[3], double q[4])
Definition: rotations.cpp:171
int config_util_sensor_to_local_with_pose(Config *config, const char *name, double m[16], lcmtypes_pose_t *p)
static void matrix_multiply_4x4_4x4(const double a[16], const double b[16], double r[16])
Definition: small_linalg.h:325
header file for math utilities


mit_darpa_logs_player
Author(s): Miguel Oliveira
autogenerated on Mon Mar 2 2015 01:32:15