Introducing Latency in Python

distancesensor
disensors

#1

My GPG3 has a servo and a distance sensor. In testing these devices, I’ve been comparing the Bloxter code with the corresponding Bloxter-generated Python code, and I notice that you (i.e., Dexter Bloxter engineers) have introduced some deliberate latency into the Python code.

Specifically, after the distance sensor is initialized, you insert a 100 ms delay (“time.sleep(0.1)”). However, after the servo is initialized, no delay is inserted.

My Bloxter test code contains a continuous loop (“while my_Distance_portI2C.read_inches() < 9:”). Outside this loop, and before the loop is iterated, you insert a 50-ms delay (“time.sleep(0.05)”).

From these observations, I infer some rules for the would-be Python coder of a GPG3:

  • No wait is necessary after initializing a servo

  • Wait 100 ms after initializing the distance sensor before proceeding with the next step.

  • Wait 50 ms between queries to the distance sensor

I assume you’ve developed these, and other, rules empirically, and they’re necessary to make the GPG3 operate correctly. Have you codified them anywhere for the budding Python coder of the GPG3?


#2

Hello @WayneM
Some of these delays are introduced specifically due to the Bloxter environment and are not required with pure Python.
There’s an automatic delay in any loop if I remember correctly (I don’t have the code in front of me) in order to not send the web server that serves Bloxter into a death spiral, for example. Generally speaking, it’s not good practice to loop and constantly poll a sensor, it’s best to introduce a little delay in the loop.

These sleep statements were all introduced before we started exposing the python code.

Your best bet in knowing if delays are required for these sensors is to look at their documentation on ReadTheDocs. , more specifically at the examples and tutorials.

Cleo