From 62e06d62e41d35252f98bebf85f7260423374641 Mon Sep 17 00:00:00 2001 From: Lukas Reichwein Date: Wed, 15 Jan 2020 21:57:34 +0100 Subject: [PATCH] Added the joystick control lib with example use (Joystick.ino) --- Aufgaben.txt | 1 + Code/Joystick/Joystick.ino | 42 +++++++++++++++++ Code/libraries/Joystick/Joystick.cpp | 17 +++++++ Code/libraries/Joystick/Joystick.h | 22 +++++++++ Code/libraries/Joystick/Motor.cpp | 7 +++ Code/libraries/Joystick/Motor.h | 15 ++++++ Code/libraries/Joystick/Steuerung.cpp | 67 +++++++++++++++++++++++++++ Code/libraries/Joystick/Steuerung.h | 25 ++++++++++ Code/libraries/README.MD | 4 ++ 9 files changed, 200 insertions(+) create mode 100644 Code/Joystick/Joystick.ino create mode 100644 Code/libraries/Joystick/Joystick.cpp create mode 100644 Code/libraries/Joystick/Joystick.h create mode 100644 Code/libraries/Joystick/Motor.cpp create mode 100644 Code/libraries/Joystick/Motor.h create mode 100644 Code/libraries/Joystick/Steuerung.cpp create mode 100644 Code/libraries/Joystick/Steuerung.h create mode 100644 Code/libraries/README.MD diff --git a/Aufgaben.txt b/Aufgaben.txt index 7f58d63..f93d833 100644 --- a/Aufgaben.txt +++ b/Aufgaben.txt @@ -6,6 +6,7 @@ Komponente | Priorität | wird bearbeitet von ESC (Motorsteuerung) | hoch | Yves Servo (Lenkung) | hoch | Yves Funkkommunikation | hoch | Lukas +Joystickbasiertes lenken (lib) | hoch | Lukas mechanischer Aufbau | hoch | Yves MPPT Solarlader | niedrig | nicht in Bearbeitung Bluetooth Beacon Tracking | diff --git a/Code/Joystick/Joystick.ino b/Code/Joystick/Joystick.ino new file mode 100644 index 0000000..dc68c00 --- /dev/null +++ b/Code/Joystick/Joystick.ino @@ -0,0 +1,42 @@ +#include +//Informationen Joystick +Joystick *joystick; +const uint16_t xAxisPin = 5; +const uint16_t yAxisPin = 4; +const int16_t lowestJoystickValue = 0; +const int16_t highestJoystickValue = 511; +const int16_t spaceing = 50; + +//Informationen linker Motor +Motor *left; +const int16_t lowestPWMValueLeftMotor = -255; +const int16_t highestPWMValueLeftMotor = 255; + +//Informationen rechter Motor +Motor *right; +const int16_t lowestPWMValueRightMotor = -255; +const int16_t highestPWMValueRightMotor = 255; + + +Steuerung *steuerung; + +void setup() { + joystick = new Joystick(xAxisPin, yAxisPin, lowestJoystickValue, highestJoystickValue, spaceing); + left = new Motor(lowestPWMValueLeftMotor, highestPWMValueLeftMotor); + right = new Motor(lowestPWMValueRightMotor, highestPWMValueRightMotor); + steuerung = new Steuerung(joystick, left, right); + Serial.begin(9600); +} + +void loop() { + steuerung -> updateValues(); + + //DEBUG + Serial.print("Left motor PWMValue: "); + Serial.println(left -> PWMValue); + Serial.print("Right motor PWMValue: "); + Serial.println(right -> PWMValue); + //SendValues + delay(500); + +} diff --git a/Code/libraries/Joystick/Joystick.cpp b/Code/libraries/Joystick/Joystick.cpp new file mode 100644 index 0000000..ca7be59 --- /dev/null +++ b/Code/libraries/Joystick/Joystick.cpp @@ -0,0 +1,17 @@ +#include "Joystick.h" + +Joystick::Joystick(uint16_t x_pin, uint16_t y_pin, int16_t lowestValue, int16_t highestValue, uint16_t spaceing) { + this -> x_pin = x_pin; + this -> y_pin = y_pin; + this -> lowestValue = lowestValue; + this -> highestValue = highestValue; + this -> spaceing = spaceing; +} + +void Joystick::checkJoystickInput() { + this -> xAxisReading = analogRead(x_pin); + this -> yAxisReading = analogRead(y_pin); + //9-bit reichen, der ADC schafft bestenfalls 8-bit praezision + (this -> xAxisReading) = (this -> xAxisReading) >> 1; + (this -> yAxisReading) = (this -> yAxisReading) >> 1; +} diff --git a/Code/libraries/Joystick/Joystick.h b/Code/libraries/Joystick/Joystick.h new file mode 100644 index 0000000..a1e52c6 --- /dev/null +++ b/Code/libraries/Joystick/Joystick.h @@ -0,0 +1,22 @@ +#ifndef __JOYSTICK__ +#define __JOYSTICK__ + +#include "Motor.h" + +class Joystick { + // Pin belegungen + private: + uint16_t x_pin; // Pin Nummer an dem der X-output des Joysticks angebunden ist. + uint16_t y_pin; // Pin Nummer an dem der X-output des Joysticks angebunden ist. + public: + int16_t lowestValue; + int16_t highestValue; + uint16_t spaceing; // Abstand vom mittelpunkt um zappeln zu verhindern. + int16_t xAxisReading; + int16_t yAxisReading; + + public: + Joystick(uint16_t x_pin, uint16_t y_pin, int16_t lowestValue, int16_t highestValue, uint16_t spaceing); + void checkJoystickInput(); +}; +#endif diff --git a/Code/libraries/Joystick/Motor.cpp b/Code/libraries/Joystick/Motor.cpp new file mode 100644 index 0000000..dc9acee --- /dev/null +++ b/Code/libraries/Joystick/Motor.cpp @@ -0,0 +1,7 @@ +#include "Motor.h" + +Motor::Motor(int16_t lowestValue, int16_t highestValue) { + this -> highestValue = highestValue; + this -> lowestValue = lowestValue; + this -> PWMValue = 0; +} diff --git a/Code/libraries/Joystick/Motor.h b/Code/libraries/Joystick/Motor.h new file mode 100644 index 0000000..a56c12e --- /dev/null +++ b/Code/libraries/Joystick/Motor.h @@ -0,0 +1,15 @@ +#ifndef __MOTOR__ +#define __MOTOR__ + +#include + +class Motor { + public: + int16_t highestValue; + int16_t lowestValue; + int16_t PWMValue; + + public: + Motor(int16_t lowestValue, int16_t highestValue); +}; +#endif diff --git a/Code/libraries/Joystick/Steuerung.cpp b/Code/libraries/Joystick/Steuerung.cpp new file mode 100644 index 0000000..ea015b2 --- /dev/null +++ b/Code/libraries/Joystick/Steuerung.cpp @@ -0,0 +1,67 @@ +#include "Steuerung.h" + +Steuerung::Steuerung(Joystick* joystick, Motor* left, Motor* right) { + this -> joystick = joystick; + this -> left = left; + this -> right = right; +} + +void Steuerung::mapReadingsToMatchPWMValues() { + (this -> xValue) = map(joystick -> xAxisReading, joystick -> lowestValue, joystick -> highestValue, left -> lowestValue, left -> highestValue); + (this -> yValue) = map(joystick -> yAxisReading, joystick -> lowestValue, joystick -> highestValue, right -> highestValue, right -> lowestValue); +} + +void Steuerung::applyPWMValuesDependingOnReadings() { + if ((abs(this -> xValue) > (joystick -> spaceing)) || (abs(this -> yValue) > (joystick -> spaceing))) { + if (this -> yValue >= 0) { + if (this -> xValue >= 0) { + //+y , +x + (left -> PWMValue) = this -> yValue; + (right -> PWMValue) = (this -> yValue) - (this -> xValue); + if ((this -> xValue) >= (this -> yValue)) { + (left -> PWMValue) = 255; + (right -> PWMValue) = -255; + } + } else { + //+y , -x + (left -> PWMValue) = this -> yValue; + (right -> PWMValue) = (this -> yValue) + (this -> xValue); + if (abs(this -> xValue) >= (this -> yValue)) { + (left -> PWMValue) = -255; + (right -> PWMValue) = 255; + } + } + } else { + if (this -> xValue >= 0) { + //-y , +x + (left -> PWMValue) = this -> yValue; + (right -> PWMValue) = (this -> yValue) + (this -> xValue); + if (this -> xValue >= abs(this -> yValue)) { + (left -> PWMValue) = 255; + (right -> PWMValue) = -255; + } + } else { + //-y , -x + (left -> PWMValue) = this -> yValue; + (right -> PWMValue) = (this -> yValue) - (this -> xValue); + if (abs(this -> xValue) >= abs(this -> yValue)) { + (left -> PWMValue) = -255; + (right -> PWMValue) = 255; + } + } + } + if (abs(this -> xValue) < (joystick -> spaceing)) { + (left -> PWMValue) = this -> yValue; + (right -> PWMValue) = this -> yValue; + } + } else { + (left -> PWMValue) = 0; + (right -> PWMValue) = 0; + } + +} +void Steuerung::updateValues() { + joystick -> checkJoystickInput(); + mapReadingsToMatchPWMValues(); + applyPWMValuesDependingOnReadings(); +} diff --git a/Code/libraries/Joystick/Steuerung.h b/Code/libraries/Joystick/Steuerung.h new file mode 100644 index 0000000..16553a6 --- /dev/null +++ b/Code/libraries/Joystick/Steuerung.h @@ -0,0 +1,25 @@ +#ifndef __STEUERUNG__ +#define __STEUERUNG__ + +#include "Motor.h" +#include "Joystick.h" + +class Steuerung { + private: + Joystick* joystick; + Motor* left; + Motor* right; + int16_t xValue; + int16_t yValue; + + public: + Steuerung(Joystick* joystick, Motor* left, Motor* right); + void updateValues(); + + private: + void mapReadingsToMatchPWMValues(); + void applyPWMValuesDependingOnReadings(); + + +}; +#endif diff --git a/Code/libraries/README.MD b/Code/libraries/README.MD new file mode 100644 index 0000000..55cb764 --- /dev/null +++ b/Code/libraries/README.MD @@ -0,0 +1,4 @@ +Im Ordner libraries befindet sich die Joystick librarie, +welche in den Ordner ...Arduino/libraries verschoben werden muss. +Die Joystick.ino enthält eine beispielhafte verwendung der lib, +sie kann mittels Arduino IDE Compiliert und auf den Arduino geuploaded werden. \ No newline at end of file