Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fence warn at the margins #28840

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

andyp1per
Copy link
Collaborator

@andyp1per andyp1per commented Dec 11, 2024

This PR allows you to get a repeated warning when flying outside FENCE_MARGIN but inside the fence itself. Can be enabled by setting FENCE_OPTIONS=4. Multiple people have requested this because they are required to have a hard fence with failsafe action for regulatory reasons but want to know when they are close so they can avoid the action (typically when flying manually)

@andyp1per andyp1per force-pushed the pr-fence-warn-margin branch from ce3674f to 9fbfa49 Compare December 12, 2024 14:05
@andyp1per andyp1per force-pushed the pr-fence-warn-margin branch from 9fbfa49 to 336ea4e Compare December 13, 2024 20:44
@Hwurzburg Hwurzburg added the WikiNeeded needs wiki update label Dec 13, 2024
@andyp1per andyp1per force-pushed the pr-fence-warn-margin branch from 336ea4e to 9fbfa49 Compare December 15, 2024 22:08
@Ryanf55
Copy link
Collaborator

Ryanf55 commented Dec 16, 2024

This PR allows you to get a repeated warning when flying outside FENCE_MARGIN but inside the fence itself. Can be enabled by setting FENCE_OPTIONS=4. Multiple people have requested this because they are required to have a hard fence with failsafe action for regulatory reasons but want to know when they are close so they can avoid the action (typically when flying manually)

Do you have any references to the specific regulatory requirements for fences?

@rmackay9
Copy link
Contributor

Let's be careful about spamming the user when in Loiter mode and the user pushes the vehicle against the fence

@andyp1per andyp1per force-pushed the pr-fence-warn-margin branch 2 times, most recently from 3248d57 to 5f08d05 Compare January 25, 2025 22:23
@andyp1per
Copy link
Collaborator Author

Getting the maths right on this was actually quite hard. Think I might have it now.

@Ryanf55
Copy link
Collaborator

Ryanf55 commented Jan 25, 2025

Getting the maths right on this was actually quite hard. Think I might have it now.

Thoughts on unit tests to prove they are right?

@andyp1per
Copy link
Collaborator Author

Thoughts on unit tests to prove they are right?

I added a unit test, did you not see it?

@andyp1per
Copy link
Collaborator Author

Let's be careful about spamming the user when in Loiter mode and the user pushes the vehicle against the fence

So @rmackay9 what would you suggest here? If they have the option on and avoidance on and are in loiter mode, don't send the message?

@Ryanf55
Copy link
Collaborator

Ryanf55 commented Jan 26, 2025

Thoughts on unit tests to prove they are right?

I added a unit test, did you not see it?

Totally missed it. Those look great. I'm guessing the choice of sphere approximation over something like ellipsoid with Vincenty's distance is because fences are usually small enough and with not enough accuracy that we can just neglect the difference?

@andyp1per
Copy link
Collaborator Author

Thoughts on unit tests to prove they are right?

I added a unit test, did you not see it?

Totally missed it. Those look great. I'm guessing the choice of sphere approximation over something like ellipsoid with Vincenty's distance is because fences are usually small enough and with not enough accuracy that we can just neglect the difference?

Yeah, I was relying on the reference @tpwrules sent me which shows that for small angles the haversine approximation is totally fine (the unit test demonstrates meter resolution). What wasn't fine was the original implementation which did not work at all for lat/lng 😛
It does make me wonder how accurate the current polyfence calculation is, but I haven't checked it.

@andyp1per andyp1per force-pushed the pr-fence-warn-margin branch from 345f1f2 to 91d63c3 Compare January 26, 2025 19:35
@Ryanf55
Copy link
Collaborator

Ryanf55 commented Jan 26, 2025

Thoughts on unit tests to prove they are right?

I added a unit test, did you not see it?

Totally missed it. Those look great. I'm guessing the choice of sphere approximation over something like ellipsoid with Vincenty's distance is because fences are usually small enough and with not enough accuracy that we can just neglect the difference?

Yeah, I was relying on the reference @tpwrules sent me which shows that for small angles the haversine approximation is totally fine (the unit test demonstrates meter resolution). What wasn't fine was the original implementation which did not work at all for lat/lng 😛 It does make me wonder how accurate the current polyfence calculation is, but I haven't checked it.

Yep, we just might want a wiki note to not try to use fences to define boundaries the size of a country because you can be far enough off that you'll make the wrong people mad.

@andyp1per
Copy link
Collaborator Author

andyp1per commented Jan 26, 2025 via email

@andyp1per andyp1per requested a review from peterbarker January 26, 2025 20:50
@andyp1per andyp1per force-pushed the pr-fence-warn-margin branch 2 times, most recently from 2854ea4 to a0b9e86 Compare January 30, 2025 11:23
Copy link
Contributor

@tridge tridge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see if you can use the exiting function

@nexton-winjeel
Copy link
Contributor

It's probably worth putting this new information in the FENCE_STATUS message.

@andyp1per
Copy link
Collaborator Author

Using the Cartesian version of Polygon_closest_distance_point() saves about 1k, the overall cost is still 1k since this was not used before.

@andyp1per
Copy link
Collaborator Author

I added some tests for the existing Polygon_closest_distance_point(), it gives accuracy down to about 30cm - which doesn't seem great, but its <1m so probably acceptable.

}

// Haversine distance between two points given in 1e7 degrees
static ftype haversine(long lat1_1e7, long lon1_1e7, long lat2_1e7, long lon2_1e7)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused code?

Copy link
Contributor

@peterbarker peterbarker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to remove the new unused code

Need performance numbers

Move the in-margin stuff to a thread?

if (_poly_loader.breached()) {

Location loc;
bool have_location = AP::ahrs().get_location(loc);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bool have_location = AP::ahrs().get_location(loc);
const bool have_location = AP::ahrs().get_location(loc);

@tridge
Copy link
Contributor

tridge commented Feb 26, 2025

we need the worst case CPU cost, and quite possibly move to a IO callback, maybe at 20Hz or so

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants