Hi,
I want to try leJOS with a child in my neighborhood.
So I read a little bit about leJOS and was impressed from
all the possibilities. A great job by the developers, thanks.
To understand the Behavior(-Arbitrator) pattern, I tried this
on my PC and think I found the same problem/bug as "trandi".
This is never tested on the NXT.
I think, the suppress method is not called for active behavior
and so the behavior doesn't terminate. Specially, the behavior with
the highest prio is not suppressed.
I have (for understanding/learning) written a simple test with similar behaviors.
Each behavior runs 10 seconds (variable duration).
Interval[From, To] Behavior
[00:00:00, 00:00:10] no behavior is active (both takeControl == false)
[00:00:10, 00:00:20] B1 is running
[00:00:20, 00:00:30] B2 is running
[00:00:30, 00:00:40] no behavior is active (both takeControl == false)
[00:00:40, 00:00:50] B1 is running
[00:00:50, 00:01:00] B2 is running
[00:01:00, 00:01:10] no behavior is active (both takeControl == false)
....
If I change in Arbitrator.java the ">" to "!=", it works correct for my understanding
of the pattern.
if (active != NONE && _highestPriority != active)
Can somebody please verify this problem again, or do I misunderstand the pattern?
Uwe / Germany
- Code: Select all
package ula.robotics.lejos.examples;
import java.text.SimpleDateFormat;
import lejos.robotics.subsumption.Arbitrator;
import lejos.robotics.subsumption.Behavior;
public class ArbTest {
private static int behaviors = 0; // reinitalized by init()
private static int duration = 10;
private static long now() {
return System.currentTimeMillis();
}
private static long timeSlot() {
return now() / 1000 % (behaviors * duration) / duration;
}
private Behavior createBehavior(final int bId) {
return new Behavior() {
private String name = "B" + bId;
private int id = bId;
private boolean suppress = true;
@Override
public boolean takeControl() {
boolean result = timeSlot() == id;
// print("takeCtrl=" + result);
return result;
}
@Override
public void suppress() {
print("suppress=true");
suppress = true;
}
@Override
public void action() {
print("action started");
suppress = false;
while (!suppress)
Thread.yield();
print("action stopped");
}
private void print(String message) {
synchronized (ArbTest.class) {
System.out.format("%s - %-20s - %s", name, message, new SimpleDateFormat("HH:mm:ss").format(now()));
System.out.println();
}
}
};
}
private void init() {
Behavior[] bList = new Behavior[] {
createBehavior(2),
createBehavior(1),
};
behaviors = bList.length + 1;
Arbitrator arbitrator = new Arbitrator(bList);
arbitrator.start();
}
public static void main(String[] args) {
new ArbTest().init();
}
}