Saturday, March 15, 2014

Lab Session 6

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

Goals:


  • Perform exercises from Lab Session 6

Plan:

  • Rebuild the robot according to the Express-Bot instructions at the nxtprograms.com
  • Do the vehicle 1 experiments:
    • Mount the sound sensor.
    • Program the robot to drive forward when it registers a loud sound.
    • Program the robot to drive both forward and backward based on sound measurements.
  • Do the vehicle 2 experiments:
    • Replace the sound sensor with two light sensors
    • Program the robort to drive towards og away from light.
  • Do the vehicle 3 experiments (if enough time)

Results:

Vehicle 1:

We started by building the Express-Bot according to the following instructions: express-bot.pdf, and mounted the sound sensor as can be seen in the following picture:



We were asked to implement a vehicle that moves faster forward the louder the environment sound is. To do this we created a class called SoundFollower. 

We decided to use the following normalization method presented in todays lecture to map the raw values to the interval [0,1]:



We started by defining the min value to 100 and the max value to 500 just to have some values we later could change to get at better behavior of the robot.

When we got at reading of the raw value from the sound sensor we used the normalization method to get a value we could use to adjust the motor power. This adjustments is done by adding a minPower with the differens between 100 and minPower multiplied with the normalized raw value:



(The complete source code can be found here: SoundFollower.java)

After making this program we uploaded it to the NXT, placed it on the floor close to another group who had some speakers playing music. We observed that the robot moved fast forward when the music was loud and slow down untill it stood still when the music weak.

Since this was the behavior we hoped for, we decided to move on to the next exercise to make the car drive both forward and backward depending on the measurements of the sound sensor.

To do this we added the following method, which uses the normalization method from above and afterwards maps the interval from [0,1] to [-1,1]:



The we changed code in the while loop to do different things depending on the result from the widen method is negative or positive. If the result is negative we take the absolute value of it and calclulate the power as before, but this time make the robot drive backwards. It the result is positive we do the exact same as before:




(The complete source code can be found here: SoundFollower2.jave)

When running this program on the NXT we observed that the robot keept driving backwards until a loud sound occured which made it drive a little forward and then start drive backward again when the sound was gone. We tried to change the variables: min and max, to make the robot more "eager" to drive forward. Our best result was when min was set to 0 and max to 200.

We talk about making the robot oscillate at a certain distance from a sound source, but we did not have a good sound source and had already used a lot of time on these experiments so we decided to move on and look at the experiments for vehicle 2.


-------------------------------------------------------------------------------------------------------------------------------
Vehicle 2:

We started by replacing the sound sensor with to light sensors. The setup can be seen in the videos below.

In these experiments we should implement a robot driving towards or away from light. We did this by using the two light sensors and the normalization method as described above. The big differens from the car that drove when registering a loud sound is that the car now have a light sensor determining the power for each motor:



(The complete source code can be found here: LightFollower.java)

When running this program on the NXT and using a mobilephone as a lightsource we observed that the car drove towards the light. If we switch the leftPower and rightPower we observed that the car now drove away from the light. This behavior can be seen in the following video:



After this we implemented that the robot when exposed to ambient light sound stand still but when the light increases (get brighter) should start moving towards the light. We did this by implementing an average for each light sensor, that registers how the light have changed over time. If the current normalized raw ligth value is bigger than the average then the car starts moving towards the light:



(The complete source code can be found here: LightFollower.java)

When running the program we opserved that the car moved when adding light but after a small amount of time it stoped untill we added more light. This behavior can be seen in the following video:



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

Vehicle 3:

We did not have time to do experiments with this type of vehicle.




Conclusion

From these experiments we have learned how to make a direct mapping from the raw values measured by the sensors to the reaction of the actuators. We are doing this by normalizing the raw values and using this normalization to directly determine the power of a motor.

In the experiments where we had more sensors and actuators with one sensor dertermine the reaction of one actuator we observed that swithing which sensor gives information to which actuators makes the robot have different behavior, i.e. drive towards or away from the light.



References





Tuesday, March 11, 2014

Lab Session 5

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

Goals:


  • Perform exercises from Lab Session 5

Plan:

  • Connect to the NXT via bluetooth
  • Build the robot according to the exercise
  • Program the robot to be self balancing


Results:


After activating bluetooth on our laptop with windows 8, we could see and connect to the NXT. We could upload programs and could use NXJ Browse to interact with the NXT.

We uploaded Sejway.java to the NXT to see how well the program could balance the robot in its current state. We discovered quickly, that in order to configure the robot, we had to balance it on its two wheels, while pressing the enter button at the same time, which would slightly move the robot. This resulted in the robot driving forward or backward even though it was in a balanced position. The measurement of the lightvalue was erronous due to the movement of the button press.

We tested the robot and the original program on a wooden table in the lab, but there were too many shadows and lightsources that would make it impossible for the program to balance the robot after moving a few centimeters.

Therefor, we went the computer-room, where we could shut the windows, letting much less sunlight into the room. Here, the program performed much better, letting the robot drive and keep its balance for a few seconds:

The first change we made to the program, was for it to take many samples with the lightsensor for it to compute and average target intensity, so it does not use the value it reads when the button is pressed, but gives us some to balance the robot, after the button is pressed.



After using this method of initializing the program a few times, we decided that it was still annoying to have to press the button on the robot. You would loose every sense of balance, the moment you press the button. It should be easier to press a button on the computer, while someone else balances the robot.

Because we wanted to do this without the USB cable, we used the bluetooth connection to send a signal, that would make the program start measuring. The program for the robot had to be changed, to use the bluetooth connection and wait for a single message. To do this, we took inspiration from the sample programs that come with lejos:


Before measuring the light values, we initialize the bluetooth connection and then wait for a byte to be recieved.

For the robot to recieve some signal, we had to have a program for the computer-side of things. We took PCcarController.java and changed so that when the "Go" button is pressed, it only sends a byte to the connected bluetooth device, while the values in the text fields are ignored.




This made it easier to set up the robot. The next thing we wanted to do, is change the gain-values for the PID controller. We did not want to do this by changing the program, recompiling and uploading it to the NXT again, and then setting the NXT up again, as we did last week. Instead, we wanted to be able to control the values on the fly, while the robot is driving around.

We changed the program that is running on the NXT to read data from the bluetooth connection, while it tries to balance the robot. It should recieve 4 bytes: one that defines the proportional gain, one for the integral gain and one for the derivative gain. The fourth byte would be added to the target light value the program uses to keep the robot in balance.


When the robot discovers that is has 4 bytes to read, it reads all four bytes without blocking the rest of the program by waiting for byte to be recieved. Then it sets the PID values and adjusts the offset-variable, which is the target light value.

To be able to send these data to the NXT, we changed the PCcarController a lot, adding text fields and another button to send the message. The data was send like this:

This part reads the values from the text fields and sends them as a package of four bytes.
The final implementation can be found here: Sejway.java and PCController.java.
This setup allowed us to change the settings of the program and immediatly see how the robots behaviour changes. The most import important value to change was the light level the robot tries to stay at. The PID values were OK from the start, but is has been difficult to let the program measure the light value it should stay at. Now we could see if the robot drove forward or backward even though it should be balanced, and could adjust the offset value accordingly.

We thought about using the DataLogger class to measure the error over time, to find a way to improve the PID values even more, but we had not enough time left to do this properly.
  


 
Conclusion
The biggest problem was that in all places the brightness differed on the tables and surfaces we let the robot drive on.

Changing parameters in real time worked really well, and we want to extend our program to not just send data to the robot, but also get data back. We could send the error values with timestamps from the robot and draw a graph on the computer to estimate the oscillation, without having to stop the NXT and downloading the log file. This would make it much easier to use the oscillation to adjust the PID values.








Monday, March 3, 2014

Lab session 4

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

Goals:

  • Perform exercises from Lab Session 4

Plan:

  • Mount the light sensor on the car.
  • Do exercises 1-6


Results:

Exercise 1 - Black white detection

We mounted the light sensor on the car . We wrote a program using the BlackWhiteSensor.java.
Source code:
Exercise1.java

We calibrated the sensor by using the black and white areas of one of the lab tables. We then tried to check how good our program is at distinguishing between black and white surfaces. The sensor could distinguish between black and white surfaces, and to some degree dark and light colors, like yellow being detected as white and blue being detected as black. When a color was gray or its brightness right between black and white, the robot would print "not sure" on the display, which happened on the green goal-area of the lab tables. This means that it should be possible to make a distinction between white, green/gray and black, because there is enough difference between the intensities of the colors.

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


Exercise 2 - Line follower with calibration


We uploaded the LineFollowerCal.java and we observed the car's behavior. 

The car is able to follow the black line. When it detects green it starts turning left. This can be explained by the fact that the program interprets the green color as being white.

Video 1

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


Exercise 3 - Three color sensor with calibration


We wrote a program that classifies sensor values as either white, black or green.

We made a class BlackWhiteGreenSensor.java based on the code of BlackWhiteSensor.java class. 
We assumed, due to previous lab experiments and the first exercise, that the sensor value for green would be somewhere between the black and white values. We therefore defined two threshold values as the median between the readings of green and black and the readings of green and white:


blackGreenValue = (blackLightValue + greenLightValue)/2;
greenWhiteValue = (greenLightValue + whiteLightValue)/2;


We then used the threshold values to distinguish between black, white and green. The source code can be seen in the following link:
BlackWhiteGreenSensor.java

The BlackWhiteGreenSensor.java was then used in the following program in order to detect the three different colors:

Exercise3.java

We then uploaded the new program to the nxt car and made calibration of the three colors. Afterwards we tested that our program, and it was able to distinguish between the three colors when putting the sensor on the black, white and green surfaces of the lab tables.


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


Exercise 4 - Line follower that stops in the green goal zone


We used the BlackWhiteGreenSensor.java class to make a program that follows a black line and stop at at a green area at the end of the line.

The while loop of the first LineFollower program was the following:


while (!Button.ESCAPE.isDown()) {
    LCD.drawInt(sensor.light(), 4, 10, 2);
    LCD.refresh();
    if (sensor.black()) {
        Car.forward(power, 0);
    } else if(sensor.white()){
  Car.forward(0, power);
    } else if(sensor.green()){
  Car.stop();
  } else {
        Car.forward(0,power);
}
    Thread.sleep(10);}


When running the program, we noticed how the car at times stopped on the edge of the black line, as it can be seen in the following video:
linefollower1.mp4

We argued that this happened, because on the edge of the line, the sensor catches both black and white light and the value returned (an average of black and white values over the sensed area) is interpreted as green in the logic of our program. 

We then tried to make some changes to the program. In the new version of the program, when a sensor value is classified as green, we check for how long we have seen green. We let the car stop only if the time we have seen green is bigger than 70 ms. In this way we want to make sure that the car is really on a green spot, since if the car is in the edge of the line, the following readings should be either black or white. The entire code can be seen in the following link:
LineFollower2.java

We tested the program, and observed that the car now behaved as required by the exercise. The behavior with the new program can be seen in the following video:
linefollower2.mp4


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

Exercise 5 -  PID line follower

We tried to implement PID control for the car. We did this by implementing the proportional, integral and derivative error as in lab session 2 exercise 5 (lab2). We then tried to find constants for the proportional, integral and derivative gain. We did this by trying different values and adjusting one the gains of the different factors. 

We found some values where the car could drive along the line and stop in the green area. But we did not succeed in finding values that made the car drive smooth along the line.
Also, we had to let the robot drive slowly in order to drive correctly. When we tried to change the minimum speed to make it drive faster, it would consistently driver right though the line. At the end of the lab session, we did not find a set of values to let it drive fast and correctly.

We were missing some kind of methodology to come up with some set of values that would work nicely.
  

  The source code for our PID line follower can be found here: PIDLineFollower

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

Exercise 6 

We did't have the time to do this exercise. 



Conclusions

Detecting colors with a light sensor can be tricky. Due to the fact that the returned values measure the light intensity, colors that reflect light with similar intensities are difficult to distinguish. 
Furthermore, since the light intensity is measured over an area, black and white detection can also be messed up by the fact that the value returned is an average of the light intensities of the area. But the sensor was good enough do differentiate between white green and black, unless we would have a perfect PID controller that lets the robot drive perfectly on the line, then the measured light-intensity could be a gray value that always would match green. Then we would need to switch to two light sensors or a color-sensor.

Finding values for the gains of the PID controllers was hard, we could let the robot drive along the line, but it was neither smooth nor fast. We need to find a methodology to work towards a set of values that works. Maybe we need to slightly change the way the values are used for powering the motors or try to use the data-logger to get more detailed information on the oscillation and the value of the proportional, derivative and integral term and how they interact.