Lesson goals:

Are as described in the lesson 1 exercises notes:

  • Install the leJOS Java system
  • Build the 9797 LEGO car with light sensor
  • Exercise 1
  • Exercise 2
  • Exercise 3
  • Exercise 4
  • Exercise 5

Additional “Optional” Goals

Get bluetooth communication up and running

The Plan

Because of the thorough guide described in the lesson 1 exercises notes, no further planning was necessary.

We followed the goals as described earlier, and started off with installing the leJOS Java System on all our machines, with three running Windows 7 and one running Windows 8, and then building the 9797 LEGO car with attached light sensor.

Then we did the exercises as explained.

Exercise 1

Plan

By using the built robot, we can measure different light values from different colored lego bricks.

Experiment

We placed the light sensor above different LEGO bricks with different colors and made a table of light values corresponding to the different colors. We also did this to see if the distance from the sensor to the bricks had any impact on the sensordata. All the data was put into a table (see Table 1).

Setup for reading the value from the NXT light sensor Setup for reading the value from the NXT light sensor

Programming

We were handed the programming section for following lines through this link. The program segments for reading the lights can be found on the link. Here, we create a LightSensor object that can read input values from the method light.readValue().

Results

With LED on and sample interval 10ms
Color 0 cm from 0.5 cm from Delta
White 58% 63% 5
Pink 57% 62% 5
Yellow 57% 61% 4
Orange 57% 61% 4
Light Green 50% 51% 1
Dark Green 41% 40% 1
Red 54% 58% 4
Light Blue 42% 42% 0
Brown 44% 46% 2
Light Gray 49% 52% 3
Dark Gray 43% 42% 1
Black 37% 37% 0

Table 1

So we observe a difference in values dependant on the distance of the sensor from the bricks. Furthermore, it is noteworthy that the black is just black no matter the difference, and the white has different values. This can be because black is absorbing light so it will have less of a variation, compared to lighter-colored bricks which – in the case of white – can have a difference because of the light reflections.

By knowing the percentages, the robot can be programmed to react if a certain threshold has been passed (or if it is reached). In our code we have the magic number blackWhiteThreshold = 45 which states that if (light.readValue() blackWhiteThreshold)the robot will drive to the right. (Otherwise it will drive left). This magic number is found by using the two numbers found, and finding the average value. This would be: 58 (white) + 37 (black) = 95. Then the 95/2 = 47,5. This means that the magic number (the threshold) for our experiments is 47,5.

Exercise 2

Plan

By using the built robot, we can measure different light values from different colored lego bricks when having the red LED turned off.

Experiment

We did the same as in Exercise 1, however, this time we turned the red LED off.

Setup for reading the value from the NXT light sensor without its LED on Setup for reading the value from the NXT light sensor without its LED on

Programming

We were handed the programming section for following lines through this link.

The same code as in Exercise 1 is used, however we change light.setFloodlight(true) into light.setFloodlight(false).

Results

With LED off and sample interval 10ms
Color 0 cm from 0.5 cm from Delta
White 35% 37% 2
Pink 34% 37% 3
Yellow 33% 36% 3
Orange 34% 36% 2
Light Green 32% 34% 2
Dark Green 26% 31% 5
Red 32% 36% 4
Light Blue 28% 32% 4
Brown 25% 28% 3
Light Gray 28% 30% 2
Dark Gray 25% 27% 2
Black 22% 23% 1

Table 2

We observe the same as in exercise 1. However, the values are generally lower. This can be because the LED itself lights the bricks which will improve the samples.

Exercise 3

Plan

Try different sample intervals and see what happens.

Experiment

We used the code provided from the exercise notes. We made the robot do 4 runs, with measurements every 10msec, 100msec, 500msec and 1000msec.

Programming

This time, we used the programming section from here. In the code, for each run, we only changed Thread.sleep(10) to the different intervals.

Results

By increasing the sample value, the robot becomes more unreliable in it’s maneuvering so it will zig-zag more than before. This is because if the robot checks sensor data every second, there is a larger chance that it will veer off course.

Exercise 4

Plan

Try different sample intervals and log this data. Use this data to plot a graph of light values.

Experiment

We used the code provided from the exercise notes for also logging the data. We made the robot do 4 runs, with measurements every 10msec, 100msec, 500msec and 1000msec. All the data logged was importen in a Google Drive Spreadsheet to create the graphs.

Programming

This time, we used the programming section from here. In the code, for each run, we only changed Thread.sleep(10) to the different intervals.

Results

Datalog 10ms See video of the 10ms sample rate run here

Datalog 100ms See video of the 100ms sample rate run here

Datalog 500ms See video of the 500ms sample rate run here

Datalog 1000ms See video of the 1000ms sample rate run here

It is obvious to see that the longer the intervals, the longer the oscillations, the more imprecise the robot will be. However, it could seem that at a certain interval the oscillations do not change much. Why this is, we do not know.

Exercise 5

Plan

The plan is to try and use strings directly in the calls to the LCD, instead of the variables left and right. In this way, we can see how strings will affect the memory of the robot during execution compared to using variables.

Experiment

We ran the program two times, making the robot drive two trips. First trip was for logging the variables, and the second trip was for logging the strings. This was then put in a spreadsheet.

Programming

We used the same code as Exercise 4, but this time we also logged Runtime.getRuntime().freeMemory() every 10ms.

Results

Free memory log

We can see there is a difference between using a string directly and a predetermined variable when writing to the LCD screen. The memory usage of the direct input was somewhat higher than the predetermined variable, as depicted on the graph, where it is observable that the time between the graphs’ minimums and maximums are different. The sudden drop of memory use can indicate there is a garbage collector clearing the memory.

Bonus Exercise – BlueTooth Connection

Plan

The plan is to get BlueTooth connection from our laptop running Microsoft Windows 7 to the NXT.

Experiments

We just established connection through our IDE (Eclipse) through the examples included when installing the leJOS toolkit. A guide for this can be found here.

Programming

No programming necessary.

Results

As shown in this video, we created the BlueTooth connection and it made the robot drive around.

General

Problems

One of the laptops had problems installing BlueTooth devices. The actual problem was a faulty update of the BlueTooth driver which was fixed by uninstalling and reinstalling the driver. The same laptop also had issues with the NXT being registered as an Arduino. The laptop had drivers installed for the NXT and Arduino where both devices would be registered under the same device id. Several forums had different approaches on how to fix this problem on Windows, but we chose a much simpler solution: Uninstall the Arduino driver and IDE, reinstall the NXT driver and do not work with Arduino, on that laptop, in the duration of this course.

Another – which is actually semi unrelated to the course itself – is understanding basic spreadsheets.

Conclusion

From the exercises, we can conclude the following:

Light Values Sensed light values will change dependant of the colors of the material. Also, how far away you move the sensor from the material will also impact how well it is sensed. Furthermore, turned the red LED on/off will have an impact according to the ambient lighting in the room.

Different Sensing Intervals We have found that changing the sensing intervals will change the behavior of the robot. If the sensing interval is large (e.g. 1000ms), the robot will behave more inaccurate.

Memory Use We have shown that the memory usage of a direct input is higher than a predetermined variable. This means that we should avoid using the strings if possible and instead point to a variable in the memory.