]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/turn_indicator.cxx
add to project files
[flightgear.git] / src / Instrumentation / turn_indicator.cxx
1 // turn_indicator.cxx - an electric-powered turn indicator.
2 // Written by David Megginson, started 2003.
3 //
4 // This file is in the Public Domain and comes with no warranty.
5
6 #include <simgear/compiler.h>
7 #include <iostream>
8 #include <string>
9 #include <sstream>
10
11 #include "turn_indicator.hxx"
12 #include <Main/fg_props.hxx>
13 #include <Main/util.hxx>
14
15 using std::string;
16
17 // Use a bigger number to be more responsive, or a smaller number
18 // to be more sluggish.
19 #define RESPONSIVENESS 0.5
20
21
22 TurnIndicator::TurnIndicator ( SGPropertyNode *node) :
23     _last_rate(0),
24     _name(node->getStringValue("name", "turn-indicator")),
25     _num(node->getIntValue("number", 0))
26 {
27 }
28
29 TurnIndicator::~TurnIndicator ()
30 {
31 }
32
33 void
34 TurnIndicator::init ()
35 {
36     string branch;
37     branch = "/instrumentation/" + _name;
38
39     SGPropertyNode *node = fgGetNode(branch.c_str(), _num, true );
40     _roll_rate_node = fgGetNode("/orientation/roll-rate-degps", true);
41     _yaw_rate_node = fgGetNode("/orientation/yaw-rate-degps", true);
42     _electric_current_node = 
43         fgGetNode("/systems/electrical/outputs/turn-coordinator", true);
44     _rate_out_node = node->getChild("indicated-turn-rate", 0, true);
45 }
46
47 void
48 TurnIndicator::bind ()
49 {
50     std::ostringstream temp;
51     string branch;
52     temp << _num;
53     branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
54
55     fgTie((branch + "/serviceable").c_str(),
56           &_gyro, &Gyro::is_serviceable, &Gyro::set_serviceable);
57     fgTie((branch + "/spin").c_str(),
58           &_gyro, &Gyro::get_spin_norm, &Gyro::set_spin_norm);
59 }
60
61 void
62 TurnIndicator::unbind ()
63 {
64     std::ostringstream temp;
65     string branch;
66     temp << _num;
67     branch = "/instrumentation/" + _name + "[" + temp.str() + "]";
68
69     fgUntie((branch + "/serviceable").c_str());
70     fgUntie((branch + "/serviceable").c_str());
71 }
72
73 void
74 TurnIndicator::update (double dt)
75 {
76                                 // Get the spin from the gyro
77     double power = _electric_current_node->getDoubleValue() / 12.0;
78     _gyro.set_power_norm(power);
79     _gyro.update(dt);
80     double spin = _gyro.get_spin_norm();
81
82                                 // Calculate the indicated rate
83     double factor = 1.0 - ((1.0 - spin) * (1.0 - spin) * (1.0 - spin));
84     double rate = ((_roll_rate_node->getDoubleValue() / 20.0) +
85                    (_yaw_rate_node->getDoubleValue() / 3.0));
86
87                                 // Clamp the rate
88     if (rate < -2.5)
89         rate = -2.5;
90     else if (rate > 2.5)
91         rate = 2.5;
92
93                                 // Lag left, based on gyro spin
94     rate = -2.5 + (factor * (rate + 2.5));
95     rate = fgGetLowPass(_last_rate, rate, dt*RESPONSIVENESS);
96     _last_rate = rate;
97     
98                                 // Publish the indicated rate
99     _rate_out_node->setDoubleValue(rate);
100 }
101
102 // end of turn_indicator.cxx