]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIShip.cxx
Mathias Froehlich:
[flightgear.git] / src / AIModel / AIShip.cxx
1 // FGAIShip - FGAIBase-derived class creates an AI ship
2 //
3 // Written by David Culp, started October 2003.
4 // - davidculp2@comcast.net
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include <simgear/math/point3d.hxx>
25 #include <math.h>
26
27 #include "AIFlightPlan.hxx"
28 #include "AIShip.hxx"
29
30
31 FGAIShip::FGAIShip(object_type ot) : FGAIBase(ot) {
32 }
33
34 FGAIShip::~FGAIShip() {
35 }
36
37 void FGAIShip::readFromScenario(SGPropertyNode* scFileNode) {
38   if (!scFileNode)
39     return;
40
41   FGAIBase::readFromScenario(scFileNode);
42
43   setRudder(scFileNode->getFloatValue("rudder", 0.0));
44   setName(scFileNode->getStringValue("name", "Titanic"));
45   
46   std::string flightplan = scFileNode->getStringValue("flightplan");
47   if (!flightplan.empty()){
48     FGAIFlightPlan* fp = new FGAIFlightPlan(flightplan);
49     setFlightPlan(fp);
50   }
51 }
52
53 bool FGAIShip::init() {
54    
55    hdg_lock = false;
56    rudder = 0.0;
57    no_roll = false;
58     
59    rudder_constant = 0.5;
60    roll_constant = 0.001;
61    speed_constant = 0.05;
62    hdg_constant = 0.01;
63    
64    return FGAIBase::init();
65 }
66
67 void FGAIShip::bind() {
68     FGAIBase::bind();
69
70     props->tie("surface-positions/rudder-pos-deg",
71                 SGRawValuePointer<float>(&rudder));
72     props->tie("controls/heading-lock",
73                 SGRawValuePointer<bool>(&hdg_lock));
74     props->tie("controls/tgt-speed-kts",
75                 SGRawValuePointer<double>(&tgt_speed));
76     props->tie("controls/tgt-heading-degs",
77                 SGRawValuePointer<double>(&tgt_heading)); 
78     props->tie("controls/constants/rudder",
79                 SGRawValuePointer<double>(&rudder_constant));
80     props->tie("controls/constants/roll",
81                 SGRawValuePointer<double>(&roll_constant));
82     props->tie("controls/constants/rudder",
83                 SGRawValuePointer<double>(&rudder_constant));
84     props->tie("controls/constants/speed",
85                 SGRawValuePointer<double>(&speed_constant)); 
86
87     props->setStringValue("name", name.c_str());
88 }
89
90 void FGAIShip::unbind() {
91     FGAIBase::unbind();
92     props->untie("surface-positions/rudder-pos-deg");
93     props->untie("controls/heading-lock");
94     props->untie("controls/tgt-speed-kts");
95     props->untie("controls/tgt-heading-degs");
96     props->untie("controls/constants/roll");
97     props->untie("controls/constants/rudder");
98     props->untie("controls/constants/speed");           
99
100 }
101
102 void FGAIShip::update(double dt) {
103
104    FGAIBase::update(dt);
105    Run(dt);
106    Transform();
107 }
108
109
110
111 void FGAIShip::Run(double dt) {
112
113    if (fp) ProcessFlightPlan(dt);
114
115    double sp_turn_radius_ft;
116    double rd_turn_radius_ft;
117    double speed_north_deg_sec;
118    double speed_east_deg_sec;
119    double dist_covered_ft;
120    double alpha;
121    double rudder_limit;
122    double raw_roll;   
123
124    // adjust speed
125    double speed_diff = tgt_speed - speed;
126    if (fabs(speed_diff) > 0.1) {
127      if (speed_diff > 0.0) speed += speed_constant * dt;
128      if (speed_diff < 0.0) speed -= speed_constant * dt;
129    } 
130    
131    // convert speed to degrees per second
132    speed_north_deg_sec = cos( hdg / SGD_RADIANS_TO_DEGREES )
133                           * speed * 1.686 / ft_per_deg_lat;
134    speed_east_deg_sec  = sin( hdg / SGD_RADIANS_TO_DEGREES )
135                           * speed * 1.686 / ft_per_deg_lon;
136
137    // set new position
138    pos.setlat( pos.lat() + speed_north_deg_sec * dt);
139    pos.setlon( pos.lon() + speed_east_deg_sec * dt); 
140
141    
142    // adjust heading based on current rudder angle
143    if (rudder <= -0.25 || rudder >= 0.25)  {
144    /*  turn_radius_ft = 0.088362 * speed * speed
145                        / tan( fabs(rudder) / SG_RADIANS_TO_DEGREES );
146      turn_circum_ft = SGD_2PI * turn_radius_ft;
147      dist_covered_ft = speed * 1.686 * dt; 
148      alpha = dist_covered_ft / turn_circum_ft * 360.0;*/
149      
150      if (turn_radius_ft <= 0) turn_radius_ft = 0; // don't allow nonsense values
151      if (rudder > 45) rudder = 45;
152      if (rudder < -45) rudder = -45;
153
154 // adjust turn radius for speed. The equation is very approximate.
155      sp_turn_radius_ft = 10 * pow ((speed - 15),2) + turn_radius_ft;
156 //     cout << " speed turn radius " << sp_turn_radius_ft ; 
157
158 // adjust turn radius for rudder angle. The equation is even more approximate.     
159      float a = 19;
160      float b = -0.2485;
161      float c = 0.543;
162      
163      rd_turn_radius_ft = (a * exp(b * fabs(rudder)) + c) * sp_turn_radius_ft;
164      
165 //     cout <<" rudder turn radius " << rd_turn_radius_ft << endl;
166           
167 // calculate the angle, alpha, subtended by the arc traversed in time dt        
168      alpha = ((speed * 1.686 * dt)/rd_turn_radius_ft) * SG_RADIANS_TO_DEGREES;
169
170    
171 // make sure that alpha is applied in the right direction   
172      hdg += alpha * sign( rudder );
173      if ( hdg > 360.0 ) hdg -= 360.0;
174      if ( hdg < 0.0) hdg += 360.0;
175
176 //adjust roll for rudder angle and speed. Another bit of voodoo    
177      raw_roll =  -0.0166667 * speed * rudder;
178    }
179    else
180    {
181 // rudder angle is 0  
182      raw_roll = 0;
183 //     cout << " roll "<< roll << endl;
184    }
185
186     //low pass filter
187      roll = (raw_roll * roll_constant) + (roll * (1 - roll_constant));
188          
189      /*cout  << " rudder: " << rudder << " raw roll: "<< raw_roll<<" roll: " << roll ;
190      cout  << " hdg: " << hdg << endl ;*/
191
192    // adjust target rudder angle if heading lock engaged
193    if (hdg_lock) {
194      double rudder_sense = 0.0;
195      double diff = fabs(hdg - tgt_heading);
196      if (diff > 180) diff = fabs(diff - 360);
197      double sum = hdg + diff;
198      if (sum > 360.0) sum -= 360.0;
199      if (fabs(sum - tgt_heading) < 1.0) { 
200        rudder_sense = 1.0;
201      } else {
202        rudder_sense = -1.0;
203      } 
204      if (diff < 15){ 
205          tgt_rudder = diff * rudder_sense;
206          }
207          else
208          {
209          tgt_rudder = 45 * rudder_sense;
210      }     
211    }
212
213    // adjust rudder angle
214     double rudder_diff = tgt_rudder - rudder;
215     // set the rudder limit by speed
216     if (speed <= 40 ){
217        rudder_limit = (-0.825 * speed) + 35;
218     }else{
219        rudder_limit = 2;
220    }
221
222     if (fabs(rudder_diff) > 0.1) {
223         if (rudder_diff > 0.0){
224             rudder += rudder_constant * dt;
225             if (rudder > rudder_limit) rudder = rudder_limit;// apply the rudder limit
226         } else if (rudder_diff < 0.0){
227             rudder -= rudder_constant * dt;
228             if (rudder < -rudder_limit) rudder = -rudder_limit;
229         }
230 }
231
232
233
234 }//end function
235
236
237 void FGAIShip::AccelTo(double speed) {
238    tgt_speed = speed;
239 }
240
241 void FGAIShip::PitchTo(double angle) {
242    tgt_pitch = angle;
243 }
244
245
246 void FGAIShip::RollTo(double angle) {
247    tgt_roll = angle;
248 }
249
250
251 void FGAIShip::YawTo(double angle) {
252 }
253
254
255 void FGAIShip::ClimbTo(double altitude) {
256 }
257
258
259 void FGAIShip::TurnTo(double heading) {
260    tgt_heading = heading;
261    hdg_lock = true;
262 }
263
264
265 double FGAIShip::sign(double x) {
266
267   if ( x < 0.0 ) { return -1.0; }
268   else { return 1.0; }
269 }
270
271 void FGAIShip::setFlightPlan(FGAIFlightPlan* f) {
272   fp = f;
273 }
274
275 void FGAIShip::setName(const string& n) {
276   name = n;
277 }
278
279 void FGAIShip::ProcessFlightPlan(double dt) {
280   // not implemented yet
281 }
282
283 void FGAIShip::setRudder(float r) {
284   rudder = r;
285 }
286
287 void FGAIShip::setRoll(double rl) {
288    roll = rl;
289 }