Browse Source

Distanzmessung den Anforderungen entsprechend designed

master
Yves Ehrlich 5 years ago
parent
commit
3971048caa
  1. 57
      Code/miniRobot/miniRobot.ino
  2. 64
      Code/miniRobot/temperatureDistance.ino
  3. 4
      Code/miniRobotRC/_main.ino

57
Code/miniRobot/miniRobot.ino

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

64
Code/miniRobot/temperatureDistance.ino

@ -1,58 +1,5 @@
#include <OneWire.h>
#define maxDistance 400
int trig;
int echo;
unsigned long distance3 = 0;
unsigned long distance2 = 0;
//ISR for PCINT2
ISR(PCINT2_vect) {
distance3 = pulseIn(echo, HIGH);
if(distance3 > 0){
distance2 = distance3;
}
PCICR &= ~0b00000111;
PCMSK2 &= ~0b00010000;
delayMicroseconds(10);
}
void setEchoPins(int pin1, int pin2){
trig = pin1;
echo = pin2;
}
int16_t calculateDistance(){
int16_t result2 = (int16_t)(((float) distance2/ 58.0));
if(result2 > maxDistance){
result2 = maxDistance;
}
return result2;
}
void measureDistance(){
digitalWrite(trig, HIGH);
// ... wait for 10 µs ...
delayMicroseconds(10);
// ... put the trigger down ...
digitalWrite(trig, LOW);
Serial.println("messe...");
PCICR |= 0b00000100;
PCMSK2 |= (1 << echo);
}
int16_t dallas(int x, byte start){
OneWire ds(x);
@ -85,17 +32,6 @@ int16_t dallas(int x, byte start){
void tempDistSetup(){
dallas(4, 1);
// Initializing Trigger Output and Echo Input
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
// Reset the trigger pin and wait a half a second
digitalWrite(trig, LOW);
delayMicroseconds(500);
sei();
}
void runMeasurements(){

4
Code/miniRobotRC/_main.ino

@ -28,13 +28,13 @@ void loop() {
lcdLines[5] = temp_str;
refreshLCD();
}
tasten.clearButton(buttonStart);
tasten.clearButton(buttonStart); /*
while(!tasten.getButtonCycle(buttonStart)){
motorMapping();
//updateTemp();
refreshLCD();
}
tasten.clearButton(buttonStart);
tasten.clearButton(buttonStart); */
while(!tasten.getButtonCycle(buttonStart)){
joystickSteuerung(); //TODO ()
//updateTemp();

Loading…
Cancel
Save