PI4J und HC-SR04

L I V E Stammtisch ab 20:30 Uhr im Chat
  • Hallo liebe Gemeinde,
    ich programmiere schon seit längerem Java auf dem Raspberry 2b, nun möchte ich diesen: HC-SR04 Ultraschallsensor gerne in mein Projekt einbinden, jedoch gibt es da einigen Probleme.
    Nun zum Problem der unten gezeigte Code funktioniert aber nur sehr sehr sehr ungefähr, heißt ich bekomme Ergebnisse jedoch mit "Verunreinigungen".
    Der Sensor ist an Pins 29 und 28 angeschlossen.
    Das sieht dann zum Beispiel so aus (Eigentlich sollte sich die Distanz nicht ändern!):

    LEFT: + 10
    LEFT: + 12
    LEFT: + 19
    LEFT: + 100002
    LEFT: + 22
    LEFT: + 4
    LEFT: + 10
    LEFT: + 16
    LEFT: + 13
    LEFT: + 11
    LEFT: + 235120323

    Dazu kommt noch das sobald ich einen Gegenstand mehr als einen halben Meter vor den Sensor halte keine Ergebnisse mehr kommen.
    Vielleicht habt ihr ja Ideen, oder einen besser funktionierenden Code :helpnew: !
    MFG Luick.


    import com.pi4j.component.servo.Servo;
    import com.pi4j.io.gpio.GpioController;
    import com.pi4j.io.gpio.GpioFactory;
    import com.pi4j.io.gpio.GpioPinDigitalInput;
    import com.pi4j.io.gpio.GpioPinDigitalOutput;
    import com.pi4j.io.gpio.Pin;
    import com.pi4j.io.gpio.RaspiPin;

    import de.ye.car_dir.SERVO;
    import de.ye.car_eng.ENGINE;
    import de.ye.car_modi.THREAD_MODI;

    public class SENS_FRONT_LEFT implements Runnable{

       private final static float SOUND_SPEED = 340.29f;  
       
       private final static int TRIG_DURATION_IN_MICROS = 10;
       private final static int WAIT_DURATION_IN_MILLIS = 60; 

       private final static int TIMEOUT = 2100;
       private static int dist;
       
       private final static GpioController gpio = GpioFactory.getInstance();
       
       private GpioPinDigitalInput echoPin;
       private GpioPinDigitalOutput trigPin;
       public static int sens;
               
       private void prep_s( Pin echoPin, Pin trigPin ) {
           this.echoPin = gpio.provisionDigitalInputPin( echoPin );
           this.trigPin = gpio.provisionDigitalOutputPin( trigPin );
           this.trigPin.low();
       }
       
       public float measureDistance() throws TimeoutException {
           this.triggerSensor();
           this.waitForSignal();
           long duration = this.measureSignal();
           
           return duration * SOUND_SPEED / ( 2 * 10000 );
       }

       private void triggerSensor() {
           try {
               this.trigPin.high();
               Thread.sleep( 0, TRIG_DURATION_IN_MICROS * 1000 );
               this.trigPin.low();
           } catch (InterruptedException ex) {
            //dist=5;
           }
       }
       
       private void waitForSignal() throws TimeoutException {
           int countdown = TIMEOUT;
           
           whilethis.echoPin.isLow() && countdown > 0 ) {
    countdown--;
           }
           
           if( countdown <= 0 ) {
            //dist=5;
           }
       }
       
       private long measureSignal() throws TimeoutException {
           int countdown = TIMEOUT;
           long start = System.nanoTime();
           whilethis.echoPin.isHigh() && countdown > 0 ) {
    countdown--;
           }
           long end = System.nanoTime();
           
           if( countdown <= 0 ) {
            //dist=5;
           }
           
           return (long)Math.ceil( ( end - start ) / 1000.0 );  
       }
       
       public void run() {
        ////////
    Pin echoPin = RaspiPin.GPIO_29; 
    Pin trigPin = RaspiPin.GPIO_28; 
        ////////
    prep_s( echoPin, trigPin );
           
           whiletrue ) {
               try {
                Thread.sleep(CONFIG.LATENCY_SIDE);
    dist = Math.round(this.measureDistance());
                System.out.println("LEFT: " + dist);
               }
               catch( TimeoutException e ) {
                   System.err.println( e );
               } catch (InterruptedException e) {
     // TODO Auto-generated catch block
    e.printStackTrace();
     }

               try {
                   Thread.sleep( WAIT_DURATION_IN_MILLIS );
               } catch (InterruptedException ex) {
                   System.err.println( "Interrupt during trigger" );
               }
           }
       }
       private static class TimeoutException extends Exception {

           private final String reason;
           
           public TimeoutException( String reason ) {
               this.reason = reason;
           }
           
           @Override
           public String toString() {
               return this.reason;
           }
       }
    }

Jetzt mitmachen!

Du hast noch kein Benutzerkonto auf unserer Seite? Registriere dich kostenlos und nimm an unserer Community teil!