Lukas Reichwein
5 years ago
9 changed files with 200 additions and 0 deletions
-
1Aufgaben.txt
-
42Code/Joystick/Joystick.ino
-
17Code/libraries/Joystick/Joystick.cpp
-
22Code/libraries/Joystick/Joystick.h
-
7Code/libraries/Joystick/Motor.cpp
-
15Code/libraries/Joystick/Motor.h
-
67Code/libraries/Joystick/Steuerung.cpp
-
25Code/libraries/Joystick/Steuerung.h
-
4Code/libraries/README.MD
@ -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); |
|||
|
|||
} |
@ -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; |
|||
} |
@ -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 |
@ -0,0 +1,7 @@ |
|||
#include "Motor.h"
|
|||
|
|||
Motor::Motor(int16_t lowestValue, int16_t highestValue) { |
|||
this -> highestValue = highestValue; |
|||
this -> lowestValue = lowestValue; |
|||
this -> PWMValue = 0; |
|||
} |
@ -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 |
@ -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(); |
|||
} |
@ -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 |
@ -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. |
Write
Preview
Loading…
Cancel
Save
Reference in new issue