I wanna write a program for rc-plane navigation
The plane must navigate to 2 waypoints waypoint_1 & waypoint_2 When it reaches to waypoint_1 (with miss-distance =< 5) then navigate to waypoint_2
But i have a problem.
First look at this :
void loop(){
if (distance-to-waypoint_1 > 5 meters) {
Navigate to waypoint_1
if distace-to-waypoint_1 < 5 meters :
Navigate-to-waypoint_2
If i use a code like this, plane will navigate to waypoint_1 and when it reaches somewhere that has less than 5 meters distance to waypoint_1, then will navigate to waypoint_2, but if plane fly away and the distance between it's location and waypoint_1 goes greater than 5 meters, plane return to previous navigation and will navigate to waypoint_1 again and ignore navigation to waypoint_2
So, i need a form of programme to first do a loop for navigation to navigate the plane to waypoint_1 and when it reaches the waypoint_1 , do another loop to navigate the plane to waypoint_2
Thanks
-
When distance between the plane position and waypoint_1 will be equal or less than 5 meters, then will navigate to waypoint_2 but the problem is that when the plane moves away from waypoint_1 to go to waypoint_2, its distance from waypoint_1 will be more than 5 meters, and the first if condition will be established again and it will navigate to waypoint_1 again.Curious guy– Curious guy2022年11月06日 22:56:58 +00:00Commented Nov 6, 2022 at 22:56
-
Because of the problem with if condition, if condition will not work for this purpose and it will be established again because of distanceCurious guy– Curious guy2022年11月06日 23:20:14 +00:00Commented Nov 6, 2022 at 23:20
-
Ok let's say i wanna use the distance as a criterion to know where am i and where I'm not, somehow i measure the distance between my position and Bob's house and if will be greater than 5 meters, i know that I'm not reached Bob's house, so i will use the direction to reach Bob's house. When the distance (only criterion that i use) between my position and Bob's house will be equal or less than 5 meters, i know that i reached Bob's house then, i will use another direction to reach out Mary's house but when i walk away from Bob's house to go to Mary's house, the distance betweenCurious guy– Curious guy2022年11月06日 23:47:05 +00:00Commented Nov 6, 2022 at 23:47
-
My position and Bob's house will be greater than 5 meters (because the only criterion that i use is distance between my position and Houses) so i'll use a new direction to go back to Bob's house again.Curious guy– Curious guy2022年11月06日 23:50:44 +00:00Commented Nov 6, 2022 at 23:50
-
Let us continue this discussion in chat.Curious guy– Curious guy2022年11月07日 00:36:08 +00:00Commented Nov 7, 2022 at 0:36
1 Answer 1
I think you should use a different logic by using a variable to hold the current target waypoint and separate the code to reach a waypoint and to change it.
Something like this (in pseudocode):
declare variable target_waypoint and set it to waypoint1
loop
Fly towards target waypoint
if distance to target waypoint lower than 5
change target_waypoint to waypoint 2
You can easily extent that for more waypoints by using an array of waypoints and a variable holding the index of the next waypoint:
declare waypoints as array
declare index variable, initialize with zero
set target_waypoint to waypoints[index]
loop
Fly towards target waypoint
if distance to target waypoint lower than 5
increment index
if index greater than max index
Either set to zero to loop through all waypoints again
or do whatever you want to do after the waypoints
set target_waypoint to waypoints[index]
The code that moves you to the waypoint doesn't care about what waypoint is next. It's only doing this one job. And when you reached a waypoint you simply move the target of that code to a different position.
Much like you yourself would walk between different waypoints. Setting first waypoint as target, walking towards it until you reached it, setting the next waypoint as target and doing the walking again.