ardupilot icon indicating copy to clipboard operation
ardupilot copied to clipboard

Copter: During missions copter faces a bt off from next waypoint

Open rmackay9 opened this issue 13 years ago • 5 comments

The cause of the problem is the copter decides the angle to face from the moment that the intermediate point hits the current waypoint target. Instead of being based on the vehicles location, the copter's heading should be based on the angle between the two waypoints.

rmackay9 avatar Jul 29 '13 04:07 rmackay9

Yes I know. Basically it's that unprecise bearing calculation in the code. (Somthing like this: 9000.0f + atan2f(-dLat, dLon) ....) If you use the better formula from here: http://www.movable-type.co.uk/scripts/latlong.html var y = Math.sin(dLon) * Math.cos(lat2); var x = Math.cos(lat1)_Math.sin(lat2) - Math.sin(lat1)_Math.cos(lat2)*Math.cos(dLon); var brng = Math.atan2(y, x).toDeg();

You will see the copter looking the waypoint dead in the eye. That's why I changed the multiwii calculation (based on arducopter 2.7.x I think) from this: static void GPS_distance_cm_bearing(int32_t * lat1, int32_t * lon1, int32_t * lat2, int32_t * lon2, uint32_t * dist, int32_t * bearing) { float dLat = _lat2 - *lat1; // difference of latitude in 1/10 000 000 degrees float dLon = (float) (_lon2 - *lon1) * GPS_scaleLonDown; *dist = sqrtf(sq(dLat) + sq(dLon)) * 1.113195f;

*bearing = 9000.0f + atan2f(-dLat, dLon) * 5729.57795f;      // Convert the output radians to 100xdeg
if (*bearing < 0)
    *bearing += 36000;

}

to this:

void GPS_distance_cm_bearing(int32_t * lat1, int32_t * lon1, int32_t * lat2, int32_t * lon2, uint32_t * dist, int32_t * bearing) { float dLatRAW, dLonRAW, x, y, lat1RAD, lat2RAD, Coslat2RAD; if (_lat2 != 0 && *lat1 != 0 && *lon2 != 0 && *lon1 != 0) // Crashpilot Errorcheck { if (CosLatScaleLon == 0.0f) GPS_calc_longitude_scaling(); dLatRAW = (float)(_lat2 - _lat1); // difference of latitude in 1/10 000 000 degrees dLonRAW = (float)(_lon2 - _lon1);
x = dLonRAW * CosLatScaleLon; *dist = sqrtf(dLatRAW * dLatRAW + x * x) * MagicEarthNumber; // dist in cm dLatRAW = dLatRAW * GPSRAWtoRAD; // 10^7 DEGdelta to Rad dLonRAW = dLonRAW * GPSRAWtoRAD; lat1RAD = *lat1 * GPSRAWtoRAD; lat2RAD = *lat2 * GPSRAWtoRAD; Coslat2RAD = cosf(lat2RAD); y = sinf(dLonRAW) * Coslat2RAD; x = cosf(lat1RAD) * sinf(lat2RAD) - sinf(lat1RAD) * Coslat2RAD * cos(dLonRAW); *bearing = constrain((int32_t)(atan2f(y, x) * RADtoDEG100), -18000, 18000); if (_bearing < 0) *bearing += 36000; } else // Error! { *dist = 0; *bearing = 0; } }

I hope something similar could help arducopter 3.X code as well. Cheers Rob

Crashpilot1000 avatar Aug 01 '13 00:08 Crashpilot1000

Just for completeness, I don't want to annoy you. MagicEarthNumber 1.113195f GPSRAWtoRAD 0.0000001f * M_PI / 180.0f RADtoDEG100 18000.0f / M_PI

Crashpilot1000 avatar Aug 01 '13 00:08 Crashpilot1000

The code used to (and still may) lock in the heading the next WP at the moment it reaches the current WP. This was to prevent yawing at the last minute and becoming unstable. (there is a deeper explanation, but trust me, it was a problem.) I think the current nav code no longer needs this. Also, the current code is pretty efficient and accurate within reason. Going to the heavier code makes a lot of sense in PX4!

jason4short avatar Aug 01 '13 01:08 jason4short

In master the copter now points directly at the waypoint. I hear the comments about making the copter point in the direction it's flying instead so we might try and do that for AC3.2 as well.

rmackay9 avatar Mar 31 '14 01:03 rmackay9

Actually, I'm going to close this issue because the original problem is fixed in master and I've raised a new issue for the enhanced behaviour discussed.

rmackay9 avatar Mar 31 '14 01:03 rmackay9