Closing chassis object

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

Moderators: roger, gloomyandy, skoehler

SUPERETDUPER
New User
Posts: 7
Joined: Sat Apr 15, 2017 8:52 pm

Closing chassis object

Postby SUPERETDUPER » Sun Apr 23, 2017 11:40 pm

I know that for motors you need to close the object when you're done with it.

Code: Select all

EV3LargeRegulatedMotor motor = new EV3LargeRegulatedMotor(MotorPort.A);
//Do something
motor.close()


When building a chassis object we supply (at least) 2 motors. Do those motors need to be closed? Is there a close method in the chassis interface which will do this for us? Does not closing our chassis motors result in a memory leak? If we close our motors that we used as chassis parameters, will there be an error?

Code: Select all

EV3LargeRegulatedMotor leftMotor  = new EV3LargeRegulatedMotor(MotorPort.B);
EV3LargeRegulatedMotor rightMotor = new EV3LargeRegulatedMotor(MotorPort.C);

Wheel wheelLeft = WheeledChassis.modelWheel(leftMotor,WHEEL_DIAMETER).offset(-AXIS_LENGTH / 2);
Wheel wheelRight = WheeledChassis.modelWheel(rightMotor,WHEEL_DIAMETER).offset(AXIS_LENGTH / 2);

mChassis =  new WheeledChassis(new Wheel[]{wheelLeft, wheelRight},WheeledChassis.TYPE_DIFFERENTIAL);

//Is this required/allowed
leftMotor.close();
rightMotor.close();

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

Re: Closing chassis object

Postby gloomyandy » Mon Apr 24, 2017 6:53 am

Hi,
the answer is not a very clear one I'm afraid. Yes you really should close all of these things before your program exits. Unfortunately you probably can't. The reason is that the current version of the chassis does not have a close method, nor does it have any way to terminate the thread that tracks the chassis position. This means that if you ever call getPoseProvider then there is a thread running that will continue to use the motors even if you have stopped using the other chassis methods. As a result if you close the motors then this thread will probablt throw an exception. This may not really matter, but it is not ideal.

The good news is that resources like the motors will be closed automatically when your program exits. So assuming this is a program that runs on the EV3 (and it is not trying to use the chassis as a remote object), then the system will take care of releasing used resources for you.

Perhaps you can explain what it is you are concerned about?
leJOS news https://lejosnews.wordpress.com/

SUPERETDUPER
New User
Posts: 7
Joined: Sat Apr 15, 2017 8:52 pm

Re: Closing chassis object

Postby SUPERETDUPER » Sat Apr 29, 2017 3:26 pm

Thank you,

Does this mean that I do not need to close any of my ports if the program exits?

What happens when I call System.exit(0)?

What is the proper way to force a program to exit from a separate thread. In my case I have the escape key listener calling System.exit(0). Does this cause a memory leak?

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

Re: Closing chassis object

Postby gloomyandy » Sat Apr 29, 2017 3:59 pm

The best way to exit a program is to do it cleanly and this means closing everything you have opened. If you don't do this then things like buffered data may get lost and some hardware devices may get left in an indeterminate state. Having said that leJOS, Java and the underlying operating system take defensive steps to try and ensure that things are cleaned up as much as is possible when a program terminates as a result devices will be closed and there will be no memory leak. However this does mean that for instance some sensors may be left in an active state (since there is no generic way to cleanly close down say an i2c sensor), depending on the hardware this may or not be an issue.

So if you want to have some sort of emergency exit button, then I suggest that a way to do that is to have a boolean called say shutdown in a class that monitors the escape key. When the key is pressed then the boolean is set to true. have all of your threads check this boolean and if they find it set to true have them clean up any devices they "own" and close them etc. As a final emergency step have the thread that monitors the escape key call System.exit a short period after setting the boolean to true. That way your threads get chance to close down cleanly, but if they don't do so the program will exit anyway. You could try and close things from your "exit thread" but in general it is usually better to have a single thread deal with a resource including closing it down, trying to close devices etc. that may be in use from another thread can often cause problems that are hard to debug and may cause issues with the hardware. There are lots of variations on this sort of theme, but the above works reasonably well in most cases, you might also want to consider using the Java thread interrupt system:
https://docs.oracle.com/javase/tutorial ... rrupt.html
which may help if your threads make use of blocking calls (but then again it probably won't!).

Good luck!
leJOS news https://lejosnews.wordpress.com/

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

Re: Closing chassis object

Postby gloomyandy » Sat Apr 29, 2017 4:01 pm

Oh and remember that you can use the leJOS menu to abort/kill your program by pressing the enter and down key at the same time.
leJOS news https://lejosnews.wordpress.com/


Return to “EV3 Software”

Who is online

Users browsing this forum: No registered users and 1 guest