From c80644ce0050745292d6ec3f9dddd64bd90497fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Ta=C5=9F?= <74613555+burkutken@users.noreply.github.com> Date: Tue, 28 Jan 2025 22:39:08 +0300 Subject: [PATCH] Add files via upload --- main/main.ino | 77 ++++++++++++++ smooted/smooted.ino | 90 ++++++++++++++++ smooted_w_display/smooted_w_display.ino | 130 ++++++++++++++++++++++++ 3 files changed, 297 insertions(+) create mode 100644 main/main.ino create mode 100644 smooted/smooted.ino create mode 100644 smooted_w_display/smooted_w_display.ino diff --git a/main/main.ino b/main/main.ino new file mode 100644 index 0000000..37c8c12 --- /dev/null +++ b/main/main.ino @@ -0,0 +1,77 @@ +#include //not needed + +#define J_LEFT_POTX A2 // Left joystick X-axis (Throttle) +#define J_LEFT_POTY A3 // Left joystick Y-axis (Yaw) +#define J_RIGHT_POTX A0 // Right joystick X-axis (Roll) +#define J_RIGHT_POTY A1 // Right joystick Y-axis (Pitch) + +int throttle = 0; +int yaw = 0; +int roll = 0; +int pitch = 0; + +//this is without display + +void initial_pos () { + + throttle = 0; + yaw = 127; + roll = 127; + pitch = 127; + +} + +bool arePotsSetAsWanted() { + return throttle < 35 && yaw < 35 && roll < 35 && pitch < 35; +} + +// Function to start serial communication and handle LED behavior +void start_serial() { + Serial.begin(19200); // Start Serial Communication + pinMode(LED_BUILTIN, OUTPUT); // Configure LED pin + + //if u want u can delete here, its just for configuration with your joysticks to starting up + if (arePotsSetAsWanted()) { + digitalWrite(LED_BUILTIN, LOW); // Turn off LED if pots are correctly set + Serial.println("Serial started successfully. Joysticks are in the default position."); + } else { + Serial.println("Joysticks are not in the default position. Adjust them!"); + // Blink LED to indicate error + for (int i = 0; i < 10; i++) { // Blink 10 times + digitalWrite(LED_BUILTIN, HIGH); + delay(1000); + digitalWrite(LED_BUILTIN, LOW); + delay(1000); + } + } +} + + +void setup() { + + pinMode(J_LEFT_POTX, INPUT); + pinMode(J_LEFT_POTY, INPUT); + pinMode(J_RIGHT_POTX, INPUT); + pinMode(J_RIGHT_POTY, INPUT); + + initial_pos(); + start_serial(); + +} + +void loop() { + throttle = map(analogRead(J_LEFT_POTX), 0, 1023, 0, 255); + yaw = map(analogRead(J_LEFT_POTY), 0, 1023, 0, 255); + roll = map(analogRead(J_RIGHT_POTY), 0, 1023, 0, 255); + pitch = map(analogRead(J_RIGHT_POTX), 0, 1023, 0, 255); + + Serial.print("Throttle = "); + Serial.print(throttle); + Serial.print(" Yaw = "); + Serial.print(yaw); + Serial.print(" Pitch = "); + Serial.print(pitch); + Serial.print(" Roll = "); + Serial.println(roll); + delay(100); +} diff --git a/smooted/smooted.ino b/smooted/smooted.ino new file mode 100644 index 0000000..9812b2a --- /dev/null +++ b/smooted/smooted.ino @@ -0,0 +1,90 @@ +#include +#include + + +// Define potentiometer pins for the joysticks +#define J_LEFT_POTX A2 // Left joystick X-axis (Throttle) +#define J_LEFT_POTY A3 // Left joystick Y-axis (Yaw) +#define J_RIGHT_POTX A0 // Right joystick X-axis (Roll) +#define J_RIGHT_POTY A1 // Right joystick Y-axis (Pitch) + +// Joystick variables +int throttle = 0; +int yaw = 0; +int roll = 0; +int pitch = 0; + +// Define smoothing parameters +const int SMOOTHING_FACTOR = 5; // Number of samples for averaging +int throttleSamples[SMOOTHING_FACTOR] = {0}; +int yawSamples[SMOOTHING_FACTOR] = {0}; +int rollSamples[SMOOTHING_FACTOR] = {0}; +int pitchSamples[SMOOTHING_FACTOR] = {0}; + +// Deadband threshold +const int DEAD_BAND = 5; + +// Function to calculate the moving average +int movingAverage(int *samples, int newValue) { + for (int i = 0; i < SMOOTHING_FACTOR - 1; i++) { + samples[i] = samples[i + 1]; + } + samples[SMOOTHING_FACTOR - 1] = newValue; + + int sum = 0; + for (int i = 0; i < SMOOTHING_FACTOR; i++) { + sum += samples[i]; + } + return sum / SMOOTHING_FACTOR; +} + +// Function to apply a deadband +int applyDeadband(int value, int center, int threshold) { + if (abs(value - center) <= threshold) { + return center; + } + return value; +} + +void setup() { + Serial.begin(19200); // Start Serial Communication + pinMode(LED_BUILTIN, OUTPUT); // Configure LED pin + + // Initialize joystick pins + pinMode(J_LEFT_POTX, INPUT); + pinMode(J_LEFT_POTY, INPUT); + pinMode(J_RIGHT_POTX, INPUT); + pinMode(J_RIGHT_POTY, INPUT); +} + +void loop() { + // Read analog joystick values + int rawThrottle = analogRead(J_LEFT_POTX); + int rawYaw = analogRead(J_LEFT_POTY); + int rawRoll = analogRead(J_RIGHT_POTY); + int rawPitch = analogRead(J_RIGHT_POTX); + + // Apply smoothing + throttle = movingAverage(throttleSamples, map(rawThrottle, 0, 1023, 0, 255)); + yaw = movingAverage(yawSamples, map(rawYaw, 0, 1023, 0, 255)); + roll = movingAverage(rollSamples, map(rawRoll, 0, 1023, 0, 255)); + pitch = movingAverage(pitchSamples, map(rawPitch, 0, 1023, 0, 255)); + + // Apply deadband + yaw = applyDeadband(yaw, 127, DEAD_BAND); + roll = applyDeadband(roll, 127, DEAD_BAND); + pitch = applyDeadband(pitch, 127, DEAD_BAND); + + // Output the smoothed and processed values + Serial.print("Throttle = "); + Serial.print(throttle); + Serial.print(" Yaw = "); + Serial.print(yaw); + Serial.print(" Roll = "); + Serial.print(roll); + Serial.print(" Pitch = "); + Serial.println(pitch); + + // Simulate control signals (for debugging, replace with your drone's communication protocol) + delay(100); +} diff --git a/smooted_w_display/smooted_w_display.ino b/smooted_w_display/smooted_w_display.ino new file mode 100644 index 0000000..0389089 --- /dev/null +++ b/smooted_w_display/smooted_w_display.ino @@ -0,0 +1,130 @@ +#include + +// Initialize the OLED display +U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NO_ACK); // Use U8G_I2C_OPT_NONE if needed + +// Define joystick pins +#define J_LEFT_POTX A2 // Throttle +#define J_LEFT_POTY A3 // Yaw +#define J_RIGHT_POTX A0 // Pitch +#define J_RIGHT_POTY A1 // Roll + +// Joystick variables +int throttle = 0; +int yaw = 0; +int roll = 0; +int pitch = 0; + +// Smoothing parameters +const int SMOOTHING_FACTOR = 5; +int throttleSamples[SMOOTHING_FACTOR] = {0}; +int yawSamples[SMOOTHING_FACTOR] = {0}; +int rollSamples[SMOOTHING_FACTOR] = {0}; +int pitchSamples[SMOOTHING_FACTOR] = {0}; + +// Deadband threshold +const int DEAD_BAND = 5; + +// Function to calculate the moving average +int movingAverage(int *samples, int newValue) { + for (int i = 0; i < SMOOTHING_FACTOR - 1; i++) { + samples[i] = samples[i + 1]; + } + samples[SMOOTHING_FACTOR - 1] = newValue; + + int sum = 0; + for (int i = 0; i < SMOOTHING_FACTOR; i++) { + sum += samples[i]; + } + return sum / SMOOTHING_FACTOR; +} + +// Function to apply a deadband +int applyDeadband(int value, int center, int threshold) { + if (abs(value - center) <= threshold) { + return center; + } + return value; +} + +// Function to display the opening screen +void displayOpeningScreen() { + unsigned long startTime = millis(); + + // Loop for 3 seconds + while (millis() - startTime < 3000) { + u8g.firstPage(); + do { + // Display "Technwave" in the center of the screen + u8g.setFont(u8g_font_fur17); // Larger font for opening screen + u8g.setPrintPos((128 - u8g.getStrWidth("technwave")) / 2, 32); // Center the text + u8g.print("technwave"); + } while (u8g.nextPage()); + } +} + +// Function to display joystick values on the OLED +void displayJoystickValues() { + u8g.firstPage(); + do { + // Draw titles and values on the screen + u8g.setFont(u8g_font_6x10); // Set font size + u8g.drawStr(0, 10, "Joystick Values:"); + + u8g.setPrintPos(0, 25); + u8g.print("Throttle: "); + u8g.print(throttle); + + u8g.setPrintPos(0, 35); + u8g.print("Yaw: "); + u8g.print(yaw); + + u8g.setPrintPos(0, 45); + u8g.print("Pitch: "); + u8g.print(pitch); + + u8g.setPrintPos(0, 55); + u8g.print("Roll: "); + u8g.print(roll); + } while (u8g.nextPage()); +} + +void setup() { + // Initialize the OLED display + u8g.setFont(u8g_font_6x10); + u8g.setColorIndex(1); // Set color to black on white + + // Display the opening screen + displayOpeningScreen(); + + // Initialize joystick pins + pinMode(J_LEFT_POTX, INPUT); + pinMode(J_LEFT_POTY, INPUT); + pinMode(J_RIGHT_POTX, INPUT); + pinMode(J_RIGHT_POTY, INPUT); +} + +void loop() { + // Read analog joystick values + int rawThrottle = analogRead(J_LEFT_POTX); + int rawYaw = analogRead(J_LEFT_POTY); + int rawRoll = analogRead(J_RIGHT_POTY); + int rawPitch = analogRead(J_RIGHT_POTX); + + // Apply smoothing + throttle = movingAverage(throttleSamples, map(rawThrottle, 0, 1023, 0, 255)); + yaw = movingAverage(yawSamples, map(rawYaw, 0, 1023, 0, 255)); + roll = movingAverage(rollSamples, map(rawRoll, 0, 1023, 0, 255)); + pitch = movingAverage(pitchSamples, map(rawPitch, 0, 1023, 0, 255)); + + // Apply deadband + yaw = applyDeadband(yaw, 127, DEAD_BAND); + roll = applyDeadband(roll, 127, DEAD_BAND); + pitch = applyDeadband(pitch, 127, DEAD_BAND); + + // Display joystick values on the OLED + displayJoystickValues(); + + // Delay for stability + delay(100); +}