|
1 |
| -# BioAmp-Tool-Python |
| 1 | +## BioAmp-Tool-Python |
2 | 2 |
|
3 |
| -A python tool to record data from BioAmp hardware.This project is designed to read data from an Arduino via a serial connection, process the data, and stream it using the Lab Streaming Layer (LSL) protocol. It also logs the processed data to a CSV file for further analysis. |
| 3 | +The BioAmp Tool is a Python script designed to interface with an Arduino-based bioamplifier, read data from it, and optionally log this data to CSV or stream it via Lab Streaming Layer (LSL). It provides real-time data processing and statistical analysis over 10-second and 10-minute intervals. |
4 | 4 |
|
5 | 5 | ## Features
|
6 | 6 |
|
7 |
| -- Auto-detects connected Arduino devices. |
8 |
| -- Reads and processes data packets from Arduino. |
9 |
| -- Streams data via LSL for real-time analysis. |
10 |
| -- Logs data to a CSV file, including counters and channel data. |
11 |
| -- Calculates and logs sampling rate and drift. |
12 |
| -- Handles missing samples and prints relevant errors. |
| 7 | +- **Automatic Arduino Detection:** Automatically detects connected Arduino devices via serial ports. |
| 8 | +- **Data Reading:** Reads and processes data packets from the Arduino. |
| 9 | +- **CSV Logging:** Optionally logs data to a CSV file. |
| 10 | +- **LSL Streaming:** Optionally streams data to an LSL outlet for integration with other software. |
| 11 | +- **Verbose Output:** Provides detailed statistics and error reporting. |
13 | 12 |
|
14 | 13 | ## Requirements
|
15 | 14 |
|
16 |
| -- Python 3.7 or higher |
17 |
| -- [pySerial](https://pypi.org/project/pyserial/) |
18 |
| -- [pylsl](https://pypi.org/project/pylsl/) |
19 |
| -- An Arduino device capable of sending serial data packets |
| 15 | +- Python 3.x |
| 16 | +- `pyserial` library (for serial communication) |
| 17 | +- `pylsl` library (for LSL streaming) |
| 18 | +- `argparse`, `time`, `csv`, `datetime` (standard libraries) |
20 | 19 |
|
21 | 20 | ## Installation
|
22 | 21 |
|
23 |
| -1. **Clone the repository**: |
24 |
| - ```bash |
25 |
| - git clone https://github.com/upsidedownlabs/BioAmp-Tool-Python.git |
26 |
| - ``` |
27 |
| - |
28 |
| -2. **Install the required Python packages**: |
29 |
| - ```bash |
30 |
| - pip install -r requirements.txt |
31 |
| - ``` |
| 22 | +1. Ensure you have Python 3.x installed. |
| 23 | +2. Install the required Python libraries: |
| 24 | + ```bash |
| 25 | + pip install pyserial pylsl |
| 26 | + ``` |
32 | 27 |
|
33 | 28 | ## Usage
|
34 | 29 |
|
35 |
| -1. **Connect your Arduino** to your computer via USB. |
| 30 | +To use the script, you can run it from the command line with various options: |
36 | 31 |
|
37 |
| -2. **Run the script** with the desired options: |
38 |
| - ```bash |
39 |
| - python bioamptool.py --detect |
40 |
| - ``` |
| 32 | +```bash |
| 33 | +python bioamp_tool.py [options] |
| 34 | +``` |
41 | 35 |
|
42 |
| -3. **View the output** on your console, which will include minute-by-minute data counts, 10-minute summaries, sampling rate , any detected errors or drift. |
| 36 | +### Options |
43 | 37 |
|
44 |
| -## Command-line Options |
| 38 | +- `-p`, `--port` <port>: Specify the serial port to use (e.g., COM3, /dev/ttyUSB0). |
| 39 | +- `-b`, `--baudrate` <baudrate>: Set the baud rate for serial communication (default is 57600). |
| 40 | +- `--csv`: Enable CSV logging. Data will be saved to a file with a timestamped name. |
| 41 | +- `--lsl`: Enable LSL streaming. Sends data to an LSL outlet. |
| 42 | +- `-v`, `--verbose`: Enable verbose output with detailed statistics and error reporting. |
45 | 43 |
|
46 |
| -- `-d, --detect`: Auto-detect the Arduino COM port. |
47 |
| -- `-p, --port`: Specify the COM port (e.g., `COM3` on Windows or `/dev/ttyUSB0` on Linux). |
48 |
| -- `-b, --baudrate`: Set the baud rate for serial communication (default is `57600`). |
49 | 44 |
|
50 |
| -Example: |
51 |
| -```bash |
52 |
| -python bioamptool.py --detect |
53 |
| -``` |
| 45 | +## Script Functions |
54 | 46 |
|
55 |
| -## Data Logging |
| 47 | +### `auto_detect_arduino(baudrate, timeout=1)` |
| 48 | + |
| 49 | +Detects an Arduino connected via serial port. Returns the port name if detected. |
| 50 | + |
| 51 | +### `read_arduino_data(ser, csv_writer=None)` |
| 52 | + |
| 53 | +Reads and processes data from the Arduino. Writes data to CSV and/or LSL stream if enabled. |
| 54 | + |
| 55 | +### `start_timer()` |
| 56 | + |
| 57 | +Initializes timers for 10-second and 10-minute intervals. |
56 | 58 |
|
57 |
| -- **CSV Output**: The script saves the processed data in a CSV file named `packet_data.csv`. |
58 |
| - - The CSV contains the following columns: |
59 |
| - - `Counter`: The sample counter from the Arduino. |
60 |
| - - `Channel1` to `Channel6`: The data values from each channel. |
| 59 | +### `log_ten_second_data(verbose=False)` |
61 | 60 |
|
62 |
| -- **Log Intervals**: The script logs data counts every minute and provides a summary every 10 minutes, including the sampling rate and drift in seconds per hour. |
| 61 | +Logs and resets data for the 10-second interval. |
63 | 62 |
|
64 |
| -## LSL Streaming |
| 63 | +### `log_ten_minute_data(verbose=False)` |
65 | 64 |
|
66 |
| -- **Stream Name**: `BioAmpDataStream` |
67 |
| -- **Stream Type**: `EXG` |
68 |
| -- **Channel Count**: `6` |
69 |
| -- **Sampling Rate**: `250 Hz` |
70 |
| -- **Data Format**: `float32` |
| 65 | +Logs data and statistics for the 10-minute interval. |
71 | 66 |
|
72 |
| -Use an LSL viewer (e.g., BrainVision Recorder) to visualize the streamed data in real-time. |
| 67 | +### `parse_data(port, baudrate, lsl_flag=False, csv_flag=False, verbose=False)` |
| 68 | + |
| 69 | +Parses data from Arduino and manages logging and streaming. |
| 70 | + |
| 71 | +### `main()` |
| 72 | + |
| 73 | +Handles command-line argument parsing and initiates data processing. |
| 74 | + |
| 75 | +## Data Logging |
| 76 | + |
| 77 | + -CSV Output: The script saves the processed data in a CSV file named data_%Y-%m-%d_%H-%M-%S.csv. |
| 78 | + |
| 79 | +The CSV contains the following columns: |
| 80 | + -Counter: The sample counter from the Arduino. |
| 81 | + Channel1 to Channel6: The data values from each channel. |
| 82 | + -Log Intervals: The script logs data counts every minute and provides a summary every 10 minutes, including the sampling rate and drift in seconds per hour. |
| 83 | + |
| 84 | +### LSL Streaming |
| 85 | + |
| 86 | +Stream Name: BioAmpDataStream |
| 87 | +Stream Type: EXG |
| 88 | +Channel Count: 6 |
| 89 | +Sampling Rate: 250 Hz |
| 90 | +Data Format: float32 |
| 91 | +Use an LSL viewer (e.g., BrainVision LSL Viewer) to visualize the streamed data in real-time. |
73 | 92 |
|
74 | 93 | ## Troubleshooting
|
75 | 94 |
|
76 |
| -- **Arduino Not Detected**: Ensure your Arduino is properly connected and recognized by your system. Use the `--detect` option to automatically find the Arduino port. |
77 |
| -- **Invalid Data Packets**: If you see messages about invalid data packets, check the data format and synchronization bytes being sent from the Arduino. |
78 |
| -- **Zero LSL Stream Utilization**: If the LSL stream shows 0% utilization, verify the stream is properly set up and data is being pushed to the outlet. |
| 95 | +- **Arduino Not Detected:** Ensure the Arduino is properly connected and powered. Check the serial port and baud rate settings. |
| 96 | +- **CSV File Not Created:** Ensure you have write permissions in the directory where the script is run. |
| 97 | +- **LSL Stream Issues:** Verify that the `pylsl` library is installed and configured correctly. |
79 | 98 |
|
80 | 99 | ## Contributors
|
81 | 100 |
|
82 |
| -We are thankful to our awesome contributors, the list below is alphabetically sorted. |
83 |
| - |
84 |
| -- [Payal Lakra](https://github.com/payallakra) |
| 101 | +We are thankful to our awesome contributors. |
85 | 102 |
|
| 103 | +@PayalLakra |
0 commit comments