uas_sensor_orientation.cpp
Go to the documentation of this file.
00001 
00009 /*
00010  * Copyright 2015 Nuno Marques.
00011  *
00012  * This file is part of the mavros package and subject to the license terms
00013  * in the top-level LICENSE file of the mavros repository.
00014  * https://github.com/mavlink/mavros/tree/master/LICENSE.md
00015  */
00016 #include <array>
00017 #include <mavros/mavros_uas.h>
00018 
00019 #define DEG_TO_RAD (M_PI / 180.0f)
00020 
00021 using namespace mavros;
00022 
00023 // internal type: name - rotation
00024 typedef std::pair<const std::string, const Eigen::Quaterniond> OrientationPair;
00025 
00026 // internal data initializer
00027 static const OrientationPair make_orientation(const std::string &name,
00028         const double roll,
00029         const double pitch,
00030         const double yaw)
00031 {
00032         const Eigen::Quaterniond rot = UAS::quaternion_from_rpy(Eigen::Vector3d(roll, pitch, yaw) * DEG_TO_RAD);
00033         return std::make_pair(name, rot);
00034 }
00035 
00036 static const std::array<const OrientationPair, 39> sensor_orientations = {{
00037 /*  0 */ make_orientation("NONE",                         0.0,   0.0,   0.0),
00038 /*  1 */ make_orientation("YAW_45",                       0.0,   0.0,  45.0),
00039 /*  2 */ make_orientation("YAW_90",                       0.0,   0.0,  90.0),
00040 /*  3 */ make_orientation("YAW_135",                      0.0,   0.0, 135.0),
00041 /*  4 */ make_orientation("YAW_180",                      0.0,   0.0, 180.0),
00042 /*  5 */ make_orientation("YAW_225",                      0.0,   0.0, 225.0),
00043 /*  6 */ make_orientation("YAW_270",                      0.0,   0.0, 270.0),
00044 /*  7 */ make_orientation("YAW_315",                      0.0,   0.0, 315.0),
00045 /*  8 */ make_orientation("ROLL_180",                   180.0,   0.0,   0.0),
00046 /*  9 */ make_orientation("ROLL_180_YAW_45",            180.0,   0.0,  45.0),
00047 /* 10 */ make_orientation("ROLL_180_YAW_90",            180.0,   0.0,  90.0),
00048 /* 11 */ make_orientation("ROLL_180_YAW_135",           180.0,   0.0, 135.0),
00049 /* 12 */ make_orientation("PITCH_180",                    0.0, 180.0,   0.0),
00050 /* 13 */ make_orientation("ROLL_180_YAW_225",           180.0,   0.0, 225.0),
00051 /* 14 */ make_orientation("ROLL_180_YAW_270",           180.0,   0.0, 270.0),
00052 /* 15 */ make_orientation("ROLL_180_YAW_315",           180.0,   0.0, 315.0),
00053 /* 16 */ make_orientation("ROLL_90",                     90.0,   0.0,   0.0),
00054 /* 17 */ make_orientation("ROLL_90_YAW_45",              90.0,   0.0,  45.0),
00055 /* 18 */ make_orientation("ROLL_90_YAW_90",              90.0,   0.0,  90.0),
00056 /* 19 */ make_orientation("ROLL_90_YAW_135",             90.0,   0.0, 135.0),
00057 /* 20 */ make_orientation("ROLL_270",                   270.0,   0.0,   0.0),
00058 /* 21 */ make_orientation("ROLL_270_YAW_45",            270.0,   0.0,  45.0),
00059 /* 22 */ make_orientation("ROLL_270_YAW_90",            270.0,   0.0,  90.0),
00060 /* 23 */ make_orientation("ROLL_270_YAW_135",           270.0,   0.0, 135.0),
00061 /* 24 */ make_orientation("PITCH_90",                     0.0,  90.0,   0.0),
00062 /* 25 */ make_orientation("PITCH_270",                    0.0, 270.0,   0.0),
00063 /* 26 */ make_orientation("PITCH_180_YAW_90",             0.0, 180.0,  90.0),
00064 /* 27 */ make_orientation("PITCH_180_YAW_270",            0.0, 180.0, 270.0),
00065 /* 28 */ make_orientation("ROLL_90_PITCH_90",            90.0,  90.0,   0.0),
00066 /* 29 */ make_orientation("ROLL_180_PITCH_90",          180.0,  90.0,   0.0),
00067 /* 30 */ make_orientation("ROLL_270_PITCH_90",          270.0,  90.0,   0.0),
00068 /* 31 */ make_orientation("ROLL_90_PITCH_180",           90.0, 180.0,   0.0),
00069 /* 32 */ make_orientation("ROLL_270_PITCH_180",         270.0, 180.0,   0.0),
00070 /* 33 */ make_orientation("ROLL_90_PITCH_270",           90.0, 270.0,   0.0),
00071 /* 34 */ make_orientation("ROLL_180_PITCH_270",         180.0, 270.0,   0.0),
00072 /* 35 */ make_orientation("ROLL_270_PITCH_270",         270.0, 270.0,   0.0),
00073 /* 36 */ make_orientation("ROLL_90_PITCH_180_YAW_90",    90.0, 180.0,  90.0),
00074 /* 37 */ make_orientation("ROLL_90_YAW_270",             90.0,   0.0, 270.0),
00075 /* 38 */ make_orientation("ROLL_315_PITCH_315_YAW_315", 315.0, 315.0, 315.0)
00076 }};
00077 
00078 std::string UAS::str_sensor_orientation(MAV_SENSOR_ORIENTATION orientation)
00079 {
00080         const size_t idx(orientation);
00081         if (idx >= sensor_orientations.size()) {
00082                 ROS_ERROR_NAMED("uas", "SENSOR: wrong orientation index: %zu", idx);
00083                 return std::to_string(idx);
00084         }
00085 
00086         return sensor_orientations[idx].first;
00087 }
00088 
00089 Eigen::Quaterniond UAS::sensor_orientation_matching(MAV_SENSOR_ORIENTATION orientation)
00090 {
00091         const size_t idx(orientation);
00092         if (idx >= sensor_orientations.size()) {
00093                 ROS_ERROR_NAMED("uas", "SENSOR: wrong orientation index: %zu", idx);
00094                 return Eigen::Quaterniond::Identity();
00095         }
00096 
00097         return sensor_orientations[idx].second;
00098 }
00099 
00100 int UAS::orientation_from_str(const std::string &sensor_orientation)
00101 {
00102         // 1. try to find by name
00103         for (size_t idx = 0; idx < sensor_orientations.size(); idx++) {
00104                 if (sensor_orientations[idx].first == sensor_orientation)
00105                         return idx;
00106         }
00107 
00108         // 2. try convert integer
00109         // fallback for old configs that uses numeric orientation.
00110         try {
00111                 int idx = std::stoi(sensor_orientation, 0, 0);
00112                 if (0 > idx || size_t(idx) > sensor_orientations.size()) {
00113                         ROS_ERROR_NAMED("uas", "SENSOR: orientation index out of bound: %d", idx);
00114                         return -1;
00115                 }
00116                 else
00117                         return idx;
00118         }
00119         catch (std::invalid_argument &ex) {
00120                 // failed
00121         }
00122 
00123         ROS_ERROR_STREAM_NAMED("uas", "SENSOR: wrong orientation str: " << sensor_orientation);
00124 
00125         return -1;
00126 }


mavros
Author(s): Vladimir Ermakov
autogenerated on Sat Jun 8 2019 19:55:19