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