From c8b904ce0baf0e33a72c3b7df4b8572d05581d05 Mon Sep 17 00:00:00 2001 From: Christopher Stone Date: Fri, 9 Mar 2018 22:00:11 +0000 Subject: [PATCH] Renamed serial polling code, and moved it to a separate module --- robots/little_john/telemetry/code/monitor/version1/getdata.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ robots/little_john/telemetry/code/monitor/version1/main.py | 42 +++++------------------------------------- 2 files changed, 52 insertions(+), 37 deletions(-) create mode 100644 robots/little_john/telemetry/code/monitor/version1/getdata.py diff --git a/robots/little_john/telemetry/code/monitor/version1/getdata.py b/robots/little_john/telemetry/code/monitor/version1/getdata.py new file mode 100644 index 0000000..6a18f29 --- /dev/null +++ b/robots/little_john/telemetry/code/monitor/version1/getdata.py @@ -0,0 +1,47 @@ +# Functions to process data coming in from a serial data stream +# Written as a telemetry tool by: +# The UoN Robot Wars Project, 2018 + +import logging +import os +import time +import serial +#from serialselect import selectserial + +def getData(starttime, datafeed, testseries, testseries2, fake=False): + """Check serial port for incoming data""" + # Note: 'elapsed' is time since last call of this function + # This works, but it might be better for performance to have two seperate functions, only one of which is run. + if fake: + timefromstart = (time.time()-starttime) + values = [timefromstart, 100*math.sin(timefromstart)] + #logging.info("Generated test data: " + str(values)) + testseries.addpoint(values) + testseries2.addpoint(values) + else: + try: + incoming = datafeed.readline() + except: + logging.error("Failed to read input data") + + try: + if os=='Windows': + values = incoming.strip().split(b", ") + else: + values = incoming.strip() + values = values.split(b', ') + except: + logging.error("Failed to parse input data") + return + + try: + for n, value in enumerate(values): + values[n] = float(value) + except: + logging.error("Failed to convert input to float") + return + #logging.info("Recieved data: " + str(values)) + testseries.addpoint([time.time()-starttime] + values) + testseries2.addpoint(values) + + diff --git a/robots/little_john/telemetry/code/monitor/version1/main.py b/robots/little_john/telemetry/code/monitor/version1/main.py index cbb96c8..5e4034a 100755 --- a/robots/little_john/telemetry/code/monitor/version1/main.py +++ b/robots/little_john/telemetry/code/monitor/version1/main.py @@ -23,6 +23,7 @@ from serialselect import selectserial from colours import * from series import Series from plot import Plot +from getdata import getData logging.basicConfig(format='%(levelname)s:\t%(message)s', level=logging.DEBUG) logging.info("Logging system active") @@ -50,45 +51,12 @@ plots = [] plots.append(Plot(testseries)) plots.append(Plot(testseries2)) +# Yes I know these are absurd placeholder names. I'm leaving it like that for the moment because I'm not sure if this is really the only (or best) way to achive the desired result. clock.schedule_interval takes a function, not the result of the function as its argument, so how can you pass arguments to it? -def pollSerial(elapsed): - """Check serial port for incoming data""" - # Note: 'elapsed' is time since last call of this function - # This works, but it might be better for performance to have two seperate functions, only one of which is run. - try: - incoming = datafeed.readline() - except: - logging.error("Failed to read input data") - - try: - if os=='Windows': - values = incoming.strip().split(b", ") - else: - values = incoming.strip() - values = values.split(b', ') - except: - logging.error("Failed to parse input data") - return - - try: - for n, value in enumerate(values): - values[n] = float(value) - except: - logging.error("Failed to convert input to float") - return - #logging.info("Recieved data: " + str(values)) - testseries.addpoint([time.time()-starttime] + values) - testseries2.addpoint(values) - - -def fakePollSerial(elapsed): - """This function immitates the behaviour of pollSerial, for testing purposes""" - timefromstart = (time.time()-starttime) - values = [timefromstart, 100*math.sin(timefromstart)] - #logging.info("Generated test data: " + str(values)) - testseries.addpoint(values) +def shrubbery(ni): + getData(starttime, datafeed, testseries, testseries2) # Pyglet looks after the main event loop, but this ensures that data keeps being read in -pyglet.clock.schedule_interval(pollSerial, 0.01) +pyglet.clock.schedule_interval(shrubbery, 0.01) pyglet.app.run() -- libgit2 0.21.2