Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve loop timing accuracy measure #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions controller/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,13 @@ def run(self):
# Check accuracy
lastEffectiveDuration = (datetime.datetime.now() - startTime).total_seconds() * 1000
lastWantedDuration = (endTime-startTime).total_seconds() * 1000
print('Cycle Accuracy: ')
print('\tLast cycle duration (real/target): ', round(lastEffectiveDuration, 2), '/', round(lastWantedDuration,2), 'ms')
print('\tError:', round(lastEffectiveDuration-lastWantedDuration, 2), 'ms' )

# 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


# Pause
if self.paused():
Expand All @@ -270,15 +274,7 @@ def run(self):
# check Stop
if not self.playing():
break

# ----

# 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


self.core.emit('playing-at', row['Stimulus'], index)

# Play Sample
Expand All @@ -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:
Expand All @@ -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():
Expand Down