1 // FGAIGroundVehicle - FGAIShip-derived class creates an AI Ground Vehicle
2 // by adding a ground following utility
4 // Written by Vivian Meazza, started August 2009.
5 // - vivian.meazza at lineone.net
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.
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.
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.
25 #include <simgear/sg_inlines.h>
27 #include <Viewer/view.hxx>
28 #include <Scenery/scenery.hxx>
29 #include <Airports/dynamics.hxx>
30 #include <Main/globals.hxx>
32 #include "AIGroundVehicle.hxx"
36 FGAIGroundVehicle::FGAIGroundVehicle() :
37 FGAIShip(otGroundVehicle),
57 FGAIGroundVehicle::~FGAIGroundVehicle() {}
59 void FGAIGroundVehicle::readFromScenario(SGPropertyNode* scFileNode) {
63 FGAIShip::readFromScenario(scFileNode);
65 setName(scFileNode->getStringValue("name", "groundvehicle"));
66 setParentName(scFileNode->getStringValue("parent", ""));
67 setNoRoll(scFileNode->getBoolValue("no-roll", true));
68 setContactX1offset(scFileNode->getDoubleValue("contact-x1-offset", 0.0));
69 setContactX2offset(scFileNode->getDoubleValue("contact-x2-offset", 0.0));
70 setXOffset(scFileNode->getDoubleValue("hitch-x-offset", 35.0));
71 setYOffset(scFileNode->getDoubleValue("hitch-y-offset", 0.0));
72 setZOffset(scFileNode->getDoubleValue("hitch-z-offset", 0.0));
73 setPitchoffset(scFileNode->getDoubleValue("pitch-offset", 0.0));
74 setRolloffset(scFileNode->getDoubleValue("roll-offset", 0.0));
75 setYawoffset(scFileNode->getDoubleValue("yaw-offset", 0.0));
76 setPitchCoeff(scFileNode->getDoubleValue("pitch-coefficient", 0.1));
77 setElevCoeff(scFileNode->getDoubleValue("elevation-coefficient", 0.25));
78 setTowAngleGain(scFileNode->getDoubleValue("tow-angle-gain", 1.0));
79 setTowAngleLimit(scFileNode->getDoubleValue("tow-angle-limit-deg", 2.0));
80 setInitialTunnel(scFileNode->getBoolValue("tunnel", false));
81 //we may need these later for towed vehicles
82 // setSubID(scFileNode->getIntValue("SubID", 0));
83 // setGroundOffset(scFileNode->getDoubleValue("ground-offset", 0.0));
84 // setFormate(scFileNode->getBoolValue("formate", true));
87 void FGAIGroundVehicle::bind() {
90 tie("controls/constants/elevation-coeff",
91 SGRawValuePointer<double>(&_elevation_coeff));
92 tie("controls/constants/pitch-coeff",
93 SGRawValuePointer<double>(&_pitch_coeff));
94 tie("position/ht-AGL-ft",
95 SGRawValuePointer<double>(&_ht_agl_ft));
96 tie("hitch/rel-bearing-deg",
97 SGRawValuePointer<double>(&_relbrg));
98 tie("hitch/tow-angle-deg",
99 SGRawValuePointer<double>(&_tow_angle));
100 tie("hitch/range-ft",
101 SGRawValuePointer<double>(&_range_ft));
102 tie("hitch/x-offset-ft",
103 SGRawValuePointer<double>(&_x_offset));
104 tie("hitch/y-offset-ft",
105 SGRawValuePointer<double>(&_y_offset));
106 tie("hitch/z-offset-ft",
107 SGRawValuePointer<double>(&_z_offset));
108 tie("hitch/parent-x-offset-ft",
109 SGRawValuePointer<double>(&_parent_x_offset));
110 tie("hitch/parent-y-offset-ft",
111 SGRawValuePointer<double>(&_parent_y_offset));
112 tie("hitch/parent-z-offset-ft",
113 SGRawValuePointer<double>(&_parent_z_offset));
114 tie("controls/constants/tow-angle/gain",
115 SGRawValuePointer<double>(&_tow_angle_gain));
116 tie("controls/constants/tow-angle/limit-deg",
117 SGRawValuePointer<double>(&_tow_angle_limit));
118 tie("controls/contact-x1-offset-ft",
119 SGRawValuePointer<double>(&_contact_x1_offset));
120 tie("controls/contact-x2-offset-ft",
121 SGRawValuePointer<double>(&_contact_x2_offset));
124 bool FGAIGroundVehicle::init(bool search_in_AI_path) {
125 if (!FGAIShip::init(search_in_AI_path))
131 void FGAIGroundVehicle::reinit() {
136 props->setStringValue("controls/parent-name", _parent.c_str());
138 if (setParentNode()){
139 _parent_x_offset = _selected_ac->getDoubleValue("hitch/x-offset-ft");
140 _parent_y_offset = _selected_ac->getDoubleValue("hitch/y-offset-ft");
141 _parent_z_offset = _selected_ac->getDoubleValue("hitch/z-offset-ft");
142 _hitch_x_offset_m = _selected_ac->getDoubleValue("hitch/x-offset-ft")
144 _hitch_y_offset_m = _selected_ac->getDoubleValue("hitch/y-offset-ft")
146 _hitch_z_offset_m = _selected_ac->getDoubleValue("hitch/z-offset-ft")
154 void FGAIGroundVehicle::update(double dt) {
155 // SG_LOG(SG_AI, SG_ALERT, "updating GroundVehicle: " << _name );
156 FGAIShip::update(dt);
158 RunGroundVehicle(dt);
161 void FGAIGroundVehicle::setNoRoll(bool nr) {
165 void FGAIGroundVehicle::setContactX1offset(double x1) {
166 _contact_x1_offset = x1;
169 void FGAIGroundVehicle::setContactX2offset(double x2) {
170 _contact_x2_offset = x2;
173 void FGAIGroundVehicle::setXOffset(double x) {
177 void FGAIGroundVehicle::setYOffset(double y) {
181 void FGAIGroundVehicle::setZOffset(double z) {
185 void FGAIGroundVehicle::setPitchCoeff(double pc) {
189 void FGAIGroundVehicle::setElevCoeff(double ec) {
190 _elevation_coeff = ec;
193 void FGAIGroundVehicle::setTowAngleGain(double g) {
197 void FGAIGroundVehicle::setTowAngleLimit(double l) {
198 _tow_angle_limit = l;
201 void FGAIGroundVehicle::setElevation(double h, double dt, double coeff){
202 double c = dt / (coeff + dt);
203 _elevation_ft = (h * c) + (_elevation_ft * (1 - c));
206 void FGAIGroundVehicle::setPitch(double p, double dt, double coeff){
207 double c = dt / (coeff + dt);
208 _pitch_deg = (p * c) + (_pitch_deg * (1 - c));
211 void FGAIGroundVehicle::setTowAngle(double ta, double dt, double coeff){
212 ta *= _tow_angle_gain;
213 double factor = -0.0045 * speed + 1;
214 double limit = _tow_angle_limit * factor;
215 // cout << "speed "<< speed << " _factor " << _factor<<" " <<_tow_angle_limit<< endl;
216 _tow_angle = pow(ta,2) * sign(ta) * factor;
217 SG_CLAMP_RANGE(_tow_angle, -limit, limit);
220 bool FGAIGroundVehicle::getPitch() {
223 double vel = props->getDoubleValue("velocities/true-airspeed-kt", 0);
224 double contact_offset_x1_m = _contact_x1_offset * SG_FEET_TO_METER;
225 double contact_offset_x2_m = _contact_x2_offset * SG_FEET_TO_METER;
226 double _z_offset_m = _parent_z_offset * SG_FEET_TO_METER;
228 SGVec3d front(-contact_offset_x1_m, 0, 0);
229 SGVec3d rear(-contact_offset_x2_m, 0, 0);
230 SGVec3d Front = getCartPosAt(front);
231 SGVec3d Rear = getCartPosAt(rear);
233 SGGeod geodFront = SGGeod::fromCart(Front);
234 SGGeod geodRear = SGGeod::fromCart(Rear);
236 double front_elev_m = 0;
237 double rear_elev_m = 0;
238 double elev_front = 0;
239 double elev_rear = 0;
240 //double max_alt = 10000;
242 if (globals->get_scenery()->get_elevation_m(SGGeod::fromGeodM(geodFront, 3000),
243 elev_front, NULL, 0)){
244 front_elev_m = elev_front + _z_offset_m;
248 if (globals->get_scenery()->get_elevation_m(SGGeod::fromGeodM(geodRear, 3000),
249 elev_rear, NULL, 0)){
250 rear_elev_m = elev_rear;
255 double diff = front_elev_m - rear_elev_m;
256 _pitch = atan2 (diff,
257 fabs(contact_offset_x1_m) + fabs(contact_offset_x2_m)) * SG_RADIANS_TO_DEGREES;
258 _elevation = (rear_elev_m + diff/2) * SG_METER_TO_FEET;
260 double diff = rear_elev_m - front_elev_m;
261 _pitch = atan2 (diff,
262 fabs(contact_offset_x1_m) + fabs(contact_offset_x2_m)) * SG_RADIANS_TO_DEGREES;
263 _elevation = (front_elev_m + diff/2) * SG_METER_TO_FEET;
269 if (prev->getAltitude() == 0 || curr->getAltitude() == 0) return false;
271 static double distance;
273 static double curr_alt;
274 static double prev_alt;
277 //cout << "new waypoint, calculating pitch " << endl;
278 curr_alt = curr->getAltitude();
279 prev_alt = prev->getAltitude();
280 //cout << "prev_alt" <<prev_alt << endl;
281 d_alt = (curr_alt - prev_alt) * SG_METER_TO_FEET;
282 //_elevation = prev->altitude;
283 distance = SGGeodesy::distanceM(SGGeod::fromDeg(prev->getLongitude(), prev->getLatitude()),
284 SGGeod::fromDeg(curr->getLongitude(), curr->getLatitude()));
285 _pitch = atan2(d_alt, distance * SG_METER_TO_FEET) * SG_RADIANS_TO_DEGREES;
286 //cout << "new waypoint, calculating pitch " << _pitch <<
287 // " " << _pitch_offset << " " << _elevation <<endl;
290 double distance_to_go = SGGeodesy::distanceM(SGGeod::fromDeg(pos.getLongitudeDeg(), pos.getLatitudeDeg()),
291 SGGeod::fromDeg(curr->getLongitude(), curr->getLatitude()));
293 /*cout << "tunnel " << _tunnel
294 << " distance prev & curr " << prev->name << " " << curr->name << " " << distance * SG_METER_TO_FEET
295 << " distance to go " << distance_to_go * SG_METER_TO_FEET
296 << " d_alt ft " << d_alt
299 if (distance_to_go > distance)
300 _elevation = prev_alt;
302 _elevation = curr_alt - (tan(_pitch * SG_DEGREES_TO_RADIANS) * distance_to_go * SG_METER_TO_FEET);
309 void FGAIGroundVehicle::setParent(){
311 double lat = _selected_ac->getDoubleValue("position/latitude-deg");
312 double lon = _selected_ac->getDoubleValue("position/longitude-deg");
313 double elevation = _selected_ac->getDoubleValue("position/altitude-ft");
315 _selectedpos.setLatitudeDeg(lat);
316 _selectedpos.setLongitudeDeg(lon);
317 _selectedpos.setElevationFt(elevation);
319 _parent_speed = _selected_ac->getDoubleValue("velocities/true-airspeed-kt");
321 SGVec3d rear_hitch(-_hitch_x_offset_m, _hitch_y_offset_m, 0);
322 SGVec3d RearHitch = getCartHitchPosAt(rear_hitch);
324 SGGeod rearpos = SGGeod::fromCart(RearHitch);
326 double user_lat = rearpos.getLatitudeDeg();
327 double user_lon = rearpos.getLongitudeDeg();
329 double range, bearing;
331 calcRangeBearing(pos.getLatitudeDeg(), pos.getLongitudeDeg(),
332 user_lat, user_lon, range, bearing);
333 _range_ft = range * 6076.11549;
334 _relbrg = calcRelBearingDeg(bearing, hdg);
337 void FGAIGroundVehicle::calcRangeBearing(double lat, double lon, double lat2, double lon2,
338 double &range, double &bearing) const
340 // calculate the bearing and range of the second pos from the first
341 double az2, distance;
342 geo_inverse_wgs_84(lat, lon, lat2, lon2, &bearing, &az2, &distance);
343 range = distance * SG_METER_TO_NM;
347 SGVec3d FGAIGroundVehicle::getCartHitchPosAt(const SGVec3d& _off) const {
348 double hdg = _selected_ac->getDoubleValue("orientation/true-heading-deg");
349 double pitch = _selected_ac->getDoubleValue("orientation/pitch-deg");
350 double roll = _selected_ac->getDoubleValue("orientation/roll-deg");
352 // Transform that one to the horizontal local coordinate system.
353 SGQuatd hlTrans = SGQuatd::fromLonLat(_selectedpos);
355 // and postrotate the orientation of the AIModel wrt the horizontal
357 hlTrans *= SGQuatd::fromYawPitchRollDeg(hdg, pitch, roll);
359 // The offset converted to the usual body fixed coordinate system
360 // rotated to the earth fiexed coordinates axis
361 SGVec3d off = hlTrans.backTransform(_off);
363 // Add the position offset of the AIModel to gain the earth centered position
364 SGVec3d cartPos = SGVec3d::fromGeod(_selectedpos);
366 return cartPos + off;
369 void FGAIGroundVehicle::AdvanceFP(){
372 string parent_next_name =_selected_ac->getStringValue("waypoint/name-next");
374 while(fp->getNextWaypoint() != 0 && fp->getNextWaypoint()->getName() != "END" && count < 5){
375 SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name
376 <<" advancing waypoint to: " << parent_next_name);
378 if (fp->getNextWaypoint()->getName() == parent_next_name){
379 SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name
380 << " not setting waypoint already at: " << fp->getNextWaypoint()->getName());
385 fp->IncrementWaypoint(false);
386 curr = fp->getCurrentWaypoint();
387 next = fp->getNextWaypoint();
389 if (fp->getNextWaypoint()->getName() == parent_next_name){
390 SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name
391 << " waypoint set to: " << fp->getNextWaypoint()->getName());
399 while(fp->getPreviousWaypoint() != 0 && fp->getPreviousWaypoint()->getName() != "END"
401 SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name
402 << " retreating waypoint to: " << parent_next_name
403 << " at: " << fp->getNextWaypoint()->getName());
405 if (fp->getNextWaypoint()->getName() == parent_next_name){
406 SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name
407 << " not setting waypoint already at:" << fp->getNextWaypoint()->getName() );
412 fp->DecrementWaypoint(false);
413 curr = fp->getCurrentWaypoint();
414 next = fp->getNextWaypoint();
416 if (fp->getNextWaypoint()->getName() == parent_next_name){
417 SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name
418 << " waypoint set to: " << fp->getNextWaypoint()->getName());
427 void FGAIGroundVehicle::setTowSpeed(){
429 //double diff = _range_ft - _x_offset;
432 if (_range_ft > _x_offset * 3) x = 50;
434 if (_relbrg < -90 || _relbrg > 90){
435 setSpeed(_parent_speed - 5 - x);
436 //cout << _name << " case 1r _relbrg spd - 5 " << _relbrg << " " << diff << endl;
437 }else if (_range_ft > _x_offset + 0.25 && _relbrg >= -90 && _relbrg <= 90){
438 setSpeed(_parent_speed + 1 + x);
439 //cout << _name << " case 2r _relbrg spd + 1 " << _relbrg << " "
440 // << diff << " range " << _range_ft << endl;
441 } else if (_range_ft < _x_offset - 0.25 && _relbrg >= -90 && _relbrg <= 90){
442 setSpeed(_parent_speed - 1 - x);
443 //cout << _name << " case 3r _relbrg spd - 2 " << _relbrg << " "
444 // << diff << " " << _range_ft << endl;
446 setSpeed(_parent_speed);
447 //cout << _name << " else r _relbrg " << _relbrg << " " << diff << endl;
452 void FGAIGroundVehicle::RunGroundVehicle(double dt){
456 ///////////////////////////////////////////////////////////////////////////
457 // Check execution time (currently once every 0.05 sec or 20 fps)
458 // Add a bit of randomization to prevent the execution of all flight plans
459 // in synchrony, which can add significant periodic framerate flutter.
460 // Randomization removed to get better appearance
461 ///////////////////////////////////////////////////////////////////////////
463 //cout << "_start_sec " << _start_sec << " time_sec " << time_sec << endl;
464 if (_dt_count < _next_run)
467 _next_run = 0.05 /*+ (0.015 * sg_random())*/;
470 setElevation(_elevation, _dt_count, _elevation_coeff);
471 ClimbTo(_elevation_ft);
472 setPitch(_pitch, _dt_count, _pitch_coeff);
477 AccelTo(prev->getSpeed());
484 string parent_next_name = _selected_ac->getStringValue("waypoint/name-next");
485 bool parent_waiting = _selected_ac->getBoolValue("waypoint/waiting");
486 //bool parent_restart = _selected_ac->getBoolValue("controls/restart");
488 if (parent_next_name == "END" && fp->getNextWaypoint()->getName() != "END" ){
489 SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name
490 << " setting END: getting new waypoints ");
493 setTunnel(_initial_tunnel);
494 if(_restart) _missed_count = 200;
495 /*} else if (parent_next_name == "WAIT" && fp->getNextWaypoint()->name != "WAIT" ){*/
496 } else if (parent_waiting && !_waiting){
497 SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name
498 << " setting WAIT/WAITUNTIL: getting new waypoints ");
502 } else if (parent_next_name != "WAIT" && fp->getNextWaypoint()->getName() == "WAIT"){
503 SG_LOG(SG_AI, SG_DEBUG, "AIGroundVeh1cle: " << _name
504 << " wait done: getting new waypoints ");
507 fp->IncrementWaypoint(false);
508 next = fp->getNextWaypoint();
510 if (next->getName() == "WAITUNTIL" || next->getName() == "WAIT"
511 || next->getName() == "END"){
514 fp->IncrementWaypoint(false);
515 curr = fp->getCurrentWaypoint();
516 next = fp->getNextWaypoint();
520 } else if (_range_ft > (_x_offset +_parent_x_offset)* 4
522 SG_LOG(SG_AI, SG_ALERT, "AIGroundVeh1cle: " << _name
523 << " rescue: reforming train " << _range_ft
526 setTowAngle(0, dt, 1);
527 setSpeed(_parent_speed + (10 * sign(_parent_speed)));
529 } else if (_parent_speed > 1){
532 setTowAngle(_relbrg, dt, 1);
534 } else if (_parent_speed < -1){
539 setTowAngle(-(180 - (360 + _relbrg)), dt, 1);
541 setTowAngle(-(180 - _relbrg), dt, 1);
544 setSpeed(_parent_speed);
546 // FGAIShip::update(_dt_count);
551 // end AIGroundvehicle