WEBVTT

00:00.180 --> 00:06.450
This is step number three of the final project in this step, we are going to look at the application

00:06.450 --> 00:11.070
when the measured distance is lower than a certain threshold.

00:11.250 --> 00:18.330
So basically what this means is if you put this ultrasonic sensor in a real robot and mobile robot,

00:18.330 --> 00:24.030
for example, if there is an obstacle that is too close, you are going to make the robot stop and you

00:24.030 --> 00:30.960
are going to override the default behavior, but some warnings, et cetera, until the user removes

00:30.960 --> 00:33.840
the obstacle and unlock the application.

00:33.880 --> 00:40.560
OK, so in this step, only the lock functionality and in the next step we are going to unlock the application.

00:40.580 --> 00:43.960
So how to lock the application?

00:43.980 --> 00:52.890
I'm going to create a new boolean here, which is locked and set it to false by default.

00:53.370 --> 00:56.160
So this is a flag we are going to check and to set.

00:56.350 --> 00:58.390
And as you will see, it's quite easy.

00:58.920 --> 01:02.550
If we want to look at the application, we just set it to true.

01:02.820 --> 01:08.760
If we want to unlock, we set it to false and then we can just add in our code at some different places,

01:09.130 --> 01:09.510
Jake.

01:09.510 --> 01:12.540
If it's locked, if it's not locked and do some actions.

01:12.700 --> 01:14.610
I'm going to add also here.

01:16.480 --> 01:17.110
Define.

01:21.210 --> 01:28.590
Distance, OK, and let's say 10 centimeters, the ten point zero, so this is something you can change

01:28.890 --> 01:30.750
on your own application if you want to.

01:30.780 --> 01:34.980
OK, but for my application here, I'm going to use 10 centimeters.

01:34.980 --> 01:37.830
So below 10 centimeters, we lock the application.

01:38.130 --> 01:40.420
So I have my here, here.

01:40.440 --> 01:43.740
And what I can do now is actually the distance.

01:45.440 --> 01:52.350
When I get a new distance, I said so, I said some to break, etc., and then what I can do is check

01:52.370 --> 01:59.260
if distance is lower than not.

02:00.970 --> 02:07.930
Distance, if the distance is lower than the long distance we have defined, then I do is locked.

02:10.860 --> 02:15.840
And actually, what I'm going to do, I'm not going to just lock it here, I'm going to create a function

02:15.840 --> 02:25.580
called lock, so voided lock and in that function and who is locked is true.

02:26.040 --> 02:27.980
And actually, I'm going to do something else here.

02:27.990 --> 02:31.920
I'm going to check first if the application is not locked.

02:31.920 --> 02:41.110
So if exclamation mark is locked and only if it's not locked, I'm going to say it is locked to true

02:41.730 --> 02:43.700
and then call the lock function.

02:47.220 --> 02:52.340
So why use a function here, because, well, later on, you will be able to do more things, OK, and

02:52.350 --> 02:55.530
you can call the log if you want, from different parts of your application.

02:56.460 --> 03:01.200
And then in that function, you can see that first a check if is locked is false first.

03:01.440 --> 03:08.580
So this will enable us to only look at the application when it is not locked at because every time you

03:08.580 --> 03:14.180
but look to true and then you do some actions where you are going to do that many times here.

03:14.190 --> 03:20.490
By doing this, we ensure that we enter this book of code only when we go from the state, not logged

03:20.820 --> 03:22.200
to the state locked.

03:22.560 --> 03:22.830
All right.

03:22.830 --> 03:27.320
So we have a flag that is false, that is first and false.

03:27.330 --> 03:31.470
And then when the distance is lower than long distance, we set it to true.

03:32.040 --> 03:36.620
So the lock mechanism is already here, but now it's not doing anything.

03:36.630 --> 03:41.900
OK, so we need to do some action depending on if it's locked or not locked.

03:42.300 --> 03:48.480
What I'm going to do now is I'm going to say that when the state is locked, we're going to make the

03:48.480 --> 03:52.770
red and yellow and blink together at the same time.

03:53.190 --> 04:00.510
And this will of the right, the default behavior, for example, here, the one energy that is blinking

04:00.510 --> 04:02.520
at different rates will not happen.

04:02.520 --> 04:05.730
It will simply blink along with the right.

04:06.360 --> 04:10.370
So I'm going first to define here.

04:12.170 --> 04:17.390
Defined, so let's call it zero lead in 12.

04:18.480 --> 04:22.080
Let's add here some liabilities for.

04:23.710 --> 04:26.590
OP ed, that's just.

04:27.490 --> 04:38.560
Use the same thing here, so last time your energy blinked and then error added, delay willing to put

04:38.560 --> 04:39.820
that at three hundred.

04:41.330 --> 04:42.380
And in Iraq.

04:44.660 --> 04:50.300
And states, so we have our three variables to use the time, functionality and to be able to toggle

04:50.750 --> 04:54.410
the editing now and going also to add a function here.

04:54.420 --> 05:04.340
So we have toggle one managing to avoid toggle zero and can we create a separate function for that and

05:04.340 --> 05:06.450
the error state.

05:06.530 --> 05:08.690
So this function will toggle the state.

05:08.990 --> 05:13.170
So I'm going to use the one line if we are going to check.

05:13.190 --> 05:24.410
So there are any states, if the state is high interrogation match, we set it to low as we set it to

05:24.410 --> 05:28.200
high and then digital rights with that.

05:28.250 --> 05:28.790
There are.

05:30.710 --> 05:31.340
Naledi.

05:33.760 --> 05:36.010
And there are already state.

05:38.220 --> 05:39.990
And we have the Torgau, there are.

05:41.350 --> 05:43.390
So we were down in the city.

05:43.930 --> 05:50.910
I'm going to do spin mode zero in with about.

05:51.830 --> 05:58.910
It took initialise the pain and now what I'm going to do so now we are going to decide what we do when

05:59.180 --> 06:02.890
the application is locked, what we do when the application is not locked.

06:03.530 --> 06:06.000
Let's check each action we have here.

06:06.410 --> 06:10.050
So first, we trigger that ultrasonic sensor here and we measure the distance.

06:10.370 --> 06:11.860
This webbing to keep it OK.

06:12.020 --> 06:12.920
That is very important.

06:12.950 --> 06:13.850
We're going to keep that.

06:13.940 --> 06:21.830
And now this is the behavior of the one which is going to blink faster if the obstacle is closer.

06:22.340 --> 06:24.110
But we want to override this.

06:24.680 --> 06:31.250
What I'm going to do is if is locked, which we are going to do is make SilverLink.

06:33.370 --> 06:34.750
Errol and.

06:36.040 --> 06:47.110
One reads at the same time, so this would only happen if the flag is locked is true, and then else

06:49.060 --> 06:50.410
as I put this in the.

06:51.910 --> 06:55.290
And let's fix the incantation here.

06:57.580 --> 07:01.510
And actually, let's put this at the top of the loop.

07:02.620 --> 07:03.100
He.

07:05.740 --> 07:13.990
So now in the look, we first check if the flag is locked is true, then we do whatever we need to do

07:14.020 --> 07:20.460
when the application is logged, which would override some of the default behaviours that we put in

07:20.470 --> 07:20.590
the.

07:21.310 --> 07:23.560
So if the application is not logged, we do this.

07:23.560 --> 07:24.880
If it's locked, we do this.

07:25.450 --> 07:36.100
So what I'm going to do here is thank no minus last time you're all linked.

07:36.460 --> 07:41.050
If this is greater than zero editing delay.

07:43.700 --> 07:47.990
Then I'm going to do last time, plus delay.

07:50.000 --> 07:51.500
And I'm going to do toggled.

07:53.520 --> 08:00.600
Hero Eddie and I'm also going to do toggled one Eddie.

08:01.900 --> 08:07.300
So only if it's locked, I took a lead and a warning at the same time.

08:08.110 --> 08:12.790
Another thing is that you still set the warning, bring rates from the distance here.

08:13.300 --> 08:16.730
But this is actually used in that block of code.

08:16.900 --> 08:18.850
A warning allegedly here.

08:18.860 --> 08:24.770
We're going to talk about the warning energy depending on the eral and not the warning, evidently.

08:24.830 --> 08:26.520
So we don't really care about that line.

08:26.540 --> 08:28.310
We can just leave it like this.

08:28.810 --> 08:35.050
So now at 300, so this is three hundred defined here at three hundred milliseconds.

08:35.500 --> 08:39.480
Every three hundred milliseconds, we toggle the error and the warning reads.

08:39.820 --> 08:42.810
You can see the code here for the two functions will do the same thing.

08:43.300 --> 08:49.990
And just what I'm going to do is when we lock the application, so we set the flag to true, I'm going

08:49.990 --> 08:58.720
to do warning state, so I'm going to set the state to low and error state.

09:00.520 --> 09:06.680
So which means that I set the state for the two entities at the same so the same state.

09:06.700 --> 09:11.790
So then when I toggle it with toggle high and high and then low and low at the same time.

09:11.800 --> 09:19.600
OK, because what could happen here is that let's say so you first toggle the energy, so it's high,

09:19.600 --> 09:23.410
low, high, etc. and then you lock the application.

09:23.410 --> 09:30.690
When you lock the application, for example, the right LG is out and the yellow LCD is pulled on,

09:30.910 --> 09:38.140
which means that it's going to blink, but it's going to alternate between the yellow and red, which

09:38.140 --> 09:39.270
is maybe what you want to do.

09:39.280 --> 09:41.680
But in my case, it's not what I want to do.

09:41.680 --> 09:45.040
I want to make the league exactly at the same time.

09:45.340 --> 09:50.980
So I initialize also the state to the same state when I look at the application.

09:52.070 --> 09:59.420
So I think this is good, let's verify you could name it final project step three.

10:01.340 --> 10:03.890
OK, we don't have any compilation, Eros.

10:04.010 --> 10:05.810
I am going to upload.

10:08.400 --> 10:15.720
So you can see here we still have our yellow LCD, which is blinking, and if I put my hand closer to

10:15.720 --> 10:22.110
the end, it is blinking faster and faster and faster until I reach the point where you can see now

10:22.560 --> 10:27.250
the red and yellow are blinking at the same time.

10:27.270 --> 10:29.260
OK, I can do whatever I want.

10:29.520 --> 10:31.060
Now the application is locked.

10:31.140 --> 10:32.830
OK, there is nothing I can do.

10:33.630 --> 10:38.510
So the behavior for the yellow lady here has been replaced.

10:39.270 --> 10:42.290
So of course now we don't have the unlock mechanism.

10:42.300 --> 10:45.500
So if you want to just unlock, you have to restart the application.

10:45.750 --> 10:50.940
I'm going to press the reset button, which will stop the application from start from scratch.

10:51.450 --> 11:00.790
And now I can you can see I can move my hand and if I get closer, I will look at the application begin.

11:01.650 --> 11:07.680
So we have a nice warning, which means that, for example, your robot is going to close from a wall

11:07.680 --> 11:13.530
or an obstacle so you can, for example, stop the robot and then make the two entities blink.

11:13.860 --> 11:20.030
And as you can see, even if you remove the obstacle, even if the measure, the distance is no longer

11:20.030 --> 11:22.830
than before, the application is still locked.

11:23.310 --> 11:25.890
This is a mechanism that is quite useful.

11:26.220 --> 11:33.660
When there is some danger, you lock the application and then you ask for a human manual interaction

11:33.660 --> 11:37.820
with the circuit or the robot to unlock the application.

11:37.830 --> 11:44.490
So the user will need to first check and make sure everything is OK and then press on some button so

11:44.490 --> 11:47.520
the application can continue working as before.
