|
@ -12,9 +12,9 @@ |
|
|
RF24 radio(A0, 3); // CE, CSN
|
|
|
RF24 radio(A0, 3); // CE, CSN
|
|
|
|
|
|
|
|
|
byte commands[32]; //byte 0 = command
|
|
|
byte commands[32]; //byte 0 = command
|
|
|
unsigned long timer; |
|
|
|
|
|
int16_t temperature = 0; |
|
|
|
|
|
uint16_t distance = 50; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void inline clearCommands() { |
|
|
void inline clearCommands() { |
|
|
for(uint8_t i=0; i<32; i++) { |
|
|
for(uint8_t i=0; i<32; i++) { |
|
@ -64,6 +64,17 @@ volatile bool driveOn = false; |
|
|
volatile long driveTimeout = 0; |
|
|
volatile long driveTimeout = 0; |
|
|
volatile long driveTimeDiff = 0; |
|
|
volatile long driveTimeDiff = 0; |
|
|
|
|
|
|
|
|
|
|
|
volatile bool startNewMeasurement = true; |
|
|
|
|
|
|
|
|
|
|
|
volatile uint32_t pulseStart = 0; |
|
|
|
|
|
volatile uint32_t pulseLength = 1; |
|
|
|
|
|
|
|
|
|
|
|
volatile bool newResult = false; |
|
|
|
|
|
|
|
|
|
|
|
uint32_t timer = 0; |
|
|
|
|
|
int16_t temperature = 0; |
|
|
|
|
|
uint16_t distance = 50; |
|
|
|
|
|
|
|
|
void setup() { |
|
|
void setup() { |
|
|
Serial.begin(115200); |
|
|
Serial.begin(115200); |
|
|
// motorA.setPWM16(2,RESOLUTION);
|
|
|
// motorA.setPWM16(2,RESOLUTION);
|
|
@ -84,48 +95,37 @@ void setup() { |
|
|
|
|
|
|
|
|
void loop() { |
|
|
void loop() { |
|
|
//Temperatur- und Abstandsmessung
|
|
|
//Temperatur- und Abstandsmessung
|
|
|
//Serial.println(temperature);
|
|
|
|
|
|
//Serial.println(distance);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned long currentMillis = millis(); |
|
|
|
|
|
|
|
|
|
|
|
if((unsigned long)(currentMillis - timer) >= 100){ |
|
|
|
|
|
|
|
|
if((millis() - timer) >= 100){ |
|
|
temperature = dallas(4, 0); |
|
|
temperature = dallas(4, 0); |
|
|
|
|
|
if(startNewMeasurement) { |
|
|
measureDistance(); |
|
|
measureDistance(); |
|
|
|
|
|
} |
|
|
|
|
|
timer = millis(); |
|
|
|
|
|
} |
|
|
|
|
|
if(newResult) { |
|
|
|
|
|
// Serial.println("berechnen");
|
|
|
distance = calculateDistance(); |
|
|
distance = calculateDistance(); |
|
|
timer = currentMillis; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (radio.available()) { |
|
|
if (radio.available()) { |
|
|
radio.read(&commands, sizeof(commands)); |
|
|
radio.read(&commands, sizeof(commands)); |
|
|
commandInterpretation(); |
|
|
commandInterpretation(); |
|
|
} |
|
|
} |
|
|
//Serial.println(driveOn);
|
|
|
|
|
|
|
|
|
//Antrieb abschalten wenn kein neuer Fahrbefehl kommt
|
|
|
if(((millis() - driveTimeDiff) > driveTimeout)) { |
|
|
if(((millis() - driveTimeDiff) > driveTimeout)) { |
|
|
pwmA = 0; |
|
|
pwmA = 0; |
|
|
pwmB = 0; |
|
|
pwmB = 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Wenn Mindestdistanz unterschritten, stopp
|
|
|
if(distance < 20){ |
|
|
if(distance < 20){ |
|
|
//Serial.println("Achtung!");
|
|
|
|
|
|
if(pwmA < 0 && pwmB < 0){ |
|
|
if(pwmA < 0 && pwmB < 0){ |
|
|
pwmA = 0; |
|
|
pwmA = 0; |
|
|
pwmB = 0; |
|
|
pwmB = 0; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
drive.setPWM_A(pwmA); |
|
|
drive.setPWM_A(pwmA); |
|
|
drive.setPWM_B(pwmB); |
|
|
drive.setPWM_B(pwmB); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void commandInterpretation() { |
|
|
void commandInterpretation() { |
|
@ -184,21 +184,14 @@ void commandInterpretation() { |
|
|
driveTime |= (0x00FF & commands[i+2]); |
|
|
driveTime |= (0x00FF & commands[i+2]); |
|
|
driveTimeout = (long)driveTime; |
|
|
driveTimeout = (long)driveTime; |
|
|
driveTimeDiff = millis(); |
|
|
driveTimeDiff = millis(); |
|
|
Serial.println(driveTimeout); |
|
|
|
|
|
|
|
|
//Serial.println(driveTimeout);
|
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
default : { /* pwmA = 0;
|
|
|
|
|
|
pwmB = 0; |
|
|
|
|
|
forwardA = true; |
|
|
|
|
|
forwardB = true; |
|
|
|
|
|
driveOn = false; */ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default : { |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
clearCommands(); |
|
|
clearCommands(); |
|
|
|
|
|
|
|
|
} |
|
|
} |