def __init__(self): """ On initialisation the Bluetooth class retrieves the current Temperature and Humidity from the DataLogger class and sends a notification via Pushbullet with a message stating that a Bluetooth connection has been made aswell as the current Temperature and Humidity. """ #declare variables that handle interactions with other classes self.__bluetooth = BluetoothManager() self.__dataLogger = DataLogger() #declare variables that store the current temperature and humidity to 1 decimal point self.__currentTemperature = round(self.__dataLogger.getTemperature(), 1) self.__currentHumidity = round(self.__dataLogger.getHumidity(), 1) # using the bluetoothManager initialisation to create a message for the current temperature and humidity self.__bluetooth.createMessage(self.__currentTemperature, self.__currentHumidity) # self.__deviceName = input("Please enter your device' name: ") #Hardcode the device that bluetooth will search for self.__deviceName = "Yonas" #search for the device that needs to be connected once found print on cmd line that it is, if not found continue searching self.__bluetooth.connectToNearByDevice(self.__deviceName) #once that device has been connected send a pushbullet notifcation with the message self.__bluetooth.sendMessage()
def main(): state = { "should_exit": False, "is_manual_mode": True, "is_cruise_mode": False, } dm, js, cam = hardware_setup() ws_server = start_ws() dl = DataLogger(LOG_PATH + "/data.csv") logging.info("All services up and running") last_ws_ts = time.time() last_sync_ts = time.time() while not state["should_exit"]: event = js.get_event() # dispatch control command if state["is_manual_mode"]: if state["is_cruise_mode"]: if "direction" in event: event["direction"] = state["direction"] if "throttle" in event: event["throttle"] = state["throttle"] dm.batch_update(event) else: ai_command = fetch_ai_command() state.update(ai_command) event.update(ai_command) dm.batch_update(ai_command) state.update(event) if state["should_exit"]: continue ts = time.time() # broadcast state/event to client if ts - last_sync_ts > 0.5: # sync state every 0.5s state.update(dm.get_state()) ws_server.broadcast(state) last_sync_ts = time.time() if ts - last_ws_ts > 0.1: # push delta only every 0.1s if there is any event.update(dm.get_event()) if len(event) > 0: ws_server.broadcast(event) last_ws_ts = time.time() #logging image_file_name = ('%.6f' % ts) + ".jpg" state.update({"timestamp": ts, "image_file_name": image_file_name}) cam.capture_and_mark(image_file_name) dl.write(state)
def __init__(self, config, log_market=False): self.config = config self.log = logging.getLogger('DataFeed') self.addr = str(config['Subscriptions']['Socket']) self.pairs = [str(p) for p in config['Subscriptions']['Symbols']] self.channels = [ str(c) for c in config['Subscriptions']['Channels'].keys() if not str(c).startswith('--') ] self.subbed_messages = [ str(m) for m in config['Subscriptions']['Messages'] ] for ch in self.channels: for msg in config['Subscriptions']['Channels'][ch]: self.subbed_messages.append(str(msg)) self.dataLogger = DataLogger(self.pairs, self.subbed_messages, log_market) self._endpoint = ws.create_connection(str(self.addr), max_size=None) self._msgQ = Queue() self._handleThread = Thread(\ target = self._handle_q,\ name = 'Message_Handler_Thread') self._handleThread.daemon = True self._listenThread = Thread(\ target = self._recvLoop,\ name = 'Listener_Thread') self._listenThread.daemon = True self._isRunning = False self.pendingException = None self.log.debug('Initialized DataFeed!')
def __init__(self): """ On initialisation the MonitorAndNotify class reads the current data and inserts into the datbase every minute and only sends a notification once a day """ # Scheduler = Schedule() # Instantiate all relevant classes data = DataLogger() dataManager = DatabaseManager(data) monitor = Monitor() weather = Weather(data, dataManager, monitor) sense = VirtualSenseHat.getSenseHat() # Scheduler.createSchedule() # verify if the temperature is within the range weather.verifyTemperature() # verify if the humidity is within the range weather.verifyHumidity() # close the connection to the database dataManager.closeDBConnection() # clear anything displayed on the sense hat sense.clear()
def __init__(self, **kwargs): Thread.__init__(self) # Serial thread self.serialthread = SerialPort() # Controller Read/Write variables over serial self.controller = DistantIO() # Serial protocol self.protocol = Protocol() # Variable manager self.variable_manager = VariableManager(self) self.variable_manager.start() self.running = True; # Data logger self.logger = DataLogger()
def __init__(self, page): ''' Simple constructor that looks for a ref and logs some stats about this article. We ignore revisions for now, and only work with the current rev. Later on, this should receive a number of revisions and parse each of them, only checking for refs on the current rev. We also assume that no numRedirects are passed into this constructor, as the stats we are logging would be useless for those. ''' ns, tag = string.split(page.tag, '}', 1) title = page.find(ns + "}title") revision = page.find(ns + "}revision") textNode = revision.find(ns + "}text") # Parses the text and logs the article if it's missing a reftag or template t = textNode.text hasRef = self.hasRefTag(t) hasRefSection = self.hasRefSection(t) self.length = len(t) self.numlinks = len(re.findall('\[\[', t)) self.numtemplates = len(re.findall('\{\{', t)) # Regex for extlinks should be fixed and look for any single [ # not preceded by another where alphanums follows. self.numextlinks = len(re.findall('\[\ ?(http|https|www)', t)) self.imagecount = len(re.findall(r'\[\[(File|Image)\:', t)) if hasRef == False: if hasRefSection == False: DataLogger.l("/tmp/wp_missing_norefsection.txt", "Missing: [[" + title.text + "]]") else: DataLogger.l("/tmp/wp_missing_refsection.txt", "Missing: [[" + title.text + "]]")
class SingleMode: # init function, sets up global variables and objects, builds the GUI def __init__(self, master): # global variables self.master = master # available sensors list self.Sensors = ["----", "TMP36", "LMT86", "ADXL335 X-axis", "ADXL335 Y-axis", "ADXL335 Z-axis", "ADXL335 Total", "BMP180 Temp", "BMP180 Pressure", "TSL2561"] # State controls if the software actively measures self.State = False # Interval controls the interval between measurements self.Interval = 100 # Sensor_values displays actual measured values self.Sensor_value1 = Tk.StringVar(master) self.Sensor_value1.set("0.0") # SensorSelects controls which sensor are selected self.SensorSelect1 = Tk.StringVar(master) self.SensorSelect1.set(self.Sensors[0]) # ChannelSelects controls which channels are selected self.ChannelSelect1 = Tk.IntVar(master) self.ChannelSelect1.set(0) # FreqeuncySelect controls frequency of measurements self.FrequencySelect = Tk.IntVar(master) self.FrequencySelect.set(10) # Sensor_units displays units of actively measured values self.Sensor_unit1 = Tk.StringVar(master) self.Sensor_unit1.set("--") # creates SensorReader and DataLogger objects self.sr = SensorReader(True, True, False) self.DL1 = DataLogger(False) # configures the grid layout master.columnconfigure(1, weight=0) master.columnconfigure(2, weight=2) master.columnconfigure(3, weight=1) master.columnconfigure(4, weight=1) master.rowconfigure(1, weight=1) master.rowconfigure(2, weight=0) master.rowconfigure(3, weight=0) master.rowconfigure(4, weight=0) # configures the window properties master.resizable(0, 0) master.geometry("500x250+300+300") master.title("LabPi SINGLE MODE") # GUI elements config # labels which display current value and unit self.sensor_value1 = Tk.Label(master, textvariable=self.Sensor_value1, font=("Helvetica", 60)) self.sensor_value1.grid(row=1, column=1, columnspan=3) self.sensor_unit1 = Tk.Label(master, textvariable=self.Sensor_unit1, font=("Helvetica", 50)) self.sensor_unit1.grid(row=1, column=4, columnspan=3) # static labels self.Select_label1 = Tk.Label(master, text="Sensor 1:") self.Select_label1.grid(row=2, column=1, sticky="nsew") self.Select_label2 = Tk.Label(master, text="Channel 1:") self.Select_label2.grid(row=3, column=1, sticky="nsew") self.Select_label3 = Tk.Label(master, text="Frequency:") self.Select_label3.grid(row=4, column=1, sticky="nsew") # optionmenus for configuring self.sensor_Select1 = Tk.OptionMenu(master, self.SensorSelect1, *self.Sensors) self.sensor_Select1.grid(row=2, column=2, sticky="nsew") self.channel_Select1 = Tk.OptionMenu(master, self.ChannelSelect1, 0, 1, 2, 3) self.channel_Select1.grid(row=3, column=2, sticky="nsew") self.frequency_Select = Tk.OptionMenu(master, self.FrequencySelect, 1, 2, 5, 10, 25, 50) self.frequency_Select.grid(row=4, column=2, sticky="nsew") # control buttons self.Start_button = Tk.Button(master, text="Start", command=self.start) self.Start_button.grid(row=2, column=3, sticky="nsew") self.Stop_button = Tk.Button(master, text="Stop", command=self.stop) self.Stop_button.grid(row=3, column=3, sticky="nsew") self.Save_button = Tk.Button(master, text="Save", command=self.save) self.Save_button.grid(row=2, column=4, sticky="nsew") self.Plot_button = Tk.Button(master, text="Plot", command=self.plot) self.Plot_button.grid(row=3, column=4, sticky="nsew") # refresh function, checks if State=True, then runs measurement again after set interval def refresh(self): if self.State: self.master.after(self.Interval, self.refresh) self.updateVar1() # start function, configures app for measurement, then starts the refresh function def start(self): # clean previously logged data self.DL1.eraseData() # update measuring frequency self.updateFrequency() # update unit self.updateUnit1() # write info block into data log self.DL1.writeInfo(info="sensor:" + str(self.SensorSelect1.get()) + ", date:" + time.strftime("%Y-%m-%d %H:%M:%S") + ", interval: " + str(self.Interval)) # disable GUI elements self.sensor_Select1.configure(state="disabled") self.Start_button.configure(state="disabled") self.channel_Select1.configure(state="disabled") self.frequency_Select.configure(state="disabled") self.Save_button.configure(state="disabled") self.Plot_button.configure(state="disabled") # set state to True and run refresh function self.State = True self.refresh() # save function, saves the logged data via DataLogger function def save(self): self.DL1.saveData(location="measurements/", name=("Sensor1"+time.strftime("%Y-%m-%d-%H%M%S"))) # plot function, plots logged data with pyplot, configure pyplot def plot(self): plt.plot(self.DL1.readData()) plt.ylabel(self.Sensor_unit1.get()) plt.xlabel("Samples") plt.grid(True) plt.show() # stop function, sets State to False, enables disabled GUI elements def stop(self): self.State = False self.sensor_Select1.configure(state="normal") self.Start_button.configure(state="normal") self.channel_Select1.configure(state="normal") self.frequency_Select.configure(state="normal") self.Save_button.configure(state="normal") self.Plot_button.configure(state="normal") # updateUnit function, updates displayed unit to proper one def updateUnit1(self): if self.SensorSelect1.get() == "----": self.Sensor_unit1.set("--") elif self.SensorSelect1.get() == "TMP36": self.Sensor_unit1.set("C") elif self.SensorSelect1.get() == "LMT86": self.Sensor_unit1.set("C") elif self.SensorSelect1.get() == "ADXL335 X-axis": self.Sensor_unit1.set("m/s2") elif self.SensorSelect1.get() == "ADXL335 Y-axis": self.Sensor_unit1.set("m/s2") elif self.SensorSelect1.get() == "ADXL335 Z-axis": self.Sensor_unit1.set("m/s2") elif self.SensorSelect1.get() == "ADXL335 Total": self.Sensor_unit1.set("m/s2") elif self.SensorSelect1.get() == "BMP180 Temp": self.Sensor_unit1.set("C") elif self.SensorSelect1.get() == "BMP180 Pressure": self.Sensor_unit1.set("Pa") elif self.SensorSelect1.get() == "TSL2561": self.Sensor_unit1.set("lux") # updateVar function, updates displayed unit to proper one, writes data to data log def updateVar1(self): if self.SensorSelect1.get() == "TMP36": value = self.sr.readTMP36(self.ChannelSelect1.get()) elif self.SensorSelect1.get() == "LMT86": value = self.sr.readLMT86(self.ChannelSelect1.get()) elif self.SensorSelect1.get() == "ADXL335 X-axis": value = self.sr.readADXL335XAccel(self.ChannelSelect1.get()) elif self.SensorSelect1.get() == "ADXL335 Y-axis": value = self.sr.readADXL335YAccel(self.ChannelSelect1.get()) elif self.SensorSelect1.get() == "ADXL335 Z-axis": value = self.sr.readADXL335ZAccel(self.ChannelSelect1.get()) elif self.SensorSelect1.get() == "ADXL335 Total": value = self.sr.readADXL335TotalAccel(0, 1, 2) elif self.SensorSelect1.get() == "BMP180 Temp": value = self.sr.readBMP085Temp() elif self.SensorSelect1.get() == "BMP180 Pressure": value = self.sr.readBMP085Pressure() elif self.SensorSelect1.get() == "TSL2561": value = self.sr.readTSL2561Lux() else: value = 0 self.Sensor_value1.set(str(value)) self.DL1.writeData(value) # updateFrequency function, sets interval between measurements to selected value def updateFrequency(self): if self.FrequencySelect.get() == 1: self.Interval = 1000 elif self.FrequencySelect.get() == 2: self.Interval = 500 elif self.FrequencySelect.get() == 5: self.Interval = 200 elif self.FrequencySelect.get() == 10: self.Interval = 100 elif self.FrequencySelect.get() == 25: self.Interval = 40 elif self.FrequencySelect.get() == 50: self.Interval = 20
def __init__(self, master): # global variables self.master = master # available sensors list self.Sensors = ["----", "TMP36", "LMT86", "ADXL335 X-axis", "ADXL335 Y-axis", "ADXL335 Z-axis", "ADXL335 Total", "BMP180 Temp", "BMP180 Pressure", "TSL2561"] # State controls if the software actively measures self.State = False # Interval controls the interval between measurements self.Interval = 100 # Sensor_values displays actual measured values self.Sensor_value1 = Tk.StringVar(master) self.Sensor_value1.set("0.0") self.Sensor_value2 = Tk.StringVar(master) self.Sensor_value2.set("0.0") # SensorSelects controls which sensor are selected self.SensorSelect1 = Tk.StringVar(master) self.SensorSelect1.set(self.Sensors[0]) self.SensorSelect2 = Tk.StringVar(master) self.SensorSelect2.set(self.Sensors[0]) # ChannelSelects controls which channels are selected self.ChannelSelect1 = Tk.IntVar(master) self.ChannelSelect1.set(0) self.ChannelSelect2 = Tk.IntVar(master) self.ChannelSelect2.set(0) # FreqeuncySelect controls frequency of measurements self.FrequencySelect = Tk.IntVar(master) self.FrequencySelect.set(10) # Sensor_units displays units of actively measured values self.Sensor_unit1 = Tk.StringVar(master) self.Sensor_unit1.set("--") self.Sensor_unit2 = Tk.StringVar(master) self.Sensor_unit2.set("--") # creates SensorReader and DataLogger objects self.sr = SensorReader(True, True, False) self.DL1 = DataLogger(False) self.DL2 = DataLogger(False) # configures the grid layout master.columnconfigure(1, weight=0) master.columnconfigure(2, weight=2) master.columnconfigure(3, weight=1) master.columnconfigure(4, weight=1) master.rowconfigure(1, weight=1) master.rowconfigure(2, weight=1) master.rowconfigure(3, weight=0) master.rowconfigure(4, weight=0) master.rowconfigure(5, weight=0) master.rowconfigure(6, weight=0) master.rowconfigure(7, weight=0) # configures the window properties master.resizable(0, 0) master.geometry("500x500+300+300") master.title("LabPi DUAL MODE") # GUI elements config # labels which display current value and unit self.sensor_value1 = Tk.Label(master, textvariable=self.Sensor_value1, font=("Helvetica", 60)) self.sensor_value1.grid(row=1, column=1, columnspan=3) self.sensor_unit1 = Tk.Label(master, textvariable=self.Sensor_unit1, font=("Helvetica", 50)) self.sensor_unit1.grid(row=1, column=4, columnspan=3) self.sensor_value2 = Tk.Label(master, textvariable=self.Sensor_value2, font=("Helvetica", 60)) self.sensor_value2.grid(row=2, column=1, columnspan=3) self.sensor_unit2 = Tk.Label(master, textvariable=self.Sensor_unit2, font=("Helvetica", 50)) self.sensor_unit2.grid(row=2, column=4, columnspan=3) # static labels self.Select_label1 = Tk.Label(master, text="Sensor 1:") self.Select_label1.grid(row=3, column=1, sticky="nsew") self.Select_label2 = Tk.Label(master, text="Channel 1:") self.Select_label2.grid(row=4, column=1, sticky="nsew") self.Select_label3 = Tk.Label(master, text="Sensor 2:") self.Select_label3.grid(row=5, column=1, sticky="nsew") self.Select_label4 = Tk.Label(master, text="Channel 2:") self.Select_label4.grid(row=6, column=1, sticky="nsew") self.Select_label5 = Tk.Label(master, text="Frequency:") self.Select_label5.grid(row=7, column=1, sticky="nsew") # optionmenus for configuring self.sensor_Select1 = Tk.OptionMenu(master, self.SensorSelect1, *self.Sensors) self.sensor_Select1.grid(row=3, column=2, sticky="nsew") self.channel_Select1 = Tk.OptionMenu(master, self.ChannelSelect1, 0, 1, 2, 3) self.channel_Select1.grid(row=4, column=2, sticky="nsew") self.sensor_Select2 = Tk.OptionMenu(master, self.SensorSelect2, *self.Sensors) self.sensor_Select2.grid(row=5, column=2, sticky="nsew") self.channel_Select2 = Tk.OptionMenu(master, self.ChannelSelect2, 0, 1, 2, 3) self.channel_Select2.grid(row=6, column=2, sticky="nsew") self.frequency_Select = Tk.OptionMenu(master, self.FrequencySelect, 1, 2, 5, 10, 25, 50) self.frequency_Select.grid(row=7, column=2, sticky="nsew") # control buttons self.Start_button = Tk.Button(master, text="Start", command=self.start) self.Start_button.grid(row=3, column=3, rowspan=2, sticky="nsew") self.Stop_button = Tk.Button(master, text="Stop", command=self.stop) self.Stop_button.grid(row=5, column=3, rowspan=2, sticky="nsew") self.Save_button = Tk.Button(master, text="Save", command=self.save) self.Save_button.grid(row=3, column=4, rowspan=2, sticky="nsew") self.Plot_button = Tk.Button(master, text="Plot", command=self.plot) self.Plot_button.grid(row=5, column=4, rowspan=2, sticky="nsew")
class Model(Thread): def __init__(self, **kwargs): Thread.__init__(self) # Serial thread self.serialthread = SerialPort() # Controller Read/Write variables over serial self.controller = DistantIO() # Serial protocol self.protocol = Protocol() # Variable manager self.variable_manager = VariableManager(self) self.variable_manager.start() self.running = True; # Data logger self.logger = DataLogger() def get_ports(self): return self.serialthread.get_ports() def connect_com(self,COM_port): # Start serial thread (can run without COM port connected) if not self.serialthread.isAlive(): self.serialthread.start() self.serialthread.connect(COM_port,115200) #Model update running in a thread def run(self): while self.running: if self.serialthread.char_available(): c = self.serialthread.get_char() if not c is None: self.protocol.process_rx(c) if self.protocol.available(): p = self.protocol.get() # Dump payload if controller is in heavy load ? pub.sendMessage('new_rx_payload',rxpayload=p)#USED ? if not p is None: self.controller.decode(p) def disconnect_com(self): self.serialthread.disconnect() def stop(self): self.running = False self.serialthread.stop() self.variable_manager.stop() if self.serialthread.isAlive(): self.serialthread.join(0.1) if self.serialthread.isAlive(): self.serialthread.join(1) if self.serialthread.isAlive(): print("--- Serial thread not properly joined.") if self.variable_manager.isAlive(): self.variable_manager.join(0.1) if self.variable_manager.isAlive(): self.variable_manager.join(1) self.stop_controller() def start_controller(self): #Get command for querying variable table MCU side cmd = self.controller.encode(cmd='table') #Feed command to serial protocol payload processor frame = self.protocol.process_tx(cmd) #Send command self.serialthread.write(frame) def stop_controller(self): #TODO : Tell MCU to stop sending all data pass def start_log(self): self.logger.start() def stop_log(self): self.logger.record_all() def read_var(self, varid): # Get command cmd = self.controller.encode(cmd='read',var_id=varid) # Feed command to serial protocol payload processor frame = self.protocol.process_tx(cmd) # Send command self.serialthread.write(frame) def write_var(self,varid,value): # Get command cmd = self.controller.encode(cmd='write',var_id=varid,value=value) if cmd == None: return # Feed command to serial protocol payload processor frame = self.protocol.process_tx(cmd) # Send command self.serialthread.write(frame) def stop_read_var(self,varid): # Get command cmd = self.controller.encode(cmd='stop',var_id=varid) if cmd == None: return # Feed command to serial protocol payload processor frame = self.protocol.process_tx(cmd) # Send command self.serialthread.write(frame) def stop_read_all_vars(): # Get command cmd = self.controller.encode(cmd='stopall') if cmd == None: return # Feed command to serial protocol payload processor frame = self.protocol.process_tx(cmd) # Send command self.serialthread.write(frame) def get_var_info(self,varid): return self.controller.get_var_info(varid)
class Plotter: """ This class is responsible for plotting all the data items in the list to a file in png format using gnuplot """ def __init__(self, outputFileName, title, xlabel, ylabel, N, interval, gnuplot = "gnuplot"): """ The ctor outputFileName The file name with which the png file is to be created title Title of the plot xlabel X axis label ylabel Y axis label N number of data objects to get in interval Interval at which the data is to captured gnuplot The gnuplot program, on windows env, supply the complete path """ if (type(title) != type('') or type(xlabel) != type('') or type(ylabel) != type('')): raise ValueError, "This function expects 'title', 'xlabel' and 'ylabel' all to \ be strings" if (type(interval) != type(1) or type(N) != type(1) ): raise ValueError, "This function requires both 'interval' and 'N' to be of integer type" if (type(gnuplot) != type('')): raise ValueError, "Path to gnuplot must be a string" self.gnuplot = gnuplot self.plotItems = [] # will hold all the plot items self.stopEvent = threading.Event() self.n = N self.interval = interval self.DataLogger = None self.out = outputFileName self.reRgb = re.compile(r'#[a-fA-F0-9]{6}$') self.gnuplotScript = """ set term png size 1000,500 set output [output_here] set xlabel [xlabel_here] set ylabel [ylabel_here] set xdata time set timefmt "%b-%d-%H:%M:%S" set grid """ self.gnuplotScript = self.gnuplotScript.replace('[output_here]', "'"+outputFileName+"'") self.gnuplotScript = self.gnuplotScript.replace('[xlabel_here]', "'"+xlabel+"'") self.gnuplotScript = self.gnuplotScript.replace('[ylabel_here]', "'"+ylabel+"'") self.tempScript = "" def addPlotItem(self, itemName, itemColor, itemCb): """ This will add items to our "plotItems" list and when it is time to get the data, this list will be walked through itemName Name of the item as it appears in the graph itemColor Color with this data item is to be plotted, in hexadecimal format itemCb Call back function for this item that will supply the data Note: This has to be called before calling the "begin" function """ if not self.reRgb.match(itemColor): raise ValueError, "Not a valid RGB color code" self.plotItems.append(PlotItem(itemName, itemColor, itemCb)) def __theCallBack__(self): """ The call back method, which will execute all the callback functions in the list and will aggregate the data """ dataList = [] for plotItem in self.plotItems: try: temp = plotItem.itemCb() except Exception as ex: raise ex # "One of the call back function has failed " if (type(temp) != type(1) and type(temp) != type(1.1)): raise ValueError, "The function %s should return an int or a float type" %plotItem.itemCb.__name__ dataList.append(temp) # Return the data list that we have to the logger return dataList def begin(self): """ Starts the logger thread """ if self.DataLogger == None: self.DataLogger = DataLogger(self.__theCallBack__, self.n, self.interval, self.stopEvent) self.DataLogger.start() def stop(self): """ Stop the logging thread """ self.stopEvent.set() try: if DataLogger != None: print "Stopping the datalogger " self.DataLogger.join() self.DataLogger = None except: pass self.tempScript = "" def __del__(self): self.stop() def getOutPutFileName(self): return self.out def __addToGnuPlotCmdColorTitle__(self, color, title): """ Internal method that will add plot title and color """ if self.tempScript.find("plot") < 0: self.tempScript = self.tempScript + "\nplot " self.tempScript = self.tempScript + "'-' using 1:2 with lines title '%s' lt rgb '%s', " %(title, color) def __addToGnuPlotCmdDataTime__(self, data, time): """ Add the date and time information """ self.tempScript = self.tempScript + "\n%s %s" %(time, data) def __addToGnuPlotCmdEOD__(self): """ Add end of data """ self.tempScript = self.tempScript + "\ne\n" def __addToGnuPlotCmdXRange__(self, startTime, endTime): """ Set the start and end time """ start = startTime.strftime("%b-%d-%H:%M:%S") end = endTime.strftime("%b-%d-%H:%M:%S") self.tempScript = self.gnuplotScript self.tempScript = self.tempScript + """\nset xrange ["%s":"%s"]\n""" %(start, end) self.tempScript = self.tempScript + r'set format x "%H:%M\n%b-%d' def plot(self): """ This method will get the recorded data and plots it to png file using gnuplot """ recordedList = self.DataLogger.getDataList() if len(recordedList) == 0: return -1 # No records n = len(self.plotItems) # Set the x axis range temp = len(recordedList[0]) startTime = recordedList[0][0].time endTime = recordedList[0][temp-1].time self.__addToGnuPlotCmdXRange__(startTime, endTime) for j in range (0, n): self.__addToGnuPlotCmdColorTitle__(self.plotItems[j].color, self.plotItems[j].name) i=0 for record in recordedList: # Now, add all the data points for gnuplot to chew on for rec in record: timeData = rec.time.strftime("%b-%d-%H:%M:%S") data = str(rec.data) self.__addToGnuPlotCmdDataTime__(data, timeData) self.__addToGnuPlotCmdEOD__() i = i+1 #print self.tempScript plot = subprocess.Popen([self.gnuplot], stdin=subprocess.PIPE) plot.communicate(self.tempScript) print self.tempScript self.tempScript = ""
class DataFeed: def __init__(self, config, log_market=False): self.config = config self.log = logging.getLogger('DataFeed') self.addr = str(config['Subscriptions']['Socket']) self.pairs = [str(p) for p in config['Subscriptions']['Symbols']] self.channels = [ str(c) for c in config['Subscriptions']['Channels'].keys() if not str(c).startswith('--') ] self.subbed_messages = [ str(m) for m in config['Subscriptions']['Messages'] ] for ch in self.channels: for msg in config['Subscriptions']['Channels'][ch]: self.subbed_messages.append(str(msg)) self.dataLogger = DataLogger(self.pairs, self.subbed_messages, log_market) self._endpoint = ws.create_connection(str(self.addr), max_size=None) self._msgQ = Queue() self._handleThread = Thread(\ target = self._handle_q,\ name = 'Message_Handler_Thread') self._handleThread.daemon = True self._listenThread = Thread(\ target = self._recvLoop,\ name = 'Listener_Thread') self._listenThread.daemon = True self._isRunning = False self.pendingException = None self.log.debug('Initialized DataFeed!') class BaseError(Exception): def __init__(self, outer_obj, msg): self.outer_obj = outer_obj self.err = msg def what(self): pass class SubscribeError(BaseError): #Exception raised due to subscribe errors def __init__(self, outer_obj, msg): super(DataFeed.SubscribeError, self).__init__(outer_obj, msg) getattr(self.outer_obj, 'raise_exc')(self) def __str__(self): return self.what() def what(self): return 'DataFeed.SubscribeError: Could not subscribe to desired pair(s)/channel(s) - %s' % self.err def check(self): if self.pendingException is not None: pendingException = self.pendingException self.pendingException = None raise pendingException def raise_exc(self, err): self.pendingException = err def close(self): self._isRunning = False self._listenThread.join() self._msgQ.join() self._handleThread.join() def subscribe(self): def all_same_type(typ, iterable): return all([isinstance(obj, typ) for obj in iterable]) #Prepare [pairs] for the sub message pair_str = [] if not isinstance(self.pairs, list): if isinstance(self.pairs, tuple): pair_str = ['-'.join((p.upper() for p in self.pairs))] elif isinstance(self.pairs, str): pair_str = [self.pairs.upper()] else: self.SubscribeError(self, 'Invalid Pair Requested') return else: if all_same_type(tuple, self.pairs): for pair in self.pairs: pair_str.append('-'.join( [pair[i].upper() for i in range(len(pair))])) elif all_same_type(str, self.pairs): pair_str = [p.upper() for p in self.pairs] else: self.SubscribeError(self, 'Invalid \'pairs\' Argument: Valid options:\n' + \ ' \'XXX-XXX\' One Pair: String\n' + \ ' (\'XXX\', \'XXX\') One Pair: Tuple\n' + \ ' [\'XXX-XXX\', ...] Multiple Pairs: String List\n' + \ ' [(\'XXX\', \'XXX\'), ...] Multiple Pars: List of One Pair Tuples') return #Prapare [channels] for the sub message channel_str = [] available_channels = [ 'heartbeat', 'ticker', 'level2', 'user', 'matches', 'full' ] if isinstance(self.channels, list): channel_str = [c for c in self.channels if c in available_channels] elif isinstance(self.channels, str): channel_str = [self.channels] else: self.SubscribeError( self, 'Invalid \'channels\' Argument: must be a string or list of strings.' ) return #build subscribe message sub_msg = json.dumps(\ {\ "type": "subscribe",\ "product_ids": pair_str,\ "channels": channel_str }) self._endpoint.send(sub_msg) self._isRunning = True status = self._recvOnce() start = time() while not status and time() - start < 10: status = self._recvOnce() if status: return True else: self.log.error( 'Timed out waiting for subscription confirmation from GDAX!') return False def start(self): self._handleThread.start() self._listenThread.start() self.log.info('Entering Receive Loop.') def _recvLoop(self): while self._isRunning: msg = json.loads(self._endpoint.recv()) if msg['type'] in self.subbed_messages: cb = getattr(self, msg['type'] + '_cb') self._msgQ.put((cb, msg)) else: self._msgQ.put((self.unknownType_cb, msg)) self.check() sleep(0.25) return def _recvOnce(self): msg = json.loads(self._endpoint.recv()) if str(msg['type']) == 'subscriptions': return getattr(self, msg['type'] + '_cb')(msg) if msg['type'] in self.subbed_messages: cb = getattr(self, msg['type'] + '_cb') self._msgQ.put((cb, msg)) else: self._msgQ.put((self.unknownType_cb, msg)) return False def _handle_q(self): while self._isRunning or not self._msgQ.empty(): task = self._msgQ.get() task[0](task[1]) self._msgQ.task_done() def unknownType_cb(self, msg): print 'Unknown Message type encountered: %s\n' % msg['type'] print msg def subscriptions_cb(self, response): status_channels = [] status_pairs = [] for chan_from_msg in response['channels']: status_channels.append(str(chan_from_msg['name']) in self.channels) for pair_from_msg in chan_from_msg['product_ids']: status_pairs.append(str(pair_from_msg) in self.pairs) return (all(status_channels) and all(status_channels)) def error_cb(self, msg): self.log.error('GDAX ERROR: %s - %s' % (msg['message'], msg['reason'])) #print 'GDAX responded with an error message: %s - %s' %(msg['message'], msg['reason']) del msg def heartbeat_cb(self, msg): #print 'Received Hearbeat Message #%s for %s' % (msg['sequence'], msg['product_id']) self.dataLogger.log(msg) #This is where the necessary data will be parsed and sent to the brain #-----> Might need some interproccess communication? del msg def ticker_cb(self, msg): #print msg self.dataLogger.log(msg) #print 'Received Ticker Message #%s for %s with seq. #%s' \ # % (msg['sequence'], msg['product_id'], msg['sequence']) #print ' Most Recent Price: %s' % msg['price'] def l2update_cb(self, msg): self.dataLogger.log(msg) def snapshot_cb(self, msg): self.dataLogger.log(msg) '''
class MyRobot(wpilib.IterativeRobot): def __init__(self): super().__init__() self.pdp = wpilib.PowerDistributionPanel() self.subsystems = {} self.datalogger = DataLogger(logfile=open("robot.log", mode='a'), curtime=wpilib.DriverStation.getInstance().getMatchTime) def robotInit(self): subprocess.Popen("/home/lvuser/grip", shell=True) # Start GRIP process self.subsystems['drive'] = DriveSubsystem() self.subsystems['shooter'] = ShooterSubsystem(self) self.subsystems['tomahawk'] = TomahawkSubsystem() self.teleopCommand = TeleopCommand(self) self.autonomousCommand = CommandGroup() self.autonomousCommand.setTimeout(15) self.autoPositionChooser = wpilib.SendableChooser() self.autoPositionChooser.addDefault("Position 1", 1) self.autoPositionChooser.addObject("Position 2", 2) self.autoPositionChooser.addObject("Position 3", 3) self.autoPositionChooser.addObject("Position 4", 4) self.autoPositionChooser.addObject("Position 5", 5) wpilib.SmartDashboard.putData("PositionChooser", self.autoPositionChooser) self.autoDefenseChooser = wpilib.SendableChooser() self.autoDefenseChooser.addDefault("Nothing", None) self.autoDefenseChooser.addObject("Reach", AutoDriveCommand(self, power=.5, dist=100)) self.autoDefenseChooser.addObject("Rough Terrain", AutoDriveCommand(self, power=.7, dist=200)) self.autoDefenseChooser.addObject("Rock Wall", AutoDriveCommand(self, power=.9, dist=200, holdpos=config.articulateAngleHigh)) self.autoDefenseChooser.addObject("Moat", AutoDriveCommand(self, power=-.9, dist=270)) self.autoDefenseChooser.addObject("Ramparts", AutoDriveCommand(self, power=-.7, dist=200, holdpos=config.articulateAngleHigh)) self.autoDefenseChooser.addObject("Low Bar", AutoDriveCommand(self, power=.6, dist=200, holdpos=config.articulateAngleLow)) self.autoDefenseChooser.addObject("Portcullis", AutoDriveCommand(self, power=.6, dist=200, holdpos=config.articulateAngleLow)) self.autoDefenseChooser.addObject("Cheval de Frise", AutoChevalDeFriseCommand(self)) wpilib.SmartDashboard.putData("DefenseChooser", self.autoDefenseChooser) self.autoActionChooser = wpilib.SendableChooser() self.autoActionChooser.addDefault("Nothing", None) self.autoActionChooser.addObject("Aim", AutoTargetCommand(self, shoot=False)) self.autoActionChooser.addObject("Shoot", AutoTargetCommand(self, shoot=True)) wpilib.SmartDashboard.putData("ActionChooser", self.autoActionChooser) wpilib.SmartDashboard.putNumber("Aim at", 150) self.datalogger.ready() def teleopPeriodic(self): Scheduler.getInstance().run() self.datalogger.log_data() def autonomousPeriodic(self): Scheduler.getInstance().run() self.datalogger.log_data() def teleopInit(self): self.autonomousCommand.cancel() self.jsAutoTargetButton = JoystickButton(wpilib.Joystick(2), 8) self.autoTargetCommand = AutoTargetCommand(self) self.teleopCommand.start() self.autoTargetCommand.start() def autonomousInit(self): defense = self.autoDefenseChooser.getSelected() action = self.autoActionChooser.getSelected() position = self.autoPositionChooser.getSelected() k = [] if defense is not None: k += [defense] if action is not None: action.setPosition(position) k += [action] for cmd in k: cmd.setParent(None) self.autonomousCommand = AutonomousCommand(k) self.autonomousCommand.start() def autoAimShooter(self): shooter = self.subsystems['shooter'] drive = self.subsystems['drive'] pitch, yawDelta = shooter.calculateShooterParams() drive.driveAngle += yawDelta shooter.setArticulateAngle(pitch)
def log(self): DataLogger.l("/tmp/wplog.txt", "Num redirects: " + str(self.numRedirects)) DataLogger.l("/tmp/wplog.txt", "Num dabs: " + str(self.numDabs)) DataLogger.l("/tmp/wplog.txt", "Num lists: " + str(self.numLists)) DataLogger.l("/tmp/wplog.txt", "Num pages per ns: " + pprint.pformat(self.nscounts)) DataLogger.l("/tmp/wplog.txt", "Avg length: " + pprint.pformat(self.lengthTotal / len(self.articlestats))) DataLogger.l("/tmp/wplog.txt", "Avg images per article: " + pprint.pformat(self.numImgsTotal / len(self.articlestats))) DataLogger.l("/tmp/wplog.txt", "Avg extlinks: " + pprint.pformat(self.numExtLinksTotal / len(self.articlestats))) DataLogger.l("/tmp/wplog.txt", "Avg wplinks: " + pprint.pformat(self.numLinksTotal / len(self.articlestats))) DataLogger.l("/tmp/wplog.txt", "Avg templates: " + pprint.pformat(self.numTemplatesTotal / len(self.articlestats)))
def log(self): DataLogger.l("/tmp/wplog.txt", "Avg templates: " + pprint.pformat(self.numTemplatesTotal / len(self.articlestats)))
print("Setting sample frequency to 50") mainBoard.setSampleFrequency(50) print("Sample frequency set. Message from board: " + mainBoard.readLine()) print("Setting sample average size to 1") mainBoard.setSampleAverageSize(1) print("Sample average set. Message from board: " + mainBoard.readLine()) print("Setting device report rate to 1000") mainBoard.setDeviceReportFrequency(1000) print("Device report rate set. Message from board: " + mainBoard.readLine()) print("Setting device sample enabled bit to 1") mainBoard.setSampleEnable(1) print("Device sample enabled set. Message from board: " + mainBoard.readLine()) print() print("Starting board read: ") print() headers = ['A0', 'A1', 'A2', 'A3', 'A4', 'A5'] logger = DataLogger('run1.xlsx', 7, headers) sampleRun = Run.Run(mainBoard, logger) sampleRun.start() time.sleep(16) print("Run Successful!") print("Number of Samples: " + str(sampleRun.stop()))
def __init__(self): self.l = DataLogger.getInstance()
#!/usr/bin/env python """This is to run a third instance of the 'DataLogger' application Friedrich Schotte, 18 Jun 2011""" from DataLogger import DataLogger import wx app = wx.PySimpleApp(0) win = DataLogger(name="DataLogger3") app.MainLoop()
rancilio_temperature_status_handler() Rancilio.update() rancilio_heater_status_handler() rancilio_ready_handler() time.sleep(1) # stop event is set Rancilio.setHeaterOutput(0) configs = Configurator.instance() # Threading stuff stopEvent = Event() error_in_method_event = Event() dataLogger = DataLogger(stopEvent, error_in_method_event) dataLogger.addTemperatureSensor('boiler', configs.boilerTempSensor1) dataLogger.addTemperatureSensor('boiler', configs.boilerTempSensor2) configs.dataLogger = dataLogger temperatureAcquisitionProcess = Thread(target=dataLogger.acquireData) temperatureAcquisitionProcess.start() Rancilio = RancilioSilvia() configs.Rancilio = Rancilio rancilioError = RancilioError.instance() rancilioError.blynkShutDownFcn = blynk_shut_down rancilioError.blynkAliveFcn = blynk_check_live_connection shm = SystemHealthMonitor() shm.stopEvent = stopEvent
#! /usr/bin/env python3 import argparse from DataLogger import DataLogger parser = argparse.ArgumentParser() parser.add_argument( "--clear", action="store_true", help= "Clear all data stored on Solar Light UV Radiometer internal data logger.") args = parser.parse_args() dataLogger = DataLogger(args.clear) dataLogger.start()
import sys sys.path.append("/home/nick/github/Controls/RaspberryPi/") sys.path.append("/home/nick/github/DataLogger/") print(sys.path) from RaspberryPi.rpy_pid_controller.Apr5test_pid_shell import pid_wrapper from RaspberryPi.rpy_motorcontroller.MotorController_hat import MotorController from RaspberryPi.rpy_pid_controller.devices.mpu6050 import mpu6050 from RaspberryPi.game_controller.xbox_controller import XboxController from DataLogger import DataLogger from writers.csv import csv from time import sleep writer = csv('APR7_') logger = DataLogger(writer) imu = mpu6050(0x68) pid = pid_wrapper(imu) motors = MotorController() ctrlr = XboxController() logger.add_device(imu) logger.add_device(pid) logger.add_device(motors) logger.add_device(ctrlr) targets = [0, 0] #FIXME Need Correct target angles and correct orientation yaw angle. pid.set_targets(targets) pid.run() logger.start()
from writers.csv import csv from DataLogger import DataLogger from time import sleep from devices.ArduinoDevice import ArduinoDevice writer = csv('testLog') logger = DataLogger(writer) logger.add_device(ArduinoDevice()) logger.start() try: while True: print("hi") sleep(1) finally: logger.end()
def __init__(self): super().__init__() self.pdp = wpilib.PowerDistributionPanel() self.subsystems = {} self.datalogger = DataLogger(logfile=open("robot.log", mode='a'), curtime=wpilib.DriverStation.getInstance().getMatchTime)
#!/usr/bin/env python """This is to run a second instance of the 'DataLogger' application Friedrich Schotte, 18 Jun 2011-30 Mar 2014""" __version__ = "1.1" from DataLogger import DataLogger import wx app = wx.App(redirect=False) win = DataLogger(name="DataLogger2") app.MainLoop()
def start_data_logging(): from DataLogger import DataLogger DataLogger()
def begin(self): """ Starts the logger thread """ if self.DataLogger == None: self.DataLogger = DataLogger(self.__theCallBack__, self.n, self.interval, self.stopEvent) self.DataLogger.start()
import os import time from DataLogger import DataLogger def get_image_file_name(): ts = time.time() return ts, ('%.6f' % ts) + ".jpg" state = { "direction": 130.32, "throttle": 0.8, "lat": 123.21123234, "is_manual_mode": False } state["timestamp"], state["image_file_name"] = get_image_file_name() dl = DataLogger(os.getcwd() + "/demo.csv") dl.write(state) state["timestamp"], state["image_file_name"] = get_image_file_name() dl.write(state) state["timestamp"], state["image_file_name"] = get_image_file_name() dl.write(state)
def start_data_logging(): DataLogger("test.csv")