| 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 | |
| 21 | using 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 | |
| 28 | TurnIndicator::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 | |
| 42 | TurnIndicator::~TurnIndicator () |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | TurnIndicator::init () |
| 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 | |
| 64 | initServicePowerProperties(node); |
| 65 | |
| 66 | reinit(); |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | TurnIndicator::reinit () |
| 71 | { |
| 72 | _last_rate = 0; |
| 73 | _gyro.reinit(); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | TurnIndicator::update (double dt) |
| 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 |