From baa85c075dcb7aec5c62a4c8102a548795439f13 Mon Sep 17 00:00:00 2001 From: Lx37 Date: Mon, 25 Jul 2022 15:09:47 +0200 Subject: [PATCH 1/2] improve timming accuracy measure --- controller/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controller/core.py b/controller/core.py index a45811a..51946dc 100644 --- a/controller/core.py +++ b/controller/core.py @@ -257,6 +257,7 @@ def run(self): # Check accuracy lastEffectiveDuration = (datetime.datetime.now() - startTime).total_seconds() * 1000 lastWantedDuration = (endTime-startTime).total_seconds() * 1000 + startTime = datetime.datetime.now() print('Cycle Accuracy: ') print('\tLast cycle duration (real/target): ', round(lastEffectiveDuration, 2), '/', round(lastWantedDuration,2), 'ms') print('\tError:', round(lastEffectiveDuration-lastWantedDuration, 2), 'ms' ) @@ -274,7 +275,6 @@ def run(self): # ---- # Start next sample timing - startTime = datetime.datetime.now() audioTime = startTime + datetime.timedelta(milliseconds= audio_duration*1000) # end of audio: time to turn off GPIO stopTime = audioTime + datetime.timedelta(milliseconds= isi_duration*1000-10) # end of audio+ISI-10ms: stop audio stream endTime = audioTime + datetime.timedelta(milliseconds= isi_duration*1000) # end of audio+ISI: start next sample From 9dc556704b01088cadd303abb362c5dcc1d456d7 Mon Sep 17 00:00:00 2001 From: Lx37 Date: Thu, 28 Jul 2022 16:23:03 +0200 Subject: [PATCH 2/2] move the print to improve timing accuracy and join all timing reset together --- controller/core.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/controller/core.py b/controller/core.py index 51946dc..bea4557 100644 --- a/controller/core.py +++ b/controller/core.py @@ -257,10 +257,13 @@ def run(self): # Check accuracy lastEffectiveDuration = (datetime.datetime.now() - startTime).total_seconds() * 1000 lastWantedDuration = (endTime-startTime).total_seconds() * 1000 + + # Start next sample timing startTime = datetime.datetime.now() - print('Cycle Accuracy: ') - print('\tLast cycle duration (real/target): ', round(lastEffectiveDuration, 2), '/', round(lastWantedDuration,2), 'ms') - print('\tError:', round(lastEffectiveDuration-lastWantedDuration, 2), 'ms' ) + audioTime = startTime + datetime.timedelta(milliseconds= audio_duration*1000) # end of audio: time to turn off GPIO + stopTime = audioTime + datetime.timedelta(milliseconds= isi_duration*1000-10) # end of audio+ISI-10ms: stop audio stream + endTime = audioTime + datetime.timedelta(milliseconds= isi_duration*1000) # end of audio+ISI: start next sample + # Pause if self.paused(): @@ -271,14 +274,7 @@ def run(self): # check Stop if not self.playing(): break - - # ---- - - # Start next sample timing - audioTime = startTime + datetime.timedelta(milliseconds= audio_duration*1000) # end of audio: time to turn off GPIO - stopTime = audioTime + datetime.timedelta(milliseconds= isi_duration*1000-10) # end of audio+ISI-10ms: stop audio stream - endTime = audioTime + datetime.timedelta(milliseconds= isi_duration*1000) # end of audio+ISI: start next sample - + self.core.emit('playing-at', row['Stimulus'], index) # Play Sample @@ -295,10 +291,12 @@ def run(self): # Wait for audio duration : stream.write() might return before audio buffer is completely flushed self.wait(audioTime, 'audio') + + # UnTrigger GPIO if is_RPI(): - GPIO.output(GPIO_trigOn, 0) + GPIO.output(GPIO_trigOn, 0) except sd.PortAudioError: if self._playing: @@ -310,6 +308,11 @@ def run(self): self._playing = False break + # Print cycle accuracy + print('Cycle Accuracy: ') + print('\tLast cycle duration (real/target): ', round(lastEffectiveDuration, 2), '/', round(lastWantedDuration,2), 'ms') + print('\tError:', round(lastEffectiveDuration-lastWantedDuration, 2), 'ms' ) + # Still playing: let last sample/isi terminate if self.playing():