FlightGear next
attitude_indicator_electric.cxx
Go to the documentation of this file.
1// attitude_indicator_electric.hxx - an electrically-powered attitude indicator.
2// Written by David Megginson, started 2002.
3// Ported to Electric by Benedikt Wolf 2023
4// Enhanced by Benedikt Hallinger, 2023
5//
6// This file is in the Public Domain and comes with no warranty.
7
8
9#ifdef HAVE_CONFIG_H
10# include "config.h"
11#endif
12
13#include <simgear/compiler.h>
14
15#include <iostream>
16#include <string>
17#include <sstream>
18
19#include <cmath> // fabs()
20
22#include <Main/fg_props.hxx>
23#include <Main/util.hxx>
24
25using std::string;
26
28:
29spin_thresh(0.8),
30max_roll_error(40.0),
31max_pitch_error(12.0)
32{
33 SGPropertyNode* gyro_cfg = node->getChild("gyro", 0, true);
34 _gyro_spin_up = gyro_cfg->getDoubleValue("spin-up-sec", 4.0);
35 _gyro_spin_down = gyro_cfg->getDoubleValue("spin-down-sec", 180.0);
36
37 readConfig(node, "attitude-indicator-electric");
38}
39
43
44void
46{
47 string branch = nodePath();
48
49 SGPropertyNode *node = fgGetNode(branch, true );
50 SGPropertyNode *n;
51
52 _pitch_in_node = fgGetNode("/orientation/pitch-deg", true);
53 _roll_in_node = fgGetNode("/orientation/roll-deg", true);
54 SGPropertyNode *cnode = node->getChild("config", 0, true);
55 _tumble_flag_node = cnode->getChild("tumble-flag", 0, true);
56 _caged_node = node->getChild("caged-flag", 0, true);
57 _tumble_node = node->getChild("tumble-norm", 0, true);
58 if( ( n = cnode->getChild("spin-thresh", 0, false ) ) != NULL )
59 spin_thresh = n->getDoubleValue();
60 if( ( n = cnode->getChild("max-roll-error-deg", 0, false ) ) != NULL )
61 max_roll_error = n->getDoubleValue();
62 if( ( n = cnode->getChild("max-pitch-error-deg", 0, false ) ) != NULL )
63 max_pitch_error = n->getDoubleValue();
64 _pitch_int_node = node->getChild("internal-pitch-deg", 0, true);
65 _roll_int_node = node->getChild("internal-roll-deg", 0, true);
66 _pitch_out_node = node->getChild("indicated-pitch-deg", 0, true);
67 _roll_out_node = node->getChild("indicated-roll-deg", 0, true);
68 _off_node = node->getChild("off-flag", 0, true);
69 _spin_node = node->getChild("spin", 0, true);
70
71 SGPropertyNode* gyro_node = node->getChild("gyro", 0, true);
72 _gyro_spin_up_node = gyro_node->getChild("spin-up-sec", 0, true);
73 _gyro_spin_down_node = gyro_node->getChild("spin-down-sec", 0, true);
74 if (!_gyro_spin_up_node->hasValue())
75 _gyro_spin_up_node->setDoubleValue(_gyro_spin_up);
76 if (!_gyro_spin_down_node->hasValue())
77 _gyro_spin_down_node->setDoubleValue(_gyro_spin_down);
78
80
81 reinit();
82}
83
84void
86{
87 _roll_int_node->setDoubleValue(0.0);
88 _pitch_int_node->setDoubleValue(0.0);
89 _gyro.reinit();
90}
91
92void
94{
95 // If it's caged, it doesn't indicate
96 if (_caged_node->getBoolValue()) {
97 _roll_int_node->setDoubleValue(0.0);
98 _pitch_int_node->setDoubleValue(0.0);
99 return;
100 }
101
102 // Get the spin from the gyro
103 _gyro.set_power_norm(isServiceableAndPowered());
104 _gyro.set_spin_up(_gyro_spin_up_node->getDoubleValue());
105 _gyro.set_spin_down(_gyro_spin_down_node->getDoubleValue());
106 _gyro.set_spin_norm(_spin_node->getDoubleValue());
107 _gyro.update(dt);
108 double spin = _gyro.get_spin_norm();
109 _spin_node->setDoubleValue( spin );
110
111 _off_node->setBoolValue( !( isServiceableAndPowered() && spin >= 0.25 ) );
112
113 // Calculate the responsiveness
114 double responsiveness = spin * spin * spin * spin * spin * spin;
115
116 // Get the indicated roll and pitch
117 double roll = _roll_in_node->getDoubleValue();
118 double pitch = _pitch_in_node->getDoubleValue();
119
120 // Calculate the tumble for the
121 // next pass.
122 if (_tumble_flag_node->getBoolValue()) {
123 double tumble = _tumble_node->getDoubleValue();
124 if (fabs(roll) > 45.0) {
125 double target = (fabs(roll) - 45.0) / 45.0;
126 target *= target; // exponential past +-45 degrees
127 if (roll < 0)
128 target = -target;
129
130 if (fabs(target) > fabs(tumble))
131 tumble = target;
132
133 if (tumble > 1.0)
134 tumble = 1.0;
135 else if (tumble < -1.0)
136 tumble = -1.0;
137 }
138 // Reerect in 5 minutes
139 double step = dt/300.0;
140 if (tumble < -step)
141 tumble += step;
142 else if (tumble > step)
143 tumble -= step;
144
145 roll += tumble * 45;
146 _tumble_node->setDoubleValue(tumble);
147 }
148
149 roll = fgGetLowPass(_roll_int_node->getDoubleValue(), roll,
150 responsiveness);
151 pitch = fgGetLowPass(_pitch_int_node->getDoubleValue(), pitch,
152 responsiveness);
153
154 // Assign the new values
155 _roll_int_node->setDoubleValue(roll);
156 _pitch_int_node->setDoubleValue(pitch);
157
158 // add in a gyro underspin "error" if gyro is spinning too slowly
159 double roll_error;
160 double pitch_error;
161 if ( spin <= spin_thresh ) {
162 double roll_error_factor = (spin_thresh - spin) / spin_thresh;
163 double pitch_error_factor = (spin_thresh - spin) / spin_thresh;
164 roll_error = roll_error_factor * roll_error_factor * max_roll_error;
165 pitch_error = pitch_error_factor * pitch_error_factor * max_pitch_error;
166 } else {
167 roll_error = 0.0;
168 pitch_error = 0.0;
169 }
170
171 _roll_out_node->setDoubleValue(roll + roll_error);
172 _pitch_out_node->setDoubleValue(pitch + pitch_error);
173}
174
175// end of attitude_indicator_electric.cxx
void initServicePowerProperties(SGPropertyNode *node)
void readConfig(SGPropertyNode *config, std::string defaultName)
std::string nodePath() const
bool isServiceableAndPowered() const
AttitudeIndicatorElectric(SGPropertyNode *node)
SGPropertyNode * fgGetNode(const char *path, bool create)
Get a property node.
Definition proptest.cpp:27
double fgGetLowPass(double current, double target, double timeratio)
Move a value towards a target.
Definition util.cxx:46