WEBVTT

00:00.080 --> 00:03.620
We are now in step number five for this final project.

00:03.620 --> 00:08.960
And while the project is actually working, you just saw that the turtle is actually catching all the

00:08.960 --> 00:10.190
other turtles.

00:10.190 --> 00:13.430
Now we are going to improve that project and in this step.

00:13.430 --> 00:14.840
So it's not going to be that long.

00:14.840 --> 00:20.990
We're going to choose to target the closest turtle instead of the first turtle in the list.

00:21.020 --> 00:21.320
Okay.

00:21.350 --> 00:28.670
And first I'm going to show you with the I can increase the frequency here in the turtle spawner.

00:28.700 --> 00:35.030
Let's say instead of two seconds between each turtle I'm going to put 0.5 okay.

00:35.060 --> 00:37.250
So I'm going to spawn a lot more turtles.

00:37.250 --> 00:41.330
And let's see what's going to be the behavior of the master turtle.

00:42.470 --> 00:48.770
So now I start the turtle sim node I start the controller again.

00:49.070 --> 00:53.090
Let's put turtle here and I start the spawner.

00:53.660 --> 00:57.230
So it's going to spawn turtle quite fast.

00:57.230 --> 01:05.510
And well, as you can see, the turtle is just going through all of the other target turtles, but in

01:05.510 --> 01:07.380
the order in which they appeared.

01:07.620 --> 01:09.420
Which is the order of the list.

01:09.630 --> 01:10.050
All right.

01:10.080 --> 01:17.280
We could improve that a lot by saying that we just try to go to the nearest turtle instead of the first

01:17.280 --> 01:18.450
turtle in the list.

01:18.510 --> 01:19.290
All right.

01:19.650 --> 01:22.020
So let's do that in the code.

01:22.650 --> 01:24.510
And where do we do this.

01:24.540 --> 01:27.000
Actually there is nothing to do in the turtle spawner.

01:27.030 --> 01:27.330
Okay.

01:27.360 --> 01:34.170
This node is just spawning turtles and killing turtles and also publishing the current list of alive

01:34.200 --> 01:34.950
turtles.

01:34.950 --> 01:38.670
So this is in the turtle controller.

01:38.670 --> 01:46.170
When we receive the list of alive turtles here, you can see we decide to chase the first one.

01:46.170 --> 01:52.350
But what we could do instead is to actually compute the distance for each turtle and find the nearest

01:52.350 --> 01:54.390
one, and just go to the nearest one.

01:54.390 --> 01:56.550
And actually here I'm going to give the choice.

01:56.550 --> 02:07.170
I'm going to create a variable called self dot catch closest turtle first.

02:09.090 --> 02:10.980
By default it's going to be true.

02:12.120 --> 02:23.440
And here I'm going to say if self catch closes total first we do something I'm going to put pass and

02:23.440 --> 02:29.290
then else we go to the first one okay.

02:29.320 --> 02:35.860
And then we can maybe put that as a parameter so that the user, when we actually run that node we can

02:35.860 --> 02:41.500
choose do we want to catch the closest total first or just follow the order of the list.

02:41.530 --> 02:41.830
All right.

02:41.860 --> 02:43.240
So I keep both options.

02:43.240 --> 02:48.280
Now to find the closest total we're going to receive a list of turtles.

02:48.280 --> 02:49.840
We just go through the list.

02:49.870 --> 02:55.960
We compute the distance for each turtle compared to where we are right now using the current pose.

02:55.960 --> 03:00.850
And we just say that the turtle to catch is the one with the smallest distance.

03:01.180 --> 03:10.990
So here as a helper variables, I'm just going to create the closest turtle initialized to none.

03:10.990 --> 03:18.790
And then I'm going to create closest turtle distance.

03:20.270 --> 03:23.690
And then I do for turtle in.

03:24.740 --> 03:27.020
So message turtles.

03:27.050 --> 03:28.370
Okay, that's the list.

03:28.880 --> 03:29.690
And we are in this.

03:29.720 --> 03:32.450
If so, we know that we have at least one element.

03:32.450 --> 03:34.640
So for each turtle what do I do.

03:34.670 --> 03:36.710
I will compute the distance.

03:36.740 --> 03:49.790
To compute the distance I need to first get the distance x which is well the turtle we get here x minus

03:49.790 --> 03:54.020
self dot pose dot x.

03:54.140 --> 03:54.740
Okay.

03:54.770 --> 03:56.840
It's the same thing we do here basically.

03:56.840 --> 04:06.290
And then distance y is going to be turtle dot y minus self pose y.

04:06.410 --> 04:07.520
And we can do.

04:07.550 --> 04:11.570
Actually I'm just going to take that line because it's going to be the exact same.

04:11.600 --> 04:20.780
The distance is the square root of the distance x multiplied by itself plus the distance y multiplied

04:20.780 --> 04:22.040
by itself.

04:22.610 --> 04:30.750
So with this we have the distance between the turtle so that turtle in the array and our current pose.

04:30.750 --> 04:33.090
So the pose of the master turtle.

04:33.120 --> 04:33.720
Then I'm going to do.

04:33.750 --> 04:38.940
If the closest turtle is well is none.

04:39.090 --> 04:51.300
If it's not initialized or if the distance is lower than the closest turtle distance, I just say that

04:51.300 --> 05:01.800
the closest turtle is the current one, so the turtle and the closest turtle distance is the current

05:01.800 --> 05:03.450
distance we have computed.

05:03.480 --> 05:03.870
All right.

05:03.870 --> 05:07.200
So the first time we go to the if closest turtle is none.

05:07.200 --> 05:11.220
So it's going to basically save that turtle as the closest turtle.

05:11.220 --> 05:13.620
And we also save the distance.

05:13.620 --> 05:16.980
You see we saved the distance as the closest turtle distance.

05:16.980 --> 05:20.100
And then for the next ones we just compute the new distance.

05:20.100 --> 05:25.230
And if it's shorter we save that turtle as the new closest turtle.

05:25.230 --> 05:27.780
And we also keep the distance for the next time.

05:27.780 --> 05:34.960
So basically after we are done with this for loop, we know that the closest total variable here contains

05:34.960 --> 05:36.850
the closest total.

05:36.850 --> 05:43.630
So we can do self dot total to catch is equal to closest total.

05:43.690 --> 05:44.290
All right.

05:44.290 --> 05:45.040
And that's it.

05:45.040 --> 05:50.290
Because then we just decide to not follow the first one in the list.

05:50.320 --> 05:53.800
We just decide to choose whatever is the closest.

05:54.220 --> 05:55.780
Then we're going to chase it.

05:55.780 --> 06:03.130
And at the end we just call this service catch total is going to remove that total and then send the

06:03.130 --> 06:06.730
new list with just the turtles that are alive.

06:06.730 --> 06:11.620
And one thing that's going to be interesting, and we're going to be able to see that when we test,

06:11.650 --> 06:18.130
is that whenever you receive a new list of turtles, this is going to be computed again.

06:18.130 --> 06:25.390
So if let's say the turtle is the master turtle is currently chasing one turtle, but then one other

06:25.390 --> 06:29.050
turtle is found very close to the master turtle.

06:29.080 --> 06:33.550
Then because we receive a new list, we compute the distances with all turtles.

06:33.550 --> 06:40.790
If that new turtle is closest than the current one we are chasing, we might actually change the target.

06:40.880 --> 06:41.450
Okay.

06:41.480 --> 06:43.880
So let's actually see that with an example.

06:44.090 --> 06:50.510
So now I can start I'm going to change actually the frequency.

06:50.540 --> 06:53.990
Well instead of 0.5 I'm going to use 0.8.

06:54.050 --> 06:57.020
Not to spawn turtles two too fast.

06:58.250 --> 07:00.920
So let's start the turtle sim node.

07:01.910 --> 07:03.650
Let's start the controller.

07:03.650 --> 07:05.570
And let's start the spawner.

07:05.570 --> 07:06.800
And let's see what happens.

07:08.690 --> 07:10.190
So we chased that one.

07:10.460 --> 07:11.210
Okay.

07:11.690 --> 07:12.560
Okay.

07:13.610 --> 07:15.530
And let's wait until.

07:15.920 --> 07:18.680
So maybe the case is going to be soon or not.

07:19.280 --> 07:24.380
But maybe at some point you see okay now we were going to that turtle here.

07:24.380 --> 07:26.450
But then this one spawned.

07:26.450 --> 07:31.760
So the turtle just changed its trajectory to catch that one instead.

07:31.790 --> 07:32.030
Okay.

07:32.060 --> 07:33.410
That's what I was talking about.

07:33.410 --> 07:35.870
And now you can see it's much more smooth.

07:35.900 --> 07:36.110
Okay.

07:36.140 --> 07:37.100
And it makes more sense.

07:37.100 --> 07:38.540
We go to the nearest one.

07:38.720 --> 07:39.020
Okay.

07:39.050 --> 07:40.820
We don't go to the first one on the list.

07:40.850 --> 07:42.230
We go to the nearest one.
