Distance Sensor Inconsistency

My Distance Sensor only works some of the time. When it stops working, I get the following error message:

IOError: Distance Sensor not found

The following is the code that I used both when the Distance Sensor worked and when it did not:


import gopigo3
import easygopigo3 as easy

gpg = easy,EasyGoPiGo3()
distance_sensor = easy.DistanceSensor(gpg=gpg)

while True:
distance = distance_sensor.read_mm()
print (distance)


Any help would be greatly appreciated. Thank you.

1 Like

Hello @carteri246
In order to help you better, can you tell us which OS (Raspbian for Robots, DexterOS, or a clean Raspbian from the Foundation?)

I’m going to assume you manually entered the code in here because there’s a typo that stops the code from running (but wouldn’t be responsible for the behaviour you’re getting)

I am taking a look at the library and it seems we are no longer returning exceptions. In case there’s a temporary issue, we return a value of 0. If you take a look at the documentation, there are no exceptions being returned anymore during a read. This makes me think you may not have the latest version of the library. How to update it depends on your OS.

Another approach would be for you to try the following code:

import gopigo3
import easygopigo3 as easy
import time

gpg = easy.EasyGoPiGo3()
distance_sensor = easy.DistanceSensor(gpg=gpg)

while True:
        try:
                distance = distance_sensor.read_mm()
                print (distance)
        except:
                pass
        time.sleep(0.3)

This code would skip a reading once in a while but it wouldn’t die. I’ve added a sleep delay so that it doesn’t loop too quickly. You can adjust that delay as you wish (0.05 would work too)

Cleo

Hi Cleo,

My OS is Raspbian for Robots.

I tried the code you suggested above. It worked for a few minutes and then I got the same error (IOError: Distance Sensor not found).

Could you tell me how to update the library?

Thank you.

Hello,

I’m still having trouble with this issue. Could I please get some assistance?

Thank you

My apologies @carteri246,
I was out for a few days with a health issue. I am back now. Sorry for leaving you with no answers.

Can you detail a bit what’s happening?
Are you running the program I gave you continuously and then it crashes? Or does it crash from one run to another?

To upgrade your Raspbian for Robots, you will find on the desktop a DI Update icon. Click on it (it takes a while to come up as it’s getting ready) and update the software (there is no need to update the robot itself)

Cleo

Hi Cleo,

No worries. I hope you are feeling better.
My software is updated and I’m getting a little closer to understanding the problem.

If the time.sleep() is too short (e.g. time.sleep(0.03)), the distance sensor loops too quickly and I get a temporary issue which returns “0”. After that, once I try to run the program again with a slower loop, I get the IOError. The only way I know how to resolve it is by disconnecting the robot for awhile. When I connect again, everything is fine until it runs into another issue that returns “0”.

Is there a code that would make sure it won’t loop too fast? Eventually, I would like to run the distance sensor in the background without returning any numbers in the terminal. I’m unsure if I would still run in to the same issue.

Thank you

1 Like

Hello @carteri246
I’ve tried the code again and the only way I can duplicate your issue is if my distance sensor is connected to port AD1 or AD2.
I cannot replicate it in an I2C port.

Out of curiosity, what port are you using?

Cleo

Hi Cleo,

I am using one of the I2C ports.

Thanks

Hey @carteri246
Sorry I dropped the ball on this one. Where do you stand now?

There’s going to be a major update to the supporting libraries related to the distance sensor in a couple of weeks. Your code is not going to change but the underlying code will. In the meantime, are you okay with putting delays in there?

No problem, Cleo. I think I may have solved the initial problem. I am having a lot more success now but I’m still having a little inconsistency with the distance sensor…

To solve the original problem, I first updated the GoPiGo3 repository with the following command: sudo curl https://raw.githubusercontent.com/DexterInd/Raspbian_For_Robots/master/upd_script/fetch_gopigo3.sh | bash

Then I updated the software and firmware.

Now I am able to loop the distance sensor very quickly without getting the IOError. However, now the distance sensor sometimes causes my programs not to work at all. Like the last issue, this seem to happen out of the blue. It works fine one minute and not the next. There is no error message this time. The program just keeps trying to execute until it has to be manually interrupted. Even just trying to instantiate the distance sensor (i.e. init_distance_sensor) in the Python 3.4.2 Shell causes the program to continually try to run without ever finishing and returning the “>>>” to input more code.

Do you have any ideas about this new problem?

Thank you

Oh, wait. Just got another IOError. This one is a little different:

IOError: [Ernno 5] Input/output error

Similarly you need to update the sensor code
sudo curl https://raw.githubusercontent.com/DexterInd/DI_Sensors/master/Install/update_sensors.sh | bash

Hopefully this will work now
Cleo

Hi Cleo,

Sorry for the delayed response. I think I am okay to go now. The distance sensor is still a little glitchy (i.e. if I loop too fast, the output stops or slows way down (w/o changing time.sleep) on the successive program runs). It works way better than when I first started however. I look forward to the future updates.

Thank you!

Hello @carteri246

That’s good to hear! If you need faster sampling, the continuous reading mode is probably what you would need - it is not exposed in the easygopigo3 library but still accessible. Take a look at the example

Cleo

2 Likes

Hi @cleoqc!

I’m having the same issue with the distance sensor as @carteri246 on my GoPiGo2.

I loop the reading of a distance sensor without a delay using the module easygopigo and after some time the distance sensor starts returning 0. The only thing that helps is to switch off GoPiGo and wait for 5-10 minutes. Then the sensor starts working again but the issue comes back after awhile. Is this some hardware issue?

Thanks for bringing up the continuous reading mode. It’s quite cool and I’ve been looking for it for some time. Even started another topic on this forum about this challenge :slight_smile: