FlightGear next
turn_indicator.cxx
Go to the documentation of this file.
1/*
2 * SPDX-FileName: turn_indicator.cxx
3 * SPDX-FileComment: an electric-powered turn indicator.
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 * SPDX-FileContributor: Written by David Megginson, started 2002.
6 * SPDX-FileContributor: Enhanced by Benedikt Hallinger, 2023
7 */
8
9#include "config.h"
10
11#include "turn_indicator.hxx"
12
13#include <simgear/compiler.h>
14#include <iostream>
15#include <string>
16#include <sstream>
17
18#include <Main/fg_props.hxx>
19#include <Main/util.hxx>
20
21using std::string;
22
23// Use a bigger number to be more responsive, or a smaller number
24// to be more sluggish.
25#define RESPONSIVENESS 0.5
26
27
28TurnIndicator::TurnIndicator ( SGPropertyNode *node) :
29 _last_rate(0)
30{
31 if( !node->getBoolValue("new-default-power-path", 0) ){
32 setDefaultPowerSupplyPath("/systems/electrical/outputs/turn-coordinator");
33 }
34
35 SGPropertyNode* gyro_cfg = node->getChild("gyro", 0, true);
36 _gyro_spin_up = gyro_cfg->getDoubleValue("spin-up-sec", 4.0);
37 _gyro_spin_down = gyro_cfg->getDoubleValue("spin-down-sec", 180.0);
38
39 readConfig(node, "turn-indicator");
40}
41
45
46void
48{
49 string branch = nodePath();
50
51 SGPropertyNode *node = fgGetNode(branch, true );
52 _roll_rate_node = fgGetNode("/orientation/roll-rate-degps", true);
53 _yaw_rate_node = fgGetNode("/orientation/yaw-rate-degps", true);
54 _rate_out_node = node->getChild("indicated-turn-rate", 0, true);
55 _spin_node = node->getChild("spin", 0, true);
56 SGPropertyNode* gyro_node = node->getChild("gyro", 0, true);
57 _gyro_spin_up_node = gyro_node->getChild("spin-up-sec", 0, true);
58 _gyro_spin_down_node = gyro_node->getChild("spin-down-sec", 0, true);
59 if (!_gyro_spin_up_node->hasValue())
60 _gyro_spin_up_node->setDoubleValue(_gyro_spin_up);
61 if (!_gyro_spin_down_node->hasValue())
62 _gyro_spin_down_node->setDoubleValue(_gyro_spin_down);
63
65
66 reinit();
67}
68
69void
71{
72 _last_rate = 0;
73 _gyro.reinit();
74}
75
76void
78{
79 // Get the spin from the gyro
80 _gyro.set_power_norm(isServiceableAndPowered());
81 _gyro.set_spin_up(_gyro_spin_up_node->getDoubleValue());
82 _gyro.set_spin_down(_gyro_spin_down_node->getDoubleValue());
83 _gyro.set_spin_norm(_spin_node->getDoubleValue());
84 _gyro.update(dt);
85 double spin = _gyro.get_spin_norm();
86 _spin_node->setDoubleValue( spin );
87
88 // Calculate the indicated rate
89 double factor = 1.0 - ((1.0 - spin) * (1.0 - spin) * (1.0 - spin));
90 double rate = ((_roll_rate_node->getDoubleValue() / 20.0) +
91 (_yaw_rate_node->getDoubleValue() / 3.0));
92
93 // Clamp the rate
94 if (rate < -2.5)
95 rate = -2.5;
96 else if (rate > 2.5)
97 rate = 2.5;
98
99 // Lag left, based on gyro spin
100 rate = -2.5 + (factor * (rate + 2.5));
101 rate = fgGetLowPass(_last_rate, rate, dt*RESPONSIVENESS);
102 _last_rate = rate;
103
104 // Publish the indicated rate
105 _rate_out_node->setDoubleValue(rate);
106}
107
108// end of turn_indicator.cxx
#define RESPONSIVENESS
Definition adf.cxx:31
void initServicePowerProperties(SGPropertyNode *node)
void readConfig(SGPropertyNode *config, std::string defaultName)
std::string nodePath() const
bool isServiceableAndPowered() const
void update(double dt) override
void reinit() override
void init() override
virtual ~TurnIndicator()
TurnIndicator(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