Go to the documentation of this file.00001 #include <stdint.h>
00002 #include <math.h>
00003 #include "crtp.h"
00004
00005
00006
00007
00008
00009
00010 static inline uint32_t quatcompress(float const q[4])
00011 {
00012
00013 unsigned i_largest = 0;
00014 for (unsigned i = 1; i < 4; ++i) {
00015 if (fabsf(q[i]) > fabsf(q[i_largest])) {
00016 i_largest = i;
00017 }
00018 }
00019
00020
00021
00022
00023 unsigned negate = q[i_largest] < 0;
00024
00025
00026
00027 float const SMALL_MAX = 1.0 / sqrt(2);
00028
00029
00030 uint32_t comp = i_largest;
00031 for (unsigned i = 0; i < 4; ++i) {
00032 if (i != i_largest) {
00033 unsigned negbit = (q[i] < 0) ^ negate;
00034 unsigned mag = ((1 << 9) - 1) * (fabsf(q[i]) / SMALL_MAX) + 0.5f;
00035 comp = (comp << 10) | (negbit << 9) | mag;
00036 }
00037 }
00038
00039 return comp;
00040 }
00041
00042
00043
00044 void quatdecompress(uint32_t comp, float q[4])
00045 {
00046 float const SMALL_MAX = 1.0 / sqrt(2);
00047 unsigned const mask = (1 << 9) - 1;
00048
00049 int const i_largest = comp >> 30;
00050 float sum_squares = 0;
00051 for (int i = 3; i >= 0; --i) {
00052 if (i != i_largest) {
00053 unsigned mag = comp & mask;
00054 unsigned negbit = (comp >> 9) & 0x1;
00055 comp = comp >> 10;
00056 q[i] = SMALL_MAX * ((float)mag) / mask;
00057 if (negbit == 1) {
00058 q[i] = -q[i];
00059 }
00060 sum_squares += q[i] * q[i];
00061 }
00062 }
00063 q[i_largest] = sqrtf(1.0f - sum_squares);
00064 }
00065
00066 crtpFullStateSetpointRequest::crtpFullStateSetpointRequest(
00067 float x, float y, float z,
00068 float vx, float vy, float vz,
00069 float ax, float ay, float az,
00070 float qx, float qy, float qz, float qw,
00071 float rollRate, float pitchRate, float yawRate)
00072 : header(0x07, 0), type(6)
00073 {
00074 float s = 1000.0;
00075 this->x = s * x;
00076 this->y = s * y;
00077 this->z = s * z;
00078 this->vx = s * vx;
00079 this->vy = s * vy;
00080 this->vz = s * vz;
00081 this->ax = s * ax;
00082 this->ay = s * ay;
00083 this->az = s * az;
00084
00085 float q[4] = { qx, qy, qz, qw };
00086 this->quat = quatcompress(q);
00087 this->omegax = s * rollRate;
00088 this->omegay = s * pitchRate;
00089 this->omegaz = s * yawRate;
00090 }
00091
00092 crtpStopRequest::crtpStopRequest()
00093 : header(0X07, 0), type(0)
00094 {}
00095
00096
00097 crtpHoverSetpointRequest::crtpHoverSetpointRequest(
00098 float vx,
00099 float vy,
00100 float yawrate,
00101 float zDistance)
00102 : header(0X07, 0), type(5)
00103 {
00104 this->vx = vx;
00105 this->vy = vy;
00106 this->yawrate = yawrate;
00107 this->zDistance = zDistance;
00108 }
00109
00110
00111 crtpPositionSetpointRequest::crtpPositionSetpointRequest(
00112 float x,
00113 float y,
00114 float z,
00115 float yaw)
00116 : header(0X07, 0), type(7)
00117 {
00118 this->x = x;
00119 this->y = y;
00120 this->z = z;
00121 this->yaw = yaw;
00122 }