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.

100 lines
2.3 KiB

#define xPin A5
#define yPin A6
#define leftMax 255//215
#define leftMin -255//-100
#define rightMax 255
#define rightMin -255
volatile int16_t xValue = 0;
volatile int16_t yValue = 0;
volatile int16_t leftPWM = 0;
volatile int16_t rightPWM = 0;
const int16_t deadZone = 40;
void motorMapping() {
static long temp = millis();
koordinaten(analogRead(xPin), analogRead(yPin));
motorPWM();
pwmB = map(leftPWM, -255,255,leftMin,leftMax);
pwmA = map(rightPWM, -255,255,rightMin,rightMax);
if((millis() - temp) > 100) {
lcdLines[0] = "Links: " + String(pwmB, DEC);
lcdLines[1] = "Rechts: " + String(pwmA, DEC);
}
senden();
}
void senden() {
commands[0] = speedA;
commands[1] = highByte(pwmA);
commands[2] = lowByte(pwmA);
commands[3] = speedB;
commands[4] = highByte(pwmB);
commands[5] = lowByte(pwmB);
commands[6] = timeToDrive;
commands[7] = highByte(driveTimeout);
commands[8] = lowByte(driveTimeout);
commands[9] = goDrive;
commands[10] = getDistance;
radio.write(&commands, sizeof(commands));
}
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;
}
}