Are as described in the lesson 4 exercises notes:
- Exercise 1 – Black White Detection
- Exercise 2 – Line Follower with Calibration
- Exercise 3 – ThreeColorSensor with Calibration
- Exercise 4 – Line Follower that stops in a Goal Zone
- Exercise 5 – PD Line Follower
- Exercise 6 – Color Sensor
Additional “Optional” Goals
- Realtime BlueTooth communication
- Implemented the use of threads
Because of the thorough guide described in the lesson 3 exercises notes, no further planning was necessary.
Then we did the exercises as explained.
Exercise 1 – Black White Detection
After mounting the light sensor to Max as we did in lesson 1, we are now ready to calibrate and afterwards test the BlackWhiteSensor.java program, by placing the sensor above different surfaces, ranging from dark to bright.
To perform the experiments we started with installing the BlackWhiteSensor program to Max and then started the calibration part which dictated to place the light sensor on a black/dark surface. We did this and repeated the procedure with the light sensor above a white/bright surface. This was done a total of 3 times to make sure that the values were similar on different areas of the dark and white surfaces.
We then did the same test, using a dark green and a light green surface.
Only minor modifications were needed to test the program:
- A new class: BWCalTest.java
- The addition of a main method to BWCalTest to run the test.
- A while loop to let us read the results of the calibration before shutting down the program.
- Getter methods for retrieving the values from the BlackWhiteSensor class
The final program can be found here.
As seen on the following figure 1, the black, white and B/W Threshold, are evenly separated and with enough difference between the values to handle the different lighting condition that might occur along the day. If we take a look at the values for dark green, light green and the green threshold, they once again have a clear distinction between them, but unlike the black and white test, they only have 5 points between them and the threshold which might not be enough to handle different lighting conditions. This means that the light sensor is more than enough to react to changes from black to white but might come short when asked to handle additional colors.
Exercise 2 – Line Follower with Calibration
With the code provided we will see how the LineFollower performs when we use a calibration mechanism to clarify the different colors and black.
We installed the program on Max and proceeded to test his new abilities on a square track, as seen in this video:
As Max reaches the end of the second black square, he then turns right instead of left because of the black/white threshold value being smaller than the light green value, as seen on figure 1. Therefore, we performed the same experiment again, but this time we used the calibration method to calibrate Max to have the light green color as the black parameter, and keeping white as white, which made it possible to make the required left turn when reaching the light green surface as seen here.
We used the code supplied for this exercise without other modifications than switching the power from the left motor to the right motor to support Max’s design.
The complete code can be downloaded here.
The results of this exercise confirms the usefulness and validity of a calibration function as used in this test, making it easier for Max to follow lines in different conditions e.g. light green and white.
Exercise 3 – ThreeColorSensor with Calibration
With the BlackWhiteSensor class as a baseline, we will expand its functionality to use the Lego Color Sensor instead of the light sensor, and then instead of calibrating to only black and white, it will now also handle the color green. The color sensor itself will be mounted to the front of Max in the same way as the light sensor was mounted.
Since this test was in all ways similar to that of exercise 1, we took the same approach. However, instead of just looking at the light intensity as we did with the light sensor, we now look at the light intensity of Red, Green and Blue, which should make it easier for us to distinguish them from each other compared to when we used the light sensor.
We then, in the same way as before, wrote down the values of the individual intensities and displayed them as seen in the following figure:
The program was based on the thoughts behind the BlackWhiteSensor, but instead of sampling black and white resulting in a threshold, we now read the raw color value for: Red, Green and Blue instead of just the combined intensity shown as light (See bottom axis on Figure 2). We then did as before with defining two thresholds: one between Black and Green, and one between White and Green.
The final code can be downloaded here.
As seen in figure 2, there is a clear distinction between the black color and the green color, and between the white color and green color.
If we compare the values we got from both the green and the white values, we will notice that when observing on the blue color they yield a greater difference than e.g. on the green color – which also appears when comparing the green and black values but inversed, so that the blue color has a smaller difference than values from the green color.
This means that we could apply more validity to the blue values when looking for white/green because this should be more distinct.
These results could arguably have been achieved just as easily using the light sensor, since the threshold between the black, white and green light values are sufficiently different enough as shown in both figure 1 and figure 2. But the approach of using the color sensor will make Max capable to adapt to using different colors at once.
Exercise 4 – Line Follower that stops in a Goal Zone
Using the same principle as with the previous line follower, we now just have to make Max stop in the green area at the end of a new track, using the ThreeColorSensor instead of the BlackWhiteSensor.
Just as in exercise 2 we now use the track seen in figure 3 to test Max’ abillity to follow the black line and stop in the green area at the end of the line.
This experiment had initially cost a great deal of trouble because of the color sensor was switching between sensing modes, causing the response time to skyrocket, making it impossible for Max to respond to the curves of the track.
After setting the color sensor to full color mode by not enabling the floodlight, we could now reach the response time that we needed to correctly follow the line.
The program, as in exercise 2, created an instance of the ThreeColorSensor from exercise 3 and called its method getCurrentColor() to determine what action Max should take while driving.
The only real difference between the LineFollower from before and this, is that we have a counter for green, to counteract getting a false positive and Max stopping mid track. This means that Max needs to get 25 consecutive readings of green before stopping.
The complete program can be downloaded here.
As seen in the video here.
We were able to make Max successfully follow the line and stop in the green area at the end.
Exercise 5 – PD Line Follower
Using the guide to PID Controlling a Lego robot as the base, we would start by implementing the suggested calculations from the guide and then proceed to adjust the calculations and variables to match the ones from our setup.
Through trial and error we honed in on the correct variables and made Max follow the line of the track smoothly.
The guide presented a 13 step-by-step tutorial of finding the correct variables but since we had changed the calculations and variables used, meant that we could not follow the guide step-by-step but only as guidelines for what variables to change and in what direction.
The program is a simple loop repeating 3 steps:
- Measure color values
- Calculate power
- Adjust Max movements
The final code can be found here.
As seen in this video, Max follows the line of the track stopping in the green area.
Exercise 6 – Color Sensor
Gloat in our previous success.
Because we already in exercise 3 decided to use the color sensors full color mode to determine when it was green, instead of just using the light intensity, we therefore already know the results of the test.
The code supplied with the exercise ColorSensorSensor.java continuously shows the current RGB + Light values, and also the colors the sensor suspects the surfaces it is reading are.
This is the same as we did for our ThreeColorCalibration test except we made it save the values for the different colors for later comparison.
The results can be seen in Exercise 2 – Results.
Bonus Exercise – Realtime BlueTooth communication
Make Max react to our commands on our laptops through BlueTooth.
We used the Exercise 5 experiment, and made it possible to change the gain-value and dgain-value in realtime. The change in gain-value and dgain-value could either be done by directly writting the values in the corresponding texfields or by using the plus and minus buttons in GUI. If using the plus and minus buttons, the values would change according to the values that were defined in the step size textfield.
The GUI that we made was designed for functionality not beauty as seen below:
Our program is based on the Lesson 5 code, PCCarController and BTControlledCar where you can control how fast and how long the robot can drive – all this provided with a simple Graphical User Interface (GUI) for interaction. We have used this code for inspiration to know how to create and use the BlueTooth connection for realtime changes. We also changed the GUI so we can change Gain and DGain for Max N00b, and the code can be found here. The code for Max N00b can be found here.
The results can be seen in this video that shows Max change his oscillation and how aggressive he is when turning (Gain and DGain) when commanded by our laptop. We ignored changing Iled since we had too many issues making it work properly and we still achieved acceptable results without changing it. Iled can be changed if we want to make Max better at handling sharp corners.
Bonus Exercise – Implemented the use of threads
We want to implement the use of threads in our code, so Max can handle e.g. several sensors and listeners. If we did not thread it would not be possible for Max to listen and adapt simultaneously.
We used the Exercise 5 experiment. This is mostly coding, so we just want to see Max drive on the track.
We use the threads from the former bonus exercise. What we found – to much delight – was that it works just like in standard java syntax. The code can also be seen in the former bonus exercise.
The results are obviously in the code.
We had some issues with bad documentation of the leJOS framework, as shown when trying to figure out how the color sensor finds out what color is being sensed. This was a very cumbersome task, trying to find out which methods to actually use.
Testing on Max when he is on low battery was a problem as well. This problem was, however, easily avoided by having a coffee break while he recharges.
The light sensor can sense in grayscale and excels in differentiating the black and white spectrum, making it especially suited for line-following programs as seen in previous exercises. The sensor can, however, come short when working with multiple colors of similar grayscale values. This is where the color sensor excels, because – as we have seen – it can separate the different colors making it possible to distinguish e.g. red from blue.
We have realized, though, that if we had started with our bonus exercises we could have saved a lot of man-hours on calibrating as we could then have done it live from our laptops.