Skip to content

Commit a738c1d

Browse files
authored
299 m add delay countdown (#126)
* driver's wait() uses time.sleep and checks threading Events to exit early * adds traceable to delay * using timestamp to track delay progress * adds test * modifies test_wait to expect delay within +/-0.1 * removed countdown notification, replaces with just start and finished notifications * adds countdown back to notifications
1 parent bffacd8 commit a738c1d

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

opentrons/drivers/motor.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from opentrons.util.log import get_logger
1414
from opentrons.util.vector import Vector
15+
from opentrons.drivers.virtual_smoothie import VirtualSmoothie
1516

1617
from opentrons.util import trace
1718

@@ -500,12 +501,25 @@ def home(self, *axis):
500501
else:
501502
return False
502503

503-
def wait(self, sec):
504-
ms = int((sec % 1.0) * 1000)
505-
s = int(sec)
506-
self.check_paused_stopped()
507-
res = self.send_command(self.DWELL, S=s, P=ms)
508-
return res == b'ok'
504+
def wait(self, delay_time):
505+
start_time = time.time()
506+
end_time = start_time + delay_time
507+
arguments = {'name': 'delay-start', 'time': delay_time}
508+
trace.EventBroker.get_instance().notify(arguments)
509+
if not isinstance(self.connection, VirtualSmoothie):
510+
while time.time() + 1.0 < end_time:
511+
self.check_paused_stopped()
512+
time.sleep(1)
513+
arguments = {
514+
'name': 'countdown',
515+
'countdown': int(end_time - time.time())
516+
}
517+
trace.EventBroker.get_instance().notify(arguments)
518+
remaining_time = end_time - time.time()
519+
time.sleep(max(0, remaining_time))
520+
arguments = {'name': 'delay-finish'}
521+
trace.EventBroker.get_instance().notify(arguments)
522+
return True
509523

510524
def calm_down(self):
511525
res = self.send_command(self.CALM_DOWN)

tests/opentrons/drivers/test_motor.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import time
12
from threading import Thread
23
import unittest
34

@@ -236,7 +237,19 @@ def test_send_command_with_kwargs(self):
236237
self.assertTrue(success)
237238

238239
def test_wait(self):
240+
# set connection to be something other than VirtualSmoothie
241+
self.motor.connection = int()
242+
start_time = time.time()
239243
success = self.motor.wait(1.234)
244+
end_time = time.time()
245+
self.assertAlmostEquals(end_time - start_time, 1.234, places=1)
246+
self.assertTrue(success)
247+
248+
self.motor.connection = int()
249+
start_time = time.time()
250+
success = self.motor.wait(1.0)
251+
end_time = time.time()
252+
self.assertAlmostEquals(end_time - start_time, 1.0, places=1)
240253
self.assertTrue(success)
241254

242255
def test_wait_for_arrival(self):

0 commit comments

Comments
 (0)