]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIGroundVehicle.cxx
Support helipad names in the --runway startup option
[flightgear.git] / src / AIModel / AIGroundVehicle.cxx
1 // FGAIGroundVehicle - FGAIShip-derived class creates an AI Ground Vehicle
2 // by adding a ground following utility
3 //
4 // Written by Vivian Meazza, started August 2009.
5 // - vivian.meazza at lineone.net
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <simgear/sg_inlines.h>
26
27 #include <Viewer/viewer.hxx>
28 #include <Scenery/scenery.hxx>
29 #include <Airports/dynamics.hxx>
30
31 #include "AIGroundVehicle.hxx"
32
33 using std::string;
34
35 FGAIGroundVehicle::FGAIGroundVehicle() :
36 FGAIShip(otGroundVehicle),
37
38 _pitch(0),
39 _pitch_deg(0),
40 _speed_kt(0),
41 _range_ft(0),
42 _relbrg (0),
43 _parent_speed(0),
44 _parent_x_offset(0),
45 _parent_y_offset(0),
46 _parent_z_offset(0),
47 _dt_count(0),
48 _next_run(0),
49 _break_count(0)
50
51 {
52     invisible = false;
53     _parent = "";
54 }
55
56 FGAIGroundVehicle::~FGAIGroundVehicle() {}
57
58 void FGAIGroundVehicle::readFromScenario(SGPropertyNode* scFileNode) {
59     if (!scFileNode)
60         return;
61
62     FGAIShip::readFromScenario(scFileNode);
63
64     setName(scFileNode->getStringValue("name", "groundvehicle"));
65     setParentName(scFileNode->getStringValue("parent", ""));
66     setNoRoll(scFileNode->getBoolValue("no-roll", true));
67     setContactX1offset(scFileNode->getDoubleValue("contact-x1-offset", 0.0));
68     setContactX2offset(scFileNode->getDoubleValue("contact-x2-offset", 0.0));
69     setXOffset(scFileNode->getDoubleValue("hitch-x-offset", 35.0));
70     setYOffset(scFileNode->getDoubleValue("hitch-y-offset", 0.0));
71     setZOffset(scFileNode->getDoubleValue("hitch-z-offset", 0.0));
72     setPitchoffset(scFileNode->getDoubleValue("pitch-offset", 0.0));
73     setRolloffset(scFileNode->getDoubleValue("roll-offset", 0.0));
74     setYawoffset(scFileNode->getDoubleValue("yaw-offset", 0.0));
75     setPitchCoeff(scFileNode->getDoubleValue("pitch-coefficient", 0.1));
76     setElevCoeff(scFileNode->getDoubleValue("elevation-coefficient", 0.25));
77     setTowAngleGain(scFileNode->getDoubleValue("tow-angle-gain", 1.0));
78     setTowAngleLimit(scFileNode->getDoubleValue("tow-angle-limit-deg", 2.0));
79     setInitialTunnel(scFileNode->getBoolValue("tunnel", false));
80     //we may need these later for towed vehicles
81     //    setSubID(scFileNode->getIntValue("SubID", 0));
82     //    setGroundOffset(scFileNode->getDoubleValue("ground-offset", 0.0));
83     //    setFormate(scFileNode->getBoolValue("formate", true));
84 }
85
86 void FGAIGroundVehicle::bind() {
87     FGAIShip::bind();
88
89     tie("controls/constants/elevation-coeff",
90         SGRawValuePointer<double>(&_elevation_coeff));
91     tie("controls/constants/pitch-coeff",
92         SGRawValuePointer<double>(&_pitch_coeff));
93     tie("position/ht-AGL-ft",
94         SGRawValuePointer<double>(&_ht_agl_ft));
95     tie("hitch/rel-bearing-deg",
96          SGRawValuePointer<double>(&_relbrg));
97     tie("hitch/tow-angle-deg",
98          SGRawValuePointer<double>(&_tow_angle));
99     tie("hitch/range-ft",
100         SGRawValuePointer<double>(&_range_ft));
101     tie("hitch/x-offset-ft",
102         SGRawValuePointer<double>(&_x_offset));
103     tie("hitch/y-offset-ft",
104         SGRawValuePointer<double>(&_y_offset));
105     tie("hitch/z-offset-ft",
106         SGRawValuePointer<double>(&_z_offset));
107     tie("hitch/parent-x-offset-ft",
108         SGRawValuePointer<double>(&_parent_x_offset));
109     tie("hitch/parent-y-offset-ft",
110         SGRawValuePointer<double>(&_parent_y_offset));
111     tie("hitch/parent-z-offset-ft",
112         SGRawValuePointer<double>(&_parent_z_offset));
113     tie("controls/constants/tow-angle/gain",
114         SGRawValuePointer<double>(&_tow_angle_gain));
115     tie("controls/constants/tow-angle/limit-deg",
116         SGRawValuePointer<double>(&_tow_angle_limit));
117     tie("controls/contact-x1-offset-ft",
118         SGRawValuePointer<double>(&_contact_x1_offset));
119     tie("controls/contact-x2-offset-ft",
120         SGRawValuePointer<double>(&_contact_x2_offset));
121 }
122
123 bool FGAIGroundVehicle::init(bool search_in_AI_path) {
124     if (!FGAIShip::init(search_in_AI_path))
125         return false;
126     reinit();
127     return true;
128 }
129
130 void FGAIGroundVehicle::reinit() {
131     invisible = false;
132     _limit = 200;
133     no_roll = true;
134
135     props->setStringValue("controls/parent-name", _parent.c_str());
136
137     if (setParentNode()){
138         _parent_x_offset = _selected_ac->getDoubleValue("hitch/x-offset-ft");
139         _parent_y_offset = _selected_ac->getDoubleValue("hitch/y-offset-ft");
140         _parent_z_offset = _selected_ac->getDoubleValue("hitch/z-offset-ft");
141         _hitch_x_offset_m = _selected_ac->getDoubleValue("hitch/x-offset-ft")
142             * SG_FEET_TO_METER;
143         _hitch_y_offset_m = _selected_ac->getDoubleValue("hitch/y-offset-ft")
144             * SG_FEET_TO_METER;
145         _hitch_z_offset_m = _selected_ac->getDoubleValue("hitch/z-offset-ft")
146             * SG_FEET_TO_METER;
147         setParent();
148     }
149
150     FGAIShip::reinit();
151 }
152
153 void FGAIGroundVehicle::update(double dt) {
154     //    SG_LOG(SG_AI, SG_ALERT, "updating GroundVehicle: " << _name );
155     FGAIShip::update(dt);
156
157     RunGroundVehicle(dt);
158 }
159
160 void FGAIGroundVehicle::setNoRoll(bool nr) {
161     no_roll = nr;
162 }
163
164 void FGAIGroundVehicle::setContactX1offset(double x1) {
165     _contact_x1_offset = x1;
166 }
167
168 void FGAIGroundVehicle::setContactX2offset(double x2) {
169     _contact_x2_offset = x2;
170 }
171
172 void FGAIGroundVehicle::setXOffset(double x) {
173     _x_offset = x;
174 }
175
176 void FGAIGroundVehicle::setYOffset(double y) {
177     _y_offset = y;
178 }
179
180 void FGAIGroundVehicle::setZOffset(double z) {
181     _z_offset = z;
182 }
183
184 void FGAIGroundVehicle::setPitchCoeff(double pc) {
185     _pitch_coeff = pc;
186 }
187
188 void FGAIGroundVehicle::setElevCoeff(double ec) {
189     _elevation_coeff = ec;
190 }
191
192 void FGAIGroundVehicle::setTowAngleGain(double g) {
193     _tow_angle_gain = g;
194 }
195
196 void FGAIGroundVehicle::setTowAngleLimit(double l) {
197     _tow_angle_limit = l;
198 }
199
200 void FGAIGroundVehicle::setElevation(double h, double dt, double coeff){
201     double c = dt / (coeff + dt);
202     _elevation_ft = (h * c) + (_elevation_ft * (1 - c));
203 }
204
205 void FGAIGroundVehicle::setPitch(double p, double dt, double coeff){
206     double c = dt / (coeff + dt);
207     _pitch_deg = (p * c) + (_pitch_deg * (1 - c));
208 }
209
210 void FGAIGroundVehicle::setTowAngle(double ta, double dt, double coeff){
211     ta *= _tow_angle_gain;
212     double factor = -0.0045 * speed + 1;
213     double limit = _tow_angle_limit * factor;
214 //      cout << "speed "<< speed << " _factor " << _factor<<" " <<_tow_angle_limit<< endl; 
215      _tow_angle = pow(ta,2) * sign(ta) * factor;
216     SG_CLAMP_RANGE(_tow_angle, -limit, limit);
217 }
218
219 bool FGAIGroundVehicle::getPitch() {
220
221     if (!_tunnel){
222         double vel = props->getDoubleValue("velocities/true-airspeed-kt", 0);
223         double contact_offset_x1_m = _contact_x1_offset * SG_FEET_TO_METER;
224         double contact_offset_x2_m = _contact_x2_offset * SG_FEET_TO_METER;
225         double _z_offset_m = _parent_z_offset * SG_FEET_TO_METER;
226
227         SGVec3d front(-contact_offset_x1_m, 0, 0);
228         SGVec3d rear(-contact_offset_x2_m, 0, 0);
229         SGVec3d Front = getCartPosAt(front);
230         SGVec3d Rear = getCartPosAt(rear);
231
232         SGGeod geodFront = SGGeod::fromCart(Front);
233         SGGeod geodRear = SGGeod::fromCart(Rear);
234
235         double front_elev_m = 0;
236         double rear_elev_m = 0;
237         double elev_front = 0;
238         double elev_rear = 0;
239         //double max_alt = 10000;
240
241         if (globals->get_scenery()->get_elevation_m(SGGeod::fromGeodM(geodFront, 3000),
242             elev_front, NULL, 0)){
243                 front_elev_m = elev_front + _z_offset_m;
244         } else
245             return false;
246
247         if (globals->get_scenery()->get_elevation_m(SGGeod::fromGeodM(geodRear, 3000),
248             elev_rear, NULL, 0)){
249                 rear_elev_m = elev_rear;
250         } else
251             return false;
252
253         if (vel >= 0){
254             double diff = front_elev_m - rear_elev_m;
255             _pitch = atan2 (diff,
256                 fabs(contact_offset_x1_m) + fabs(contact_offset_x2_m)) * SG_RADIANS_TO_DEGREES;
257             _elevation = (rear_elev_m + diff/2) * SG_METER_TO_FEET;
258         } else {
259             double diff = rear_elev_m - front_elev_m;
260             _pitch = atan2 (diff,
261                 fabs(contact_offset_x1_m) + fabs(contact_offset_x2_m)) * SG_RADIANS_TO_DEGREES;
262             _elevation = (front_elev_m + diff/2) * SG_METER_TO_FEET;
263             _pitch = -_pitch;
264         }
265
266     } else {
267
268         if (prev->getAltitude() == 0 || curr->getAltitude() == 0) return false;
269
270         static double distance;
271         static double d_alt;
272         static double curr_alt;
273         static double prev_alt;
274
275         if (_new_waypoint){
276             //cout << "new waypoint, calculating pitch " << endl;
277             curr_alt = curr->getAltitude();
278             prev_alt = prev->getAltitude();
279             //cout << "prev_alt" <<prev_alt << endl;
280             d_alt = (curr_alt - prev_alt) * SG_METER_TO_FEET;
281             //_elevation = prev->altitude;
282             distance = SGGeodesy::distanceM(SGGeod::fromDeg(prev->getLongitude(), prev->getLatitude()),
283             SGGeod::fromDeg(curr->getLongitude(), curr->getLatitude()));
284             _pitch = atan2(d_alt, distance * SG_METER_TO_FEET) * SG_RADIANS_TO_DEGREES;
285             //cout << "new waypoint, calculating pitch " <<  _pitch << 
286             //    " " << _pitch_offset << " " << _elevation <<endl;
287         }
288
289         double distance_to_go = SGGeodesy::distanceM(SGGeod::fromDeg(pos.getLongitudeDeg(), pos.getLatitudeDeg()),
290             SGGeod::fromDeg(curr->getLongitude(), curr->getLatitude()));
291
292         /*cout << "tunnel " << _tunnel
293              << " distance prev & curr " << prev->name << " " << curr->name << " " << distance * SG_METER_TO_FEET
294              << " distance to go " << distance_to_go * SG_METER_TO_FEET
295              << " d_alt ft " << d_alt
296              << endl;*/
297
298         if (distance_to_go > distance)
299             _elevation = prev_alt;
300         else
301             _elevation = curr_alt - (tan(_pitch * SG_DEGREES_TO_RADIANS) * distance_to_go * SG_METER_TO_FEET);
302
303     }
304
305     return true;
306 }
307
308 void FGAIGroundVehicle::setParent(){
309
310     double lat = _selected_ac->getDoubleValue("position/latitude-deg");
311     double lon = _selected_ac->getDoubleValue("position/longitude-deg");
312     double elevation = _selected_ac->getDoubleValue("position/altitude-ft");
313
314     _selectedpos.setLatitudeDeg(lat);
315     _selectedpos.setLongitudeDeg(lon);
316     _selectedpos.setElevationFt(elevation);
317
318     _parent_speed = _selected_ac->getDoubleValue("velocities/true-airspeed-kt");
319
320     SGVec3d rear_hitch(-_hitch_x_offset_m, _hitch_y_offset_m, 0);
321     SGVec3d RearHitch = getCartHitchPosAt(rear_hitch);
322
323     SGGeod rearpos = SGGeod::fromCart(RearHitch);
324
325     double user_lat = rearpos.getLatitudeDeg();
326     double user_lon = rearpos.getLongitudeDeg();
327
328     double range, bearing;
329
330     calcRangeBearing(pos.getLatitudeDeg(), pos.getLongitudeDeg(),
331         user_lat, user_lon, range, bearing);
332     _range_ft = range * 6076.11549;
333     _relbrg = calcRelBearingDeg(bearing, hdg);
334 }
335
336 void FGAIGroundVehicle::calcRangeBearing(double lat, double lon, double lat2, double lon2,
337                             double &range, double &bearing) const
338 {
339     // calculate the bearing and range of the second pos from the first
340     double az2, distance;
341     geo_inverse_wgs_84(lat, lon, lat2, lon2, &bearing, &az2, &distance);
342     range = distance * SG_METER_TO_NM;
343 }
344
345
346 SGVec3d FGAIGroundVehicle::getCartHitchPosAt(const SGVec3d& _off) const {
347     double hdg = _selected_ac->getDoubleValue("orientation/true-heading-deg");
348     double pitch = _selected_ac->getDoubleValue("orientation/pitch-deg");
349     double roll = _selected_ac->getDoubleValue("orientation/roll-deg");
350
351     // Transform that one to the horizontal local coordinate system.
352     SGQuatd hlTrans = SGQuatd::fromLonLat(_selectedpos);
353
354     // and postrotate the orientation of the AIModel wrt the horizontal
355     // local frame
356     hlTrans *= SGQuatd::fromYawPitchRollDeg(hdg, pitch, roll);
357
358     // The offset converted to the usual body fixed coordinate system
359     // rotated to the earth fiexed coordinates axis
360     SGVec3d off = hlTrans.backTransform(_off);
361
362     // Add the position offset of the AIModel to gain the earth centered position
363     SGVec3d cartPos = SGVec3d::fromGeod(_selectedpos);
364
365     return cartPos + off;
366 }
367
368 void FGAIGroundVehicle::AdvanceFP(){
369
370     double count = 0;
371     string parent_next_name =_selected_ac->getStringValue("waypoint/name-next");
372
373     while(fp->getNextWaypoint() != 0 && fp->getNextWaypoint()->getName() != "END" && count < 5){
374         SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name
375             <<" advancing waypoint to: " << parent_next_name);
376
377         if (fp->getNextWaypoint()->getName() == parent_next_name){
378             SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name
379                 << " not setting waypoint already at: " << fp->getNextWaypoint()->getName());
380             return;
381         }
382
383         prev = curr;
384         fp->IncrementWaypoint(false);
385         curr = fp->getCurrentWaypoint();
386         next = fp->getNextWaypoint();
387
388         if (fp->getNextWaypoint()->getName() == parent_next_name){
389             SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name
390             << " waypoint set to: " << fp->getNextWaypoint()->getName());
391             return;
392         }
393
394         count++;
395
396     }// end while loop
397
398     while(fp->getPreviousWaypoint() != 0 && fp->getPreviousWaypoint()->getName() != "END"
399         && count > -10){
400             SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name
401             << " retreating waypoint to: " << parent_next_name
402             << " at: " << fp->getNextWaypoint()->getName());
403
404         if (fp->getNextWaypoint()->getName() == parent_next_name){
405             SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name
406                 << " not setting waypoint already at:" << fp->getNextWaypoint()->getName() );
407             return;
408         }
409
410         prev = curr;
411         fp->DecrementWaypoint(false);
412         curr = fp->getCurrentWaypoint();
413         next = fp->getNextWaypoint();
414
415         if (fp->getNextWaypoint()->getName() == parent_next_name){
416             SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name
417             << " waypoint set to: " << fp->getNextWaypoint()->getName());
418             return;
419         }
420
421         count--;
422
423     }// end while loop
424 }
425
426 void FGAIGroundVehicle::setTowSpeed(){
427
428     //double diff = _range_ft - _x_offset;
429     double  x = 0;
430
431     if (_range_ft > _x_offset * 3) x = 50;
432
433     if (_relbrg < -90 || _relbrg > 90){
434         setSpeed(_parent_speed - 5 - x);
435         //cout << _name << " case 1r _relbrg spd - 5 " << _relbrg << " " << diff << endl;
436     }else if (_range_ft > _x_offset + 0.25 && _relbrg >= -90 && _relbrg <= 90){
437         setSpeed(_parent_speed + 1 + x);
438         //cout << _name << " case 2r _relbrg spd + 1 " << _relbrg << " "
439         //    << diff << " range " << _range_ft << endl;
440     } else if (_range_ft < _x_offset - 0.25 && _relbrg >= -90 && _relbrg <= 90){
441         setSpeed(_parent_speed - 1 - x);
442         //cout << _name << " case 3r _relbrg spd - 2 " << _relbrg << " "
443         //    << diff << " " << _range_ft << endl;
444     } else {
445         setSpeed(_parent_speed);
446         //cout << _name << " else r _relbrg " << _relbrg << " " << diff << endl;
447     }
448
449 }
450
451 void FGAIGroundVehicle::RunGroundVehicle(double dt){
452
453     _dt_count += dt;
454
455     ///////////////////////////////////////////////////////////////////////////
456     // Check execution time (currently once every 0.05 sec or 20 fps)
457     // Add a bit of randomization to prevent the execution of all flight plans
458     // in synchrony, which can add significant periodic framerate flutter.
459     // Randomization removed to get better appearance
460     ///////////////////////////////////////////////////////////////////////////
461
462     //cout << "_start_sec " << _start_sec << " time_sec " << time_sec << endl;
463     if (_dt_count < _next_run)
464         return;
465
466     _next_run = 0.05 /*+ (0.015 * sg_random())*/;
467
468     if (getPitch()){
469         setElevation(_elevation, _dt_count, _elevation_coeff);
470         ClimbTo(_elevation_ft);
471         setPitch(_pitch, _dt_count, _pitch_coeff);
472         PitchTo(_pitch_deg);
473     }
474
475     if(_parent == ""){
476         AccelTo(prev->getSpeed());
477         _dt_count = 0;
478         return;
479     }
480
481     setParent();
482
483     string parent_next_name = _selected_ac->getStringValue("waypoint/name-next");
484     bool parent_waiting = _selected_ac->getBoolValue("waypoint/waiting");
485     //bool parent_restart = _selected_ac->getBoolValue("controls/restart"); 
486
487     if (parent_next_name == "END" && fp->getNextWaypoint()->getName() != "END" ){
488         SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name
489             << " setting END: getting new waypoints ");
490         AdvanceFP();
491         setWPNames();
492         setTunnel(_initial_tunnel);
493         if(_restart) _missed_count = 200;
494         /*} else if (parent_next_name == "WAIT" && fp->getNextWaypoint()->name != "WAIT" ){*/
495     } else if (parent_waiting && !_waiting){
496         SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name
497             << " setting WAIT/WAITUNTIL: getting new waypoints ");
498         AdvanceFP();
499         setWPNames();
500         _waiting = true;
501     } else if (parent_next_name != "WAIT" && fp->getNextWaypoint()->getName() == "WAIT"){
502         SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name
503             << " wait done: getting new waypoints ");
504         _waiting = false;
505         _wait_count = 0;
506         fp->IncrementWaypoint(false);
507         next = fp->getNextWaypoint();
508
509         if (next->getName() == "WAITUNTIL" || next->getName() == "WAIT"
510             || next->getName() == "END"){
511         } else {
512             prev = curr;
513             fp->IncrementWaypoint(false);
514             curr = fp->getCurrentWaypoint();
515             next = fp->getNextWaypoint();
516         }
517
518         setWPNames();
519     } else if (_range_ft > (_x_offset +_parent_x_offset)* 4
520         ){
521         SG_LOG(SG_AI, SG_ALERT, "AIGroundVeh1cle: " << _name
522             << " rescue: reforming train " << _range_ft 
523             );
524
525         setTowAngle(0, dt, 1);
526         setSpeed(_parent_speed + (10 * sign(_parent_speed)));
527
528     } else if (_parent_speed > 1){
529
530         setTowSpeed();
531         setTowAngle(_relbrg, dt, 1);
532
533     } else if (_parent_speed < -1){
534
535         setTowSpeed();
536
537         if (_relbrg < 0)
538             setTowAngle(-(180 - (360 + _relbrg)), dt, 1);
539         else
540             setTowAngle(-(180 - _relbrg), dt, 1);
541
542     } else
543         setSpeed(_parent_speed);
544
545 //    FGAIShip::update(_dt_count);
546     _dt_count = 0;
547
548 }
549
550 // end AIGroundvehicle