EV3UltrasonicSensor about Invalid sensor mode

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

Moderators: roger, gloomyandy, skoehler

wpimain2
Novice
Posts: 36
Joined: Tue May 17, 2016 11:38 am

EV3UltrasonicSensor about Invalid sensor mode

Postby wpimain2 » Sat Jun 11, 2016 2:57 am

After I restart the EV3 I run the program,then it was Interruptted to jump to this place :

Code: Select all

  public UARTSensor(Port port, int mode)
    {
        this.port = port.open(UARTPort.class);
        if (!this.port.setMode(mode))
        {
            this.port.close();
            throw new IllegalArgumentException("Invalid sensor mode");
        }
        currentMode = mode;
        releaseOnClose(this.port);
    }

the error tips show that because of this sentence "us=new EV3UltrasonicSensor(SensorPort.S4);".But When I run the program again it runs correctly。why?

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

Re: EV3UltrasonicSensor about Invalid sensor mode

Postby gloomyandy » Sat Jun 11, 2016 6:39 am

No idea. Does it do it every time? If so post a short test program that reproduces the problem and provide details of exactly how you have things set up. Also make sure that the problem occurs when using fresh batteries.
leJOS news https://lejosnews.wordpress.com/

wpimain2
Novice
Posts: 36
Joined: Tue May 17, 2016 11:38 am

Re: EV3UltrasonicSensor about Invalid sensor mode

Postby wpimain2 » Sat Jun 11, 2016 12:01 pm

Sometimes occur.You mean the problem may be related to battery。Thank you four reply

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

Re: EV3UltrasonicSensor about Invalid sensor mode

Postby gloomyandy » Sat Jun 11, 2016 12:25 pm

Battery or cables or even the sensor.

What happens if you use a different port (port 1 or 2) does it ever happen then?
How are you running your program (are you using auto run)?
Does it make any difference if you wait a short time after the restart before running the program?

The more information you supply the more likely it is you will get help.
leJOS news https://lejosnews.wordpress.com/

wpimain2
Novice
Posts: 36
Joined: Tue May 17, 2016 11:38 am

Re: EV3UltrasonicSensor about Invalid sensor mode

Postby wpimain2 » Sat Jun 11, 2016 1:07 pm

This phenomenon is very irregular, I do not know because of what

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

Re: EV3UltrasonicSensor about Invalid sensor mode

Postby SUPERETDUPER » Fri Apr 28, 2017 2:03 pm

I have the same issue. Happens 1/3 times.

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

Re: EV3UltrasonicSensor about Invalid sensor mode

Postby gloomyandy » Fri Apr 28, 2017 2:55 pm

Sorry no idea, I have never seen the problem and so can't really investigate it. You do have all of the source to leJOS, so feel free to try and debug things and work out what is going on. Does it make any difference which port the sensor is attached to? Do you get the same problem with say the IR sensor? Do you ever see a problem with the same sensor and using the LEGO software? Do you have another Ultrasonic sensor? Do you seem the same problem with that?
leJOS news https://lejosnews.wordpress.com/

Aswin
leJOS Team Member
Posts: 310
Joined: Tue Apr 26, 2011 9:18 pm
Location: Netherlands
Contact:

Re: EV3UltrasonicSensor about Invalid sensor mode

Postby Aswin » Fri Apr 28, 2017 9:28 pm

Hi,

I also experience this problem from time to time. It is irregular indeed. At least, I have not been able to write a program to trigger the error.

My suspicion is that it has something to do with the port not always being closed automatically after the program terminates. You might want to close the sensor port manually to see if this helps.

Aswin
My NXT blog: http://nxttime.wordpress.com/

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

Re: EV3UltrasonicSensor about Invalid sensor mode

Postby gloomyandy » Sat Apr 29, 2017 7:13 am

I suspect it is timing related, the code to handle initialization and mode change is horrible (it is based on the original LEGO code) and is a little non-deterministic. The LEGO code gets away with it because the VM is constantly polling the sensor and hides small glitches. Our code does not do this we only talk to the sensor when asked to do so. As a result we do all of the initialization when you open the device, but this can take a long time (the LEGO code hides this time from the user by in effect always trying to open the device after it has been attached). It may be that for some particular sensors we do not give them enough time. If I had a sensor that failed I would be adding trace code to the initialization code. As you can see from:
https://sourceforge.net/p/lejos/ev3/cod ... TPort.java
there is a lot of commented out trace code already there!

As Aswin has said simply making sure the sensor is closed properly may help (as it will be in a better known state, when you next open it). It may also help to catch the exception and simply retry the operation if you do get an error. Not the best fix, but effectively what would happen if we change the port code to perform more retry operations which could well be the required fix.

If anyone is really troubled by this and has a sensor that consistently fails that they are prepared to loan me (or swap for one of mine that does not fail), then let me know. But I've not seen the issue can't easily work out what is going on.
leJOS news https://lejosnews.wordpress.com/

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

Re: EV3UltrasonicSensor about Invalid sensor mode

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

Just a bit more info. I have tried with at least two different sensor and it still happens. The error is during initializing. Using a try/catch and retrying should work.

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

Re: EV3UltrasonicSensor about Invalid sensor mode

Postby gloomyandy » Sat Apr 29, 2017 6:55 pm

So out of curiosity I have just run the following code on one of my EV3s. I could only find two US sensors so that's what I used...

Code: Select all

import lejos.hardware.BrickFinder;
import lejos.hardware.port.Port;
import lejos.hardware.sensor.EV3UltrasonicSensor;
import lejos.robotics.SampleProvider;
import lejos.utility.Delay;


public class USTest implements Runnable
{
    Port port;
   
    public USTest(String portName)
    {
        this.port = BrickFinder.getLocal().getPort(portName);
    }

    public static void main(String[] args)
    {
        new Thread(new USTest("S1")).start();
        new Thread(new USTest("S3")).start();
    }

    @Override
    public void run()
    {
        System.out.println("Starting...." + port.getName());
        for(int i = 0; i < 100; i++)
        {
            EV3UltrasonicSensor s = new EV3UltrasonicSensor(port);
            SampleProvider sp = s.getDistanceMode();
            float[] d = new float[sp.sampleSize()];
            sp.fetchSample(d, 0);
            s.close();
            System.out.println("Test " + i + " port " + port.getName() + " val " + d[0]);
            Delay.msDelay(100);
        }
   
    }

}



The above Program ran fine a number of times with no exceptions. I assume you are using leJOS 0.9.1? Are you using Java 7? That's what I used for my test. Perhaps you could try the above and see if it runs on your setup. If it does then there may be something else in your program causing the problem? I assume the code you are using does run directly on the EV3?
leJOS news https://lejosnews.wordpress.com/


Return to “EV3 Software”

Who is online

Users browsing this forum: No registered users and 3 guests