Hier mal meine (sehr einfache) Kollisionsabfrage:
Code:
MotorDir(BREAK, BREAK);
MotorSpeed(0, 0);
StatusLED(YELLOW);
/* compute switch state for each switch */
int switches[6] = { 0, 0, 0, 0, 0, 0 };
int i, switchValue;
for (i=0; i<6; i++) {
switchValue = power(2, (6-i-1));
if (poll-switchValue >= 0) {
switches[i] = 1;
poll -= switchValue;
}
}
/* frontal hit */
if (switches[1] == 1 || switches[4] == 1) {
MotorDir(RWD, RWD);
MotorSpeed(L_HALF, R_HALF);
sleep(300);
if (tickcount % 2 == 0) {
MotorDir(RWD, STOP);
} else {
MotorDir(STOP, RWD);
}
sleep(100 * tickcount);
} else if (switches[0] == 1) {
MotorDir(FWD, FWD);
MotorSpeed(L_HALF, STOP);
sleep(S_EDGE);
} else if (switches[5] == 1) {
MotorDir(FWD, FWD);
MotorSpeed(STOP, R_HALF);
sleep(S_EDGE);
}
Überall wo die Variable tickcount steht, kannst du das auch wegmachen oder eine andere Zeit eingeben. Tickcount ist bei mir eigentlich nur eine Zufallsvariable und zuständig um zu überprüfen, ob wirklich ein "hit" vorkommt oder nur eine elektronische Störung vorliegt (was beim Asuro manchmal komischerweise so ist... Manchmal wird einfach eine Kollision erkannt, obwohl da gar nichts ist).
Kurz zur Erklärung. Das Programm macht folgendes (Nachdem die Kollision überhaupt erkannt wurde natürlich [mit folgender Zeile: "int poll = PollSwitch();"] ):
- Anhalten + Gelbe LED an
- PollSwitch()-Signal aufteilen auf alle 6 Taster
- Frontalen oder links/rechts schrägen Hit erkennen
- Ausweichen
Lesezeichen