Monday, February 24, 2014

Lab Session 3

Date: 20. february 2014
Duration of activity: 3 hours 
Group members participating: Lukas, Daniela, Helene

Goals:

  • Perform exercises from Lab Session 3

Plan:
  • Mount the microphone on the car.
  • Do exercises 1-6.

Results:

Exercise 1


We downloaded the source code for the SonicSensorTest from the last lab session and made a small change to transforme it to a sound sensor test and called the new class SoundSensorTest. 

We changed the following line in the SonicSensorTest.java:

UltrasonicSensor us = new UltrasonicSensor(SensorPort.S1);

And replaced it with the following line:


SoundSensor us = new SoundSensor(SensorPort.S1);

Link to the complete SoundSensor.java file: SoundSensorTest.java

After we run the program on the NXT we tried to measure sound values from different distances and different volumes. The results of the measuraments can be seen in the following tables:





It was very difficulty to get accurate measurements for several reasons. We didn't have any measuring tape, so we had to estimate the distances between the sensor and the sound source. There was a lot of noise in the room which made it difficult in some cases to determine whether the measurements were detecting our sound or some noise in the background. 

In our first attempt of measuring sound from different distances, we tried to clap our hands with equal volume, but we found this very difficult. It is hard to clap with the same amount of volume. We decided to use a mobile phone to make sounds of a constant volume instead, to achieve more precise results in the next experiment. 

When we didn't make any sound the NXT showed sound values in the range from 2 to about 6, which must be due to the background noise. Therefore our measurements with a value around this range are very inaccurate and we cannot be sure that the value is caused by the sound we made or the background noise. For measurements above a value of 10 it is very clear that the microphone picks up the sound we made.

Despite the inaccuracies due to the experiment's set up, we argued that there is some proportionality between the registered sensor values, the loudness of the sound and the distance from the sound source and the sensor.

Sound from farther away and the same volume, are measured as more silent, while loud sounds from the same distance are also measured as such.

Therefore, the sensor seems to work correctly.

-----------------------------------------------------------------------------------------------------------
Exercise 2

In this exercise our goal is to gather a lot of data and plot it in a graph to see how the sound level varies over time. To do this we used the given DataLogger.java and SoundSampling.java.

We started the SoundSampling program on the NXT and used our mobile phone to make the same beeptones as in exercise 1. 


We got the following result for 3 beeps (link to the raw data: Beeptone Sample.txt):

The beeptone that was made consisted of two beeps quickly following each other. These two beeps are clearly seen on the graph represented by the two small tops for each pulse.

This shows that the sensor is able to measure short sounds at average loudness, even though there is background noise.

We notice that that the readings start between 90 dB and 100 dB and in the next 300 ms drop to below 10 dB. We saw the same behavior in all of the samplings we took. We believe that these readings must be due to how the system initializes the sound sensor or to a noise inside the system that the sound sensor reacts to. This means that when starting the system we cannot get a reliable sound value within the first 300 ms. 


-----------------------------------------------------------------------------------------------------------------
Exercise 3

We started by downloading the SoundCtrCar.java and the Car.java files and running the SoundCtrCar on the NXT. We noticed the following behavior:


  • When the program started the car stood still. 
  • When we made a loud sound ( e.g. a clap or a shout) the car began to move forward. 
  • When we made another loud sound the car started to turn right in a circle.
  • After another loud sound the car started to turn left in a circle.
  • And after another loud sound the car stopped. 
  • When keep making loud sounds the car switched from stop to moving forward to turning right to turning left to stop etc.
After this experiment we took a closer look at the source code of the SoundCtrCar. Here we noticed that when the sound value reached a value of above 90 the car switched action between moving forward, turning right, turning left and stop. Which clearly maches our observations of the cars behavior.

Another thing we noticed during our experiment was that we could not exit the program unless we had the escape button pressed when switching from turing left to stop. When looking in the source code we found the reason for this, which is that when the car waits for a loud sound (e.g. is in the inner loop) it does not check whether the escape button is pressed. In fact the check is only performed between the car has switched to stop and starts waiting for a loud sound to begin moving forward. 



--------------------------------------------------------------------------------------------------
Exercise 4

In this exercise we are asked to implement a button listener in order to make possible to exit the program at any point during execution. We did this by adding the following code to the SoundCtrCar.jave:



Explanation of the code:
We added a buttonListener that terminates the program when the assigned button is pressed. We assigned the buttonlistener to the ESCAPE button. Now we can stop the NXT by pressing the escape button, no matter in which loop (outer or inner) the program is. 

(Link to the complete SoundCtrCar with button listener implemented: SoundCtrCar.java)

--------------------------------------------------------------------------------------------------------------------
Exercise 5

In this exercise we should implement a method to detect claps. To do this we created a class called DetectClap.java and used code from the modified SoundCtrCar (with the button listener) where we modified the inner loop to only react when it detected a clap.

Before this we used the SoundSampling program to sample data for claps and got the following result (link to the raw data: Clap Sample.txt):



Observing the graph we noticed that a clap has a pattern that goes from a sound value below 50 to a sound value above 85 within 25 ms and then returns back below 50 withing 250 ms (just as described in the exercise).

After this measurement we wrote the inner loop of our DetectClap program which can be seen in the following code:



We use the tresholds of sound value 50 and 85, and time 30 ms and 85 ms to determine whether it is a clap we are measuring. 

This is done in the following way:

  • If the sound level is below 50 and we are not clapping we do nothing
  • If we reach a sound value above 85 and it is more than 30 ms since we were below 50 it could be the start of a clap, so we claim that we have started a clap (isClapping = true) and set time since clap = 0
  • If we are clapping (isClapping = true) we add our sleeptreshold of 5 ms to time since clap. Besides we check if the sound value is below 50 and it is more than 250 ms since we started clapping, if so this must be a clap and we break out of the while loop. If Not and it is more than 250 ms since we started clapping this must be something else, and we reset all of the parameters.
When testing our program we got the wanted result: When we clap, the car reacts. 

We noticed that when we made other short "pulse" sounds like short whistle, shout etc. the car also reacts. This must be due to the fact that these sounds have a pattern matching the one for claps.

(If we had been thinking about it at the time we could had gather data for these other sounds to see how much they look like clapping and maybe be able to modify our program to filter out some of these sounds. If we had access to FourierTransformation, we could also analyse the frequency spectrum of a clap and try to use it to differentiate between sounds with other spectra).


-------------------------------------------------------------------------------------------------------------------
Exercise 6

In this exercise we should let the car move in the direction of the louder sound based on readings from two sensors.

We started by making a little program which sample sound values from the two sensors to check if some calibration is needed. We found that the right sensor measures a little more than the left sensor, so we added a value of 5 to the measurements from the left sensor.

We created a class PartyFinder.java which basicly gather the readings from the left and right sensor and calculate a ratio which determines how much power the left and right wheel gets (link to the file: PartyFinder.java).

The ratio is calculated as how much the left sensor measure relative to the total measurement:


leftSoundValue / (leftSoundValue + rightSoundValue)

After that we calculate the power of each wheel. We are doing this via a power function because a very loud sound to the left should make the car more eager to turn to the left and vice versa. We added a constant to this power funktion to increase the minimum power to get the car to move:


leftPower = 25 + 10^(2.2 - ratio)
rightPower = 25 + 10^(1.2 + ratio)

We got the values in the function from trying different possibilities.


First we tried placing the sensors close to the car but the measurements on the two sensor were very close to each other when we made a noise from one of the sides, so we moved the sensor away from the car (this can be seen in the video below). This also helped the fact that the sensors were reacting to the noise from the wheels.

The final result from this exercise can be seen in the following video:





Conclusion:


The sensor readings of the sound sensor are affected by the background noise in the environment. Detecting a sound in the value range from 2 to 6 can be difficult, since in a noisy environment such as the lab, the sensor readings are never under this range. 

By logging sensor data we noticed that the sensor always starts reading values between 90 and 100 in the first 300 ms. These readings are probably due to the system itself, and we can therefore not consider the readings in the first 300 ms as fully reliable.  

Different kind of sounds can be recognized algoritmically by analyzing sound's volume patterns, but sounds with similar volume patterns (e.g. a clap and a short shout) are difficult to distinguish. 

Some mroe calibration of the sensors could be needed when using more than one sensor.


References:

Sunday, February 16, 2014

Lab Session 2

Date: 13. february 2014
Duration of activity: 3 hours 
Group members participating: Lukas, Daniela, Helene

Goals:

  • Perform exercises from Lab Session 

Plan:
  • Mount the ultrasonic sensor on the car.
  • Run the program for lab 2, see if everything works.
  • Do exercises 1-6.

Results:

Exercise 1


In the first measurament session we measured the distances using the following set-up: 




A measuring tape was used to measure actual distances, while the distances measured by the sensor were printed to the NXT screen. 

We measured distances from three different obects: a wall, a bike helmet and a scarf. The objects were chosen in order to get some variation in the dimension, shape and material of the object to be detected by the sensor. 
All the measuraments were done for distances under 255 cm, i.e. under the declared sensor range. 

MEASURING DISTANCES FROM A WALL

We noticed that the NXT measurament and the actual distances where very close for distances greater than 20 cm. For distances smaller than 20 cm we got errors from 3 to 6 cm between the two kinds of measuramet. 

We suspected that the discrepancy in the results where due to errors in the real world measurament done with the measuring tape. We then decided to repeat the measuraments for distances under 20 cm paying more attention to the accuracy of real world measurament. We used the following set-up:





The new measuraments showed very similar distances for measuring tapes and sensor values. 




MEASURING DISTANCE FROM A BIKE HELMET
The bycicle helmet has a round and irregular surface and several holes in its surface. We wanted to see if these facts could affect the performance of the sensor, but we did't notice a significat difference between in the performance of the NXT sensor compared to the wall experiment.  



The differens between the real world measurement of 13 cm to the NXT measurement of 23 cm may be due to the fact that the helmet is round, and we there fore had trouble measuring the correct real word distance.

MEASURING DISTANCE FROM A SCARF
This measurament session gave very inaccurate results. Many times the sensor value was 255 (meaning no object in a range of 255 cm) even though the actual distance was much smaller. We argued that this result is due to the fact that the scarf often absorbs the ultrasound signals instead of reflecting them back to the sensor.


(*) The measurement switched between the given number and 255 

-----------------------------------------------------------------------------------------------------------
Exercise 2

In this experiment we measured distances from a wall, changing the sampling interval of the ultrasound sensor. 


In the first part of the experiment the distance between the sensor and the wall was kept constant to 50 cm.
We set the sample interval from 300 down to 5 millisenconds, with 10 ms intervals and the NXT measurament where all accurate. 

We then decided to increase the distance from the wall to ca. 1 m in order to see if this had an effect on the NXT measurament, but again we got very accurate NXT measuraments.


We had expected that, due to sound speed, sampling with a too short delay would make the sensor unable to measure the distance, since there would be not enough time for the reflected signal to get back to the source. Since this did't happen, we looked in the LeJos library source code, and discovered that the sensor always waits at least 30 ms between different signals are sent. So, eventhough we set our own sample interval to 1, the sensor would still wait 30 ms. This explains why we got accurate measuraments even with a very low sampling interval.


-----------------------------------------------------------------------------------------------------------------
Exercise 3

In this experiment we tried to mesure distance from a big glass picture and from a wall. 
We tried to measure a distance of 254 cm, but we observed that when the distance is bigger than ca. 230 cm, the measuraments are very difficult to gather and the sensor gives very often a value of 255. Moreover, we could not get the NXT sensor to report a value of more than 240 cm. 

The time limit for measurament is 30 ms which can be seen in the source code for LeJos. Taking this time limit and the speed of sound (340,29 m/sec) into account, we argue that the sensor, in theory, can measure objects to a maximum distance of ca. 5 meters. We got this value doing the fallowing calculations:


340,29 m/s : 10 = 34.029 cm/ms


(34.029 cm/ms * 30 ms) / 2 = 510,4 cm = 5,1 m    (meaning 5,1 m out and back to the sensor)


In practice this theoretical value is restricted according to our experiments to ca. 240 cm (a value even smaller than the declared sensor value of 255 cm).


The time limit for measuraments limits how often we can measure a distance. The maximum lenght of the distance to be measured is bounded in our case by the quality of the sensor. 


--------------------------------------------------------------------------------------------------
Exercise 4

In this experiment we placed the car in front of a wall and afterwards in front of an hand. 
We ran the program and we observed that the car tries to keep a distance of ca. 35 cm from other objects and oscillates around this distance.


Video showing our first observations: http://youtu.be/kNkIeVqSetQ

We started changing values of the gain-variable in the code and made the following observations:


gain = 0.9 -> The car moves faster towards the wall.

gain = 0.1 -> 
The car mover very slow towards the wall.

We then set the gain back to its default value and tried different values for the minPower-variable


min power= 90 -> 
The car runs very fast towards the wall. 

min power= 10 -> 
no movement

min power=30 -> 
The car moves but stops before the desired distance. Not enough power to come closer. 

min power=50 -> 
The car moves up to 45 cm from the object. 


The control is a proportional control. This can be seen in the following lines of code:


error = distance - desiredDistance;

power = (int)(gain*error);

We can see that the error is multiplied by a constant, so the power is proportional to the error.


--------------------------------------------------------------------------------------------------------------------

Exercise 5

The car oscillates when the parameters are set to the values provided with the code (gain= 0.5, min power= 60)

We implemented the differential control in the program by following the pseudocode given in the exercise (see link to source code files under references).

We started experimenting with values of gain, integralGain and derivativeGain, setting them all equal to 0.5, and the car seems to move randomly. 
We didn't have the time to experiment furhter in a systematic way, so we didn't find variables that could make the car stand still. 

-------------------------------------------------------------------------------------------------------------------
Exercise 6

We didn't have the time to make this exercise. 


Conclusion:

The NXT sensor performs quite good in measuring distances from rigid objects, but not quite as good measuring distances from soft objects such as a scarf.

The LeJos API implementation ensures that even when the custom sampling rate for the ultrasound sensor is very low, the sensor is still able to measure distances in the declared range.

The quality of the sensor is a more decisive parameter than the sampling rate in determining the limits of the NXT ultrasound sensor. The NXT ultrasound sensor does not perform very well for distances close to its declared range of 255 cm. 


References:


Monday, February 10, 2014

Lab Session 1

Date: 6. feb. 2014
Duration of activity: 3 timer
Group members participating: Lukas, Daniela, Helene

Goals:
  • Perform exercises from Lab Session 1
  • Get bluetooth to work 
Plan:
  • Run the program from lesson 1, to see if everything works
  • Do exercises 1-5
  • Test bluetooth on windows 8. 
Results:

We set up eclipse and the managed to upload programs to the NXT.

Exercise 1:

We placed the light sensor 1.5 cm from different colored lego bricks, and measured the light intensity with the red led on. We got the following results:

        white     = 57
        yellow     = 57
        red     = 53
        light gray         = 47
        brown(reddish)     = 43
        dark gray         = 40       
        blue     = 40       
        green     = 37
        black     = 34

The threshold value can be obtained by calculating the average value of the black and white measurements:
threshold = (57 + 34) / 2  ~ 45


Exercise 2:

We placed the light sensor 1.5 cm from different coloured lego bricks, and measured the light intensity with the red led off. We got the following results:

        white     =  45      
        yellow     =  45       
        red     =  43       
        light gray         = 37
        brown(reddish)     = 32
        dark gray         = 30
        blue     = 36       
        green     = 29           
        black     = 28

We see that all values are about 10 lower than the measurements with the red led on.

Exercise 3:

We misunderstood the exercise. First we mesasured the light intensity for the colored lego bricks with different sampling times. But we didn't see any change in from exercise 1 and 2. Therefore we realized that we should not measure light intesities, but see what happens with the robot, when it runs the line following program with different sample intervals.

When letting the robot run with sample intervals of 5 and 10 milliseconds, it managed to follow the line nicely, but when increasing the interval to 50 and 100 milliseconds, the robot still manages to follow the line, but will sway significantly, and sometimes miss the line and drive in a circle before finding the line against. When setting the interval even higher, to 500 and 1000 milliseconds, the robot does not manage to follow the line anymore, but will simply drive around in circles.

Exercise 4:

We installed the data-logger and ran SLineFollower.java with different measuring intervals.
With intervals of 5 and 10ms, the robot oscillates with 3 hertz:


When using 50ms, the robot oscillates with 2 hertz. With 100ms, the robot does not really oscillate anymore, but changes direction randomly.
100ms:


To generate these graphs, we measured the lightintensities more often, than the program reacts to the measured values. For example, for the last measurements, we measure the lightintensity every 10ms, while the robot only can change motors every 100th millisecond.


Exercise 5:
When measuring the free memory of the NXT, there is a big difference between using cached strings (left and right variables) and creating new strings every time we need them. The garbage collector has to run every two seconds when creating new strings, while it only has to run every three seconds when using cached strings.



Bluetooth:
We did not manage to get bluetooth to work under windows 8, but we did not have a lot of time left to try to get it to work.

Conclusion:
We now have light-intensities for different colours and settings (LED on and off), which can be used to program the robot's behaviour.

Very short time-intervals for measuring and controlling the motors are not necessarily better than longer time intervals, because the motors can not react fast enough due to friction and momentum in the physical world. Not too much time can pass between measurement of sensors, otherwise the robots program will miss too much and not perform as expected.

When writing Java programs for the NXT, we need to be carefull with memory consumption and creating objects on the heap, because the NXT does not have a lot of memory, and we do not want the garbage collector run too much, to not mess up the timing of measurements in our programs.

References:
http://legolab.cs.au.dk/DigitalControl.dir/NXT/Lesson1.dir/Lesson.html