00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00033 #include <math.h>
00034 #include <assert.h>
00035 #include "config_util.h"
00036 #include "math_util.h"
00037 #include "rotations.h"
00038 #include "small_linalg.h"
00039 
00040 static int 
00041 _config_util_get_quat (Config *cfg, const char *name, double quat[4])
00042 {
00043     char key[256];
00044     sprintf(key, "calibration.%s.orientation", name);
00045     if (config_has_key(cfg, key)) {
00046         int sz = config_get_double_array(cfg, key, quat, 4);
00047         assert(sz==4);
00048         return 0;
00049     }
00050 
00051     sprintf(key, "calibration.%s.rpy", name);
00052     if (config_has_key(cfg, key)) {
00053         double rpy[3];
00054         int sz = config_get_double_array(cfg, key, rpy, 3);
00055         assert(sz == 3);
00056         int i;
00057         for (i = 0; i < 3; i++)
00058             rpy[i] = to_radians(rpy[i]);
00059         rot_roll_pitch_yaw_to_quat(rpy, quat);
00060         return 0;
00061     }
00062 
00063     sprintf(key, "calibration.%s.angleaxis", name);
00064     if (config_has_key(cfg, key)) {
00065         double aa[4];
00066         int sz = config_get_double_array(cfg, key, aa, 4);
00067         assert(sz==4);
00068 
00069         double theta = aa[0];
00070         double s = sin(theta/2);
00071 
00072         quat[0] = cos(theta/2);
00073         int i;
00074         for (i = 1; i < 4; i++)
00075             quat[i] = aa[i] * s;
00076         return 0;
00077     }
00078     return -1;
00079 }
00080 
00081 int config_util_get_quat(Config *cfg, const char *name, double quat[4])
00082 {
00083     int result = _config_util_get_quat (cfg, name, quat);
00084     return result;
00085 }
00086 
00087 int config_util_get_pos(Config *cfg, const char *name, double pos[3])
00088 {
00089     char key[256];
00090 
00091     sprintf(key, "calibration.%s.position", name);
00092     if (config_has_key(cfg, key)) {
00093         int sz = config_get_double_array(cfg, key, pos, 3);
00094         assert(sz==3);
00095         return 0;
00096     } 
00097     return -1;
00098 }
00099 
00100 int config_util_get_matrix(Config *cfg, const char *name, double m[16])
00101 {
00102     double quat[4];
00103     double pos[3];
00104 
00105     if (config_util_get_quat(cfg, name, quat))
00106         return -1;
00107 
00108     if (config_util_get_pos(cfg, name, pos))
00109         return -1;
00110 
00111     rot_quat_pos_to_matrix(quat, pos, m);
00112 
00113     return 0;
00114 }
00115 
00116 
00117 
00118 int 
00119 config_util_sensor_to_local_with_pose(Config *config, 
00120         const char *name, double m[16], 
00121         lcmtypes_pose_t *p)
00122 {
00123     double body_to_local[16];
00124 
00125     rot_quat_pos_to_matrix(p->orientation, p->pos, body_to_local);
00126 
00127     double sensor_to_calibration[16];
00128 
00129     if (config_util_get_matrix(config, name, sensor_to_calibration))
00130         return -1;
00131 
00132     char key[128];
00133     sprintf(key,"calibration.%s.relative_to", name);
00134 
00135     char *calib_frame = config_get_str_or_fail(config, key);
00136     if (!strcmp(calib_frame, "body")) {
00137 
00138         matrix_multiply_4x4_4x4(body_to_local, sensor_to_calibration, m);
00139 
00140     } else {
00141         double calibration_to_body[16];
00142         
00143         if (config_util_get_matrix(config, calib_frame, calibration_to_body))
00144             return -1;
00145 
00146         double sensor_to_body[16];
00147         matrix_multiply_4x4_4x4(calibration_to_body, sensor_to_calibration, 
00148                                 sensor_to_body);
00149         matrix_multiply_4x4_4x4(body_to_local, sensor_to_body, m);
00150     }
00151 
00152     return 0;
00153 }