Trouble with priority in behavior programming

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

Trouble with priority in behavior programming

Postby abcde13 » Tue Jul 10, 2012 9:42 pm

So, I read in the tutorials that the arbitrator takes the behaviors and their priority is determined by their index. So behavior b1 at index 0 has less priority then b3. It also says there that it looks at the takeControl() method of each behavior to see if it wants control. If it does, then it checks its priority against the priority of the active behavior. If it is higher, it activates; if not, it is suppressed. My question is, what if the current behavior no longer wants to take control (i.e. its boolean is now false) and a one of lower priority does? How does one get the lower priority behavior to activate now that it wants to take control and the other one doesn't? I'm having mind boggling problems trying to figure this out.
abcde13
New User
 
Posts: 7
Joined: Mon Jul 09, 2012 2:37 pm

Re: Trouble with priority in behavior programming

Postby roger » Wed Jul 11, 2012 1:03 am

Suppose your behavior b1 is active. It will retain control as long as (1) its action() method is running and (2) no behavior of higher priority returns true to its takeControl() method. But If b3.takeControl() return true when the arbitrator calls it, then the arbitrator will call b1.suppress() [ and b1 should immediately exit its action() ] then arbitrator calls b3.action(). If b1 then wants control again, it will not become active until b3.action() exits.
Clear?
I hope so.
Roger
roger
Moderator
 
Posts: 368
Joined: Fri Jun 01, 2007 4:31 am
Location: Berkeley, CA

Re: Trouble with priority in behavior programming

Postby abcde13 » Wed Jul 11, 2012 2:23 am

Ahh, ok. So in the while statement before the Thread.yield(), action b3 must have the !suppressed AND some other logic so that it may quit if I want the robot to be able to rinse and repeat after achieving the behavior with highest priority right? Because right now, I've got a scan, seek, halt sequence going, but I want it to be able to repeat the process if the desired object is moved farther away.
abcde13
New User
 
Posts: 7
Joined: Mon Jul 09, 2012 2:37 pm

Re: Trouble with priority in behavior programming

Postby roger » Wed Jul 11, 2012 11:13 pm

I do not understand your last post. No action() method { except for the lowest priority behavior) can run an endless loop. It must exit by itself at some time. But an action may be repeated indefinitely if the behavior always wants to be active, its action method will be called again as no higher priority behavior wants to run. What do your 3 behaviors do? What is the state of the robot when each action() method exits? What is the robot state when each behavior wants to take control? What while loop are you concerned about?
roger
Moderator
 
Posts: 368
Joined: Fri Jun 01, 2007 4:31 am
Location: Berkeley, CA

Re: Trouble with priority in behavior programming

Postby roger » Wed Jul 11, 2012 11:17 pm

I do not understand your last post. No action() method ( except for the lowest priority behavior) can run an endless loop. It must exit by itself at some time. But an action may be repeated indefinitely: if the behavior always wants to be active, its action method will be called again if no higher priority behavior wants to run.
To help me understand, you could explain: What do your 3 behaviors do? What is the state of the robot when each action() method exits? What is the robot state when each behavior wants to take control? What while loop are you concerned about?
Roger
roger
Moderator
 
Posts: 368
Joined: Fri Jun 01, 2007 4:31 am
Location: Berkeley, CA

Re: Trouble with priority in behavior programming

Postby abcde13 » Wed Jul 11, 2012 11:38 pm

Okay, I'll explain what project I'm doing this with. It's a swarm robotics project, in which the actions will be controlled through behaviors and the movement will be recorded by a navigator. There are 3 behaviors: scan, seek, and halt--priority is in that order, going from least to greatest. Scan keeps the robot turning around until its sonar sensor seems something less than 25 cm away. Then, it should switch to seek which, after aligning itself with the object, will move forward till the object is less than 10 cm away. Then it should stop in front of it.

Code for each behavior, in order:

Code: Select all
package com.mydomain;

import lejos.nxt.*;
import lejos.robotics.subsumption.*;


//Behavior "Scan"
//Search for objects in vicinity. Stop when something is in sight.

public class Scan implements Behavior {

   /**
    * @param args
    */
   private UltrasonicSensor sonar = new UltrasonicSensor(SensorPort.S3);
   private boolean suppressed = false;

   public void suppress() {
      suppressed = true;
   }

   public boolean takeControl() {
      return (true);
   }

   public void action() {
      suppressed = false;
      Motor.A.setSpeed(180);
      Motor.C.setSpeed(180);
      Motor.A.forward();
      Motor.C.backward();
      while(!suppressed) {
         Thread.yield();
         LCD.drawInt(sonar.getDistance(),3, 5, 3);
         LCD.drawString("Scanning", 6,6);
      }
      LCD.clear();
      Motor.A.stop();
          Motor.C.stop();
    }

}


Code: Select all
package com.mydomain;

import lejos.nxt.*;
import lejos.robotics.subsumption.*;

//Stop behavior
//Just stops when it is 25 centimeters/less away from object

public class Seek implements Behavior{
   private UltrasonicSensor sonar = new UltrasonicSensor(SensorPort.S3);
   private boolean suppressed = false;

   public void suppress() {
      suppressed = true;
   }

   public boolean takeControl() {
      return (sonar.getDistance() < 25 && sonar.getDistance() > 10);
   }
   
   public void alignCenter() {
      LCD.clear();
      Motor.A.resetTachoCount();
      while(sonar.getDistance() <25) {
         Motor.A.forward();
         Motor.C.backward();
      }
      int moved = Motor.A.getTachoCount();
      Motor.A.resetTachoCount();
      LCD.drawInt(moved, 4, 4);

      while(-Motor.A.getTachoCount() < moved/2) {
         LCD.drawInt(Motor.A.getTachoCount(), 5, 5);
         Motor.A.backward();
         Motor.C.forward();
      }
      Motor.A.stop();
      Motor.B.stop();
      
   }

   public void action() {
      suppressed = false;
      //alignCenter();
      Motor.A.forward();
      Motor.C.forward();
      while(!suppressed && sonar.getDistance() > 10) {
         Thread.yield();
         LCD.drawInt(sonar.getDistance(),3, 5, 3);
         LCD.drawString("Stopped", 6,6 );
      }
      LCD.clear();
      //Motor.A.stop();
          //Motor.C.stop();
    }

}


Code: Select all
package com.mydomain;

import lejos.nxt.*;
import lejos.robotics.subsumption.*;

public class Halt implements Behavior {

   /**
    * @param args
    */
   private UltrasonicSensor sonar = new UltrasonicSensor(SensorPort.S3);
   private boolean suppressed = false;

   public void suppress() {
      suppressed = true;
   }

   public boolean takeControl() {
      return (sonar.getDistance() < 10);
   }

   public void action() {
      suppressed = false;
      Motor.C.stop();
      Motor.A.stop();
      while(!suppressed && sonar.getDistance() < 10) {
         Thread.yield();
         LCD.drawInt(sonar.getDistance(),3, 5, 3);
         LCD.drawString("Scanning", 6,6);
      }
      LCD.clear();
      Motor.A.stop();
          Motor.C.stop();
    }
}


So how would I make it so if the object moves away, scan will take over again instead of halt continuing, even if scan wants to take control and halt doesn't?
abcde13
New User
 
Posts: 7
Joined: Mon Jul 09, 2012 2:37 pm

Re: Trouble with priority in behavior programming

Postby roger » Thu Jul 12, 2012 6:40 am

Since Halt is the highest priority behavior, Halt.supress() will never be called, and the loop runs forever. Is that what happens? Do you need the Halt behavior at all? It seems to me that if you remove the comments from the last two lines of Seek.action(), then the robot will stop within 10 cm of the target and the action is complete. Is that what will happen? If so, what happens next?
Roger
roger
Moderator
 
Posts: 368
Joined: Fri Jun 01, 2007 4:31 am
Location: Berkeley, CA

Re: Trouble with priority in behavior programming

Postby abcde13 » Thu Jul 12, 2012 2:13 pm

I didn't think of that. So, if I just had scan and seek, when the robot gets within 10 cm, it will exit its while loop and execute Motor.A.stop() and Motor.C.stop(). Then, neither of the actions want to take control, and it should stay in place. I'll try that out.

----

So, now with these two code snippets (the behaviors):

Code: Select all
package com.mydomain;

import lejos.nxt.*;
import lejos.robotics.subsumption.*;


//Behavior "Scan"
//Search for objects in vicinity. Stop when something is in sight.

public class Scan implements Behavior {

   /**
    * @param args
    */
   private UltrasonicSensor sonar = new UltrasonicSensor(SensorPort.S3);
   private boolean suppressed = false;

   public void suppress() {
      suppressed = true;
   }

   public boolean takeControl() {
      return (sonar.getDistance() >= 25);
   }

   public void action() {
      suppressed = false;
      Motor.A.setSpeed(180);
      Motor.C.setSpeed(180);
      Motor.A.forward();
      Motor.C.backward();
      while(!suppressed) {
         Thread.yield();
         LCD.drawInt(sonar.getDistance(),3, 5, 3);
         LCD.drawString("Scanning", 6,6);
      }
      LCD.clear();
      Motor.A.stop();
          Motor.C.stop();
    }

}


Code: Select all
package com.mydomain;

import lejos.nxt.*;
import lejos.robotics.subsumption.*;

//Seek behavior
//Just stops when it is 10 centimeters away or less from object

public class Seek implements Behavior{
   private UltrasonicSensor sonar = new UltrasonicSensor(SensorPort.S3);
   private boolean suppressed = false;

   public void suppress() {
      suppressed = true;
   }

   public boolean takeControl() {
      return (sonar.getDistance() < 25 && sonar.getDistance() > 10);
   }
   
   public void alignCenter() {
      LCD.clear();
      Motor.A.resetTachoCount();
      while(sonar.getDistance() <25) {
         Motor.A.forward();
         Motor.C.backward();
      }
      int moved = Motor.A.getTachoCount();
      Motor.A.resetTachoCount();
      LCD.drawInt(moved, 4, 4);

      while(-Motor.A.getTachoCount() < moved/2) {
         LCD.drawInt(Motor.A.getTachoCount(), 5, 5);
         Motor.A.backward();
         Motor.C.forward();
      }
      Motor.A.stop();
      Motor.B.stop();
      
   }

   public void action() {
      suppressed = false;
      //alignCenter();
      Motor.A.setSpeed(180);
      Motor.C.setSpeed(180);
      Motor.A.backward();
      Motor.C.backward();
      while(!suppressed && takeControl()) {
         Thread.yield();
         LCD.drawInt(sonar.getDistance(),3, 5, 3);
      }
      LCD.clear();
      Motor.A.stop();
          Motor.C.stop();
    }

}


I also changed the arbitrator to contain the inactive parameter to true. This way, as soon as it performs its scan, then seek, it should exit, because the way I see it, the object will still be less than 10 cm away, so neither behaviors want to takeControl(), and neither are active.

What happens though is as soon as it gets less than 10 cm away from something, it does rotates in place until it moves out of vision with the object, then it either quits, or it repeats the whole thing. I'm thinking it should be quitting, but it simply isn't
abcde13
New User
 
Posts: 7
Joined: Mon Jul 09, 2012 2:37 pm


Return to NXJ Software

Who is online

Users browsing this forum: Google [Bot] and 0 guests

more stuff