Go Down

Topic: Reaction time to signals from sensors- Vacuum Cleaner project (Read 491 times) previous topic - next topic

Norbertoino

Hi,

I'm working on a Vacuum Cleaner where I'm using 6 ultrasonic sensors to observe environment in order to stop a robot and turn it to one or another direction before it hits in an obstacle. Issue is that if I use a lot of variables to keep data and signals from the sensors, loops, conditions, begins Arduino to work slowly and reactions time to signals to stop wheels is up to 2 seconds longer than normal. In this time robot drives 5 to 10 cm too far and hits in something. How can I optimize a program? Can I find some tips which show how to write a program properly or maybe you can share with me how do you tackle this?

My project looks like this now: https://youtu.be/jCjyf8KYqfs. This is only a basic tests now in this movie but I should maybe upload something that shows the issue..

If you have any suggestions just type it!

slipstick

You want us to help you optimise a program that you haven't shown us? That's going to be tricky.

But I suppose we can start with, if you have a lot of delays or other blocking code in it then get rid of them.

Steve

Norbertoino

You are right. I attach a TXT file with code.

I am aware of delays, I am using them but they should not affect reactions time- in my opinion but I am not an expert. This is a quite long code so I added a lot of comments.

slipstick

So is that the code that runs normally? Or the code that runs 2 seconds slower?

Either way you have lots of repeated code which is almost identical apart from things like the pins it uses. It really should be split up into functions, at least in part to make it a lot more readable. It would also be more readable if some of your comments described what the sensors were doing. I.e. rather than talking about "ultrasonic sensor 3"  say something like "Forward looking sensor".

Whenever you have a lot of variables with names ending with a number they should almost always be arrays. Then you could have a function which reads a sensor and returns or sets a distance and you can call it for each sensor when needed.

I'm not sure changes like that would make things much faster but the code would be a lot shorter and you'd have a better chance of seeing where any problems might be.

Steve

Norbertoino

This is the code that runs slower.

For example I noticed that if I change this:

"
   < code >
   ...
   float threshold_distance_sensor_0_degrees = 15; 
   ...
   < code >
   ...
   if(distance_3 <= threshold_distance_sensor_0_degrees) {  // ordinary obstacle avoiding condition. value = 15
...
   < code >
"
to this:
"
   < code >
   ...
   if(distance_3 <= 15)
   ...
   < code >

program runs faster. But is this possible that refering to a variable take more time than just typing of a value?


Agree with you that using of functions/arrays could make code more readable without accelerating execution. I will do this soon.

After I will be done with ultrasonic sensor I will add a Gyro and I am afraid that code execution will be even more slow. I am wondering if Arduino tackles poorly more complicated coding or if it is me who can not write an optimal code.

slipstick

Ah that's something I missed. I suspect the difference you're seeing is not because you're referencing variables but because you define your variables as floats. Doing calculations with float datatypes is a lot slower than integer calculations. So unless you really really need things like distance_3 and threshold_distance_sensor_0_degrees to be floats try changing them all to ints and see how fast that runs.

Steve

TomGeorge

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....

TomGeorge

Hi,
Are you aware that each time you use the delay function, you stop you code, it does nothing for the delay time, it executes no code.
So delay(1000);
Will stop your code for 1 Second, so if it is racing towards an abyss for 1 Second it is totally blind.
At full speed that is 3/4 of  metre.

The straight line test will only 100% pass if your have feedback from the speed of your motors, not all motors are created equal as you have found.

Tom.... :)
Everything runs on smoke, let the smoke out, it stops running....

Norbertoino

Hi,

Steve: You're definitely right! I changed all variabels to INTs and unnecessary delays disappeared. So actually code executes very fast.

Tom: My fault, I'll improve my forum skills.
        Usefull tips, good to be aware of those things. I am becoming aware of that I was using to many delays..
        Yes Tom, but unfortunately I don't have feedback from motors so I could just give different values on each motor while PWM control.

Thank you guys for professional help!
I am continuing to improve my robot and probably coming soon with new challenges.. :)

Norbert

Go Up