From 244b2d4ed5ae652ca374dc2d8bf565761f01faa2 Mon Sep 17 00:00:00 2001 From: Kavindu_Methpura Date: Fri, 28 Feb 2025 11:47:02 +0530 Subject: [PATCH] creating a model --- .vscode/settings.json | 1 + hardware/.idea/.gitignore | 3 + hardware/.idea/hardware.iml | 10 + .../inspectionProfiles/profiles_settings.xml | 6 + hardware/.idea/misc.xml | 6 + hardware/.idea/modules.xml | 8 + hardware/.idea/vcs.xml | 6 + hardware/ML_model/mlModel/.gitignore | 5 + .../ML_model/mlModel/.vscode/extensions.json | 10 + hardware/ML_model/mlModel/include/README | 37 ++ hardware/ML_model/mlModel/lib/README | 46 ++ hardware/ML_model/mlModel/platformio.ini | 18 + hardware/ML_model/mlModel/src/main.cpp | 111 +++++ hardware/ML_model/mlModel/test/README | 11 + hardware/ML_model/test/.idea/.gitignore | 3 + .../inspectionProfiles/profiles_settings.xml | 6 + hardware/ML_model/test/.idea/misc.xml | 7 + hardware/ML_model/test/.idea/modules.xml | 8 + hardware/ML_model/test/.idea/test.iml | 10 + hardware/ML_model/test/.idea/vcs.xml | 6 + hardware/ML_model/test/arduino1.py | 69 +++ hardware/ML_model/test/ml1.py | 23 + .../test/model/Collect and Save Data.py | 29 ++ .../ML_model/test/model/Extract Gyro1 Data.py | 57 +++ hardware/ML_model/test/model/gyro1_data.csv | 76 ++++ hardware/ML_model/test/model/model.py | 15 + hardware/ML_model/test/model/predict.py | 14 + hardware/ML_model/test/model/preparedata.py | 26 ++ hardware/ML_model/test/model/sensor_data.csv | 394 ++++++++++++++++++ .../slave and master/master/master.ino | 2 +- .../connect_twogyro_and_bluetooth_module.ino | 101 +++-- .../connect_three_buttons.ino | 111 +++++ ...ct_twogyro_and_bluetooth_module_button.ino | 99 +++++ 33 files changed, 1308 insertions(+), 26 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 hardware/.idea/.gitignore create mode 100644 hardware/.idea/hardware.iml create mode 100644 hardware/.idea/inspectionProfiles/profiles_settings.xml create mode 100644 hardware/.idea/misc.xml create mode 100644 hardware/.idea/modules.xml create mode 100644 hardware/.idea/vcs.xml create mode 100644 hardware/ML_model/mlModel/.gitignore create mode 100644 hardware/ML_model/mlModel/.vscode/extensions.json create mode 100644 hardware/ML_model/mlModel/include/README create mode 100644 hardware/ML_model/mlModel/lib/README create mode 100644 hardware/ML_model/mlModel/platformio.ini create mode 100644 hardware/ML_model/mlModel/src/main.cpp create mode 100644 hardware/ML_model/mlModel/test/README create mode 100644 hardware/ML_model/test/.idea/.gitignore create mode 100644 hardware/ML_model/test/.idea/inspectionProfiles/profiles_settings.xml create mode 100644 hardware/ML_model/test/.idea/misc.xml create mode 100644 hardware/ML_model/test/.idea/modules.xml create mode 100644 hardware/ML_model/test/.idea/test.iml create mode 100644 hardware/ML_model/test/.idea/vcs.xml create mode 100644 hardware/ML_model/test/arduino1.py create mode 100644 hardware/ML_model/test/ml1.py create mode 100644 hardware/ML_model/test/model/Collect and Save Data.py create mode 100644 hardware/ML_model/test/model/Extract Gyro1 Data.py create mode 100644 hardware/ML_model/test/model/gyro1_data.csv create mode 100644 hardware/ML_model/test/model/model.py create mode 100644 hardware/ML_model/test/model/predict.py create mode 100644 hardware/ML_model/test/model/preparedata.py create mode 100644 hardware/ML_model/test/model/sensor_data.csv create mode 100644 hardware/connect_twogyro_and_bluetooth_module_button/connect_three_buttons/connect_three_buttons.ino create mode 100644 hardware/connect_twogyro_and_bluetooth_module_button/connect_twogyro_and_bluetooth_module_button.ino diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..9e26dfee --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/hardware/.idea/.gitignore b/hardware/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/hardware/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/hardware/.idea/hardware.iml b/hardware/.idea/hardware.iml new file mode 100644 index 00000000..1a20cf2b --- /dev/null +++ b/hardware/.idea/hardware.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/hardware/.idea/inspectionProfiles/profiles_settings.xml b/hardware/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..105ce2da --- /dev/null +++ b/hardware/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/hardware/.idea/misc.xml b/hardware/.idea/misc.xml new file mode 100644 index 00000000..fc93934a --- /dev/null +++ b/hardware/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/hardware/.idea/modules.xml b/hardware/.idea/modules.xml new file mode 100644 index 00000000..efa50d95 --- /dev/null +++ b/hardware/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/hardware/.idea/vcs.xml b/hardware/.idea/vcs.xml new file mode 100644 index 00000000..6c0b8635 --- /dev/null +++ b/hardware/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/hardware/ML_model/mlModel/.gitignore b/hardware/ML_model/mlModel/.gitignore new file mode 100644 index 00000000..89cc49cb --- /dev/null +++ b/hardware/ML_model/mlModel/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/hardware/ML_model/mlModel/.vscode/extensions.json b/hardware/ML_model/mlModel/.vscode/extensions.json new file mode 100644 index 00000000..080e70d0 --- /dev/null +++ b/hardware/ML_model/mlModel/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/hardware/ML_model/mlModel/include/README b/hardware/ML_model/mlModel/include/README new file mode 100644 index 00000000..49819c0d --- /dev/null +++ b/hardware/ML_model/mlModel/include/README @@ -0,0 +1,37 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the convention is to give header files names that end with `.h'. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/hardware/ML_model/mlModel/lib/README b/hardware/ML_model/mlModel/lib/README new file mode 100644 index 00000000..93793971 --- /dev/null +++ b/hardware/ML_model/mlModel/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into the executable file. + +The source code of each library should be placed in a separate directory +("lib/your_library_name/[Code]"). + +For example, see the structure of the following example libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +Example contents of `src/main.c` using Foo and Bar: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +The PlatformIO Library Dependency Finder will find automatically dependent +libraries by scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/hardware/ML_model/mlModel/platformio.ini b/hardware/ML_model/mlModel/platformio.ini new file mode 100644 index 00000000..7278dd1c --- /dev/null +++ b/hardware/ML_model/mlModel/platformio.ini @@ -0,0 +1,18 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:uno] +platform = atmelavr +board = uno +framework = arduino +lib_deps = + electroniccats/MPU6050@^1.4.1 + stevemarple/SoftWire@^2.0.10 + testato/SoftwareWire@^1.6.0 diff --git a/hardware/ML_model/mlModel/src/main.cpp b/hardware/ML_model/mlModel/src/main.cpp new file mode 100644 index 00000000..52f043cb --- /dev/null +++ b/hardware/ML_model/mlModel/src/main.cpp @@ -0,0 +1,111 @@ +#include "Wire.h" +#include "MPU6050.h" +#include + +SoftwareSerial BTSerial(2, 3); // Bluetooth TX (D3) & RX (D2) + +MPU6050 gyro1(0x68); // First MPU6050 (AD0 → GND) +MPU6050 gyro2(0x69); // Second MPU6050 (AD0 → VCC) + +int16_t gyroData[2][6]; // 2D array to store sensor data + +// Button pins +const int buttonPins[] = {4, 5, 6}; // Three buttons +int buttonStates[3] = {0, 0, 0}; // Store button states + +void setup() { + Serial.begin(115200); + BTSerial.begin(9600); + Wire.begin(); + + // Initialize buttons as INPUT_PULLUP + for (int i = 0; i < 3; i++) { + pinMode(buttonPins[i], INPUT_PULLUP); + } + + Serial.println("Initializing Gyroscopes..."); + + gyro1.initialize(); + if (!gyro1.testConnection()) Serial.println("Gyro1 (0x68) not responding!"); + else { + Serial.println("Gyro1 connected."); + calibrateMPU6050(gyro1); + } + + gyro2.initialize(); + if (!gyro2.testConnection()) Serial.println("Gyro2 (0x69) not responding!"); + else { + Serial.println("Gyro2 connected."); + calibrateMPU6050(gyro2); + } +} + +void calibrateMPU6050(MPU6050 &gyro) { + Serial.println("Calibrating..."); + int32_t gx_offset = 0, gy_offset = 0, gz_offset = 0; + + for (int i = 0; i < 200; i++) { + int16_t ax, ay, az, gx, gy, gz; + gyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); + + gx_offset += gx; + gy_offset += gy; + gz_offset += gz; + + delay(10); + } + + gx_offset /= 200; + gy_offset /= 200; + gz_offset /= 200; + + gyro.setXGyroOffset(-gx_offset); + gyro.setYGyroOffset(-gy_offset); + gyro.setZGyroOffset(-gz_offset); + + Serial.println("Calibration complete."); +} + +void loop() { + // Read data from first gyro + gyro1.getMotion6(&gyroData[0][0], &gyroData[0][1], &gyroData[0][2], &gyroData[0][3], &gyroData[0][4], &gyroData[0][5]); + + // Read data from second gyro + gyro2.getMotion6(&gyroData[1][0], &gyroData[1][1], &gyroData[1][2], &gyroData[1][3], &gyroData[1][4], &gyroData[1][5]); + + // Read button states (LOW when pressed, HIGH when released) + for (int i = 0; i < 3; i++) { + buttonStates[i] = digitalRead(buttonPins[i]); + } + + // Print and send data + sendData(); + + Serial.println("------------------------"); + delay(500); +} + +void sendData() { + for (int i = 0; i < 2; i++) { + Serial.print("Gyro"); Serial.print(i + 1); Serial.print(": "); + BTSerial.print("Gyro"); BTSerial.print(i + 1); BTSerial.print(": "); + + for (int j = 0; j < 6; j++) { + Serial.print(gyroData[i][j]); Serial.print("\t"); + BTSerial.print(gyroData[i][j]); BTSerial.print(","); + } + Serial.println(); + BTSerial.println(); + } + + // Send button states over Bluetooth + for (int i = 0; i < 3; i++) { + Serial.print("Button "); Serial.print(i + 1); + Serial.print(": "); + Serial.println(buttonStates[i] == LOW ? "PRESSED" : "RELEASED"); + + BTSerial.print("Button "); BTSerial.print(i + 1); + BTSerial.print(": "); + BTSerial.println(buttonStates[i] == LOW ? "PRESSED" : "RELEASED"); + } +} diff --git a/hardware/ML_model/mlModel/test/README b/hardware/ML_model/mlModel/test/README new file mode 100644 index 00000000..9b1e87bc --- /dev/null +++ b/hardware/ML_model/mlModel/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html diff --git a/hardware/ML_model/test/.idea/.gitignore b/hardware/ML_model/test/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/hardware/ML_model/test/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/hardware/ML_model/test/.idea/inspectionProfiles/profiles_settings.xml b/hardware/ML_model/test/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..105ce2da --- /dev/null +++ b/hardware/ML_model/test/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/hardware/ML_model/test/.idea/misc.xml b/hardware/ML_model/test/.idea/misc.xml new file mode 100644 index 00000000..b2f5fc8d --- /dev/null +++ b/hardware/ML_model/test/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/hardware/ML_model/test/.idea/modules.xml b/hardware/ML_model/test/.idea/modules.xml new file mode 100644 index 00000000..51ab9749 --- /dev/null +++ b/hardware/ML_model/test/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/hardware/ML_model/test/.idea/test.iml b/hardware/ML_model/test/.idea/test.iml new file mode 100644 index 00000000..9c9f2e4e --- /dev/null +++ b/hardware/ML_model/test/.idea/test.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/hardware/ML_model/test/.idea/vcs.xml b/hardware/ML_model/test/.idea/vcs.xml new file mode 100644 index 00000000..c2365ab1 --- /dev/null +++ b/hardware/ML_model/test/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/hardware/ML_model/test/arduino1.py b/hardware/ML_model/test/arduino1.py new file mode 100644 index 00000000..f871e281 --- /dev/null +++ b/hardware/ML_model/test/arduino1.py @@ -0,0 +1,69 @@ +import time +import pyfirmata +from smbus2 import SMBus + +# Bluetooth Serial Port (Change this to match your system) +SERIAL_PORT = "COM9" # Windows example: "COM3", Linux/macOS: "/dev/rfcomm0" + +# Initialize PyFirmata +board = pyfirmata.Arduino(SERIAL_PORT) + +# Define Button Pins +button_pins = [4, 5, 6] + +# Initialize Buttons as Inputs +for pin in button_pins: + board.digital[pin].mode = pyfirmata.INPUT + +# Initialize I2C (for MPU6050 sensors) +MPU6050_ADDR_1 = 0x68 # First MPU6050 (AD0 → GND) +MPU6050_ADDR_2 = 0x69 # Second MPU6050 (AD0 → VCC) + +bus = SMBus(1) # Use SMBus(1) for Raspberry Pi, check for other systems + +def initialize_mpu6050(address): + """ Initialize MPU6050 by waking it up """ + bus.write_byte_data(address, 0x6B, 0) # Wake up sensor + time.sleep(0.1) + +def read_mpu6050(address): + """ Read accelerometer and gyroscope data from MPU6050 """ + data = bus.read_i2c_block_data(address, 0x3B, 14) + accel_x = (data[0] << 8) | data[1] + accel_y = (data[2] << 8) | data[3] + accel_z = (data[4] << 8) | data[5] + gyro_x = (data[8] << 8) | data[9] + gyro_y = (data[10] << 8) | data[11] + gyro_z = (data[12] << 8) | data[13] + + return [accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z] + +# Initialize MPU6050 Sensors +initialize_mpu6050(MPU6050_ADDR_1) +initialize_mpu6050(MPU6050_ADDR_2) + +print("Reading Data...") + +while True: + try: + # Read Gyro Data + gyro1_data = read_mpu6050(MPU6050_ADDR_1) + gyro2_data = read_mpu6050(MPU6050_ADDR_2) + + # Read Button States + button_states = [board.digital[pin].read() for pin in button_pins] + + # Print Data + print(f"Gyro1: {gyro1_data}") + print(f"Gyro2: {gyro2_data}") + + for i, state in enumerate(button_states): + print(f"Button {i + 1}: {'PRESSED' if state == 0 else 'RELEASED'}") + + print("------------------------") + time.sleep(0.5) + + except KeyboardInterrupt: + print("Exiting...") + board.exit() + break diff --git a/hardware/ML_model/test/ml1.py b/hardware/ML_model/test/ml1.py new file mode 100644 index 00000000..7561822b --- /dev/null +++ b/hardware/ML_model/test/ml1.py @@ -0,0 +1,23 @@ +import serial + +# Replace 'COM12' with your actual COM port (e.g., 'COM3' on Windows or '/dev/ttyUSB0' on Linux/Mac) +serial_port = "COM12" +baud_rate = 9600 + +try: + # Open the serial connection + ser = serial.Serial(serial_port, baud_rate, timeout=1) + print(f"Connected to {serial_port} at {baud_rate} baud.") + + while True: + if ser.in_waiting > 0: # Check if data is available + data = ser.readline().decode('utf-8').strip() # Read and decode data + print(f"Received: {data}") + +except serial.SerialException as e: + print(f"Error: {e}") +except KeyboardInterrupt: + print("\nDisconnected.") +finally: + if 'ser' in locals() and ser.is_open: + ser.close() diff --git a/hardware/ML_model/test/model/Collect and Save Data.py b/hardware/ML_model/test/model/Collect and Save Data.py new file mode 100644 index 00000000..0e80d39a --- /dev/null +++ b/hardware/ML_model/test/model/Collect and Save Data.py @@ -0,0 +1,29 @@ +import serial +import csv +import time + +def read_and_save_data(serial_port='COM12', baud_rate=9600, file_name="sensor_data.csv"): + ser = serial.Serial(serial_port, baud_rate, timeout=1) + + with open(file_name, mode='w', newline='') as file: + writer = csv.writer(file) + writer.writerow(["timestamp", "raw_data"]) # Store all data as raw text + + while True: + try: + if ser.in_waiting > 0: + # Read line from serial and ignore encoding errors + data = ser.readline().decode('utf-8', errors='ignore').strip() + + # Save with timestamp + timestamp = time.strftime("%Y-%m-%d %H:%M:%S") + writer.writerow([timestamp, data]) + + # Print received data + print(f"[{timestamp}] {data}") + + except Exception as e: + print("Error:", e) + +# Start collecting data +read_and_save_data() diff --git a/hardware/ML_model/test/model/Extract Gyro1 Data.py b/hardware/ML_model/test/model/Extract Gyro1 Data.py new file mode 100644 index 00000000..34b110f0 --- /dev/null +++ b/hardware/ML_model/test/model/Extract Gyro1 Data.py @@ -0,0 +1,57 @@ +import pandas as pd + +# Input file (raw data) +INPUT_FILE = "sensor_data.csv" + +# Output file (cleaned Gyro1 data) +OUTPUT_FILE = "gyro1_data.csv" + + +def extract_gyro1_data(): + # Read the CSV with at least two columns: timestamp, raw_data + df = pd.read_csv(INPUT_FILE) + + # Prepare a list to hold valid Gyro1 rows + gyro1_rows = [] + + # Iterate through each row in the dataframe + for index, row in df.iterrows(): + raw_line = str(row["raw_data"]).strip() + + # Check if line starts with "Gyro1:" + if raw_line.startswith("Gyro1:"): + # Remove "Gyro1:" prefix + data_part = raw_line.replace("Gyro1:", "").strip() + + # Some lines have a trailing comma -> remove it if present + if data_part.endswith(","): + data_part = data_part[:-1] # remove last character + + # Split on commas + parts = data_part.split(",") + + # Expect exactly 6 numeric values (gx, gy, gz, ax, ay, az) + if len(parts) == 6: + try: + # Convert all parts to integers + gx, gy, gz, ax, ay, az = map(int, parts) + + # Append to our valid Gyro1 list + gyro1_rows.append([gx, gy, gz, ax, ay, az]) + except ValueError: + # If conversion fails, skip this line + pass + + # Convert the collected data into a new DataFrame + gyro1_df = pd.DataFrame( + gyro1_rows, + columns=["gx", "gy", "gz", "ax", "ay", "az"] + ) + + # Save to CSV + gyro1_df.to_csv(OUTPUT_FILE, index=False) + print(f"Saved {len(gyro1_df)} Gyro1 rows to {OUTPUT_FILE}") + + +if __name__ == "__main__": + extract_gyro1_data() diff --git a/hardware/ML_model/test/model/gyro1_data.csv b/hardware/ML_model/test/model/gyro1_data.csv new file mode 100644 index 00000000..8a2c35c0 --- /dev/null +++ b/hardware/ML_model/test/model/gyro1_data.csv @@ -0,0 +1,76 @@ +gx,gy,gz,ax,ay,az +15636,-600,-1948,2057,372,-141 +4572,2676,6400,-4321,927,-1443 +15480,-4152,6044,-1415,17552,-1981 +21016,-5452,6600,-8491,-9746,9381 +15860,-1596,2832,-32,-9526,-2799 +17112,-3128,-5388,-1929,-1731,283 +10144,5036,-8444,5686,9619,3469 +12996,3660,-11468,-1042,-7615,859 +3736,8812,-13668,2436,-6465,1782 +2736,8092,-12580,3152,422,3222 +812,6072,-16672,3548,537,392 +2616,3572,-13736,2601,3401,5361 +1980,9928,-14924,419,457,353 +1980,9928,-14924,419,457,353 +4564,9692,-10568,5714,-399,1001 +-6404,7720,-12168,10104,-22902,3597 +-9232,4080,-11724,2117,-90,-7446 +-896,9284,-12768,4659,-1065,43 +-3408,4072,-16172,7348,-1339,-1294 +-5288,5096,-3976,-2784,-1070,1987 +-8380,1776,-16008,19127,5508,-4979 +-6200,3024,-11812,-2944,-2824,-1527 +1868,10476,-15580,-201,2767,-6026 +-5976,11492,-8324,-1,3517,9729 +-2376,12844,-13256,10408,1720,6872 +13428,1700,13648,8985,-6134,-4407 +3152,13916,664,21928,-32084,-14145 +-12372,4168,-10016,538,463,-5126 +-7972,5348,-9180,9850,4424,5787 +-6316,7888,680,-2882,1757,-1781 +-4744,7860,-8244,5402,-2827,3340 +-708,10772,-15668,3206,-3493,2453 +-3848,5816,-11916,1974,4261,656 +-8268,8664,-7580,-601,-2658,-5806 +-7732,10076,-13720,6316,-10141,-8230 +2264,9196,-18100,121,-2839,4942 +-2308,3556,-9340,2821,6066,3314 +-5828,3252,-7024,-497,-6022,-5708 +-4848,5540,-7740,5390,853,6918 +-9684,2104,-7508,1288,1935,-9192 +-3428,12736,-17136,5283,-111,6930 +-12104,6900,-9896,-1529,-3952,1741 +-3284,4912,-6024,-2094,-1989,-14259 +-1508,5428,-17664,5370,11278,-585 +3108,420,-13256,-8314,2741,2753 +-7020,6576,-13472,1372,-5889,-1863 +-1708,4012,-15844,7415,7903,-1591 +14456,1348,-8864,330,7568,-1165 +16704,-1208,-7400,-5388,-12101,5637 +17208,-328,-2268,914,-818,-1678 +16168,308,-2260,1351,-1953,-1126 +16688,520,-2480,1373,-2126,-971 +16744,636,-2636,1359,-2134,-1022 +16724,540,-2644,1388,-2088,-986 +16808,428,-2620,1359,-2095,-1005 +16724,544,-2596,1377,-2125,-1000 +16768,496,-2548,1374,-2116,-1008 +16716,608,-2444,1378,-2102,-996 +16700,532,-2476,1373,-2125,-1005 +16772,496,-2592,1373,-2116,-998 +16740,432,-2552,1366,-2106,-1010 +16760,520,-2436,1366,-2121,-998 +16804,572,-2484,1367,-2120,-1006 +16656,516,-2564,1389,-2105,-1025 +16684,520,-2476,1379,-2100,-993 +16740,504,-2348,1382,-2125,-998 +16644,496,-2672,1363,-2109,-1026 +16760,536,-2668,1395,-2118,-1024 +16696,456,-2496,1339,-2113,-1009 +16724,528,-2544,1378,-2103,-996 +16756,500,-2448,1361,-2116,-1037 +16732,512,-2424,1368,-2097,-988 +16780,476,-2396,1364,-2119,-1004 +16760,460,-2396,1369,-2113,-1001 +16768,492,-2440,1389,-2112,-1034 diff --git a/hardware/ML_model/test/model/model.py b/hardware/ML_model/test/model/model.py new file mode 100644 index 00000000..0e1b4701 --- /dev/null +++ b/hardware/ML_model/test/model/model.py @@ -0,0 +1,15 @@ +import tensorflow as tf +from tensorflow.keras.models import Sequential +from tensorflow.keras.layers import LSTM, Dense + +# Build LSTM model +model = Sequential([ + LSTM(50, activation='relu', return_sequences=True, input_shape=(time_steps, 6)), + LSTM(50, activation='relu'), + Dense(6) # Output same format as input (gyro_x, gyro_y, gyro_z, acc_x, acc_y, acc_z) +]) + +model.compile(optimizer='adam', loss='mse') + +# Train the model +model.fit(X_train, y_train, epochs=50, batch_size=16, validation_data=(X_test, y_test)) diff --git a/hardware/ML_model/test/model/predict.py b/hardware/ML_model/test/model/predict.py new file mode 100644 index 00000000..952a7ab2 --- /dev/null +++ b/hardware/ML_model/test/model/predict.py @@ -0,0 +1,14 @@ +import matplotlib.pyplot as plt + +# Get predictions +y_pred = model.predict(X_test) + +# Convert back to original scale +y_pred_original = scaler.inverse_transform(y_pred) + +# Plot predictions vs actual values +plt.figure(figsize=(10,5)) +plt.plot(y_pred_original[:, 0], label="Predicted Gyro X") +plt.plot(y_test[:, 0], label="Actual Gyro X") +plt.legend() +plt.show() diff --git a/hardware/ML_model/test/model/preparedata.py b/hardware/ML_model/test/model/preparedata.py new file mode 100644 index 00000000..6d0ade69 --- /dev/null +++ b/hardware/ML_model/test/model/preparedata.py @@ -0,0 +1,26 @@ +import pandas as pd +import numpy as np +from sklearn.model_selection import train_test_split +from sklearn.preprocessing import MinMaxScaler + +# Load data +data = pd.read_csv("gyro1_data.csv") + +# Normalize data +scaler = MinMaxScaler() +data_scaled = scaler.fit_transform(data) + +# Convert to input-output pairs (for LSTM) +X = [] +y = [] +time_steps = 10 # Number of previous readings used for prediction + +for i in range(len(data_scaled) - time_steps): + X.append(data_scaled[i:i+time_steps]) + y.append(data_scaled[i+time_steps]) + +X = np.array(X) +y = np.array(y) + +# Split into training and testing +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) diff --git a/hardware/ML_model/test/model/sensor_data.csv b/hardware/ML_model/test/model/sensor_data.csv new file mode 100644 index 00000000..7e9348a1 --- /dev/null +++ b/hardware/ML_model/test/model/sensor_data.csv @@ -0,0 +1,394 @@ +timestamp,raw_data +2025-02-27 14:10:20,",-664,-2124,2082,298,786," +2025-02-27 14:10:20,"Gyro2: 16436,-856,-5568,-962,-489,-511," +2025-02-27 14:10:20,Button 1: RELEASED +2025-02-27 14:10:20,Button 2: RELEASED +2025-02-27 14:10:20,Button 3: RELEASED +2025-02-27 14:10:20,",-664,-2124,2082,298,786," +2025-02-27 14:10:20,"Gyro2: 16436,-856,-5568,-962,-489,-511," +2025-02-27 14:10:20,Button 1: RELEASED +2025-02-27 14:10:20,Button 2: RELEASED +2025-02-27 14:10:20,Button 3: RELEASED +2025-02-27 14:10:22,"Gyro2: 16492,-936,-5492,-961,-497,-495," +2025-02-27 14:10:22,Button 1: RELEASED +2025-02-27 14:10:22,Button 2: RELEASED +2025-02-27 14:10:22,Button 3: RELEASED +2025-02-27 14:10:23,"Gyro1: 15636,-600,-1948,2057,372,-141," +2025-02-27 14:10:23,"Gyro2: 15048,-892,-5172,-1164,-378,-1568," +2025-02-27 14:10:23,Button 1: RELEASED +2025-02-27 14:10:23,Button 2: RELEASED +2025-02-27 14:10:23,Button 3: RELEASED +2025-02-27 14:10:23,"Gyro1: 4572,2676,6400,-4321,927,-1443," +2025-02-27 14:10:23,"Gyro2: 7336,1704,7140,-8021,-560,-1180," +2025-02-27 14:10:23,Button 1: RELEASED +2025-02-27 14:10:23,Button 2: RELEASED +2025-02-27 14:10:23,Button 3: RELEASED +2025-02-27 14:10:24,"Gyro1: 15480,-4152,6044,-1415,17552,-1981," +2025-02-27 14:10:24,"Gyro2: 17316,-6768,-4252,-1591,19342,-2402," +2025-02-27 14:10:24,Button 1: RELEASED +2025-02-27 14:10:24,Button 2: RELEASED +2025-02-27 14:10:24,Button 3: RELEASED +2025-02-27 14:10:25,"Gyro1: 21016,-5452,6600,-8491,-9746,9381," +2025-02-27 14:10:25,"Gyro2: 19732,-6352,1956,-9946,-9539,8452," +2025-02-27 14:10:25,Button 1: RELEASED +2025-02-27 14:10:25,Button 2: RELEASED +2025-02-27 14:10:25,Button 3: RELEASED +2025-02-27 14:10:25,"Gyro1: 15860,-1596,2832,-32,-9526,-2799," +2025-02-27 14:10:25,"Gyro2: 15268,-1616,-1740,-3567,-10433,-4141," +2025-02-27 14:10:25,Button 1: RELEASED +2025-02-27 14:10:25,Button 2: RELEASED +2025-02-27 14:10:25,Button 3: RELEASED +2025-02-27 14:10:26,"Gyro1: 17112,-3128,-5388,-1929,-1731,283," +2025-02-27 14:10:26,"Gyro2: 15620,-3460,-8824,-5133,-2414,-707," +2025-02-27 14:10:26,Button 1: RELEASED +2025-02-27 14:10:26,Button 2: RELEASED +2025-02-27 14:10:26,Button 3: RELEASED +2025-02-27 14:10:27,"Gyro1: 10144,5036,-8444,5686,9619,3469," +2025-02-27 14:10:27,"Gyro2: 11492,4852,-11876,3338,8966,2791," +2025-02-27 14:10:27,Button 1: RELEASED +2025-02-27 14:10:27,Button 2: RELEASED +2025-02-27 14:10:27,Button 3: RELEASED +2025-02-27 14:10:27,"Gyro1: 12996,3660,-11468,-1042,-7615,859," +2025-02-27 14:10:27,"Gyro2: 9360,4640,-14584,-4266,-7938,-747," +2025-02-27 14:10:27,Button 1: RELEASED +2025-02-27 14:10:27,Button 2: RELEASED +2025-02-27 14:10:27,Button 3: RELEASED +2025-02-27 14:10:28,"Gyro1: 3736,8812,-13668,2436,-6465,1782," +2025-02-27 14:10:28,"Gyro2: 2152,8968,-14628,-801,-6869,291," +2025-02-27 14:10:28,Button 1: RELEASED +2025-02-27 14:10:28,Button 2: RELEASED +2025-02-27 14:10:28,Button 3: RELEASED +2025-02-27 14:10:29,"Gyro1: 2736,8092,-12580,3152,422,3222," +2025-02-27 14:10:29,"Gyro2: 1072,8092,-14764,703,-465,1761," +2025-02-27 14:10:29,Button 1: RELEASED +2025-02-27 14:10:29,Button 2: RELEASED +2025-02-27 14:10:29,Button 3: RELEASED +2025-02-27 14:10:29,"Gyro1: 812,6072,-16672,3548,537,392," +2025-02-27 14:10:29,"Gyro2: -1692,6284,-17940,135,-109,-1036," +2025-02-27 14:10:29,Button 1: RELEASED +2025-02-27 14:10:29,Button 2: RELEASED +2025-02-27 14:10:29,Button 3: RELEASED +2025-02-27 14:10:30,"Gyro1: 2616,3572,-13736,2601,3401,5361," +2025-02-27 14:10:30,"Gyro2: 788,3860,-12072,-244,2342,3938," +2025-02-27 14:10:30,Button 1: RELEASED +2025-02-27 14:10:30,Button 2: RELEASED +2025-02-27 14:10:30,Button 3: RELEASED +2025-02-27 14:10:31,"Gyro1: 1980,9928,-14924,419,457,353," +2025-02-27 14:10:31,"Gyro2: 660,9808,-15124,-2697,-534,-673," +2025-02-27 14:10:31,Button 1: RELEASED +2025-02-27 14:10:31,Button 2: RELEASED +2025-02-27 14:10:31,Button 3: RELEASED +2025-02-27 14:10:31,"Gyro1: 1980,9928,-14924,419,457,353," +2025-02-27 14:10:31,"Gyro2: 660,9808,-15124,-2697,-534,-673," +2025-02-27 14:10:31,Button 1: RELEASED +2025-02-27 14:10:31,Button 2: RELEASED +2025-02-27 14:10:31,Button 3: RELEASED +2025-02-27 14:10:32,"Gyro1: 4564,9692,-10568,5714,-399,1001," +2025-02-27 14:10:32,"Gyro2: 3976,9832,-11964,2475,-993,-597," +2025-02-27 14:10:32,Button 1: RELEASED +2025-02-27 14:10:32,Button 2: RELEASED +2025-02-27 14:10:32,Button 3: RELEASED +2025-02-27 14:10:33,"Gyro1: -6404,7720,-12168,10104,-22902,3597," +2025-02-27 14:10:33,"Gyro2: -5928,7936,-14316,7506,-23840,1529," +2025-02-27 14:10:33,Button 1: RELEASED +2025-02-27 14:10:33,Button 2: RELEASED +2025-02-27 14:10:33,Button 3: RELEASED +2025-02-27 14:10:33,"Gyro1: -9232,4080,-11724,2117,-90,-7446," +2025-02-27 14:10:33,"Gyro2: -10468,4652,-12484,-1944,-599,-8581," +2025-02-27 14:10:33,Button 1: RELEASED +2025-02-27 14:10:33,Button 2: RELEASED +2025-02-27 14:10:33,Button 3: RELEASED +2025-02-27 14:10:34,"Gyro1: -896,9284,-12768,4659,-1065,43," +2025-02-27 14:10:34,"Gyro2: -2936,9544,-13972,1578,-1797,-1696," +2025-02-27 14:10:34,Button 1: RELEASED +2025-02-27 14:10:34,Button 2: RELEASED +2025-02-27 14:10:34,Button 3: RELEASED +2025-02-27 14:10:35,"Gyro1: -3408,4072,-16172,7348,-1339,-1294," +2025-02-27 14:10:35,"Gyro2: -6644,4088,-16840,4059,-2530,-3627," +2025-02-27 14:10:35,Button 1: RELEASED +2025-02-27 14:10:35,Button 2: RELEASED +2025-02-27 14:10:35,Button 3: RELEASED +2025-02-27 14:10:35,"Gyro1: -5288,5096,-3976,-2784,-1070,1987," +2025-02-27 14:10:35,"Gyro2: -5968,5488,-4248,-5738,-1343,1171," +2025-02-27 14:10:35,Button 1: RELEASED +2025-02-27 14:10:35,Button 2: RELEASED +2025-02-27 14:10:35,Button 3: RELEASED +2025-02-27 14:10:36,"Gyro1: -8380,1776,-16008,19127,5508,-4979," +2025-02-27 14:10:36,"Gyro2: -10044,2300,-15912,15530,3847,-8066," +2025-02-27 14:10:36,Button 1: RELEASED +2025-02-27 14:10:36,Button 2: RELEASED +2025-02-27 14:10:36,Button 3: RELEASED +2025-02-27 14:10:37,"Gyro1: -6200,3024,-11812,-2944,-2824,-1527," +2025-02-27 14:10:37,"Gyro2: -7804,3536,-11896,-6411,-3477,-2404," +2025-02-27 14:10:37,Button 1: RELEASED +2025-02-27 14:10:37,Button 2: RELEASED +2025-02-27 14:10:37,Button 3: RELEASED +2025-02-27 14:10:37,"Gyro1: 1868,10476,-15580,-201,2767,-6026," +2025-02-27 14:10:37,"Gyro2: -468,10844,-16680,-4256,1953,-7137," +2025-02-27 14:10:37,Button 1: RELEASED +2025-02-27 14:10:37,Button 2: RELEASED +2025-02-27 14:10:37,Button 3: RELEASED +2025-02-27 14:10:38,"Gyro1: -5976,11492,-8324,-1,3517,9729," +2025-02-27 14:10:38,"Gyro2: -6068,12044,-8800,-1650,3047,8754," +2025-02-27 14:10:38,Button 1: RELEASED +2025-02-27 14:10:38,Button 2: RELEASED +2025-02-27 14:10:38,Button 3: RELEASED +2025-02-27 14:10:39,"Gyro1: -2376,12844,-13256,10408,1720,6872," +2025-02-27 14:10:39,"Gyro2: -4428,13256,-14672,8434,693,4292," +2025-02-27 14:10:39,Button 1: RELEASED +2025-02-27 14:10:39,Button 2: RELEASED +2025-02-27 14:10:39,Button 3: RELEASED +2025-02-27 14:10:54,"Gyro1: 13428,1700,13648,8985,-6134,-4407," +2025-02-27 14:10:54,"Gyro2: 14612,1532,9784,25797,-23485,-19450," +2025-02-27 14:10:54,Button 1: RELEASED +2025-02-27 14:10:54,Button 2: RELEASED +2025-02-27 14:10:54,Button 3: RELEASED +2025-02-27 14:10:54,"Gyro1: 3152,13916,664,21928,-32084,-14145," +2025-02-27 14:10:54,"Gyro2: 3836,14352,-2516,32767,-32768,-31249," +2025-02-27 14:10:54,Button 1: RELEASED +2025-02-27 14:10:54,Button 2: RELEASED +2025-02-27 14:10:54,Button 3: RELEASED +2025-02-27 14:10:55,"Gyro1: -12372,4168,-10016,538,463,-5126," +2025-02-27 14:10:55,"Gyro2: -13448,4892,-9368,17121,-16640,-18817," +2025-02-27 14:10:55,Button 1: RELEASED +2025-02-27 14:10:55,Button 2: RELEASED +2025-02-27 14:10:55,Button 3: RELEASED +2025-02-27 14:10:55,"Gyro1: -7972,5348,-9180,9850,4424,5787," +2025-02-27 14:10:55,"Gyro2: -8716,5900,-11280,28487,-13249,-9106," +2025-02-27 14:10:56,Button 1: RELEASED +2025-02-27 14:10:56,Button 2: RELEASED +2025-02-27 14:10:56,Button 3: RELEASED +2025-02-27 14:10:56,"Gyro1: -6316,7888,680,-2882,1757,-1781," +2025-02-27 14:10:56,"Gyro2: -6100,8140,-648,14571,-15077,-15127," +2025-02-27 14:10:56,Button 1: RELEASED +2025-02-27 14:10:56,Button 2: RELEASED +2025-02-27 14:10:56,Button 3: RELEASED +2025-02-27 14:10:57,"Gyro1: -4744,7860,-8244,5402,-2827,3340," +2025-02-27 14:10:57,"Gyro2: -5904,8036,-8956,23240,-19781,-11247," +2025-02-27 14:10:57,Button 1: RELEASED +2025-02-27 14:10:57,Button 2: RELEASED +2025-02-27 14:10:57,Button 3: RELEASED +2025-02-27 14:10:57,"Gyro1: -7172,10524,-80443699,-10027," +2025-02-27 14:10:58,"Gyro2: -8032,10816,-7988,11396,-20625,-23291," +2025-02-27 14:10:58,Button 1: RELEASED +2025-02-27 14:10:58,Button 2: RELEASED +2025-02-27 14:10:58,Button 3: RELEASED +2025-02-27 14:10:58,"Gyro1: -708,10772,-15668,3206,-3493,2453," +2025-02-27 14:10:58,"Gyro2: -2864,10996,-16744,20855,-20606,-11916," +2025-02-27 14:10:58,Button 1: RELEASED +2025-02-27 14:10:58,Button 2: RELEASED +2025-02-27 14:10:58,Button 3: RELEASED +2025-02-27 14:10:59,"Gyro1: -3848,5816,-11916,1974,4261,656," +2025-02-27 14:10:59,"Gyro2: -4788,6052,-11660,19293,-12610,-13142," +2025-02-27 14:10:59,Button 1: RELEASED +2025-02-27 14:10:59,Button 2: RELEASED +2025-02-27 14:10:59,Button 3: RELEASED +2025-02-27 14:10:59,"Gyro1: -8268,8664,-7580,-601,-2658,-5806," +2025-02-27 14:11:00,"Gyro2: -8964,9020,-7624,15892,-19698,-19493," +2025-02-27 14:11:00,Button 1: RELEASED +2025-02-27 14:11:00,Button 2: RELEASED +2025-02-27 14:11:00,Button 3: RELEASED +2025-02-27 14:11:00,"Gyro1: -7732,10076,-13720,6316,-10141,-8230," +2025-02-27 14:11:00,"Gyro2: -9388,10460,-13868,22252,-27395,-22935," +2025-02-27 14:11:00,Button 1: RELEASED +2025-02-27 14:11:00,Button 2: RELEASED +2025-02-27 14:11:00,Button 3: RELEASED +2025-02-27 14:11:01,"Gyro1: 2264,9196,-18100,121,-2839,4942," +2025-02-27 14:11:01,"Gyro2: -1572,9352,-18248,17853,-19836,-9368," +2025-02-27 14:11:01,Button 1: RELEASED +2025-02-27 14:11:01,Button 2: RELEASED +2025-02-27 14:11:01,Button 3: RELEASED +2025-02-27 14:11:01,"Gyro1: -2308,3556,-9340,2821,6066,3314," +2025-02-27 14:11:02,"Gyro2: -3280,3828,-10252,20815,-10733,-10740," +2025-02-27 14:11:02,Button 1: RELEASED +2025-02-27 14:11:02,Button 2: RELEASED +2025-02-27 14:11:02,Button 3: RELEASED +2025-02-27 14:11:02,"Gyro1: -5828,3252,-7024,-497,-6022,-5708," +2025-02-27 14:11:02,"Gyro2: -6772,3436,-7496,15894,-22973,-19548," +2025-02-27 14:11:02,Button 1: RELEASED +2025-02-27 14:11:02,Button 2: RELEASED +2025-02-27 14:11:02,Button 3: RELEASED +2025-02-27 14:11:03,"Gyro1: -4848,5540,-7740,5390,853,6918," +2025-02-27 14:11:03,"Gyro2: -5240,5912,-8888,23814,-16083,-7447," +2025-02-27 14:11:03,Button 1: RELEASED +2025-02-27 14:11:03,Button 2: RELEASED +2025-02-27 14:11:03,Button 3: RELEASED +2025-02-27 14:11:03,"Gyro1: -9684,2104,-7508,1288,1935,-9192," +2025-02-27 14:11:04,"Gyro2: -10400,2472,-7516,17433,-15178,-22926," +2025-02-27 14:11:04,Button 1: RELEASED +2025-02-27 14:11:04,Button 2: RELEASED +2025-02-27 14:11:04,Button 3: RELEASED +2025-02-27 14:11:04,"Gyro1: -3428,12736,-17136,5283,-111,6930," +2025-02-27 14:11:04,"Gyro2: -5172,13364,-17176,23404,-17153,-7441," +2025-02-27 14:11:04,Button 1: RELEASED +2025-02-27 14:11:04,Button 2: RELEASED +2025-02-27 14:11:04,Button 3: RELEASED +2025-02-27 14:11:05,"Gyro1: -12104,6900,-9896,-1529,-3952,1741," +2025-02-27 14:11:05,"Gyro2: -12824,7228,-9108,16052,-20882,-11852," +2025-02-27 14:11:05,Button 1: RELEASED +2025-02-27 14:11:05,Button 2: RELEASED +2025-02-27 14:11:05,Button 3: RELEASED +2025-02-27 14:11:05,"Gyro1: -3284,4912,-6024,-2094,-1989,-14259," +2025-02-27 14:11:06,"Gyro2: -4408,5148,-7372,13409,-19275,-27880," +2025-02-27 14:11:06,Button 1: RELEASED +2025-02-27 14:11:06,Button 2: RELEASED +2025-02-27 14:11:06,Button 3: RELEASED +2025-02-27 14:11:06,"Gyro1: -1508,5428,-17664,5370,11278,-585," +2025-02-27 14:11:06,"Gyro2: -3280,5860,-17836,22629,-5356,-14674," +2025-02-27 14:11:06,Button 1: RELEASED +2025-02-27 14:11:06,Button 2: RELEASED +2025-02-27 14:11:06,Button 3: RELEASED +2025-02-27 14:11:07,"Gyro1: 3108,420,-13256,-8314,2741,2753," +2025-02-27 14:11:07,"Gyro2: 284,788,-16132,9903,-14594,-10314," +2025-02-27 14:11:07,Button 1: RELEASED +2025-02-27 14:11:07,Button 2: RELEASED +2025-02-27 14:11:07,Button 3: RELEASED +2025-02-27 14:11:07,"Gyro1: -7020,6576,-13472,1372,-5889,-1863," +2025-02-27 14:11:08,"Gyro2: -5384,5968,-14136,18471,-22051,-15261," +2025-02-27 14:11:08,Button 1: RELEASED +2025-02-27 14:11:08,Button 2: RELEASED +2025-02-27 14:11:08,Button 3: RELEASED +2025-02-27 14:11:08,"Gyro1: -1708,4012,-15844,7415,7903,-1591," +2025-02-27 14:11:08,"Gyro2: -3696,4472,-16812,24601,-8895,-16047," +2025-02-27 14:11:08,Button 1: RELEASED +2025-02-27 14:11:08,Button 2: RELEASED +2025-02-27 14:11:08,Button 3: RELEASED +2025-02-27 14:11:09,"Gyro1: 14456,1348,-8864,330,7568,-1165," +2025-02-27 14:11:09,"Gyro2: 13260,1236,-11556,17473,-9144,-14706," +2025-02-27 14:11:09,Button 1: RELEASED +2025-02-27 14:11:09,Button 2: RELEASED +2025-02-27 14:11:09,Button 3: RELEASED +2025-02-27 14:11:09,"Gyro1: 16704,-1208,-7400,-5388,-12101,5637," +2025-02-27 14:11:10,"Gyro2: 17080,-1396,-10028,12306,-28229,-7401," +2025-02-27 14:11:10,Button 1: RELEASED +2025-02-27 14:11:10,Button 2: RELEASED +2025-02-27 14:11:10,Button 3: RELEASED +2025-02-27 14:11:10,"Gyro1: 17208,-328,-2268,914,-818,-1678," +2025-02-27 14:11:10,"Gyro2: 17320,-660,-5200,17820,-17663,-15411," +2025-02-27 14:11:10,Button 1: RELEASED +2025-02-27 14:11:10,Button 2: RELEASED +2025-02-27 14:11:10,Button 3: RELEASED +2025-02-27 14:11:11,"Gyro1: 16168,308,-2260,1351,-1953,-1126," +2025-02-27 14:11:11,"Gyro2: 15692,196,-5596,18449,-19034,-15098," +2025-02-27 14:11:11,Button 1: RELEASED +2025-02-27 14:11:11,Button 2: RELEASED +2025-02-27 14:11:11,Button 3: RELEASED +2025-02-27 14:11:11,"Gyro1: 16688,520,-2480,1373,-2126,-971," +2025-02-27 14:11:12,"Gyro2: 16316,456,-5972,18478,-19140,-14922," +2025-02-27 14:11:12,Button 1: RELEASED +2025-02-27 14:11:12,Button 2: RELEASED +2025-02-27 14:11:12,Button 3: RELEASED +2025-02-27 14:11:12,"Gyro1: 16744,636,-2636,1359,-2134,-1022," +2025-02-27 14:11:12,"Gyro2: 16436,312,-5956,18501,-19121,-14961," +2025-02-27 14:11:12,Button 1: RELEASED +2025-02-27 14:11:12,Button 2: RELEASED +2025-02-27 14:11:12,Button 3: RELEASED +2025-02-27 14:11:13,"Gyro1: 16724,540,-2644,1388,-2088,-986," +2025-02-27 14:11:13,"Gyro2: 16356,256,-6044,18480,-19126,-14950," +2025-02-27 14:11:13,Button 1: RELEASED +2025-02-27 14:11:13,Button 2: RELEASED +2025-02-27 14:11:13,Button 3: RELEASED +2025-02-27 14:11:13,"Gyro1: 16808,428,-2620,1359,-2095,-1005," +2025-02-27 14:11:14,"Gyro2: 16472,276,-5976,18490,-19150,-14966," +2025-02-27 14:11:14,Button 1: RELEASED +2025-02-27 14:11:14,Button 2: RELEASED +2025-02-27 14:11:14,Button 3: RELEASED +2025-02-27 14:11:14,"Gyro1: 16724,544,-2596,1377,-2125,-1000," +2025-02-27 14:11:14,"Gyro2: 16328,244,-5976,18503,-19115,-14962," +2025-02-27 14:11:14,Button 1: RELEASED +2025-02-27 14:11:14,Button 2: RELEASED +2025-02-27 14:11:14,Button 3: RELEASED +2025-02-27 14:11:15,"Gyro1: 16768,496,-2548,1374,-2116,-1008," +2025-02-27 14:11:15,"Gyro2: 16360,208,-5944,18495,-19142,-14942," +2025-02-27 14:11:15,Button 1: RELEASED +2025-02-27 14:11:15,Button 2: RELEASED +2025-02-27 14:11:15,Button 3: RELEASED +2025-02-27 14:11:15,"Gyro1: 16716,608,-2444,1378,-2102,-996," +2025-02-27 14:11:16,"Gyro2: 16392,332,-5856,18490,-19148,-14952," +2025-02-27 14:11:16,Button 1: RELEASED +2025-02-27 14:11:16,Button 2: RELEASED +2025-02-27 14:11:16,Button 3: RELEASED +2025-02-27 14:11:16,"Gyro1: 16700,532,-2476,1373,-2125,-1005," +2025-02-27 14:11:16,"Gyro2: 16408,152,-5836,18493,-19145,-14922," +2025-02-27 14:11:16,Button 1: RELEASED +2025-02-27 14:11:16,Button 2: RELEASED +2025-02-27 14:11:16,Button 3: RELEASED +2025-02-27 14:11:17,"Gyro1: 16772,496,-2592,1373,-2116,-998," +2025-02-27 14:11:17,"Gyro2: 16444,252,-5940,18479,-19130,-14953," +2025-02-27 14:11:17,Button 1: RELEASED +2025-02-27 14:11:17,Button 2: RELEASED +2025-02-27 14:11:17,Button 3: RELEASED +2025-02-27 14:11:17,"Gyro1: 16740,432,-2552,1366,-2106,-1010," +2025-02-27 14:11:18,"Gyro2: 16476,236,-6040,18493,-19129,-14935," +2025-02-27 14:11:18,Button 1: RELEASED +2025-02-27 14:11:18,Button 2: RELEASED +2025-02-27 14:11:18,Button 3: RELEASED +2025-02-27 14:11:18,"Gyro1: 16760,520,-2436,1366,-2121,-998," +2025-02-27 14:11:18,"Gyro2: 16492,296,-6000,18461,-19107,-14961," +2025-02-27 14:11:18,Button 1: RELEASED +2025-02-27 14:11:18,Button 2: RELEASED +2025-02-27 14:11:18,Button 3: RELEASED +2025-02-27 14:11:19,"Gyro1: 16804,572,-2484,1367,-2120,-1006," +2025-02-27 14:11:19,"Gyro2: 16412,312,-5912,18492,-19130,-14956," +2025-02-27 14:11:19,Button 1: RELEASED +2025-02-27 14:11:19,Button 2: RELEASED +2025-02-27 14:11:19,Button 3: RELEASED +2025-02-27 14:11:19,"Gyro1: 16656,516,-2564,1389,-2105,-1025," +2025-02-27 14:11:20,"Gyro2: 16484,328,-5956,18506,-19132,-14963," +2025-02-27 14:11:20,Button 1: RELEASED +2025-02-27 14:11:20,Button 2: RELEASED +2025-02-27 14:11:20,Button 3: RELEASED +2025-02-27 14:11:20,"Gyro1: 16684,520,-2476,1379,-2100,-993," +2025-02-27 14:11:20,"Gyro2: 16288,252,-6000,18485,-19153,-14963," +2025-02-27 14:11:20,Button 1: RELEASED +2025-02-27 14:11:20,Button 2: RELEASED +2025-02-27 14:11:20,Button 3: RELEASED +2025-02-27 14:11:21,"Gyro1: 16740,504,-2348,1382,-2125,-998," +2025-02-27 14:11:21,"Gyro2: 16388,208,-5952,18496,-19136,-14960," +2025-02-27 14:11:21,Button 1: RELEASED +2025-02-27 14:11:21,Button 2: RELEASED +2025-02-27 14:11:21,Button 3: RELEASED +2025-02-27 14:11:21,"Gyro1: 16644,496,-2672,1363,-2109,-1026," +2025-02-27 14:11:22,"Gyro2: 16372,360,-5976,18502,-19111,-14948," +2025-02-27 14:11:22,Button 1: RELEASED +2025-02-27 14:11:22,Button 2: RELEASED +2025-02-27 14:11:22,Button 3: RELEASED +2025-02-27 14:11:22,"Gyro1: 16760,536,-2668,1395,-2118,-1024," +2025-02-27 14:11:22,"Gyro2: 16324,408,-5804,18499,-19136,-14949," +2025-02-27 14:11:22,Button 1: RELEASED +2025-02-27 14:11:22,Button 2: RELEASED +2025-02-27 14:11:22,Button 3: RELEASED +2025-02-27 14:11:23,"Gyro1: 16696,456,-2496,1339,-2113,-1009," +2025-02-27 14:11:23,"Gyro2: 16364,264,-6016,18483,-19128,-14956," +2025-02-27 14:11:23,Button 1: RELEASED +2025-02-27 14:11:23,Button 2: RELEASED +2025-02-27 14:11:23,Button 3: RELEASED +2025-02-27 14:11:23,"Gyro1: 16724,528,-2544,1378,-2103,-996," +2025-02-27 14:11:24,"Gyro2: 16400,264,-5844,18496,-19127,-14947," +2025-02-27 14:11:24,Button 1: RELEASED +2025-02-27 14:11:24,Button 2: RELEASED +2025-02-27 14:11:24,Button 3: RELEASED +2025-02-27 14:11:24,"Gyro1: 16756,500,-2448,1361,-2116,-1037," +2025-02-27 14:11:24,"Gyro2: 16436,272,-5916,18499,-19133,-14940," +2025-02-27 14:11:24,Button 1: RELEASED +2025-02-27 14:11:24,Button 2: RELEASED +2025-02-27 14:11:24,Button 3: RELEASED +2025-02-27 14:11:25,"Gyro1: 16732,512,-2424,1368,-2097,-988," +2025-02-27 14:11:25,"Gyro2: 16340,224,-5824,18492,-19128,-14958," +2025-02-27 14:11:25,Button 1: RELEASED +2025-02-27 14:11:25,Button 2: RELEASED +2025-02-27 14:11:25,Button 3: RELEASED +2025-02-27 14:11:25,"Gyro1: 16780,476,-2396,1364,-2119,-1004," +2025-02-27 14:11:26,"Gyro2: 16348,328,-5848,18505,-19136,-14946," +2025-02-27 14:11:26,Button 1: RELEASED +2025-02-27 14:11:26,Button 2: RELEASED +2025-02-27 14:11:26,Button 3: RELEASED +2025-02-27 14:11:26,"Gyro1: 16760,460,-2396,1369,-2113,-1001," +2025-02-27 14:11:26,"Gyro2: 16376,228,-5916,18503,-19135,-14955," +2025-02-27 14:11:26,Button 1: RELEASED +2025-02-27 14:11:26,Button 2: RELEASED +2025-02-27 14:11:26,Button 3: RELEASED +2025-02-27 14:11:27,"Gyro1: 16768,492,-2440,1389,-2112,-1034," +2025-02-27 14:11:27,"Gyro2: 16400,304,-5804,18487,-19127,-14967," +2025-02-27 14:11:27,Button 1: RELEASEn 2: RELEASED +2025-02-27 14:11:27,Button 3: RELEASED diff --git a/hardware/ardino codes/slave and master/master/master.ino b/hardware/ardino codes/slave and master/master/master.ino index 26c2f69b..37ec7beb 100644 --- a/hardware/ardino codes/slave and master/master/master.ino +++ b/hardware/ardino codes/slave and master/master/master.ino @@ -1,6 +1,6 @@ #include -SoftwareSerial BT(8,9); // TX=2, RX=3 +SoftwareSerial BT(2,3); // TX=2, RX=3 void setup() { Serial.begin(9600); diff --git a/hardware/connect_twogyro_and_bluetooth_module/connect_twogyro_and_bluetooth_module.ino b/hardware/connect_twogyro_and_bluetooth_module/connect_twogyro_and_bluetooth_module.ino index 3a415e16..80daf667 100644 --- a/hardware/connect_twogyro_and_bluetooth_module/connect_twogyro_and_bluetooth_module.ino +++ b/hardware/connect_twogyro_and_bluetooth_module/connect_twogyro_and_bluetooth_module.ino @@ -1,45 +1,96 @@ -#include -#include +#include "Wire.h" +#include "MPU6050.h" +#include + +SoftwareSerial BTSerial(2, 3); // Bluetooth TX (D3) & RX (D2) MPU6050 gyro1(0x68); // First MPU6050 (AD0 → GND) MPU6050 gyro2(0x69); // Second MPU6050 (AD0 → VCC) +// 2D array to store sensor data: [Gyro Index][6 Data Points] +int16_t gyroData[2][6]; + void setup() { - Serial.begin(115200); - Wire.begin(); // Initialize I2C as Master + Serial.begin(115200); // Serial monitor + BTSerial.begin(9600); // Bluetooth module + Wire.begin(); // Initialize hardware I2C Serial.println("Initializing Gyroscopes..."); + // Initialize Gyroscope 1 gyro1.initialize(); - if (!gyro1.testConnection()) Serial.println("Gyro1 not responding!"); else Serial.println("Gyro1 connected."); + if (!gyro1.testConnection()) { + Serial.println("Gyro1 (0x68) not responding!"); + } else { + Serial.println("Gyro1 connected."); + calibrateMPU6050(gyro1); + } + // Initialize Gyroscope 2 gyro2.initialize(); - if (!gyro2.testConnection()) Serial.println("Gyro2 not responding!"); else Serial.println("Gyro2 connected."); + if (!gyro2.testConnection()) { + Serial.println("Gyro2 (0x69) not responding!"); + } else { + Serial.println("Gyro2 connected."); + calibrateMPU6050(gyro2); + } } -void loop() { - int16_t ax, ay, az, gx, gy, gz; +// Function to calibrate MPU6050 gyroscope offsets +void calibrateMPU6050(MPU6050 &gyro) { + Serial.println("Calibrating..."); + int32_t gx_offset = 0, gy_offset = 0, gz_offset = 0; + + // Take multiple readings to calculate average offset + for (int i = 0; i < 200; i++) { + int16_t ax, ay, az, gx, gy, gz; + gyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); + + gx_offset += gx; + gy_offset += gy; + gz_offset += gz; + + delay(10); // Small delay between readings + } + + // Compute average offset + gx_offset /= 200; + gy_offset /= 200; + gz_offset /= 200; + // Set offsets + gyro.setXGyroOffset(-gx_offset); + gyro.setYGyroOffset(-gy_offset); + gyro.setZGyroOffset(-gz_offset); + + Serial.println("Calibration complete."); +} + +void loop() { // Read data from first gyro - gyro1.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); - Serial.print("Gyro1: X="); Serial.print(gx); Serial.print(" Y="); Serial.print(gy); Serial.print(" Z="); Serial.println(gz); + gyro1.getMotion6(&gyroData[0][0], &gyroData[0][1], &gyroData[0][2], &gyroData[0][3], &gyroData[0][4], &gyroData[0][5]); // Read data from second gyro - gyro2.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); - Serial.print("Gyro2: X="); Serial.print(gx); Serial.print(" Y="); Serial.print(gy); Serial.print(" Z="); Serial.println(gz); - - // Request data from second Arduino (Slave) - Wire.requestFrom(8, 6); // Request 6 bytes (X, Y, Z gyro) - if (Wire.available() == 6) { - gx = Wire.read() << 8 | Wire.read(); - gy = Wire.read() << 8 | Wire.read(); - gz = Wire.read() << 8 | Wire.read(); - - Serial.print("Gyro3: X="); Serial.print(gx); - Serial.print(" Y="); Serial.print(gy); - Serial.print(" Z="); Serial.println(gz); - } + gyro2.getMotion6(&gyroData[1][0], &gyroData[1][1], &gyroData[1][2], &gyroData[1][3], &gyroData[1][4], &gyroData[1][5]); + + // Print and send data + sendData(); Serial.println("------------------------"); - delay(1000); + delay(1000); // Wait 1 second before next reading +} + +// Function to send data via Bluetooth +void sendData() { + for (int i = 0; i < 2; i++) { // Loop through both gyros + Serial.print("Gyro"); Serial.print(i + 1); Serial.print(": "); + BTSerial.print("Gyro"); BTSerial.print(i + 1); BTSerial.print(": "); + + for (int j = 0; j < 6; j++) { // Loop through 6 data points + Serial.print(gyroData[i][j]); Serial.print("\t"); + BTSerial.print(gyroData[i][j]); BTSerial.print(","); + } + Serial.println(); + BTSerial.println(); // Send newline over Bluetooth + } } diff --git a/hardware/connect_twogyro_and_bluetooth_module_button/connect_three_buttons/connect_three_buttons.ino b/hardware/connect_twogyro_and_bluetooth_module_button/connect_three_buttons/connect_three_buttons.ino new file mode 100644 index 00000000..52f043cb --- /dev/null +++ b/hardware/connect_twogyro_and_bluetooth_module_button/connect_three_buttons/connect_three_buttons.ino @@ -0,0 +1,111 @@ +#include "Wire.h" +#include "MPU6050.h" +#include + +SoftwareSerial BTSerial(2, 3); // Bluetooth TX (D3) & RX (D2) + +MPU6050 gyro1(0x68); // First MPU6050 (AD0 → GND) +MPU6050 gyro2(0x69); // Second MPU6050 (AD0 → VCC) + +int16_t gyroData[2][6]; // 2D array to store sensor data + +// Button pins +const int buttonPins[] = {4, 5, 6}; // Three buttons +int buttonStates[3] = {0, 0, 0}; // Store button states + +void setup() { + Serial.begin(115200); + BTSerial.begin(9600); + Wire.begin(); + + // Initialize buttons as INPUT_PULLUP + for (int i = 0; i < 3; i++) { + pinMode(buttonPins[i], INPUT_PULLUP); + } + + Serial.println("Initializing Gyroscopes..."); + + gyro1.initialize(); + if (!gyro1.testConnection()) Serial.println("Gyro1 (0x68) not responding!"); + else { + Serial.println("Gyro1 connected."); + calibrateMPU6050(gyro1); + } + + gyro2.initialize(); + if (!gyro2.testConnection()) Serial.println("Gyro2 (0x69) not responding!"); + else { + Serial.println("Gyro2 connected."); + calibrateMPU6050(gyro2); + } +} + +void calibrateMPU6050(MPU6050 &gyro) { + Serial.println("Calibrating..."); + int32_t gx_offset = 0, gy_offset = 0, gz_offset = 0; + + for (int i = 0; i < 200; i++) { + int16_t ax, ay, az, gx, gy, gz; + gyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); + + gx_offset += gx; + gy_offset += gy; + gz_offset += gz; + + delay(10); + } + + gx_offset /= 200; + gy_offset /= 200; + gz_offset /= 200; + + gyro.setXGyroOffset(-gx_offset); + gyro.setYGyroOffset(-gy_offset); + gyro.setZGyroOffset(-gz_offset); + + Serial.println("Calibration complete."); +} + +void loop() { + // Read data from first gyro + gyro1.getMotion6(&gyroData[0][0], &gyroData[0][1], &gyroData[0][2], &gyroData[0][3], &gyroData[0][4], &gyroData[0][5]); + + // Read data from second gyro + gyro2.getMotion6(&gyroData[1][0], &gyroData[1][1], &gyroData[1][2], &gyroData[1][3], &gyroData[1][4], &gyroData[1][5]); + + // Read button states (LOW when pressed, HIGH when released) + for (int i = 0; i < 3; i++) { + buttonStates[i] = digitalRead(buttonPins[i]); + } + + // Print and send data + sendData(); + + Serial.println("------------------------"); + delay(500); +} + +void sendData() { + for (int i = 0; i < 2; i++) { + Serial.print("Gyro"); Serial.print(i + 1); Serial.print(": "); + BTSerial.print("Gyro"); BTSerial.print(i + 1); BTSerial.print(": "); + + for (int j = 0; j < 6; j++) { + Serial.print(gyroData[i][j]); Serial.print("\t"); + BTSerial.print(gyroData[i][j]); BTSerial.print(","); + } + Serial.println(); + BTSerial.println(); + } + + // Send button states over Bluetooth + for (int i = 0; i < 3; i++) { + Serial.print("Button "); Serial.print(i + 1); + Serial.print(": "); + Serial.println(buttonStates[i] == LOW ? "PRESSED" : "RELEASED"); + + BTSerial.print("Button "); BTSerial.print(i + 1); + BTSerial.print(": "); + BTSerial.println(buttonStates[i] == LOW ? "PRESSED" : "RELEASED"); + } +} diff --git a/hardware/connect_twogyro_and_bluetooth_module_button/connect_twogyro_and_bluetooth_module_button.ino b/hardware/connect_twogyro_and_bluetooth_module_button/connect_twogyro_and_bluetooth_module_button.ino new file mode 100644 index 00000000..9891048d --- /dev/null +++ b/hardware/connect_twogyro_and_bluetooth_module_button/connect_twogyro_and_bluetooth_module_button.ino @@ -0,0 +1,99 @@ +#include "Wire.h" +#include "MPU6050.h" +#include + +SoftwareSerial BTSerial(2, 3); // Bluetooth TX (D3) & RX (D2) + +MPU6050 gyro1(0x68); // First MPU6050 (AD0 → GND) +MPU6050 gyro2(0x69); // Second MPU6050 (AD0 → VCC) + +int16_t gyroData[2][6]; // 2D array to store sensor data +const int buttonPin = 4; // Button connected to D4 +int buttonState = 0; // Variable to store button state + +void setup() { + Serial.begin(115200); + BTSerial.begin(9600); + Wire.begin(); + + pinMode(buttonPin, INPUT_PULLUP); // Use INPUT_PULLUP instead of INPUT_PULLDOWN + + Serial.println("Initializing Gyroscopes..."); + + gyro1.initialize(); + if (!gyro1.testConnection()) Serial.println("Gyro1 (0x68) not responding!"); + else { + Serial.println("Gyro1 connected."); + calibrateMPU6050(gyro1); + } + + gyro2.initialize(); + if (!gyro2.testConnection()) Serial.println("Gyro2 (0x69) not responding!"); + else { + Serial.println("Gyro2 connected."); + calibrateMPU6050(gyro2); + } +} + +void calibrateMPU6050(MPU6050 &gyro) { + Serial.println("Calibrating..."); + int32_t gx_offset = 0, gy_offset = 0, gz_offset = 0; + + for (int i = 0; i < 200; i++) { + int16_t ax, ay, az, gx, gy, gz; + gyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); + + gx_offset += gx; + gy_offset += gy; + gz_offset += gz; + + delay(10); + } + + gx_offset /= 200; + gy_offset /= 200; + gz_offset /= 200; + + gyro.setXGyroOffset(-gx_offset); + gyro.setYGyroOffset(-gy_offset); + gyro.setZGyroOffset(-gz_offset); + + Serial.println("Calibration complete."); +} + +void loop() { + // Read data from first gyro + gyro1.getMotion6(&gyroData[0][0], &gyroData[0][1], &gyroData[0][2], &gyroData[0][3], &gyroData[0][4], &gyroData[0][5]); + + // Read data from second gyro + gyro2.getMotion6(&gyroData[1][0], &gyroData[1][1], &gyroData[1][2], &gyroData[1][3], &gyroData[1][4], &gyroData[1][5]); + + // Read button state (LOW when pressed, HIGH when released) + buttonState = digitalRead(buttonPin); + + // Print and send data + sendData(); + + Serial.println("------------------------"); + delay(500); +} + +void sendData() { + for (int i = 0; i < 2; i++) { + Serial.print("Gyro"); Serial.print(i + 1); Serial.print(": "); + BTSerial.print("Gyro"); BTSerial.print(i + 1); BTSerial.print(": "); + + for (int j = 0; j < 6; j++) { + Serial.print(gyroData[i][j]); Serial.print("\t"); + BTSerial.print(gyroData[i][j]); BTSerial.print(","); + } + Serial.println(); + BTSerial.println(); + } + + // Send button state over Bluetooth + Serial.print("Button State: "); + Serial.println(buttonState == LOW ? "PRESSED" : "RELEASED"); + BTSerial.print("Button State: "); + BTSerial.println(buttonState == LOW ? "PRESSED" : "RELEASED"); +}