]> git.mxchange.org Git - flightgear.git/blobdiff - src/FDM/YASim/Rotor.cpp
Fix for bug 1304 - crash loading XML route
[flightgear.git] / src / FDM / YASim / Rotor.cpp
index 9031c3e6c99a6c6631916d0254fff8e25e15cf8e..c222358142e18d6095a70a571ad1e4eb5d88192f 100644 (file)
@@ -1,3 +1,8 @@
+
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
 #include <simgear/debug/logstream.hxx>
 
 #include "Math.hpp"
 #include "Ground.hpp"
 #include "Rotor.hpp"
 
-#include STL_IOSTREAM
-#include STL_IOMANIP
+#include <iostream>
+#include <iomanip>
 
-SG_USING_STD(setprecision);
 
-#ifdef TEST_DEBUG
 #include <stdio.h>
-#endif
 #include <string.h>
 #include <iostream>
 #include <sstream>
 
-
+using std::setprecision;
+using std::endl;
 
 namespace yasim {
 
@@ -150,6 +153,7 @@ Rotor::Rotor()
     _max_tilt_yaw=0;
     _max_tilt_pitch=0;
     _max_tilt_roll=0;
+    _downwash_factor=1;
 }
 
 Rotor::~Rotor()
@@ -269,7 +273,7 @@ int Rotor::getValueforFGSet(int j,char *text,float *f)
     if (4>numRotorparts()) return 0; //compile first!
     if (j==0)
     {
-        sprintf(text,"/rotors/%s/cone-deg", _name);
+        snprintf(text, 256, "/rotors/%s/cone-deg", _name);
         *f=(_balance1>-1)?( ((Rotorpart*)getRotorpart(0))->getrealAlpha()
             +((Rotorpart*)getRotorpart(1*(_number_of_parts>>2)))->getrealAlpha()
             +((Rotorpart*)getRotorpart(2*(_number_of_parts>>2)))->getrealAlpha()
@@ -279,7 +283,7 @@ int Rotor::getValueforFGSet(int j,char *text,float *f)
     else
         if (j==1)
         {
-            sprintf(text,"/rotors/%s/roll-deg", _name);
+            snprintf(text, 256, "/rotors/%s/roll-deg", _name);
             _roll = ( ((Rotorpart*)getRotorpart(0))->getrealAlpha()
                 -((Rotorpart*)getRotorpart(2*(_number_of_parts>>2)))->getrealAlpha()
                 )/2*(_ccw?-1:1);
@@ -288,7 +292,7 @@ int Rotor::getValueforFGSet(int j,char *text,float *f)
         else
             if (j==2)
             {
-                sprintf(text,"/rotors/%s/yaw-deg", _name);
+                snprintf(text, 256, "/rotors/%s/yaw-deg", _name);
                 _yaw=( ((Rotorpart*)getRotorpart(1*(_number_of_parts>>2)))->getrealAlpha()
                     -((Rotorpart*)getRotorpart(3*(_number_of_parts>>2)))->getrealAlpha()
                     )/2;
@@ -297,38 +301,38 @@ int Rotor::getValueforFGSet(int j,char *text,float *f)
             else
                 if (j==3)
                 {
-                    sprintf(text,"/rotors/%s/rpm", _name);
+                    snprintf(text, 256, "/rotors/%s/rpm", _name);
                     *f=(_balance1>-1)?_omega/2/pi*60:0;
                 }
                 else
                     if (j==4)
                     {
-                        sprintf(text,"/rotors/%s/tilt/pitch-deg",_name);
+                        snprintf(text, 256, "/rotors/%s/tilt/pitch-deg",_name);
                         *f=_tilt_pitch*180/pi;
                     }
                     else if (j==5)
                     {
-                        sprintf(text,"/rotors/%s/tilt/roll-deg",_name);
+                        snprintf(text, 256, "/rotors/%s/tilt/roll-deg",_name);
                         *f=_tilt_roll*180/pi;
                     }
                     else if (j==6)
                     {
-                        sprintf(text,"/rotors/%s/tilt/yaw-deg",_name);
+                        snprintf(text, 256, "/rotors/%s/tilt/yaw-deg",_name);
                         *f=_tilt_yaw*180/pi;
                     }
                     else if (j==7)
                     {
-                        sprintf(text,"/rotors/%s/balance", _name);
+                        snprintf(text, 256, "/rotors/%s/balance", _name);
                         *f=_balance1;
                     }
                     else if (j==8)
                     {
-                        sprintf(text,"/rotors/%s/stall",_name);
+                        snprintf(text, 256, "/rotors/%s/stall",_name);
                         *f=getOverallStall();
                     }
                     else if (j==9)
                     {
-                        sprintf(text,"/rotors/%s/torque",_name);
+                        snprintf(text, 256, "/rotors/%s/torque",_name);
                         *f=-_torque;;
                     }
                     else
@@ -339,7 +343,7 @@ int Rotor::getValueforFGSet(int j,char *text,float *f)
                             return 0;
                         }
                         int w=j%3;
-                        sprintf(text,"/rotors/%s/blade[%i]/%s",
+                        snprintf(text, 256, "/rotors/%s/blade[%i]/%s",
                             _name,b,
                             w==0?"position-deg":(w==1?"flap-deg":"incidence-deg"));
                         *f=((Rotorpart*)getRotorpart(0))->getPhi()*180/pi
@@ -652,6 +656,11 @@ void Rotor::setRelLenHinge(float value)
     _rel_len_hinge=value;
 }
 
+void Rotor::setDownwashFactor(float value)
+{
+    _downwash_factor=value;
+}
+
 void Rotor::setAlphaoutput(int i, const char *text)
 {
     strncpy(_alphaoutput[i],text,255);
@@ -693,7 +702,7 @@ void Rotor::setGlobalGround(double *global_ground, float* global_vel)
     for(i=0; i<4; i++) _global_ground[i] = global_ground[i];
 }
 
-void Rotor::setParameter(char *parametername, float value)
+void Rotor::setParameter(const char *parametername, float value)
 {
 #define p(a,b) if (strcmp(parametername,#a)==0) _##a = (value * (b)); else
     p(translift_ve,1)
@@ -726,7 +735,7 @@ void Rotor::setParameter(char *parametername, float value)
         p(rotor_correction_factor,1)
         SG_LOG(SG_INPUT, SG_ALERT,
             "internal error in parameter set up for rotor: '" << 
-            parametername <<"'" << endl);
+               parametername <<"'" << std::endl);
 #undef p
 }
 
@@ -785,6 +794,12 @@ void Rotor::setCyclicail(float lval,float rval)
     _cyclicail=-(_mincyclicail+(lval+1)/2*(_maxcyclicail-_mincyclicail));
 }
 
+void Rotor::setRotorBalance(float lval)
+{
+    lval = Math::clamp(lval, -1, 1);
+    _balance2 = lval;
+}
+
 void Rotor::getPosition(float* out)
 {
     int i;
@@ -806,7 +821,7 @@ void Rotor::calcLiftFactor(float* v, float rho, State *s)
     _f_ge=1+_diameter/_ground_effect_altitude*_ground_effect_constant;
 
     // Now calculate translational lift
-    float v_vert = Math::dot3(v,_normal);
+    // float v_vert = Math::dot3(v,_normal);
     float help[3];
     Math::cross3(v,_normal,help);
     float v_horiz = Math::mag3(help);
@@ -817,6 +832,7 @@ void Rotor::calcLiftFactor(float* v, float rho, State *s)
 
     //store the gravity direction
     Glue::geodUp(s->pos, _grav_direction);
+    s->velGlobalToLocal(_grav_direction, _grav_direction);
 }
 
 void Rotor::findGroundEffectAltitude(Ground * ground_cb,State *s)
@@ -1019,7 +1035,7 @@ void Rotor::getDownWash(float *pos, float *v_heli, float *downwash)
     //at dist = rotor radius it is assumed to be 1/e * v1 + (1-1/e)* v2
 
     float v = g * v1 + (1-g) * v2;
-    Math::mul3(-v,_normal_with_yaw_roll,downwash);
+    Math::mul3(-v*_downwash_factor,_normal_with_yaw_roll,downwash);
     //the downwash is calculated in the opposite direction of the normal
 }
 
@@ -1143,7 +1159,7 @@ void Rotor::updateDirectionsAndPositions(float *rot)
 void Rotor::compile()
 {
     // Have we already been compiled?
-    if(_rotorparts.size() != 0) return;
+    if(! _rotorparts.empty()) return;
 
     //rotor is divided into _number_of_parts parts
     //each part is calcualted at _number_of_segments points
@@ -1168,7 +1184,7 @@ void Rotor::compile()
         * _diameter * _rel_blade_center * _rel_blade_center /(0.5*0.5);
     float speed=_rotor_rpm/60*_diameter*_rel_blade_center*pi;
     float lentocenter=_diameter*_rel_blade_center*0.5;
-    float lentoforceattac=_diameter*_rel_len_hinge*0.5;
+    // float lentoforceattac=_diameter*_rel_len_hinge*0.5;
     float zentforce=rotorpartmass*speed*speed/lentocenter;
     float pitchaforce=_force_at_pitch_a/_number_of_parts*.453*9.81;
     // was pounds of force, now N, devided by _number_of_parts
@@ -1183,8 +1199,8 @@ void Rotor::compile()
 
     float relamp=(omega*omega/(2*_delta*Math::sqrt(sqr(omega0*omega0-omega*omega)
         +4*_delta*_delta*omega*omega)))*_cyclic_factor;
-    float relamp_theoretical=(omega*omega/(2*delta_theoretical*Math::sqrt(sqr(omega0*omega0-omega*omega)
-        +4*delta_theoretical*delta_theoretical*omega*omega)))*_cyclic_factor;
+    //float relamp_theoretical=(omega*omega/(2*delta_theoretical*Math::sqrt(sqr(omega0*omega0-omega*omega)
+    //    +4*delta_theoretical*delta_theoretical*omega*omega)))*_cyclic_factor;
     _phi=Math::acos(_rel_len_hinge);
     _phi-=Math::atan(_delta3);
     if (!_no_torque)
@@ -1290,7 +1306,7 @@ void Rotor::compile()
         &(torque[1]),&(lift[1])); //pitch b
     rps[0]->calculateAlpha(v_wind,rho_null,0,0,0,
         &(torque[3]),&(lift[3])); //pitch 0
-    SG_LOG(SG_GENERAL, SG_INFO,
+    SG_LOG(SG_FLIGHT, SG_INFO,
         "Rotor: coefficients for airfoil:" << endl << setprecision(6)
         << " drag0: " << _dragcoef0*_number_of_parts/_number_of_blades/_c2
         << " drag1: " << _dragcoef1*_number_of_parts/_number_of_blades/_c2
@@ -1482,7 +1498,7 @@ void Rotorgear::initRotorIteration(float *lrot,float dt)
 {
     int i;
     float omegarel;
-    if (!_rotors.size()) return;
+    if (_rotors.empty()) return;
     Rotor* r0 = (Rotor*)_rotors.get(0);
     omegarel= r0->getOmegaRelNeu();
     for(i=0; i<_rotors.size(); i++) {
@@ -1498,7 +1514,7 @@ void Rotorgear::calcForces(float* torqueOut)
     // check,<if the engine can handle the torque of the rotors. 
     // If not reduce the torque to the fueselage and change rotational 
     // speed of the rotors instead
-    if (_rotors.size())
+    if (! _rotors.empty())
     {
         float omegarel,omegan;
         Rotor* r0 = (Rotor*)_rotors.get(0);
@@ -1515,7 +1531,7 @@ void Rotorgear::calcForces(float* torqueOut)
             total_torque+=r->getTorque()*omegan;
         }
         float max_torque_of_engine=0;
-        SGPropertyNode * node=fgGetNode("/rotors/gear", true);
+        // SGPropertyNode * node=fgGetNode("/rotors/gear", true);
         if (_engineon)
         {
             max_torque_of_engine=_max_power_engine*_max_rel_torque;
@@ -1526,6 +1542,8 @@ void Rotorgear::calcForces(float* torqueOut)
         }
         total_torque*=-1;
         _ddt_omegarel=0;
+
+#if 0
         float rel_torque_engine=1;
         if (total_torque<=0)
             rel_torque_engine=0;
@@ -1534,10 +1552,11 @@ void Rotorgear::calcForces(float* torqueOut)
                 rel_torque_engine=1/max_torque_of_engine*total_torque;
             else
                 rel_torque_engine=0;
+#endif
 
         //add the rotor brake and the gear fritcion
         float dt=0.1f;
-        if (r0->_rotorparts.size()) dt=((Rotorpart*)r0->_rotorparts.get(0))->getDt();
+        if (! r0->_rotorparts.empty()) dt=((Rotorpart*)r0->_rotorparts.get(0))->getDt();
 
         float rotor_brake_torque;
         rotor_brake_torque=_rotorbrake*_max_power_rotor_brake+_rotorgear_friction;
@@ -1586,7 +1605,7 @@ void Rotorgear::calcForces(float* torqueOut)
             for(j=0; j<_rotors.size(); j++) {
                 Rotor* r = (Rotor*)_rotors.get(j);
                 for(i=0; i<r->_rotorparts.size(); i++) {
-                    float torque_scalar=0;
+                    // float torque_scalar=0;
                     Rotorpart* rp = (Rotorpart*)r->_rotorparts.get(i);
                     float torque[3];
                     rp->getAccelTorque(_ddt_omegarel,torque);
@@ -1606,7 +1625,7 @@ void Rotorgear::addRotor(Rotor* rotor)
 
 void Rotorgear::compile()
 {
-    float wgt = 0;
+    // float wgt = 0;
     for(int j=0; j<_rotors.size(); j++) {
         Rotor* r = (Rotor*)_rotors.get(j);
         r->compile();