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)
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)
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.
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.
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…
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.
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.
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
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