Skip to content
Browse files

Added ClapDetecor, but doesn't work http://josemariakim.blogspot.com/…

  • Loading branch information...
1 parent 6a72232 commit 6039622fa5c37c60286f2dabfb2c9800a4fefe57 @Ranlvor Ranlvor committed Jan 4, 2012
Showing with 198 additions and 40 deletions.
  1. +26 −40 src/de/c3t/BehaviorRoboter/Behaviors/ListenToSound.java
  2. +172 −0 src/de/c3t/BehaviorRoboter/ClapDetector.java
View
66 src/de/c3t/BehaviorRoboter/Behaviors/ListenToSound.java
@@ -2,62 +2,48 @@
import java.io.File;
-import lejos.nxt.LCD;
-import lejos.nxt.SensorPort;
import lejos.nxt.Sound;
-import lejos.nxt.SoundSensor;
import lejos.robotics.subsumption.Behavior;
+import de.c3t.BehaviorRoboter.ClapDetector;
+import de.c3t.BehaviorRoboter.Main;
public class ListenToSound implements Behavior {
- private boolean supressed;
+ private boolean wantControl = false;
+
final File soundFile = new File("exterminate.wav");
- public ListenToSound() {
- Sound.setVolume(100);
- SoundSensor sound = new SoundSensor(SensorPort.S3);
- int soundLevel;
- LCD.clear();
- LCD.drawString("Level: ", 0, 0);
- soundLevel = sound.readValue();
- LCD.drawInt(soundLevel,3,7,0);
- try {
- Thread.sleep(5);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+
+ public ListenToSound() {
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ while (!Main.isShuttingDown) {
+ try {
+ ClapDetector.waitForClapSoundPattern();
+ } catch (Exception e) {
+ } // grrr, y do you throw ALL Exceptions?
+ wantControl = true;
+ }
}
- }
-
+ }).start();
+ }
@Override
public boolean takeControl() {
- // never take control until code is finished
- return false;
+ return wantControl;
}
@Override
public void action() {
- // do this if you have control
- supressed = true;
- //Sound.beep();
- //try {
- // Thread.sleep(500);
- //} catch (InterruptedException e) {
- // TODO Auto-generated catch block
- // e.printStackTrace();
- //}
+ wantControl = false;
+ int oldVolume = Sound.getVolume();
+ Sound.setVolume(100);
Sound.playSample(soundFile, 100); // 100 ... volume
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
+ Sound.setVolume(oldVolume);
+
+ Main.pilot.rotate(-180);
}
@Override
- public void suppress() {
- // stop action when called
- supressed = true;
-
+ public void suppress() { // action doesn't run for a long time -> we do not implement this
}
}
View
172 src/de/c3t/BehaviorRoboter/ClapDetector.java
@@ -0,0 +1,172 @@
+package de.c3t.BehaviorRoboter;
+//The following code is "stolen" from http://code.google.com/p/josemariakim/source/browse/trunk/lab3/ClapController/ClapDetector.java
+// and http://josemariakim.blogspot.com/2010/09/lab-session-3.html
+import lejos.nxt.*;
+/**
+ * Clap car controller
+ */
+
+public class ClapDetector
+{
+ private static int samplePeriode = 5;
+ private static SoundSensor sound = new SoundSensor(SensorPort.S3);
+ private static int last_read = 0;
+
+ private static boolean rising = false;
+
+ // This function reads sound value and looks for maximum.
+ // Returns maximum based on the last 3 readings.
+ private static int lookForMax() throws Exception
+ {
+ int read, max = 0;
+
+ read = sound.readValue();
+ LCD.drawInt(read,4,10,2);
+ if (read > last_read)
+ rising = true;
+ if (read < last_read)
+ {
+ if (rising) max = last_read;
+ rising = false;
+ }
+
+ last_read = read;
+ return max;
+ }
+
+ // This function will block while looking for a maximum peak.
+ // It looks for maximum peak between min_max and max_max limits
+ // between timestart and timeend looking for the maximum.
+ // If timestart = 0 the function returns when first maximum is found
+ private static boolean lookForMaxInInterval(int min_max, int max_max,
+ int timestart, int timeend) throws Exception
+ {
+ int count = 0;
+ int max;
+ int cntstart = timestart/samplePeriode;
+ int cntend = timeend/samplePeriode;
+
+ // Looking for maximum within limits
+ while (true)
+ {
+ // Wait sample period
+ count++;
+ Thread.sleep(samplePeriode);
+
+ // Look for maximum peak
+ max = lookForMax();
+ LCD.drawInt(max,4,10,1);
+
+ if (min_max <= max && max <= max_max)
+ {
+ // Max found
+ if (cntstart == 0) return true;
+ if (count < cntstart) return false; // Maximum to early
+ return true; // Maximum within limits
+ }
+ if (cntstart > 0 && count > cntend)
+ return false; // Timeout looking for maximum
+ }
+
+ }
+
+ // Wait for clap sound peak pattern
+ // Peak 1
+ // Peak 2
+ // Peak 3
+ public static void waitForClapSoundPattern() throws Exception
+ {
+ int state = 1;
+ boolean waiting = true;
+
+ LCD.clear();
+ LCD.drawString("State 1", 0, 0);
+ LCD.refresh();
+
+ while (waiting)
+ {
+ switch (state)
+ {
+ case 1:
+ if (lookForMaxInInterval(50,100,0,0)) // Peak 1
+ {
+ LCD.clear();
+ LCD.drawString("State 2", 0, 0);
+ LCD.refresh();
+ state = 2;
+ }
+ break;
+
+ case 2:
+ if (lookForMaxInInterval(50,100,500,2000)) // Peak 2
+ {
+ LCD.clear();
+ LCD.drawString("State 3", 0, 0);
+ LCD.refresh();
+ state = 3;
+ }
+ else
+ {
+ LCD.clear();
+ LCD.drawString("State 1", 0, 0);
+ LCD.refresh();
+ state = 1; // Timeout - Start again looking for clap
+ }
+ break;
+
+ case 3:
+ LCD.clear();
+ LCD.drawString("Clap detected", 0, 0);
+ LCD.refresh();
+ waiting = false;
+ break;
+ }
+ }
+ }
+
+ public static void main(String [] args) throws Exception
+ {
+ LCD.drawString("dB level: ",0,0);
+ LCD.refresh();
+
+ boolean res = true;
+
+ // while (! Button.ESCAPE.isPressed())
+ //{
+
+ waitForClapSoundPattern();
+
+ Motor.B.forward();
+ Motor.A.forward();
+
+ waitForClapSoundPattern();
+ Motor.B.stop();
+ Motor.A.stop();
+
+ /*
+ lookForMaxInInterval(50,100,0,0);
+
+ LCD.clear();
+ LCD.drawString("Clap1Detected", 0, 0);
+ LCD.refresh();
+
+ res = lookForMaxInInterval(50,100,500,2000);
+ if (res)
+ {
+ LCD.drawString("Clap2Detected", 0, 0);
+ LCD.refresh();
+ }
+ else
+ {
+ LCD.drawString("No clap 2 detected", 0, 0);
+ LCD.refresh();
+ }
+ */
+
+ //}
+
+ LCD.clear();
+ LCD.drawString("Program stopped", 0, 0);
+ Thread.sleep(2000);
+ }
+}

0 comments on commit 6039622

Please sign in to comment.
Something went wrong with that request. Please try again.