Browse Source

Temperature measurement now worls more reliable.

master
nickg 5 years ago
parent
commit
48327ec5c4
  1. 37
      Code/miniRobot/miniRobot.ino
  2. 23
      Code/miniRobot/temperatureDistance.ino
  3. 15
      Code/miniRobotRC/fahrsteuerung_old.ino
  4. 2
      Code/miniRobotRC/funkInit.ino
  5. 4
      Code/miniRobotRC/miniRobotRC.ino

37
Code/miniRobot/miniRobot.ino

@ -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
long timer;
unsigned long timer;
int16_t temperature = 0; int16_t temperature = 0;
int16_t distance = 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++) {
@ -70,30 +70,36 @@ void setup() {
// motorB.setPWM16(2,RESOLUTION); // motorB.setPWM16(2,RESOLUTION);
radio.begin(); radio.begin();
radio.openWritingPipe(address2); radio.openWritingPipe(address2);
radio.openReadingPipe(0, address);
radio.openReadingPipe(1, address);
radio.setPALevel(RF24_PA_MAX); radio.setPALevel(RF24_PA_MAX);
radio.startListening(); radio.startListening();
clearCommands(); clearCommands();
//Temperatur- und Abstandsmessung //Temperatur- und Abstandsmessung
setEchoPins(16, 6); //16: A2, 5: D5
setEchoPins(16, 6); //16: A2, 6: D6
tempDistSetup(); tempDistSetup();
timer = millis(); timer = millis();
} }
void loop() { void loop() {
//Temperatur- und Abstandsmessung //Temperatur- und Abstandsmessung
Serial.println(temperature);
Serial.println(distance);
temperature = dallas(4, 0);
//Serial.println(temperature);
//Serial.println(distance);
if((unsigned long)(millis() - timer) >= 100){
unsigned long currentMillis = millis();
if((unsigned long)(currentMillis - timer) >= 100){
temperature = dallas(4, 0);
measureDistance(); measureDistance();
timer = millis();
distance = calculateDistance();
Serial.println(distance);
timer = currentMillis;
} }
distance = calculateDistance();
if (radio.available()) { if (radio.available()) {
@ -108,13 +114,13 @@ void loop() {
if(distance < 20){ if(distance < 20){
Serial.println("Achtung!");
//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);
@ -166,9 +172,10 @@ void commandInterpretation() {
break; break;
} }
case getTemp : { case getTemp : {
Serial.println("Senden!");
radio.stopListening();
radio.write(&temperature, sizeof(int16_t));
//Serial.println("Senden!");
radio.stopListening();
int16_t sendData = temperature;
radio.write(&sendData, sizeof(int16_t));
radio.startListening(); radio.startListening();
break; break;
} }

23
Code/miniRobot/temperatureDistance.ino

@ -5,14 +5,15 @@
int trig; int trig;
int echo; int echo;
//int distance = 0;
int distance2 = 0;
uint16_t distance3 = 0;
uint16_t distance2 = 0;
//ISR for PCINT20 //ISR for PCINT20
ISR(PCINT2_vect) { ISR(PCINT2_vect) {
distance = pulseIn(echo, HIGH);
if(distance > 0){
distance2 = distance;
distance3 = pulseIn(echo, HIGH);
if(distance3 > 0){
distance2 = distance3;
} }
delayMicroseconds(10); delayMicroseconds(10);
@ -26,12 +27,11 @@ void setEchoPins(int pin1, int pin2){
} }
int16_t calculateDistance(){ int16_t calculateDistance(){
int16_t result = distance2/ 58;
if(result > maxDistance){
result = maxDistance;
int16_t result2 = distance2/ 58;
if(result2 > maxDistance){
result2 = maxDistance;
} }
return result;
return result2;
} }
void measureDistance(){ void measureDistance(){
@ -89,6 +89,7 @@ void tempDistSetup(){
// Reset the trigger pin and wait a half a second // Reset the trigger pin and wait a half a second
digitalWrite(trig, LOW); digitalWrite(trig, LOW);
delayMicroseconds(500); delayMicroseconds(500);
Serial.println(echo);
sei(); sei();
@ -102,7 +103,7 @@ void runMeasurements(){
} }
Serial.print("Temperatur: "); Serial.print("Temperatur: ");
Serial.println(dallas(2, 0));
Serial.println(dallas(4, 0));
Serial.print("Distanz: "); Serial.print("Distanz: ");
Serial.print(calculateDistance()); Serial.print(calculateDistance());

15
Code/miniRobotRC/fahrsteuerung_old.ino

@ -51,26 +51,31 @@ void manualDigitalDrive() {
} }
//Temperatur <3 //Temperatur <3
if((unsigned long)(millis() - temp_time) >= 1000){ // jede Sekunden
unsigned long currentMillis = millis();
if((unsigned long)(currentMillis - temp_time) >= 1000){ // jede Sekunden
temp_time = millis(); temp_time = millis();
bool err = false; bool err = false;
clearCommands(); clearCommands();
commands[0] = getTemp; commands[0] = getTemp;
radio.write(&commands, sizeof(commands) && !err); radio.write(&commands, sizeof(commands) && !err);
unsigned long start = micros(); unsigned long start = micros();
radio.startListening(); radio.startListening();
while(!radio.available()){ while(!radio.available()){
//Serial.println("nix"); //Serial.println("nix");
if((unsigned long)(micros()- start) >= 1){
unsigned long currentMicros = micros();
if((unsigned long)(currentMicros - start) >= 1){
err = true; err = true;
} }
} }
if(!err){ if(!err){
radio.read(&temperature, sizeof(int16_t));
int16_t readData;
radio.read(&readData, sizeof(int16_t));
temperature = readData;
} }
radio.stopListening(); radio.stopListening();
clearCommands();
} }
// } // }

2
Code/miniRobotRC/funkInit.ino

@ -2,7 +2,7 @@ void funkInit() {
radio.begin(); radio.begin();
radio.openWritingPipe(address); radio.openWritingPipe(address);
radio.openReadingPipe(0, address2);
radio.openReadingPipe(1, address2);
radio.setPALevel(RF24_PA_MAX); radio.setPALevel(RF24_PA_MAX);
radio.stopListening(); radio.stopListening();
} }

4
Code/miniRobotRC/miniRobotRC.ino

@ -39,8 +39,8 @@ bool forwardB = true;
bool driveOn = false; bool driveOn = false;
uint16_t driveTimeout = 0; uint16_t driveTimeout = 0;
int16_t distance;
int16_t temperature = 0;
uint8_t distance;
uint8_t temperature = 0;
unsigned long temp_time = millis(); unsigned long temp_time = millis();

Loading…
Cancel
Save