Browse Source

Added the joystick control lib with example use (Joystick.ino)

lukas
Lukas Reichwein 2 years ago
parent
commit
62e06d62e4
  1. 1
      Aufgaben.txt
  2. 42
      Code/Joystick/Joystick.ino
  3. 17
      Code/libraries/Joystick/Joystick.cpp
  4. 22
      Code/libraries/Joystick/Joystick.h
  5. 7
      Code/libraries/Joystick/Motor.cpp
  6. 15
      Code/libraries/Joystick/Motor.h
  7. 67
      Code/libraries/Joystick/Steuerung.cpp
  8. 25
      Code/libraries/Joystick/Steuerung.h
  9. 4
      Code/libraries/README.MD

1
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 |

42
Code/Joystick/Joystick.ino

@ -0,0 +1,42 @@
#include <Steuerung.h>
//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);
}

17
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;
}

22
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

7
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;
}

15
Code/libraries/Joystick/Motor.h

@ -0,0 +1,15 @@
#ifndef __MOTOR__
#define __MOTOR__
#include <Arduino.h>
class Motor {
public:
int16_t highestValue;
int16_t lowestValue;
int16_t PWMValue;
public:
Motor(int16_t lowestValue, int16_t highestValue);
};
#endif

67
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();
}

25
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

4
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.
Loading…
Cancel
Save