In this additional lesson on the section on ultrasonics, Senso, I'm going to show you how you can
improve the behavior of your ultrasonic sensor with the software.
OK, if you have an erratic behavior, maybe your sensor is working just fine.
So that's not a problem.
Maybe you're using the simulation in that case.
Well, simulation is always working, but if you have the same problem as me, as you could see in the
previous lesson, then it is good to understand maybe why you have this problem or get some ideas of
why and then be able to fix this in the code.
So this kind of issue is actually something you have to be prepared to have a lot when you work with
hardware.
OK, because real life and the real world always contains parts of unknown.
And this is true for everything, especially hardware.
OK, it's not because it's working fine in simulation that is going to work fine in real life.
And it's also not because it's working fine in real life, once or twice that every time you do the
same setup, it's going to work fine also.
Right.
That's one of the main issue with hardware.
It's not really an issue.
It's just how it works.
So you have to be prepared to face that kind of issue in many of your project.
But that's a normal part of any project.
So if you have a problem with how do it, you can either fix the hardware and sometimes you can also
try to create a fix with software.
OK, so here I'm going to talk about both and not that it's also a little bit more advanced, OK?
This is, again, not a big you.
Listen, this is more of an intermediate listen.
So what could go wrong with the hardware?
First, I have shown you that maybe the connections on your breadboard and your arduino are not correct.
So make sure you check that 100 percent, maybe change some wires, change some connectors, connect
the sensor to a different place to see if you can solve the issue.
OK, if it doesn't work well, maybe it's simply because you have the bad quality sensor.
And actually, if you have a starter kit like the starter kit that I have that I'm using right now,
if you have both the cheapest sensor you can find when the thing is that it depends a lot on the manufacturer
of the hardware component.
OK, if you buy the cheapest one, you can maybe expect that disordering of the components on the sensor
is not well made, that maybe some parts that needs to be aligned are not completely aligned.
OK, that's the firmware running on the sensor is maybe not a good quality one either and many different
things.
OK, that can go wrong.
So basically when you buy cheap hardware, well, don't expect it to work 100 percent of the time.
So if you have the chance to get another ultrasonic sensor, maybe it's better to try the second one
if you have one.
And in the future, whenever you want to buy hardware component, if it's very, very, very cheap,
I advise you maybe to buy two or three.
OK, so in case you have one that is not working correctly, you can try the other one.
So if you think it is working well and your component is a bad quality, well, the only option you
have is to change.
The components are also maybe to try to make a fix from the software, which we are going to do here,
and also a third cause that maybe is simply the environment.
OK, where are you operating the sensor?
Because DICENZO is actually sending ultrasonic waves in the environment.
So for my specific example, I am actually in a winning recording here.
I am in a small room with a lot of glass walls and a lot of stuff in the wall.
So maybe the ultrasonic waves are going to bounce everywhere and come back to the ultrasonic sensor,
which will read wrong data from time to time.
OK, you may want to try in a bigger environment, for example, in the outside or just in a bigger
room to try to see if you have the same problem.
Right.
So now you have a few of the reasons that maybe it's not working.
If it's not working all right, if it's working correctly, you don't need to worry about it.
But now what can you do if you are in the same situation as me?
So you have correctly checked your how do I set up?
Well, you just have one sensor so you can change it and you can change on the.
And so I'm doing this on purpose so I can show you how to fix hardware with software.
You can modify the software a little bit.
And that's what we are going to do when we get the ultrasonic distance here.
So we get the duration in Microcircuit of the Pulse and then we compute the distance.
I'm going to apply a few filters here so we can regulate a little bit the values from the software.
So this is not, of course, a one hundred percent solution to the problem.
OK, but this is a nice fix that will allow us to continue and progress with this sensor and the circuit.
So.
What I'm going to do is I'm going to upload these programs activity 15, OK, where we put on a different
energy from the distance we get.
And also because here we print.
OK, listen, make sure you have the same print along with distance.
So I'm going to I can open the science monitor and you can see the different values we get.
OK.
So we have some values and you can see we have some weird values from time to time to get a better visualization
here.
I'm going to open the cell clutter, OK?
And let's see, what do we get?
So here you can see I am in 140, OK, this is the wall behind me.
I'm going to put my hand and do some movement in front of the sensor.
And you can see we OK, first thing we see is that we have some values that go way too high.
OK, from time to time and the maximum value we could get here is 400 centimeters, OK, because this
is the max range for the sensor.
But you can see we get the value up to more than 3000 centimeters, which is well, which is definitely
not correct.
So the first thing maybe we can do is that anytime we get a value that is above four meters or 400 centimeters,
well, we just don't take this value into account.
So to do that, I'm going to create a new viable here that I'm going to call double previous.
Distance and let's initialize it to 400.
OK, and I'm going to go here, so I'm reading the duration and computing the distance, and I'm going
to filter I'm going to add some filters here for the distance.
Let's see if.
Distance is greater.
And 400.
Then what I'm going to do is I'm not going to take this into account, I'm simply going to return to
their previous distance.
And if the distance is lower than 400, so if the distance is correct, I'm going to the previous distance
is equal to the distance and then return the distance.
So what this will do, let's let's make an example.
So first, we have a pretty decent 400.
We just initialized with a venue.
So let's say we have a correct value here.
So we get this done.
Let's say one hundred forty.
The distance is lower than four hundred.
So now we said the previous distance, which become the distance to the previous distance, becomes
one hundred and forty.
OK, then we return one hundred and forty.
The next thing we read, let's say we have a with the disease like three thousand so we get three thousand.
Here we check that.
OK, three thousand is actually greater than 400.
So instead of using this value, we just return the previous one, the previous one, which is now one
hundred forty.
So let's upload this program here.
And let's open the CIA again and you will see, OK, I get 140, you see, I still get many variations
in the volume, OK, that I never get any value that is above one hundred forty or any value that is
above actually 400, OK, because 400 is the maximum the cap for the sensor.
So I can be sure by using this filter that I don't get any value above four hundred.
So that's already a good first step.
So first, eliminate any value that is outside of the range of the sensor.
And no, I'm going to do another thing and this is called a complementary filter.
What is a complementary filter?
So a complementary filter.
I'm not going to go into too many details here if you want to check that outside of the code.
OK, this is a mathematical filter that we are going to use.
And this is very often used to smooth out some data that you read from the sensor.
So the complementary filter is actually quite simple, is instead of just returning the new distance,
OK, we are going to return an average of the previous distance and a new distance.
OK, let's let's actually write the code and then give some explanation to the new distance.
We become previous distance, let's say, multiplied by zero point five.
Let's.
Distance multiplied by zero point five, but we can't the distance here.
OK, we have the previous distance from the previous run of that function and then we do.
So this is an example with zero point five and zero point five.
So the distance becomes so the distance we have just measured.
We take half of this value and half of the previous one.
So to make a complementary filter, you simply have to store the previous value.
So you have to, of course, the data at a certain frequency.
So he attempts, OK, and so you get you still the previous distance to the previous data.
And then when you get the new data, you make a mix of the previous data and the new data.
So, for example, if you have just one spike, which is very high, this spike would actually be smoothed
and you will not see so much weird behavior.
So we can use the distance with zero point five and distance with zero point five.
The actual important thing is that this and this equals to one.
OK, so we could use, for example, the zero point seven and zero point three here and the bigger the
number for the previous data.
OK, the bigger the resistance to change.
And actually, let's mix some examples.
OK, I'm going to go with zero point five and zero point five.
Let's upload that program so you can see just one additional line.
And I'm going to happen to say a lot, and you can see so we still have some weird variations, OK,
but not not that much.
It's actually much better if you can see that the.
Signal is a little bit smoother.
All right, and now let's add, let's say here on nine and point one, which means that whenever we
get to a new distance, we are going to keep 90 percent of the previous distance and only add 10 percent
of the new distance.
So he that's applaud.
You will see that he's very smooth, but also in shape, put my hand here.
It's very smooth, but also very low key to detect changes.
I remove my hand.
You can see it really takes some time to go through here to 140.
I just put my hand again now and you can see it's really low, really slow, but it's much smoother.
So now you have the formula for the complementary filter.
And what you need to tinker is basically those two diameters.
So the percentage, 90 percent, 10 percent here, make sure that the total is one hundred percent on
just one.
OK, so the bigger the coefficient for the data, the smoother the values you are going to read.
But also this.
The data will be updated a little bit late.
OK, so if you need to be very reactive to what's happening in the environment, you don't want to just
get the one of the three segments, OK?
You want to get the warning as fast as you can.
So you need to find a tradeoff between how smooth you want the data to be and how reactive you want
to get the new available data.
So you have two ways actually doing that.
You can change the equation here and you can also increase the frequency.
So if you read faster, this is going to be updated faster.
OK, and instead of 100, I'm going to put 60.
60 is the minimum value recommended by the manufacturer for this component.
I'm going to go to 60 and not lower.
OK, but with this, we are going to read the data first.
And I'm going to use, let's see, 70 here and 30 percent.
So every 60 milliseconds, I'm going to read the new data from the sensor, compute the distance and
check that the distance is correct.
If not, I wrote on the previous one and then apply a complementary filter.
OK, 70 percent of the previous one, 30 percent of the new one.
So let's applaud that and let's see what we get here.
Let's open the CIA plotter and I'm going to put my hands in front.
Of the SENSEOR.
OK, and you can see now, OK, the data we read is much smoother and Andy here is actually going to
be yellow now and I remove my hand and it's going to green.
So it's already quite fast for us to measure the correct distance.
I put my hand again yellow.
I go closer and red.
OK, so we have some accurate data, which is quite smooth.
You can see on the cell plotter and it's also quite reactive.
Great.
So you have seen in this lesson how to actually fix the problem you have with the hardware by actually
applying a few lines of software to filter the data you receive from the sensor.
So this is, of course, a little bit more advanced.
It's OK if you don't understand everything now, you might have to do some more research on the complementary
filter and will know that we have our sensor working correctly.
We can go to the next section and actually we will come back to DICENZO and we are going to use it a
lot.
Well, the final project of this class.