WEBVTT

00:00.210 --> 00:07.530
In this additional lesson on the section on ultrasonics, Senso, I'm going to show you how you can

00:07.530 --> 00:12.160
improve the behavior of your ultrasonic sensor with the software.

00:12.180 --> 00:17.230
OK, if you have an erratic behavior, maybe your sensor is working just fine.

00:17.250 --> 00:18.280
So that's not a problem.

00:18.600 --> 00:21.060
Maybe you're using the simulation in that case.

00:21.090 --> 00:25.680
Well, simulation is always working, but if you have the same problem as me, as you could see in the

00:25.680 --> 00:31.950
previous lesson, then it is good to understand maybe why you have this problem or get some ideas of

00:31.950 --> 00:35.070
why and then be able to fix this in the code.

00:35.280 --> 00:40.500
So this kind of issue is actually something you have to be prepared to have a lot when you work with

00:40.500 --> 00:40.900
hardware.

00:40.970 --> 00:46.990
OK, because real life and the real world always contains parts of unknown.

00:47.010 --> 00:50.410
And this is true for everything, especially hardware.

00:50.600 --> 00:55.080
OK, it's not because it's working fine in simulation that is going to work fine in real life.

00:55.530 --> 01:01.800
And it's also not because it's working fine in real life, once or twice that every time you do the

01:01.800 --> 01:04.550
same setup, it's going to work fine also.

01:05.010 --> 01:05.290
Right.

01:05.320 --> 01:07.740
That's one of the main issue with hardware.

01:07.750 --> 01:08.670
It's not really an issue.

01:08.670 --> 01:09.910
It's just how it works.

01:10.410 --> 01:14.480
So you have to be prepared to face that kind of issue in many of your project.

01:14.490 --> 01:17.380
But that's a normal part of any project.

01:17.700 --> 01:23.550
So if you have a problem with how do it, you can either fix the hardware and sometimes you can also

01:23.550 --> 01:25.950
try to create a fix with software.

01:26.130 --> 01:32.320
OK, so here I'm going to talk about both and not that it's also a little bit more advanced, OK?

01:32.340 --> 01:34.710
This is, again, not a big you.

01:34.920 --> 01:38.540
Listen, this is more of an intermediate listen.

01:39.060 --> 01:42.820
So what could go wrong with the hardware?

01:43.050 --> 01:48.590
First, I have shown you that maybe the connections on your breadboard and your arduino are not correct.

01:48.600 --> 01:55.640
So make sure you check that 100 percent, maybe change some wires, change some connectors, connect

01:55.650 --> 01:59.000
the sensor to a different place to see if you can solve the issue.

01:59.210 --> 02:04.920
OK, if it doesn't work well, maybe it's simply because you have the bad quality sensor.

02:05.190 --> 02:10.800
And actually, if you have a starter kit like the starter kit that I have that I'm using right now,

02:11.280 --> 02:18.060
if you have both the cheapest sensor you can find when the thing is that it depends a lot on the manufacturer

02:18.060 --> 02:19.320
of the hardware component.

02:19.370 --> 02:24.990
OK, if you buy the cheapest one, you can maybe expect that disordering of the components on the sensor

02:24.990 --> 02:31.440
is not well made, that maybe some parts that needs to be aligned are not completely aligned.

02:31.780 --> 02:38.790
OK, that's the firmware running on the sensor is maybe not a good quality one either and many different

02:38.790 --> 02:39.210
things.

02:39.210 --> 02:40.320
OK, that can go wrong.

02:40.410 --> 02:46.350
So basically when you buy cheap hardware, well, don't expect it to work 100 percent of the time.

02:46.540 --> 02:52.620
So if you have the chance to get another ultrasonic sensor, maybe it's better to try the second one

02:52.620 --> 02:53.300
if you have one.

02:53.640 --> 02:58.320
And in the future, whenever you want to buy hardware component, if it's very, very, very cheap,

02:58.320 --> 03:00.240
I advise you maybe to buy two or three.

03:00.610 --> 03:05.440
OK, so in case you have one that is not working correctly, you can try the other one.

03:05.610 --> 03:11.520
So if you think it is working well and your component is a bad quality, well, the only option you

03:11.520 --> 03:12.480
have is to change.

03:12.480 --> 03:17.720
The components are also maybe to try to make a fix from the software, which we are going to do here,

03:17.820 --> 03:22.440
and also a third cause that maybe is simply the environment.

03:22.440 --> 03:24.990
OK, where are you operating the sensor?

03:25.290 --> 03:30.950
Because DICENZO is actually sending ultrasonic waves in the environment.

03:31.200 --> 03:36.330
So for my specific example, I am actually in a winning recording here.

03:36.510 --> 03:42.720
I am in a small room with a lot of glass walls and a lot of stuff in the wall.

03:42.960 --> 03:49.050
So maybe the ultrasonic waves are going to bounce everywhere and come back to the ultrasonic sensor,

03:49.050 --> 03:52.000
which will read wrong data from time to time.

03:52.410 --> 03:59.700
OK, you may want to try in a bigger environment, for example, in the outside or just in a bigger

03:59.700 --> 04:02.120
room to try to see if you have the same problem.

04:02.340 --> 04:02.580
Right.

04:02.640 --> 04:06.600
So now you have a few of the reasons that maybe it's not working.

04:06.600 --> 04:10.470
If it's not working all right, if it's working correctly, you don't need to worry about it.

04:10.710 --> 04:13.770
But now what can you do if you are in the same situation as me?

04:13.770 --> 04:16.850
So you have correctly checked your how do I set up?

04:17.370 --> 04:22.080
Well, you just have one sensor so you can change it and you can change on the.

04:22.170 --> 04:28.740
And so I'm doing this on purpose so I can show you how to fix hardware with software.

04:29.100 --> 04:31.590
You can modify the software a little bit.

04:31.590 --> 04:36.690
And that's what we are going to do when we get the ultrasonic distance here.

04:36.960 --> 04:42.240
So we get the duration in Microcircuit of the Pulse and then we compute the distance.

04:42.510 --> 04:48.780
I'm going to apply a few filters here so we can regulate a little bit the values from the software.

04:48.780 --> 04:52.860
So this is not, of course, a one hundred percent solution to the problem.

04:53.040 --> 04:59.300
OK, but this is a nice fix that will allow us to continue and progress with this sensor and the circuit.

04:59.490 --> 04:59.700
So.

05:00.510 --> 05:08.820
What I'm going to do is I'm going to upload these programs activity 15, OK, where we put on a different

05:08.820 --> 05:11.300
energy from the distance we get.

05:11.550 --> 05:14.010
And also because here we print.

05:14.130 --> 05:18.060
OK, listen, make sure you have the same print along with distance.

05:18.960 --> 05:24.600
So I'm going to I can open the science monitor and you can see the different values we get.

05:24.660 --> 05:25.130
OK.

05:26.550 --> 05:33.330
So we have some values and you can see we have some weird values from time to time to get a better visualization

05:33.330 --> 05:33.540
here.

05:33.570 --> 05:36.390
I'm going to open the cell clutter, OK?

05:36.690 --> 05:38.280
And let's see, what do we get?

05:38.310 --> 05:44.700
So here you can see I am in 140, OK, this is the wall behind me.

05:45.040 --> 05:49.400
I'm going to put my hand and do some movement in front of the sensor.

05:49.710 --> 05:56.060
And you can see we OK, first thing we see is that we have some values that go way too high.

05:56.130 --> 06:03.030
OK, from time to time and the maximum value we could get here is 400 centimeters, OK, because this

06:03.030 --> 06:05.300
is the max range for the sensor.

06:06.390 --> 06:15.720
But you can see we get the value up to more than 3000 centimeters, which is well, which is definitely

06:15.720 --> 06:16.450
not correct.

06:16.890 --> 06:24.330
So the first thing maybe we can do is that anytime we get a value that is above four meters or 400 centimeters,

06:24.780 --> 06:27.420
well, we just don't take this value into account.

06:27.750 --> 06:34.860
So to do that, I'm going to create a new viable here that I'm going to call double previous.

06:36.800 --> 06:40.880
Distance and let's initialize it to 400.

06:43.340 --> 06:49.010
OK, and I'm going to go here, so I'm reading the duration and computing the distance, and I'm going

06:49.010 --> 06:52.700
to filter I'm going to add some filters here for the distance.

06:52.730 --> 06:53.570
Let's see if.

06:54.650 --> 06:55.850
Distance is greater.

06:56.950 --> 06:58.030
And 400.

07:00.640 --> 07:05.950
Then what I'm going to do is I'm not going to take this into account, I'm simply going to return to

07:06.610 --> 07:09.010
their previous distance.

07:09.010 --> 07:17.050
And if the distance is lower than 400, so if the distance is correct, I'm going to the previous distance

07:17.050 --> 07:20.710
is equal to the distance and then return the distance.

07:21.130 --> 07:23.530
So what this will do, let's let's make an example.

07:23.560 --> 07:25.450
So first, we have a pretty decent 400.

07:25.450 --> 07:27.570
We just initialized with a venue.

07:27.910 --> 07:29.880
So let's say we have a correct value here.

07:30.220 --> 07:31.120
So we get this done.

07:31.180 --> 07:32.620
Let's say one hundred forty.

07:33.460 --> 07:35.960
The distance is lower than four hundred.

07:36.190 --> 07:41.620
So now we said the previous distance, which become the distance to the previous distance, becomes

07:41.620 --> 07:43.120
one hundred and forty.

07:43.330 --> 07:46.030
OK, then we return one hundred and forty.

07:46.750 --> 07:53.030
The next thing we read, let's say we have a with the disease like three thousand so we get three thousand.

07:53.050 --> 07:54.450
Here we check that.

07:54.460 --> 07:57.020
OK, three thousand is actually greater than 400.

07:57.250 --> 08:04.060
So instead of using this value, we just return the previous one, the previous one, which is now one

08:04.060 --> 08:04.950
hundred forty.

08:05.320 --> 08:07.270
So let's upload this program here.

08:09.050 --> 08:18.950
And let's open the CIA again and you will see, OK, I get 140, you see, I still get many variations

08:18.950 --> 08:27.770
in the volume, OK, that I never get any value that is above one hundred forty or any value that is

08:27.770 --> 08:34.340
above actually 400, OK, because 400 is the maximum the cap for the sensor.

08:34.890 --> 08:41.390
So I can be sure by using this filter that I don't get any value above four hundred.

08:41.390 --> 08:43.850
So that's already a good first step.

08:44.240 --> 08:48.370
So first, eliminate any value that is outside of the range of the sensor.

08:48.770 --> 08:53.560
And no, I'm going to do another thing and this is called a complementary filter.

08:53.990 --> 08:55.820
What is a complementary filter?

08:56.240 --> 08:57.650
So a complementary filter.

08:57.650 --> 09:03.140
I'm not going to go into too many details here if you want to check that outside of the code.

09:03.170 --> 09:06.890
OK, this is a mathematical filter that we are going to use.

09:06.890 --> 09:12.950
And this is very often used to smooth out some data that you read from the sensor.

09:13.340 --> 09:20.860
So the complementary filter is actually quite simple, is instead of just returning the new distance,

09:20.870 --> 09:26.750
OK, we are going to return an average of the previous distance and a new distance.

09:27.050 --> 09:32.750
OK, let's let's actually write the code and then give some explanation to the new distance.

09:32.750 --> 09:39.620
We become previous distance, let's say, multiplied by zero point five.

09:40.160 --> 09:40.700
Let's.

09:41.760 --> 09:48.160
Distance multiplied by zero point five, but we can't the distance here.

09:48.270 --> 09:53.550
OK, we have the previous distance from the previous run of that function and then we do.

09:54.150 --> 09:57.250
So this is an example with zero point five and zero point five.

09:57.690 --> 10:01.290
So the distance becomes so the distance we have just measured.

10:01.740 --> 10:04.780
We take half of this value and half of the previous one.

10:05.340 --> 10:09.480
So to make a complementary filter, you simply have to store the previous value.

10:09.510 --> 10:14.390
So you have to, of course, the data at a certain frequency.

10:14.400 --> 10:21.470
So he attempts, OK, and so you get you still the previous distance to the previous data.

10:21.720 --> 10:26.430
And then when you get the new data, you make a mix of the previous data and the new data.

10:27.060 --> 10:33.630
So, for example, if you have just one spike, which is very high, this spike would actually be smoothed

10:34.110 --> 10:37.130
and you will not see so much weird behavior.

10:37.740 --> 10:42.610
So we can use the distance with zero point five and distance with zero point five.

10:42.960 --> 10:47.350
The actual important thing is that this and this equals to one.

10:47.400 --> 10:54.810
OK, so we could use, for example, the zero point seven and zero point three here and the bigger the

10:54.810 --> 10:56.910
number for the previous data.

10:57.310 --> 10:59.930
OK, the bigger the resistance to change.

11:00.240 --> 11:02.530
And actually, let's mix some examples.

11:02.550 --> 11:06.130
OK, I'm going to go with zero point five and zero point five.

11:06.390 --> 11:09.660
Let's upload that program so you can see just one additional line.

11:10.810 --> 11:18.750
And I'm going to happen to say a lot, and you can see so we still have some weird variations, OK,

11:18.940 --> 11:20.710
but not not that much.

11:21.150 --> 11:24.700
It's actually much better if you can see that the.

11:26.120 --> 11:28.280
Signal is a little bit smoother.

11:29.680 --> 11:37.750
All right, and now let's add, let's say here on nine and point one, which means that whenever we

11:37.750 --> 11:44.740
get to a new distance, we are going to keep 90 percent of the previous distance and only add 10 percent

11:44.740 --> 11:45.670
of the new distance.

11:45.980 --> 11:47.410
So he that's applaud.

11:48.590 --> 11:55.920
You will see that he's very smooth, but also in shape, put my hand here.

11:57.020 --> 12:02.050
It's very smooth, but also very low key to detect changes.

12:02.060 --> 12:03.160
I remove my hand.

12:03.710 --> 12:08.780
You can see it really takes some time to go through here to 140.

12:09.140 --> 12:18.520
I just put my hand again now and you can see it's really low, really slow, but it's much smoother.

12:19.250 --> 12:22.190
So now you have the formula for the complementary filter.

12:22.190 --> 12:26.060
And what you need to tinker is basically those two diameters.

12:26.270 --> 12:32.720
So the percentage, 90 percent, 10 percent here, make sure that the total is one hundred percent on

12:32.720 --> 12:33.620
just one.

12:34.040 --> 12:42.200
OK, so the bigger the coefficient for the data, the smoother the values you are going to read.

12:42.740 --> 12:43.490
But also this.

12:43.580 --> 12:46.340
The data will be updated a little bit late.

12:46.480 --> 12:52.640
OK, so if you need to be very reactive to what's happening in the environment, you don't want to just

12:52.640 --> 12:54.890
get the one of the three segments, OK?

12:54.890 --> 12:57.620
You want to get the warning as fast as you can.

12:57.650 --> 13:05.810
So you need to find a tradeoff between how smooth you want the data to be and how reactive you want

13:05.810 --> 13:09.030
to get the new available data.

13:09.380 --> 13:12.020
So you have two ways actually doing that.

13:12.020 --> 13:15.390
You can change the equation here and you can also increase the frequency.

13:15.410 --> 13:18.890
So if you read faster, this is going to be updated faster.

13:19.190 --> 13:22.570
OK, and instead of 100, I'm going to put 60.

13:22.850 --> 13:29.420
60 is the minimum value recommended by the manufacturer for this component.

13:29.780 --> 13:32.060
I'm going to go to 60 and not lower.

13:32.100 --> 13:36.050
OK, but with this, we are going to read the data first.

13:36.260 --> 13:42.490
And I'm going to use, let's see, 70 here and 30 percent.

13:43.280 --> 13:48.200
So every 60 milliseconds, I'm going to read the new data from the sensor, compute the distance and

13:48.200 --> 13:49.680
check that the distance is correct.

13:49.790 --> 13:54.040
If not, I wrote on the previous one and then apply a complementary filter.

13:54.480 --> 13:58.850
OK, 70 percent of the previous one, 30 percent of the new one.

13:59.750 --> 14:04.450
So let's applaud that and let's see what we get here.

14:05.420 --> 14:12.470
Let's open the CIA plotter and I'm going to put my hands in front.

14:13.350 --> 14:14.760
Of the SENSEOR.

14:16.170 --> 14:25.500
OK, and you can see now, OK, the data we read is much smoother and Andy here is actually going to

14:25.890 --> 14:29.420
be yellow now and I remove my hand and it's going to green.

14:29.760 --> 14:35.180
So it's already quite fast for us to measure the correct distance.

14:35.190 --> 14:36.930
I put my hand again yellow.

14:37.170 --> 14:38.980
I go closer and red.

14:39.270 --> 14:43.590
OK, so we have some accurate data, which is quite smooth.

14:43.700 --> 14:48.860
You can see on the cell plotter and it's also quite reactive.

14:49.560 --> 14:49.880
Great.

14:49.890 --> 14:56.400
So you have seen in this lesson how to actually fix the problem you have with the hardware by actually

14:56.400 --> 15:02.550
applying a few lines of software to filter the data you receive from the sensor.

15:02.900 --> 15:05.520
So this is, of course, a little bit more advanced.

15:05.940 --> 15:11.100
It's OK if you don't understand everything now, you might have to do some more research on the complementary

15:11.100 --> 15:16.080
filter and will know that we have our sensor working correctly.

15:16.770 --> 15:22.020
We can go to the next section and actually we will come back to DICENZO and we are going to use it a

15:22.020 --> 15:22.420
lot.

15:22.950 --> 15:25.340
Well, the final project of this class.
