User Tools

Site Tools


marvin:lab8

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
marvin:lab8 [2008/11/09 10:21] devamarvin:lab8 [2008/11/09 10:29] deva
Line 55: Line 55:
 this array is copied and stored in the constructor of each Behavior, and can then be iterated whenever suppression of the lower levels are needed. this array is copied and stored in the constructor of each Behavior, and can then be iterated whenever suppression of the lower levels are needed.
 <code java> <code java>
-private ArrayList behaviors;;+private ArrayList behaviors;
  
 public Behavior(String name, ArrayList behaviors) public Behavior(String name, ArrayList behaviors)
Line 82: Line 82:
 ====Java Thread==== ====Java Thread====
 The ''Behavior'' class extends ''Thread'' which makes it run as a thread in the JVM. The thread is configured to run as //daemon// which makes it automatically terminate together with the main thread(([[http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html#setDaemon(boolean)]])). The ''Behavior'' class extends ''Thread'' which makes it run as a thread in the JVM. The thread is configured to run as //daemon// which makes it automatically terminate together with the main thread(([[http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html#setDaemon(boolean)]])).
- 
  
 ====Threaded implementation of "Driving Towards Light"==== ====Threaded implementation of "Driving Towards Light"====
-Bent writes this part+The ''DriveTowardsLight'' class was implemented based on the code from [[marvin:lab7]].\\ 
 +The wanted behavior is the following: 
 +  * suppress lower behaviors 
 +  * read light values 
 +  * assign modified light values as motor power (l1 -> m2, l2 -> m1). 
 +  * unsuppress lower behaviors 
 +Here is the most important code from the class: 
 +<code java> 
 +private LightSensor l1; 
 +private LightSensor l2; 
 +private int offset = 60; 
 + 
 +public DriveTowardsLight(String name, ArrayList behaviors) 
 +
 +  ... 
 +  l1 = new LightSensor(SensorPort.S2); 
 +  l2 = new LightSensor(SensorPort.S3); 
 +  l1.setFloodlight(false); 
 +  l2.setFloodlight(false); 
 +
 + 
 +public void run()  
 +
 +  while (true) 
 +  { 
 +    // Read value from light sensors 
 +    int val1 = l1.readValue() + offset; 
 +    int val2 = l2.readValue() + offset; 
 + 
 +    suppressLower();                    
 +    forward(val2, val1); 
 +    delay(500);  
 +    unsuppressLower(); 
 +
 +</code> 
 +This code however would take over entirely the control of the car since it is active all the time.\\ 
 +We therefore introduced a light threshold to make the car search for light, only when there is some light to search for.\\ 
 +the code for that is shown below: 
 +<code java> 
 +private int threshold = 80; 
 + 
 +public void run()  
 +
 +  . 
 +  . 
 +  . 
 + 
 +  if(val1 > threshold || val2 > threshold) { 
 +    suppressLower();                    
 +    forward(val2, val1); 
 +    delay(500);  
 +    unsuppressLower(); 
 +  } 
 +
 +</code>
  
 **Video for "Drive towards light test"**\\ **Video for "Drive towards light test"**\\
marvin/lab8.txt · Last modified: 2008/11/14 08:50 by rieper