Commit c8b904ce0baf0e33a72c3b7df4b8572d05581d05

Authored by Christopher Stone
1 parent 2bd76084
Exists in master

Renamed serial polling code, and moved it to a separate module

robots/little_john/telemetry/code/monitor/version1/getdata.py 0 → 100644
... ... @@ -0,0 +1,47 @@
  1 +# Functions to process data coming in from a serial data stream
  2 +# Written as a telemetry tool by:
  3 +# The UoN Robot Wars Project, 2018
  4 +
  5 +import logging
  6 +import os
  7 +import time
  8 +import serial
  9 +#from serialselect import selectserial
  10 +
  11 +def getData(starttime, datafeed, testseries, testseries2, fake=False):
  12 + """Check serial port for incoming data"""
  13 + # Note: 'elapsed' is time since last call of this function
  14 + # This works, but it might be better for performance to have two seperate functions, only one of which is run.
  15 + if fake:
  16 + timefromstart = (time.time()-starttime)
  17 + values = [timefromstart, 100*math.sin(timefromstart)]
  18 + #logging.info("Generated test data: " + str(values))
  19 + testseries.addpoint(values)
  20 + testseries2.addpoint(values)
  21 + else:
  22 + try:
  23 + incoming = datafeed.readline()
  24 + except:
  25 + logging.error("Failed to read input data")
  26 +
  27 + try:
  28 + if os=='Windows':
  29 + values = incoming.strip().split(b", ")
  30 + else:
  31 + values = incoming.strip()
  32 + values = values.split(b', ')
  33 + except:
  34 + logging.error("Failed to parse input data")
  35 + return
  36 +
  37 + try:
  38 + for n, value in enumerate(values):
  39 + values[n] = float(value)
  40 + except:
  41 + logging.error("Failed to convert input to float")
  42 + return
  43 + #logging.info("Recieved data: " + str(values))
  44 + testseries.addpoint([time.time()-starttime] + values)
  45 + testseries2.addpoint(values)
  46 +
  47 +
... ...
robots/little_john/telemetry/code/monitor/version1/main.py
... ... @@ -23,6 +23,7 @@ from serialselect import selectserial
23 23 from colours import *
24 24 from series import Series
25 25 from plot import Plot
  26 +from getdata import getData
26 27  
27 28 logging.basicConfig(format='%(levelname)s:\t%(message)s', level=logging.DEBUG)
28 29 logging.info("Logging system active")
... ... @@ -50,45 +51,12 @@ plots = []
50 51 plots.append(Plot(testseries))
51 52 plots.append(Plot(testseries2))
52 53  
  54 +# 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?
53 55  
54   -def pollSerial(elapsed):
55   - """Check serial port for incoming data"""
56   - # Note: 'elapsed' is time since last call of this function
57   - # This works, but it might be better for performance to have two seperate functions, only one of which is run.
58   - try:
59   - incoming = datafeed.readline()
60   - except:
61   - logging.error("Failed to read input data")
62   -
63   - try:
64   - if os=='Windows':
65   - values = incoming.strip().split(b", ")
66   - else:
67   - values = incoming.strip()
68   - values = values.split(b', ')
69   - except:
70   - logging.error("Failed to parse input data")
71   - return
72   -
73   - try:
74   - for n, value in enumerate(values):
75   - values[n] = float(value)
76   - except:
77   - logging.error("Failed to convert input to float")
78   - return
79   - #logging.info("Recieved data: " + str(values))
80   - testseries.addpoint([time.time()-starttime] + values)
81   - testseries2.addpoint(values)
82   -
83   -
84   -def fakePollSerial(elapsed):
85   - """This function immitates the behaviour of pollSerial, for testing purposes"""
86   - timefromstart = (time.time()-starttime)
87   - values = [timefromstart, 100*math.sin(timefromstart)]
88   - #logging.info("Generated test data: " + str(values))
89   - testseries.addpoint(values)
  56 +def shrubbery(ni):
  57 + getData(starttime, datafeed, testseries, testseries2)
90 58  
91 59 # Pyglet looks after the main event loop, but this ensures that data keeps being read in
92   -pyglet.clock.schedule_interval(pollSerial, 0.01)
  60 +pyglet.clock.schedule_interval(shrubbery, 0.01)
93 61  
94 62 pyglet.app.run()
... ...