2002-09-24 14:51:37 +00:00
|
|
|
// attitude_indicator.cxx - a vacuum-powered attitude indicator.
|
|
|
|
// Written by David Megginson, started 2002.
|
|
|
|
//
|
|
|
|
// This file is in the Public Domain and comes with no warranty.
|
|
|
|
|
2002-09-27 18:27:58 +00:00
|
|
|
// TODO:
|
|
|
|
// - better spin-up
|
|
|
|
|
2004-11-19 23:01:34 +00:00
|
|
|
#include <simgear/compiler.h>
|
|
|
|
|
|
|
|
#include STL_IOSTREAM
|
|
|
|
#include STL_STRING
|
|
|
|
#include <sstream>
|
|
|
|
|
2003-04-05 13:05:24 +00:00
|
|
|
#include <math.h> // fabs()
|
|
|
|
|
2002-09-24 14:51:37 +00:00
|
|
|
#include "attitude_indicator.hxx"
|
|
|
|
#include <Main/fg_props.hxx>
|
2003-04-04 03:25:02 +00:00
|
|
|
#include <Main/util.hxx>
|
2002-09-24 14:51:37 +00:00
|
|
|
|
|
|
|
|
2004-10-16 12:37:39 +00:00
|
|
|
AttitudeIndicator::AttitudeIndicator ( SGPropertyNode *node )
|
|
|
|
:
|
|
|
|
name("attitude-indicator"),
|
|
|
|
num(0),
|
|
|
|
vacuum_system("/systems/vacuum")
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for ( i = 0; i < node->nChildren(); ++i ) {
|
|
|
|
SGPropertyNode *child = node->getChild(i);
|
|
|
|
string cname = child->getName();
|
|
|
|
string cval = child->getStringValue();
|
|
|
|
if ( cname == "name" ) {
|
|
|
|
name = cval;
|
|
|
|
} else if ( cname == "number" ) {
|
|
|
|
num = (int) child->getDoubleValue();
|
|
|
|
} else if ( cname == "vacuum-system" ) {
|
|
|
|
vacuum_system = cval;
|
|
|
|
} else {
|
2004-10-24 11:05:14 +00:00
|
|
|
SG_LOG( SG_INSTR, SG_WARN, "Error in attitude-indicator config logic" );
|
2004-10-16 12:37:39 +00:00
|
|
|
if ( name.length() ) {
|
2004-10-24 11:05:14 +00:00
|
|
|
SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
|
2004-10-16 12:37:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-24 14:51:37 +00:00
|
|
|
AttitudeIndicator::AttitudeIndicator ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AttitudeIndicator::~AttitudeIndicator ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AttitudeIndicator::init ()
|
|
|
|
{
|
2004-10-16 12:37:39 +00:00
|
|
|
string branch;
|
|
|
|
branch = "/instrumentation/" + name;
|
|
|
|
vacuum_system += "/suction-inhg";
|
|
|
|
|
|
|
|
SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
|
|
|
|
|
2002-09-24 14:51:37 +00:00
|
|
|
_pitch_in_node = fgGetNode("/orientation/pitch-deg", true);
|
|
|
|
_roll_in_node = fgGetNode("/orientation/roll-deg", true);
|
2004-10-16 12:37:39 +00:00
|
|
|
_suction_node = fgGetNode(vacuum_system.c_str(), true);
|
|
|
|
SGPropertyNode *cnode = node->getChild("config", 0, true);
|
|
|
|
_tumble_flag_node = cnode->getChild("tumble-flag", 0, true);
|
|
|
|
_caged_node = node->getChild("caged-flag", 0, true);
|
|
|
|
_tumble_node = node->getChild("tumble-norm", 0, true);
|
|
|
|
_pitch_int_node = node->getChild("internal-pitch-deg", 0, true);
|
|
|
|
_roll_int_node = node->getChild("internal-roll-deg", 0, true);
|
|
|
|
_pitch_out_node = node->getChild("indicated-pitch-deg", 0, true);
|
|
|
|
_roll_out_node = node->getChild("indicated-roll-deg", 0, true);
|
2002-09-24 14:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AttitudeIndicator::bind ()
|
|
|
|
{
|
2004-11-19 23:01:34 +00:00
|
|
|
std::ostringstream temp;
|
2004-10-16 12:37:39 +00:00
|
|
|
string branch;
|
2004-11-19 23:01:34 +00:00
|
|
|
temp << num;
|
|
|
|
branch = "/instrumentation/" + name + "[" + temp.str() + "]";
|
2004-10-16 12:37:39 +00:00
|
|
|
|
2004-11-19 23:01:34 +00:00
|
|
|
fgTie((branch + "/serviceable").c_str(),
|
2003-01-26 20:20:15 +00:00
|
|
|
&_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
|
2004-11-19 23:01:34 +00:00
|
|
|
fgTie((branch + "/spin").c_str(),
|
2003-01-25 19:49:27 +00:00
|
|
|
&_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
|
2002-09-24 14:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AttitudeIndicator::unbind ()
|
|
|
|
{
|
2004-11-19 23:01:34 +00:00
|
|
|
std::ostringstream temp;
|
2004-10-16 12:37:39 +00:00
|
|
|
string branch;
|
2004-11-19 23:01:34 +00:00
|
|
|
temp << num;
|
|
|
|
branch = "/instrumentation/" + name + "[" + temp.str() + "]";
|
2004-10-16 12:37:39 +00:00
|
|
|
|
2004-11-19 23:01:34 +00:00
|
|
|
fgUntie((branch + "/serviceable").c_str());
|
|
|
|
fgUntie((branch + "/spin").c_str());
|
2002-09-24 14:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AttitudeIndicator::update (double dt)
|
|
|
|
{
|
2003-04-05 14:37:12 +00:00
|
|
|
// If it's caged, it doesn't indicate
|
|
|
|
if (_caged_node->getBoolValue()) {
|
2003-04-25 02:22:06 +00:00
|
|
|
_roll_int_node->setDoubleValue(0.0);
|
|
|
|
_pitch_int_node->setDoubleValue(0.0);
|
2003-04-05 14:37:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-01-25 19:49:27 +00:00
|
|
|
// Get the spin from the gyro
|
|
|
|
_gyro.set_power_norm(_suction_node->getDoubleValue()/5.0);
|
|
|
|
_gyro.update(dt);
|
|
|
|
double spin = _gyro.get_spin_norm();
|
2002-09-24 14:51:37 +00:00
|
|
|
|
2003-04-04 03:25:02 +00:00
|
|
|
// Calculate the responsiveness
|
|
|
|
double responsiveness = spin * spin * spin * spin * spin * spin;
|
|
|
|
|
|
|
|
// Get the indicated roll and pitch
|
2002-09-24 14:51:37 +00:00
|
|
|
double roll = _roll_in_node->getDoubleValue();
|
|
|
|
double pitch = _pitch_in_node->getDoubleValue();
|
2002-09-24 16:37:28 +00:00
|
|
|
|
2003-04-04 03:25:02 +00:00
|
|
|
// Calculate the tumble for the
|
|
|
|
// next pass.
|
|
|
|
if (_tumble_flag_node->getBoolValue()) {
|
2003-04-05 14:37:12 +00:00
|
|
|
double tumble = _tumble_node->getDoubleValue();
|
|
|
|
if (fabs(roll) > 45.0) {
|
|
|
|
double target = (fabs(roll) - 45.0) / 45.0;
|
|
|
|
target *= target; // exponential past +-45 degrees
|
|
|
|
if (roll < 0)
|
|
|
|
target = -target;
|
|
|
|
|
|
|
|
if (fabs(target) > fabs(tumble))
|
|
|
|
tumble = target;
|
|
|
|
|
|
|
|
if (tumble > 1.0)
|
|
|
|
tumble = 1.0;
|
|
|
|
else if (tumble < -1.0)
|
|
|
|
tumble = -1.0;
|
2003-04-04 03:25:02 +00:00
|
|
|
}
|
2003-04-05 14:37:12 +00:00
|
|
|
// Reerect in 5 minutes
|
|
|
|
double step = dt/300.0;
|
|
|
|
if (tumble < -step)
|
|
|
|
tumble += step;
|
|
|
|
else if (tumble > step)
|
|
|
|
tumble -= step;
|
|
|
|
|
|
|
|
roll += tumble * 45;
|
|
|
|
_tumble_node->setDoubleValue(tumble);
|
2003-04-04 03:25:02 +00:00
|
|
|
}
|
|
|
|
|
2003-04-25 02:22:06 +00:00
|
|
|
roll = fgGetLowPass(_roll_int_node->getDoubleValue(), roll,
|
2003-04-04 03:25:02 +00:00
|
|
|
responsiveness);
|
2003-04-25 02:22:06 +00:00
|
|
|
pitch = fgGetLowPass(_pitch_int_node->getDoubleValue(), pitch,
|
2003-04-04 03:25:02 +00:00
|
|
|
responsiveness);
|
|
|
|
|
|
|
|
// Assign the new values
|
2003-04-25 02:22:06 +00:00
|
|
|
_roll_int_node->setDoubleValue(roll);
|
|
|
|
_pitch_int_node->setDoubleValue(pitch);
|
|
|
|
|
|
|
|
// add in a gyro underspin "error" if gyro is spinning too slowly
|
2003-05-27 19:13:51 +00:00
|
|
|
const double spin_thresh = 0.8;
|
2003-04-25 02:22:06 +00:00
|
|
|
const double max_roll_error = 40.0;
|
2003-04-25 03:36:51 +00:00
|
|
|
const double max_pitch_error = 12.0;
|
2003-04-25 02:22:06 +00:00
|
|
|
double roll_error;
|
|
|
|
double pitch_error;
|
|
|
|
if ( spin <= spin_thresh ) {
|
|
|
|
double roll_error_factor = (spin_thresh - spin) / spin_thresh;
|
|
|
|
double pitch_error_factor = (spin_thresh - spin) / spin_thresh;
|
|
|
|
roll_error = roll_error_factor * roll_error_factor * max_roll_error;
|
|
|
|
pitch_error = pitch_error_factor * pitch_error_factor * max_pitch_error;
|
|
|
|
} else {
|
|
|
|
roll_error = 0.0;
|
|
|
|
pitch_error = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_roll_out_node->setDoubleValue(roll + roll_error);
|
|
|
|
_pitch_out_node->setDoubleValue(pitch + pitch_error);
|
2002-09-24 14:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// end of attitude_indicator.cxx
|