Ein Roboter mit bürstenlosem Antrieb, differenzial und NRF24L01 Funk. Großflächig gebaut um ein großes Solarpanel aufzunehmen. https://gitlab.informatik.hs-fulda.de/fdai5253/roboter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.7 KiB

#define xPin A5
#define yPin A6
int16_t xValue = 0;
int16_t yValue = 0;
int16_t leftPWM = 0;
int16_t rightPWM = 0;
const int16_t deadZone = 10;
void setup() {
Serial.begin(115200);
}
void loop() {
koordinaten(analogRead(xPin), analogRead(yPin));
motorPWM();
Serial.print("X: ");
Serial.println(xValue);
Serial.print("Y: ");
Serial.println(yValue);
Serial.print("links: ");
Serial.println(leftPWM);
Serial.print("rechts: ");
Serial.println(rightPWM);
delay(200);
}
void koordinaten(uint16_t x, uint16_t y) {
//9-bit reichen, der ADC schafft bestenfalls 8-bit praezision
x = x >> 1;
y = y >> 1;
xValue = map(x, 0, 511, -255, 255);
yValue = map(y, 0, 511, 255, -255);
}
void motorPWM() {
if((abs(xValue) > deadZone) || (abs(yValue) > deadZone)) {
if(yValue >= 0) {
if(xValue >= 0) {
//+y , +x
leftPWM = yValue;
rightPWM = yValue - xValue;
if(xValue >= yValue) {
leftPWM = 255;
rightPWM = -255;
}
} else {
//+y , -x
leftPWM = yValue;
rightPWM = yValue + xValue;
if(abs(xValue) >= yValue) {
leftPWM = -255;
rightPWM = 255;
}
}
} else {
if(xValue >= 0) {
//-y , +x
leftPWM = yValue;
rightPWM = yValue + xValue;
if(xValue >= abs(yValue)) {
leftPWM = 255;
rightPWM = -255;
}
} else {
//-y , -x
leftPWM = yValue;
rightPWM = yValue - xValue;
if(abs(xValue) >= abs(yValue)) {
leftPWM = -255;
rightPWM = 255;
}
}
}
if(abs(xValue) < deadZone) {
leftPWM = yValue;
rightPWM = yValue;
}
} else {
leftPWM = 0;
rightPWM = 0;
}
}