2011-11-16 21:49:56 -04:00
|
|
|
/*
|
|
|
|
SITL handling - utility functions
|
|
|
|
|
|
|
|
This simulates the APM1 hardware sufficiently for the APM code to
|
|
|
|
think it is running on real hardware
|
|
|
|
|
|
|
|
Andrew Tridgell November 2011
|
|
|
|
*/
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <fcntl.h>
|
2012-03-28 06:47:33 -03:00
|
|
|
#include <AP_Math.h>
|
2011-11-16 21:49:56 -04:00
|
|
|
#include "desktop.h"
|
2012-03-01 00:22:11 -04:00
|
|
|
#include "util.h"
|
2011-11-16 21:49:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
void set_nonblocking(int fd)
|
|
|
|
{
|
|
|
|
unsigned v = fcntl(fd, F_GETFL, 0);
|
|
|
|
fcntl(fd, F_SETFL, v | O_NONBLOCK);
|
|
|
|
}
|
2011-11-18 19:07:11 -04:00
|
|
|
|
|
|
|
double normalise(double v, double min, double max)
|
|
|
|
{
|
|
|
|
while (v < min) {
|
|
|
|
v += (max - min);
|
|
|
|
}
|
|
|
|
while (v > max) {
|
|
|
|
v -= (max - min);
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
2011-11-28 01:01:23 -04:00
|
|
|
|
|
|
|
double normalise180(double v)
|
|
|
|
{
|
|
|
|
return normalise(v, -180, 180);
|
|
|
|
}
|
2012-03-01 00:22:11 -04:00
|
|
|
|
|
|
|
/* convert the angular velocities from earth frame to
|
|
|
|
body frame. Thanks to James Goppert for the formula
|
|
|
|
*/
|
|
|
|
void convert_body_frame(double rollDeg, double pitchDeg,
|
|
|
|
double rollRate, double pitchRate, double yawRate,
|
|
|
|
double *p, double *q, double *r)
|
|
|
|
{
|
|
|
|
double phi, theta, phiDot, thetaDot, psiDot;
|
|
|
|
|
|
|
|
phi = ToRad(rollDeg);
|
|
|
|
theta = ToRad(pitchDeg);
|
|
|
|
phiDot = ToRad(rollRate);
|
|
|
|
thetaDot = ToRad(pitchRate);
|
|
|
|
psiDot = ToRad(yawRate);
|
|
|
|
|
|
|
|
*p = phiDot - psiDot*sin(theta);
|
|
|
|
*q = cos(phi)*thetaDot + sin(phi)*psiDot*cos(theta);
|
|
|
|
*r = cos(phi)*psiDot*cos(theta) - sin(phi)*thetaDot;
|
|
|
|
}
|
2012-03-28 06:47:33 -03:00
|
|
|
|
|
|
|
// generate a random Vector3f of size 1
|
|
|
|
Vector3f rand_vec3f(void)
|
|
|
|
{
|
|
|
|
Vector3f v = Vector3f(rand_float(),
|
|
|
|
rand_float(),
|
|
|
|
rand_float());
|
|
|
|
if (v.length() != 0.0) {
|
|
|
|
v.normalize();
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|