Tuesday 14 August 2012

Maximize Sound

This is a small program which I have come up with, to learn about sound sensor and data wiring in NXT. Here the robot's "roaming boundary" is decided by decreasing sound level. That means, the robot tries to remain within the boundary where raw sound level drops by 100. The robot should move away from the sound source only until the sound level drops by 100. After which the robot is supposed to take a U turn and move more closer to the sound source. Let's go through the NXT-G program to achieve this goal. Please click on the below image in order to maximize it and take a look at the NXT-G program. Alternatively, you can also download the .rbt file and check out the comments to understand the NXT program.



Let me explain all the blocks to you one by one.

  1. Drag and drop the Sound Sensor block on the sequence beam in the Work Area
  2. Drag and drop the Variable block and put it next to the Sound Sensor block. Name this variable as maxsound
  3. Stretch a yellow wire from the Sound Sensor Block to the input of the Variable block. This will initialize the maxsound variable with the sound sensor sample
  4. Drag and drop the Motor Block on to the sequence beam and configure it using the configuration panel to move the motors B and C for two seconds
  5. Drag and Drop the sound sensor block to collect the sound sample (after forward movement of the robot in step 4)
  6. Drag and drop the Variable block and put it next to the sound sensor block. Name this variable as sound
  7. Stretch a yellow wire from the Sound Sensor Block to the input of the Variable block. This will initialize the sound variable with the sound sensor sample
  8. Drag and drop the variable block maxsound on the sequence beam, and put it next to all the previous blocks
  9. Drag and drop the variable block sound on the sequence beam, and put it next to all the previous blocks
  10. Drag and drop the Compare block and put it next to all the previous blocks
  11. Stretch yellow wires from maxsound and sound variable blocks and make them as inputs to the Compare block
  12. Drag and drop a Switch block and place it on to the sequence beam
  13. Draw a data wire from the output of the Compare block to the input of the Switch block. If sound is greater than maxsound, the upper part of Switch block will be executed. If the sound is lesser than maxsound, the lower part of the Switch block will be executed
  14. If sound is greater than maxsound, copy the value of sound variable in maxsound variable. That will keep maxsound variable updated with the maximum value of sound so far. Do this by dragging and dropping the sound and maxsound variables on to the upper part of the switch and stretching a wire from sound output to maxsound input 
  15. Next, find the difference between the peak sound reading and the latest sound reading. Check if the difference between the two sound levels is greater than 100. We can achieve this by dragging and dropping the sound and maxsound blocks on the sequence beam and putting the Maths block next to them. Stretch the yellow wires from the sound and maxsound variables to the Maths block input. Then stretch a yellow wire from the output of the Maths block to the Compare block to check if the difference between sound and maxsound is greater than 100
  16. Drag and drop a Switch block and place it on to the sequence beam. The Switch block has two parts, upper and lower. The upper part gets executed if the input is True. And the lower part gets executed if the input is False. Drag a wire from the output of the Compare block to the input of the Switch block. If the output of the Compare block is true (if the difference between sound and maxsound is greater than 100), the robot will take a U turn. This can be achieved by placing a Motor Block in the upper part of the Switch with only a single Motor on. In my case its a C motor 
  17. Place the steps from 4 to 16 in a loop which will execute them again and again forever.


In Essence the entire algorithm of bringing back the robot after reaching a point where sound level drops by 100 is as below:

  1. Keep the maxsound variable updated with maximum sound value so far
  2. Keep comparing the maxsound with the latest sound sample (The latest sound sample will keep reducing as and when the robot moves away from the sound source)
  3. If the difference between the maxsound and latest sound sample is greater than 100, make the robot take a U turn
  4. If not, keep the robot moving forward

Please take a look at the functioning of the robot in the below video clip. 




Hope you enjoyed the article!

Please go through the below article to know about Lego data wiring in detail.