Subsumption and RemoteRequestev3

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

Moderators: roger, gloomyandy, skoehler

supradical
New User
Posts: 4
Joined: Sat Dec 16, 2017 10:11 pm

Subsumption and RemoteRequestev3

Postby supradical » Sat Dec 16, 2017 10:37 pm

Hello,
I'm working on building a behavior-based robot using the subsumption architecture. I recently purchased a second ev3 kit and wanted to make use of both bricks for my robot. I'm able to pair them both by PAN and USB and I am also able to use remoterequestev3 to set work the motors on my client side brick but when I do the same with the sensors on the second brick it crashes. I'm trying to use to takeControl() when the Ultrasonic (which points downward) reads a distance greater than 5cm so the robot can identify the edge of a table.

public boolean takeControl() {
leftSonicReadings = new float[leftSonic.getDistanceMode().sampleSize()];
leftSonic.getDistanceMode().fetchSample(leftSonicReadings, 0);
leftD = (int)(leftSonicReadings[0]*1000 /10f);
LCD.drawString("Left Distance: " + leftD + " ", 0, 3);
return(leftD > 5 );
}

it works fine if my ultrasonic is on my first brick but I'd really like to figure how to utilize the sensor ports on the second brick.

User avatar
gloomyandy
leJOS Team Member
Posts: 6018
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Subsumption and RemoteRequestev3

Postby gloomyandy » Sun Dec 17, 2017 5:22 pm

It would probably help if you posted the details of the error you get and under what circumstances you get the error. I would also suggest that you write a short test program that only uses the remote sensor (no subsumption, or anything else) to check out if that side of thing is working or not. You may be running into threading issues if you are trying to use remote objects from more than one thread (and if you are using subsumption you may be doing this without even realising it!).
leJOS news https://lejosnews.wordpress.com/

supradical
New User
Posts: 4
Joined: Sat Dec 16, 2017 10:11 pm

Re: Subsumption and RemoteRequestev3

Postby supradical » Sun Dec 17, 2017 7:40 pm

I wrote a simple test program without the subsumption and it works fine. Even if I switch to a local port my project works fine. I initialize the ports in a try/catch. Here is the just of my project.. The program crashes immediately when I use the remote sensorPort.


public clas Project {
public static RegulatedMotor rightMotor, leftMotor;

public project(){

RemoteRequestEV3 brickEV1;

EV3UltrasonicSensor leftSonic, rightSonic;
try {
brickEV1 = new RemoteRequestEV3(BrickFinder.find("EV1")[0].getIPAddress());
leftSonic = new EV3UltrasonicSensor(brickEV1.getPort("S1")); //SensorPort.S2);
rightSonic = new EV3UltrasonicSensor(SensorPort.S1);

leftMotor = brickEV1.createRegulatedMotor("A", 'N');
rightMotor = Motor.A;

Behavior[] behaviors = new Behavior[3];
behaviors[0] = new Forward(rightMotor, leftMotor);
behaviors[1] = new AvoidEdge(rightMotor, leftMotor, leftSonic, rightSonic);
behaviors[2] = new Quit();
Arbitrator arbitrator = new Arbitrator(behaviors, false);
arbitrator.go();

rightSonic.close();
leftSonic.close();
brickEV1.disConnect();
}
catch (Exception e){
System.out.println("Got exception " + e);
}
}
public static void main(String[] args) {
new Project();
}
}
}


public class AvoidEdge implements Behavior {
private boolean suppress;
private RegulatedMotor rightMotor, leftMotor;

private EV3UltrasonicSensor rightSonic, leftSonic;
private float leftD, rightD;
float[] rightSonicReadings, leftSonicReadings;

public AvoidEdge(RegulatedMotor m1, RegulatedMotor m2, EV3UltrasonicSensor s1,EV3UltrasonicSensor s2) {
suppress = false;
rightMotor = m1;
leftMotor = m2;
leftSonic = s1;
rightSonic = s2;
}

public boolean takeControl() {
rightSonicReadings = new float[rightSonic.getDistanceMode().sampleSize()];
leftSonicReadings = new float[leftSonic.getDistanceMode().sampleSize()];

rightSonic.getDistanceMode().fetchSample(rightSonicReadings, 0);
rightD = (int)(rightSonicReadings[0]*1000 /10f);

leftSonic.getDistanceMode().fetchSample(leftSonicReadings, 0);
leftD = (int)(leftSonicReadings[0]*1000 /10f);

LCD.drawString("Left Distance: " + leftD + " ", 0, 3);
LCD.drawString("Right Distance: " + rightD + " ", 0, 4);

return(leftD > 5 || rightD > 5);
}
}

User avatar
gloomyandy
leJOS Team Member
Posts: 6018
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Subsumption and RemoteRequestev3

Postby gloomyandy » Sun Dec 17, 2017 8:07 pm

You still haven't provided any details as to how your program "crashes" what exception and where are you getting it. Are any of the motors remote? What happens if you don't use a remote motor?

You could also try using the RMI based remote interface, that handles multiple threads better (but may have other problems). Personally I wouldn't do any of this using the remote API. I'd write my own code for each EV3 and use sockets to communicate between the two programs. You can almost certainly partition the use of the two EV3s better that way.
leJOS news https://lejosnews.wordpress.com/

supradical
New User
Posts: 4
Joined: Sat Dec 16, 2017 10:11 pm

Re: Subsumption and RemoteRequestev3

Postby supradical » Sun Dec 17, 2017 8:48 pm

I receive IO.Exception: stream is active. I was using a remote motor as well and when I switched it to a local port my sensors work fine. I'm new to remote requests may I only use a single request per port?

User avatar
gloomyandy
leJOS Team Member
Posts: 6018
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Subsumption and RemoteRequestev3

Postby gloomyandy » Sun Dec 17, 2017 9:17 pm

You need to avoid two threads making a remote call at the same time. How you do that depends upon the type of request (ones that block for a significant time are bad!). Using RMI may avoid the issue, you may be able to simply add a critical section around the remote calls, but it depends upon what they are.
leJOS news https://lejosnews.wordpress.com/

User avatar
gloomyandy
leJOS Team Member
Posts: 6018
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Subsumption and RemoteRequestev3

Postby gloomyandy » Sun Dec 17, 2017 9:19 pm

What motor requests are you sending? It is almost certainly a bad idea to be constantly sending say a forward request over and over again, there is no need.
leJOS news https://lejosnews.wordpress.com/

supradical
New User
Posts: 4
Joined: Sat Dec 16, 2017 10:11 pm

Re: Subsumption and RemoteRequestev3

Postby supradical » Sun Dec 17, 2017 9:48 pm

in the action() function I assign the motors backward and then turn and then set them back to forward. Is it possible to create a remoteRequest for each port? I'm not too familiar with threads but would that create unique threads per port?

User avatar
gloomyandy
leJOS Team Member
Posts: 6018
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Subsumption and RemoteRequestev3

Postby gloomyandy » Mon Dec 18, 2017 12:33 am

No it is not possible to create a request for each port. The server side is single threaded. You need to either use the RMI based system or to ensure that you do not make two remote calls at the same time. Given that you are using subsumption the action and takeControl methods will be called on different threads, so you will need to use something like Java synchronization to stop the multiple calls happening at the same time. You also need to understand when your action methods are called, so for instance it may well be that your forward behaviour is being called all of the time over and over again, if that is making a request like calling forward that is not a good idea. Oh and if you post code please use the code tags it makes it much easier to read.
leJOS news https://lejosnews.wordpress.com/


Return to “EV3 Software”

Who is online

Users browsing this forum: No registered users and 2 guests