aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormarcusw2011-02-24 21:01:49 +0000
committermarcusw2011-02-24 21:01:49 +0000
commit02b4a6f1e99c3241f543e52bbd750b754f9a8c83 (patch)
treefed39df771c67809c67fb6d45ecb5ffd668f716f
parentb23ed7ccffd22ff921d24b4fc7cc6601ec5548da (diff)
Fixed race condition in the digital sensor and motcont timing code which causes a negative value to be passed to time.sleep() every few thousand calls. This one took more testing to find than any other bug I've ever discovered.
-rw-r--r--nxt/motcont.py5
-rw-r--r--nxt/sensor/digital.py10
2 files changed, 9 insertions, 6 deletions
diff --git a/nxt/motcont.py b/nxt/motcont.py
index a0ba9d9..8249337 100644
--- a/nxt/motcont.py
+++ b/nxt/motcont.py
@@ -32,8 +32,9 @@ def _tacho(tacholimit):
return tacho
def interval(delay, lastrun):
- if lastrun+delay > time.time():
- diff = time.time() - lastrun
+ now = time.time()
+ if lastrun+delay > now:
+ diff = now - lastrun
time.sleep(0.010 - diff)
class MotCont():
diff --git a/nxt/sensor/digital.py b/nxt/sensor/digital.py
index dbc730f..a2dafba 100644
--- a/nxt/sensor/digital.py
+++ b/nxt/sensor/digital.py
@@ -96,8 +96,9 @@ suppressed by passing "check_compatible=False" when creating the sensor object."
"""
value = struct.pack(format, *value)
msg = chr(self.I2C_DEV) + chr(address) + value
- if self.last_poll+self.poll_delay > time():
- diff = time() - self.last_poll
+ now = time()
+ if self.last_poll+self.poll_delay > now:
+ diff = now - self.last_poll
sleep(self.poll_delay - diff)
self.last_poll = time()
self.brick.ls_write(self.port, msg, 0)
@@ -109,8 +110,9 @@ suppressed by passing "check_compatible=False" when creating the sensor object."
"""
n_bytes = struct.calcsize(format)
msg = chr(self.I2C_DEV) + chr(address)
- if self.last_poll+self.poll_delay > time():
- diff = time() - self.last_poll
+ now = time()
+ if self.last_poll+self.poll_delay > now:
+ diff = now - self.last_poll
sleep(self.poll_delay - diff)
self.last_poll = time()
self.brick.ls_write(self.port, msg, n_bytes)