|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +"""Demonstration of multithreaded live Arduino data |
| 4 | +- CLI output only |
| 5 | +- Mode: INTERNAL_TIMER |
| 6 | +""" |
| 7 | +__author__ = "Dennis van Gils" |
| 8 | +__authoremail__ = "vangils.dennis@gmail.com" |
| 9 | +__url__ = "https://github.com/Dennis-van-Gils/DvG_Arduino_PyQt_multithread_demo" |
| 10 | +__date__ = "26-06-2020" |
| 11 | +__version__ = "2.1" |
| 12 | + |
| 13 | +import os |
| 14 | +import sys |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +import numpy as np |
| 18 | +import psutil |
| 19 | +import time |
| 20 | + |
| 21 | +from PyQt5 import QtCore |
| 22 | +from DvG_debug_functions import dprint, print_fancy_traceback as pft |
| 23 | + |
| 24 | +from DvG_dev_Arduino__fun_serial import Arduino # I.e. the `device` |
| 25 | +from DvG_QDeviceIO import QDeviceIO, DAQ_trigger |
| 26 | + |
| 27 | +# Constants |
| 28 | +DAQ_INTERVAL = 10 # 10 [ms] |
| 29 | + |
| 30 | +# Show debug info in terminal? Warning: Slow! Do not leave on unintentionally. |
| 31 | +DEBUG = False |
| 32 | + |
| 33 | +# ------------------------------------------------------------------------------ |
| 34 | +# Device state |
| 35 | +# ------------------------------------------------------------------------------ |
| 36 | + |
| 37 | + |
| 38 | +class State(object): |
| 39 | + """Reflects the actual readings, parsed into separate variables, of the |
| 40 | + Arduino. There should only be one instance of the State class. |
| 41 | + """ |
| 42 | + |
| 43 | + def __init__(self): |
| 44 | + self.time = np.nan # [ms] |
| 45 | + self.reading_1 = np.nan |
| 46 | + |
| 47 | + |
| 48 | +state = State() |
| 49 | + |
| 50 | + |
| 51 | +# ------------------------------------------------------------------------------ |
| 52 | +# Program termination routines |
| 53 | +# ------------------------------------------------------------------------------ |
| 54 | + |
| 55 | + |
| 56 | +@QtCore.pyqtSlot() |
| 57 | +def notify_connection_lost(): |
| 58 | + print("\nCRITICAL ERROR: Connection lost") |
| 59 | + exit_program() |
| 60 | + |
| 61 | + |
| 62 | +@QtCore.pyqtSlot() |
| 63 | +def exit_program(): |
| 64 | + print("\nAbout to quit") |
| 65 | + |
| 66 | + app.processEvents() |
| 67 | + |
| 68 | + qdev.quit() |
| 69 | + ard.close() |
| 70 | + |
| 71 | + app.quit() |
| 72 | + |
| 73 | + |
| 74 | +# ------------------------------------------------------------------------------ |
| 75 | +# update_CLI |
| 76 | +# ------------------------------------------------------------------------------ |
| 77 | + |
| 78 | + |
| 79 | +@QtCore.pyqtSlot() |
| 80 | +def update_CLI(): |
| 81 | + print( |
| 82 | + "%i\t%.3f\t%.4f" |
| 83 | + % (qdev.update_counter_DAQ, state.time, state.reading_1) |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +# ------------------------------------------------------------------------------ |
| 88 | +# Your Arduino update function |
| 89 | +# ------------------------------------------------------------------------------ |
| 90 | + |
| 91 | + |
| 92 | +def DAQ_function(): |
| 93 | + # Query the Arduino for its state |
| 94 | + [success, tmp_state] = ard.query_ascii_values("?", separator="\t") |
| 95 | + if not (success): |
| 96 | + dprint("'%s' reports IOError" % ard.name) |
| 97 | + return False |
| 98 | + |
| 99 | + # Parse readings into separate state variables |
| 100 | + try: |
| 101 | + [state.time, state.reading_1] = tmp_state |
| 102 | + except Exception as err: |
| 103 | + pft(err, 3) |
| 104 | + dprint("'%s' reports IOError" % ard.name) |
| 105 | + return False |
| 106 | + |
| 107 | + # Use Arduino time or PC time? |
| 108 | + # Arduino time is more accurate, but rolls over ~49 days for a 32 bit timer. |
| 109 | + use_PC_time = True |
| 110 | + if use_PC_time: |
| 111 | + state.time = time.perf_counter() |
| 112 | + |
| 113 | + return True |
| 114 | + |
| 115 | + |
| 116 | +# ------------------------------------------------------------------------------ |
| 117 | +# Main |
| 118 | +# ------------------------------------------------------------------------------ |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + |
| 122 | + # Connect to Arduino |
| 123 | + ard = Arduino(name="Ard", baudrate=115200) |
| 124 | + ard.auto_connect( |
| 125 | + Path("last_used_port.txt"), match_identity="Wave generator" |
| 126 | + ) |
| 127 | + |
| 128 | + if not (ard.is_alive): |
| 129 | + print("\nCheck connection and try resetting the Arduino.") |
| 130 | + print("Exiting...\n") |
| 131 | + sys.exit(0) |
| 132 | + |
| 133 | + # Create application |
| 134 | + app = QtCore.QCoreApplication(sys.argv) |
| 135 | + app.aboutToQuit.connect(exit_program) |
| 136 | + |
| 137 | + # Set up multithreaded communication with the Arduino |
| 138 | + qdev = QDeviceIO(ard) |
| 139 | + qdev.create_worker_DAQ( |
| 140 | + DAQ_trigger = DAQ_trigger.INTERNAL_TIMER, |
| 141 | + DAQ_function = DAQ_function, |
| 142 | + DAQ_interval_ms = DAQ_INTERVAL, |
| 143 | + debug = DEBUG,) |
| 144 | + |
| 145 | + # Connect signals to slots |
| 146 | + qdev.signal_DAQ_updated.connect(update_CLI) |
| 147 | + qdev.signal_connection_lost.connect(notify_connection_lost) |
| 148 | + |
| 149 | + # Start workers |
| 150 | + qdev.start() |
| 151 | + |
| 152 | + # -------------------------------------------------------------------------- |
| 153 | + # Start the main loop |
| 154 | + # -------------------------------------------------------------------------- |
| 155 | + |
| 156 | + while True: |
| 157 | + app.processEvents() |
| 158 | + |
| 159 | + if qdev.update_counter_DAQ >= 20: |
| 160 | + exit_program() |
| 161 | + break |
0 commit comments