euler-angle-quaternion

euler-angle-quaternion conversion

for euler angle and quaternion:
kdl conversion better than “yuiwongcppbase angle.hpp” conversion (maybe / not)
both ok. and sometimes maybe eigen conversion cannot work??
see “yuiwongtransformdemo” test and “yuiwongcppbase angle test”

#

by kdl

# ros package: `tf_conversions` when ros

# cmake
find_package(orocos_kdl REQUIRED)
find_package(kdl_conversions REQUIRED)

${orocos_kdl_LIBRARIES}
${kdl_conversions_LIBRARIES}

euler angle r. p. y. to quaternion

Quaternion Quaternion::fromRpy(
    double const roll, double const pitch, double const yaw)
{
    tf::Quaternion tfq;
    tfq.setRPY(roll, pitch, yaw);
    return Quaternion::fromTfQuaternion(tfq);
}

quaternion to r. p. y.

std::tuple<double, double, double> Quaternion::getRpy() const
{
    KDL::Rotation k;
    tf::quaternionMsgToKDL(*this, k);
    double roll, pitch, yaw;
    k.GetRPY(roll, pitch, yaw);
    return std::tuple<double, double, double>(roll, pitch, yaw);
}

conversion

euler angle r. p. y. to quaternion

Eigen::Quaterniond RpyToQuaternion(
    double const roll, double const pitch, double const yaw)
{
    /* Abbreviations for the various angular functions */
    double const cy = ::cos(yaw * 0.5);
    double const sy = ::sin(yaw * 0.5);
    double const cr = ::cos(roll * 0.5);
    double const sr = ::sin(roll * 0.5);
    double const cp = ::cos(pitch * 0.5);
    double const sp = ::sin(pitch * 0.5);
    Eigen::Quaterniond q;
    q.w() = cy * cr * cp + sy * sr * sp;
    q.x() = cy * sr * cp - sy * cr * sp;
    q.y() = cy * cr * sp + sy * sr * cp;
    q.z() = sy * cr * cp - cy * sr * sp;
    return q;
}

quaternion to r. p. y.

std::tuple<double, double, double> QuaternionToRpy(
    Eigen::Quaterniond const& q)
{
    /* roll (x-axis rotation) */
    double const sinr = +2.0 * (q.w() * q.x() + q.y() * q.z());
    double const cosr = +1.0 - 2.0 * (q.x() * q.x() + q.y() * q.y());
    double const roll = ::atan2(sinr, cosr);
    /* pitch (y-axis rotation) */
    double const sinp = +2.0 * (q.w() * q.y() - q.z() * q.x());
    double pitch;
    if (DoubleCompare(::fabs(sinp), 1.0) > 0) {
        /* use 90 degrees if out of range */
        pitch = ::copysign(M_PI / 2.0, sinp);
    } else {
        pitch = ::asin(sinp);
    }
    /* yaw (z-axis rotation) */
    double const siny = +2.0 * (q.w() * q.z() + q.x() * q.y());
    double const cosy = +1.0 - 2.0 * (q.y() * q.y() + q.z() * q.z());
    double const yaw = ::atan2(siny, cosy);
    return std::tuple<double, double, double>(roll, pitch, yaw);
}

by eigen

see
https://yuiwong.org/gitlab/cpp/cppcomm/blob/master/yuiwongcppbase/include/yuiwong/angle.hpp


see also

console output color

console output color

#


ansi code

  • 终端字符的颜色是用 esc 开头的转义序列进行控制 是文本模式下的系统显示功能
  • esc i.e. \033 (esc ascii 码十进制 27. 八进制 33)
  • 使用 ; 分隔以连续设置
code effect
\033[0m 关闭所有属性
\033[1m 粗体
\033[4m 下划线
\033[5m 闪烁
\033[7m 反显
\033[8m 消隐
\033[22m 非粗体
\033[24m 非下划线
\033[25m 非闪烁
\033[27m 非反显
\033[fg-colorm e.x. \033[30m 设置前景色
\033[bg-colorm e.x. \033[47m 设置背景色
\033[nA 光标上移 n 行
\03[nB 光标下移 n 行
\033[nC 光标右移 n 行
\033[nD 光标左移 n 行
\033[y;xH 设置光标位置 (x, y)
\033[2J 清屏
\033[K 清除从光标到行尾的内容
\033[s 保存光标位置
\033[u 恢复光标位置
\033[?25l 隐藏光标
\033[?25h 显示光标

fg color

  • 字(fg)颜色: [30, 39]
value color
30
31
32 绿
33
34
35
36 深绿
37 白色

bg color

  • 背景(bg)颜色范围: [40, 49]
value color
40
41
42 绿
43
44
45
46 深绿
47 白色

example

#define YUIWONGLOGNFTALL(__name, __fmt, __args...) \
    fprintf(stdout, \
        "\033[30;49m[%s %015ld ALL ][%s] " __fmt " (%s+%d)\n\033[0m", \
        yuiwong::LogRealTime().c_str(), ::pthread_self(), __name, ##__args, \
        __FILE__, __LINE__)
#define YUIWONGLOGNFTFATA(__name, __fmt, __args...) \
    fprintf(stderr, \
        "\033[1;31;49m[%s %015ld FATA][%s] " __fmt " (%s+%d)\n\033[0m", \
        yuiwong::LogRealTime().c_str(), ::pthread_self(), __name, ##__args, \
        __FILE__, __LINE__)
#define YUIWONGLOGNFTERRO(__name, __fmt, __args...) \
    fprintf(stderr, \
        "\033[31;49m[%s %015ld ERRO][%s] " __fmt " (%s+%d)\n\033[0m", \
        yuiwong::LogRealTime().c_str(), ::pthread_self(), __name, ##__args, \
        __FILE__, __LINE__)
#define YUIWONGLOGNFTWARN(__name, __fmt, __args...) \
    fprintf(stderr, \
        "\033[33;49m[%s %015ld WARN][%s] " __fmt " (%s+%d)\n\033[0m", \
        yuiwong::LogRealTime().c_str(), ::pthread_self(), __name, ##__args, \
        __FILE__, __LINE__)
#define YUIWONGLOGNFTNOTE(__name, __fmt, __args...) \
    fprintf(stdout, \
        "\033[1;30;49m[%s %015ld NOTE][%s] " __fmt " (%s+%d)\n\033[0m", \
        yuiwong::LogRealTime().c_str(), ::pthread_self(), __name, ##__args, \
        __FILE__, __LINE__)
#define YUIWONGLOGNFTINFO(__name, __fmt, __args...) \
    fprintf(stdout, \
        "\033[30;49m[%s %015ld INFO][%s] " __fmt " (%s+%d)\n\033[0m", \
        yuiwong::LogRealTime().c_str(), ::pthread_self(), __name, ##__args, \
        __FILE__, __LINE__)
#define YUIWONGLOGNFTDEBU(__name, __fmt, __args...) \
    fprintf(stdout, \
        "\033[36;49m[%s %015ld DEBU][%s] " __fmt " (%s+%d)\n\033[0m", \
        yuiwong::LogRealTime().c_str(), ::pthread_self(), __name, ##__args, \
        __FILE__, __LINE__)
#define YUIWONGLOGNFTDETA(__name, __fmt, __args...) \
    fprintf(stdout, \
        "\033[30;49m[%s %015ld DETA][%s] " __fmt " (%s+%d)\n\033[0m", \
        yuiwong::LogRealTime().c_str(), ::pthread_self(), __name, ##__args, \
        __FILE__, __LINE__)