torsdag den 16. september 2010

NXT Programming Lesson 3

Lesson 3 description 

Date: 16 september 2010

Duration of activity: 14.15 - 17.30
Group members participating: Nikki & Knud


GOALS for lesson 3
- To use the NXT sound sensor to turn the LEGO 9797 car into a sound controlled car

Subgoals:
1. Implement and test datalogger while also testing sound sensor
2. Test sound controlled car
3. Test Clap controlled car

Plan for achieving the goals
1.
We will use the datalogger code supplied. We will test the sound sensor by looking at the display while clapping in front of it. We will test the datalogger by importing the logged data into a graph.

2.
We will use the code supplied and test how it reacts on sounds, we will also implement the button listener, so we always can stop the program by hitting Escape

3.
We will use the code in nr. 2 again and modify it to detect claps.

Results
1.
We tested sound sensor and datalogger.
We had some problems importing the data logged by datalogger, because it was comma separated and inserting new line after a fixed number of samples. We removed the commas and made it insert a new line after each sample. This made it easy to import into a spreadsheet program supporting CSV formated data files.

We recorded 2 normal claps, a quick/light clap and a fist hammered down in the table.
The initial rise in sound is probably some calibration performed by the sound sensor, as this happens everytime.

As seen in the graph, its hard to discern the fist in the table from the claps based on only the amplitude and time over a certain threshold.
Test of sound sensor and datalogger


2.
The program defines a sound threshold value, which will trigger a new state when the sound reaches above that value.

As suggested, we used a button listener so the program can always be stopped by pressing escape. The code snippet is seen below.

Button.ESCAPE.addButtonListener( new ButtonListener() {
public void buttonPressed( Button button) {
Car.stop();
lejos.nxt.NXT.exit(0);
}
});


3.
This is our algorithm for clap detection, it is based on 3 states we therefore used switch case.

State 1 continues to state 2 when the amplitude is below 50
State 2 continues to state 3 when the amplitude is above 85 within 20 milliseconds (The lab lesson note says 25, but after some experimentation we found that 20 left out more no-clap sounds). It goes back to state 1 when 20 milliseconds has passed
State 3 checks if the amplitude falls below 50 within 250 milliseconds. When this happens we have a clap (or a very loud sound that is very short).

private static void waitForClap() {
int soundLevel;
long timeLastStage = 0;
int stage = 1;

boolean check = true;
while (check) {
soundLevel = sound.readValue();
LCD.drawInt(soundLevel,4,10,0);

switch (stage) {
//A clap is a pattern that starts with a low-amplitude sample
// (say below 50),
case 1:
if(soundLevel <>
timeLastStage = System.currentTimeMillis();
stage = 2;
}
break;
// followed by a very-high amplitude sample (say above 85)
// within 25 milliseconds,
case 2:
// Some tests showed that value 20 left out undesired detections
if((System.currentTimeMillis() - timeLastStage) <>
{
if(soundLevel > 85) { // could be a clap, go to next stage
timeLastStage = System.currentTimeMillis();
stage = 3;
}
}else { // 20milli passed, not a clap
stage = 1;
}

break;
// and then returns back to low (below 50)
// within another 250 milliseconds.
case 3:
if((System.currentTimeMillis() - timeLastStage) <= 250) {
if(soundLevel <>
// Clap!
Sound.beep();
check = false;
}
}else { // 250milli passed, not a clap
stage = 1;
}
break;
}
}
}
Source code:
http://www.liscom.dk/lego/Lab3ClapCtrCar/Lab3ClapCtrCar.java
http://www.liscom.dk/lego/Lab3ClapCtrCar/DataLogger.java
http://www.liscom.dk/lego/Lab3ClapCtrCar/SoundSampling.java



Ingen kommentarer:

Send en kommentar