| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | JSBSim |
| 4 | Author: Jon S. Berndt |
| 5 | Date started: 08/24/00 |
| 6 | ------------- Copyright (C) 2000 Jon S. Berndt ([email protected]) ------------- |
| 7 | |
| 8 | Module: FGTransmission.cpp |
| 9 | Author: T.Kreitler |
| 10 | Date started: 02/05/12 |
| 11 | |
| 12 | ------------- Copyright (C) 2012 T. Kreitler ([email protected]) ------------- |
| 13 | |
| 14 | This program is free software; you can redistribute it and/or modify it under |
| 15 | the terms of the GNU Lesser General Public License as published by the Free Software |
| 16 | Foundation; either version 2 of the License, or (at your option) any later |
| 17 | version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 20 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 21 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 22 | details. |
| 23 | |
| 24 | You should have received a copy of the GNU Lesser General Public License along with |
| 25 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 26 | Place - Suite 330, Boston, MA 02111-1307, USA. |
| 27 | |
| 28 | Further information about the GNU Lesser General Public License can also be found on |
| 29 | the world wide web at http://www.gnu.org. |
| 30 | |
| 31 | FUNCTIONAL DESCRIPTION |
| 32 | -------------------------------------------------------------------------------- |
| 33 | |
| 34 | HISTORY |
| 35 | -------------------------------------------------------------------------------- |
| 36 | 02/05/12 T.Kreitler Created |
| 37 | |
| 38 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 39 | INCLUDES |
| 40 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 41 | |
| 42 | |
| 43 | #include "FGTransmission.h" |
| 44 | |
| 45 | using std::string; |
| 46 | using std::cout; |
| 47 | using std::endl; |
| 48 | |
| 49 | namespace JSBSim { |
| 50 | |
| 51 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 52 | CLASS IMPLEMENTATION |
| 53 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 54 | |
| 55 | FGTransmission::FGTransmission(FGFDMExec *exec, int num, double dt) : |
| 56 | FreeWheelTransmission(1.0), |
| 57 | ThrusterMoment(1.0), EngineMoment(1.0), EngineFriction(0.0), |
| 58 | ClutchCtrlNorm(1.0), BrakeCtrlNorm(0.0), MaxBrakePower(0.0), |
| 59 | EngineRPM(0.0), ThrusterRPM(0.0) |
| 60 | { |
| 61 | PropertyManager = exec->GetPropertyManager(); |
| 62 | FreeWheelLag = Filter(200.0,dt); // avoid too abrupt changes in transmission |
| 63 | BindModel(num); |
| 64 | } |
| 65 | |
| 66 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 67 | |
| 68 | FGTransmission::~FGTransmission(){ |
| 69 | } |
| 70 | |
| 71 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 72 | |
| 73 | // basically P = Q*w and Q_Engine + (-Q_Rotor) = J * dw/dt, J = Moment |
| 74 | // |
| 75 | void FGTransmission::Calculate(double EnginePower, double ThrusterTorque, double dt) { |
| 76 | |
| 77 | double coupling = 1.0, coupling_sq = 1.0; |
| 78 | double fw_mult = 1.0; |
| 79 | |
| 80 | double d_omega = 0.0, engine_d_omega = 0.0, thruster_d_omega = 0.0; // relative changes |
| 81 | |
| 82 | double engine_omega = rpm_to_omega(EngineRPM); |
| 83 | double safe_engine_omega = engine_omega < 1e-1 ? 1e-1 : engine_omega; |
| 84 | double engine_torque = EnginePower / safe_engine_omega; |
| 85 | |
| 86 | double thruster_omega = rpm_to_omega(ThrusterRPM); |
| 87 | double safe_thruster_omega = thruster_omega < 1e-1 ? 1e-1 : thruster_omega; |
| 88 | |
| 89 | engine_torque -= EngineFriction / safe_engine_omega; |
| 90 | ThrusterTorque += Constrain(0.0, BrakeCtrlNorm, 1.0) * MaxBrakePower / safe_thruster_omega; |
| 91 | |
| 92 | // would the FWU release ? |
| 93 | engine_d_omega = engine_torque/EngineMoment * dt; |
| 94 | thruster_d_omega = - ThrusterTorque/ThrusterMoment * dt; |
| 95 | |
| 96 | if ( thruster_omega+thruster_d_omega > engine_omega+engine_d_omega ) { |
| 97 | // don't drive the engine |
| 98 | FreeWheelTransmission = 0.0; |
| 99 | } else { |
| 100 | FreeWheelTransmission = 1.0; |
| 101 | } |
| 102 | |
| 103 | fw_mult = FreeWheelLag.execute(FreeWheelTransmission); |
| 104 | coupling = fw_mult * Constrain(0.0, ClutchCtrlNorm, 1.0); |
| 105 | |
| 106 | if (coupling < 0.999999) { // are the separate calculations needed ? |
| 107 | // assume linear transfer |
| 108 | engine_d_omega = |
| 109 | (engine_torque - ThrusterTorque*coupling)/(ThrusterMoment*coupling + EngineMoment) * dt; |
| 110 | thruster_d_omega = |
| 111 | (engine_torque*coupling - ThrusterTorque)/(ThrusterMoment + EngineMoment*coupling) * dt; |
| 112 | |
| 113 | EngineRPM += omega_to_rpm(engine_d_omega); |
| 114 | ThrusterRPM += omega_to_rpm(thruster_d_omega); |
| 115 | |
| 116 | // simulate transit to static friction |
| 117 | coupling_sq = coupling*coupling; |
| 118 | EngineRPM = (1.0-coupling_sq) * EngineRPM + coupling_sq * 0.02 * (49.0*EngineRPM + ThrusterRPM); |
| 119 | ThrusterRPM = (1.0-coupling_sq) * ThrusterRPM + coupling_sq * 0.02 * (EngineRPM + 49.0*ThrusterRPM); |
| 120 | |
| 121 | // enforce equal rpm |
| 122 | if ( fabs(EngineRPM-ThrusterRPM) < 1e-3 ) { |
| 123 | EngineRPM = ThrusterRPM = 0.5 * (EngineRPM+ThrusterRPM); |
| 124 | } |
| 125 | } else { |
| 126 | d_omega = (engine_torque - ThrusterTorque)/(ThrusterMoment + EngineMoment) * dt; |
| 127 | EngineRPM = ThrusterRPM += omega_to_rpm(d_omega); |
| 128 | } |
| 129 | |
| 130 | // nothing will turn backward |
| 131 | if (EngineRPM < 0.0 ) EngineRPM = 0.0; |
| 132 | if (ThrusterRPM < 0.0 ) ThrusterRPM = 0.0; |
| 133 | |
| 134 | } |
| 135 | |
| 136 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 137 | |
| 138 | bool FGTransmission::BindModel(int num) |
| 139 | { |
| 140 | string property_name, base_property_name; |
| 141 | base_property_name = CreateIndexedPropertyName("propulsion/engine", num); |
| 142 | |
| 143 | property_name = base_property_name + "/brake-ctrl-norm"; |
| 144 | PropertyManager->Tie( property_name.c_str(), this, |
| 145 | &FGTransmission::GetBrakeCtrlNorm, &FGTransmission::SetBrakeCtrlNorm); |
| 146 | |
| 147 | property_name = base_property_name + "/clutch-ctrl-norm"; |
| 148 | PropertyManager->Tie( property_name.c_str(), this, |
| 149 | &FGTransmission::GetClutchCtrlNorm, &FGTransmission::SetClutchCtrlNorm); |
| 150 | |
| 151 | property_name = base_property_name + "/free-wheel-transmission"; |
| 152 | PropertyManager->Tie( property_name.c_str(), this, |
| 153 | &FGTransmission::GetFreeWheelTransmission); |
| 154 | |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 159 | // The bitmasked value choices are as follows: |
| 160 | // unset: In this case (the default) JSBSim would only print |
| 161 | // out the normally expected messages, essentially echoing |
| 162 | // the config files as they are read. If the environment |
| 163 | // variable is not set, debug_lvl is set to 1 internally |
| 164 | // 0: This requests JSBSim not to output any messages |
| 165 | // whatsoever. |
| 166 | // 1: This value explicity requests the normal JSBSim |
| 167 | // startup messages |
| 168 | // 2: This value asks for a message to be printed out when |
| 169 | // a class is instantiated |
| 170 | // 4: When this value is set, a message is displayed when a |
| 171 | // FGModel object executes its Run() method |
| 172 | // 8: When this value is set, various runtime state variables |
| 173 | // are printed out periodically |
| 174 | // 16: When set various parameters are sanity checked and |
| 175 | // a message is printed out when they go out of bounds |
| 176 | |
| 177 | void FGTransmission::Debug(int from) |
| 178 | { |
| 179 | if (debug_lvl <= 0) return; |
| 180 | |
| 181 | if (debug_lvl & 1) { // Standard console startup message output |
| 182 | if (from == 0) { // Constructor |
| 183 | } |
| 184 | } |
| 185 | if (debug_lvl & 2 ) { // Instantiation/Destruction notification |
| 186 | if (from == 0) cout << "Instantiated: FGTransmission" << endl; |
| 187 | if (from == 1) cout << "Destroyed: FGTransmission" << endl; |
| 188 | } |
| 189 | if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects |
| 190 | } |
| 191 | if (debug_lvl & 8 ) { // Runtime state variables |
| 192 | } |
| 193 | if (debug_lvl & 16) { // Sanity checking |
| 194 | } |
| 195 | if (debug_lvl & 64) { |
| 196 | if (from == 0) { // Constructor |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | } |