mandag den 27. september 2010

Lab session 3: Sound Sensor and Clap Controlled Car

Test of the Sound Sensor

The SoundSensor example is based on the SonicSensorTest, which is using the ultrasonic sensor to measure distance and displaying it on the screen. So in this case an instance of the SoundSensor class is created instead. We set a testing sampling time of 5ms. The source code of this example can be seen below.

SoundSensor s = new SoundSensor(SensorPort.S1);
int soundLevel;
LCD.drawString("Level: ", 0, 0);

while (! Button.ESCAPE.isPressed())
{
soundLevel = s.readValue();
LCD.drawInt(soundLevel,3,7,0);
Thread.sleep(5);
}

We have made some experiments to check the response of the microphone. We check how a constant sound source, moving away from the microphone, was attenuated so a smaller value was displayed on the screen.

One of the experiments we did was to check the response of the sound sensor to a variable high frequency noise. The microphone was maintained at a constant distance to the variable high frequency generator. The sound level displayed on the screen was decreasing while the frequency was increased, even though the amplitude was kept constant.

Sound Controlled Car

The program SountCtrCar.java is able to move the car robot around based on sound. The program is a mimiprogram with a sequential control as described by Fred G. Martin [1] page 190. The program performs a procedural series of steps moving the car robot around. It moves the car forward, turn right then left and finally stops when a loud sound is detected. It does it in a series of steps waiting 0.5 sec between each step. Each step is performed when the sound level is greater than a certain threshold.

The sound sensor is polled in the method waitForLoudSound as fast as possible. Only the preemptive JAVA implementation of the LeJOS NXJ will ensure that other tasks will be running. This busy polling is blocking the operation of the robot and it is not possible to stop the program with ESCAPE. The ButtonListener mechanism is added using a callback method that sets a flag indicating the program must be stopped exiting the busy polling loop in waiting for the loud sound.
Link to final version of SoundCtrCar.java

Clap Controlled Car

Our first intension was to make an algorithm based on Sivan Toledo's, [2], investigations on how the sound sensor can be used to detect claps. His method is based on detecting a pattern of 3. peaks as shown in the figure below.
Sivan Toledo's clap detection

Our approach was to design a general method that would detect a peak within a min and max amplitude in a given time interval defined by a start and end. The method lookForMax returns a peak every time a maximum is found in the input sequence of samples. Every sound sensor reading is performed in intervals of 5 ms given a sample time of 200 Hz.


                  
Code snippet - looking for max. peaks



Code snippet - looking for max. peak in interval based on amplitude and time


A state machine would be relevant to detect the clap. 3 states is defined to detect first peak A followed by peak B and C.
State machine detecting clap

After implementing the first version of the above algorithm we made some measurements of a clap using the data logger as described in the exercise. We have imported the text file recordings in Matlab and plotted the results in graphs as show in the following figures.

Below graphs are two recordings for a single clap with a sample rate of 200 Hz.

Single clap 1.


Single clap 2.

Only in the second reading we have a small peak after the clap peak, but it seems to be 1.75 seconds after the clap. We decided to test our algorithm on a double clap instead of a single clap.

Below graphs are two recordings for a double clap with a sample rate of 200 Hz.



Double clap 1.



Double clap 2.

Based on the above readings we changed the algorithm using the general peak detector loopForMaxInInterval. Peak 2 is expected in the time interval between 500 - 2000 ms after the first clap.

The below source code for the final state machine detecting a double clap. In this version we have added timeout detection returning to state 1 if the second peak isn't detected. The state machine could be extended so it would be possible to interrupt the double clap detection adding the ButtonListner mechanism to allow exiting the program with the ESCAPE button.




                                         Code snippet - state machine looking for double clap

We managed being able to start and stop the robot car based on a double clap state machine and peak detector.

The final code for the ClapDetecor can be found here: ClapDetector.java

References

[1], Fred G. Martin, Robotic Explorations
[2], Sivan Toledo, A Clap Counter, Oct 2006

Ingen kommentarer:

Send en kommentar