{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "2ff4f613-ccb4-40f5-9799-f53fec60bd15", "metadata": {}, "outputs": [], "source": [ "import serial\n", "import time\n", "import keyboard\n", "\n", "import pandas as pd\n", "import numpy as np\n", "#import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 2, "id": "30365d71-13e1-44e6-bc62-fd24fd6c1296", "metadata": {}, "outputs": [], "source": [ "# connect with Arduino\n", "\n", "ser = serial.Serial('COM3', 9600, timeout=1)\n", "time.sleep(1)" ] }, { "cell_type": "code", "execution_count": 3, "id": "ca0089c9-a0ce-4b17-8dab-f2ebbe952e7a", "metadata": { "tags": [] }, "outputs": [], "source": [ "# read write the serial connection at a given sampling rate\n", "\n", "def write_read(x):\n", " ser.write(bytes(x, 'utf-8'))\n", " time.sleep(0.01)\n", " data = ser.readline()\n", " return data" ] }, { "cell_type": "code", "execution_count": 4, "id": "7eb154f1-3041-4609-a9ed-ce4a402ef59e", "metadata": { "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Press <1> to switch power on\n", "Press <0> to switch power off\n", "Press to quit\n", ".............................\n", "2022-11-27 18:39:58.723099392 ['0', '0', '0'] \n", "You Pressed uit!\n" ] } ], "source": [ "# start to control the Arduino while collecting the data\n", "\n", "values=[]\n", "timestamp=[]\n", "i= True\n", "num = '0'\n", "print(\"Press <1> to switch power on\")\n", "print(\"Press <0> to switch power off\")\n", "print(\"Press to quit\")\n", "print(\".............................\")\n", "\n", "while i: # making a loop\n", " \n", " if keyboard.is_pressed('q'): # if key 'q' is pressed \n", " print('')\n", " print('You Pressed uit!')\n", " i = False # finishing the loop\n", " \n", " elif keyboard.is_pressed('0'): # if key '0' is pressed\n", " num = '0'\n", " \n", " elif keyboard.is_pressed('1'): # if key '1' is pressed\n", " num = '1'\n", " \n", " else:\n", " value = write_read(num) # if user pressed a key other than the given key the loop will break\n", " timestamp.append(time.time())\n", " dummy = value.decode().rstrip().split()\n", " # dum0 = np.array([[int(j)/1024*5 for j in dummy[1:]]])\n", " print(pd.to_datetime(time.time(), unit= 's'),dummy, \n", " #dum0[0],\n", " ' ', end = \"\\r\")\n", " values.append(dummy)\n", "\n", "ser.close()" ] }, { "cell_type": "markdown", "id": "0b1bf868-8efc-4a60-9d02-99d8a273d14c", "metadata": { "tags": [] }, "source": [ "# " ] }, { "cell_type": "code", "execution_count": 5, "id": "11cebe1b-4a8d-4ad7-88a2-4de9324521cd", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
SV0V1
count409.000000409.000000409.000000
mean0.4083131.3505570.594521
std0.4921231.8454171.126882
min0.0000000.0000000.000000
25%0.0000000.0000000.000000
50%0.0000000.0878910.000000
75%1.0000003.3349611.479492
max1.0000004.9951174.438477
\n", "
" ], "text/plain": [ " S V0 V1\n", "count 409.000000 409.000000 409.000000\n", "mean 0.408313 1.350557 0.594521\n", "std 0.492123 1.845417 1.126882\n", "min 0.000000 0.000000 0.000000\n", "25% 0.000000 0.000000 0.000000\n", "50% 0.000000 0.087891 0.000000\n", "75% 1.000000 3.334961 1.479492\n", "max 1.000000 4.995117 4.438477" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#timestamp[len(timestamp)-1]-timestamp[0]\n", "df = pd.DataFrame(values, index = pd.to_datetime(timestamp, unit = 's'), columns = ['S','V0','V1'])\n", "df[\"S\"] = pd.to_numeric(df[\"S\"], errors='coerce', downcast=\"float\")\n", "df[\"V0\"] = pd.to_numeric(df[\"V0\"], errors='coerce', downcast=\"float\")\n", "df[\"V1\"] = pd.to_numeric(df[\"V1\"], errors='coerce', downcast=\"float\")\n", "\n", "df[\"V0\"] = df[\"V0\"]/1024*5\n", "df[\"V1\"] = df[\"V1\"]/1024*5\n", "\n", "df.describe()" ] }, { "cell_type": "code", "execution_count": 7, "id": "c2a72641-bf31-4e3d-86b6-2b6abbc9ee66", "metadata": {}, "outputs": [], "source": [ "#df.to_excel(\"C:/Users/Admin/Pythonprojects/RAMS/data/kanweg.xlsx\")\n", "#df.to_excel(\"C:/Users/Admin/Pythonprojects/RAMS/data/Arduino_trainingset_UnbalancedIncomplete.xlsx\")" ] }, { "cell_type": "markdown", "id": "6570c8ae-444c-4629-86a9-6b32174176e6", "metadata": {}, "source": [ "# " ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }