marvin:lab8
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
marvin:lab8 [2008/11/07 13:14] – sohn | marvin:lab8 [2008/11/09 10:29] – deva | ||
---|---|---|---|
Line 29: | Line 29: | ||
===PlaySound=== | ===PlaySound=== | ||
This thread waits for 10 seconds, then stops and plays a sound while printing an s on the screen. | This thread waits for 10 seconds, then stops and plays a sound while printing an s on the screen. | ||
- | |||
====Modular Priority System==== | ====Modular Priority System==== | ||
- | Bent writes | + | The priority handling was in the initial code created by handing pointers to all lower priority Behavior to each Behavior, as shown in the code below: |
+ | <code java> | ||
+ | rd = new RandomDrive(" | ||
+ | af = new AvoidFront (" | ||
+ | ps = new PlaySounds ("Play ", | ||
+ | </ | ||
+ | This however makes shuffling of the priorities, as well as making the introduction of new Behaviors, a very tedious task. We therefore rewrote the priority system to make it more modular and flexible.\\ | ||
+ | Below is the new stucture: | ||
+ | <code java> | ||
+ | ArrayList behaviors = new ArrayList(); | ||
+ | |||
+ | RandomDrive rd; | ||
+ | AvoidFront af; | ||
+ | PlaySounds ps; | ||
+ | DriveTowardsLight dl; | ||
+ | |||
+ | rd = new RandomDrive(" | ||
+ | dl = new DriveTowardsLight(" | ||
+ | af = new AvoidFront(" | ||
+ | ps = new PlaySounds(" | ||
+ | </ | ||
+ | The idea is to use an array of Behaviors containing all lower priority Behaviors in each Behavior.\\ | ||
+ | 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> | ||
+ | private ArrayList behaviors; | ||
+ | |||
+ | public Behavior(String name, ArrayList behaviors) | ||
+ | { | ||
+ | . | ||
+ | . | ||
+ | . | ||
+ | |||
+ | // Clone the Behavior list. | ||
+ | this.behaviors = new ArrayList(); | ||
+ | for(int i = 0; i < behaviors.size(); | ||
+ | Behavior b = (Behavior)behaviors.get(i); | ||
+ | this.behaviors.add(b); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public void suppressLower() | ||
+ | { | ||
+ | for(int i = 0; i < behaviors.size(); | ||
+ | Behavior b = (Behavior)behaviors.get(i); | ||
+ | b.setSuppress(true); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
====Java Thread==== | ====Java Thread==== | ||
The '' | The '' | ||
+ | ====Threaded implementation of " | ||
+ | The '' | ||
+ | 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, | ||
+ | delay(500); | ||
+ | unsuppressLower(); | ||
+ | } | ||
+ | </ | ||
+ | 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, | ||
+ | delay(500); | ||
+ | unsuppressLower(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
**Video for "Drive towards light test" | **Video for "Drive towards light test" | ||
[[http:// | [[http:// | ||
+ | |||
+ | ====Conclusion==== | ||
+ | The layered behavior model seems to work very well for the implementation of an autonomous roboot. We believe that with an added push sensor Marvin would be able to navigate in any room avoiding any obstacles and find the exit with lights. |
marvin/lab8.txt · Last modified: 2008/11/14 08:50 by rieper