ramsay-t / ShefRobot

Wrapper classes for Java code to use Lego robots.
3 stars 1 forks source link

ColorSensor::getRGB() returns black, all the time. #40

Open Robadob opened 7 years ago

Robadob commented 7 years ago

Think this was a known bug from the outset, not sure why it was never flagged here.

Needs fixing within 7 days if possible, for COM1003.

bradsharp commented 7 years ago

Could really do with this, I'll solve it myself and submit the fix if needed.

ramsay-t commented 7 years ago

We'd very much appreciate that, since several of us have looked at it and determined that it's not a 5 min fix. It seems that the underlying lejos library is returning all zeros. It could be some mode setting is required, or it could be that there is a limitation in lejos (which is open source itself).

On 16 January 2017 16:14:49 GMT+00:00, Brad Sharp notifications@github.com wrote:

Could really do with this, I'll solve it myself and submit the fix if needed.

-- You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub: https://github.com/ramsay-t/ShefRobot/issues/40#issuecomment-272903612

-- Sent from my Android device with K-9 Mail. Please excuse my brevity.

Robadob commented 7 years ago

Feel free to give it a poke, this was a brief writeup I sent Ramsay when I gave it a quick look back in November. I think he also had one of the undergrad GTAs give it a look, which presumably turned up nothing.

Offending method: getRGB()

public java.awt.Color getRGB()
{
    float[] result = sendAction(ColorSensorAction.GET_RGB);
    //Parse returned int into the ambient light;
    return new java.awt.Color(result[0],result[1],result[2]);
}

Calls subAction(Pair<ColorSensorAction,float[]> act), triggering:

protected void subAction(Pair<ColorSensorAction,float[]> act) throws RemoteException
{
    float[] samples;
    switch (act.key) {
        ...
        case GET_RGB:
            samples = new float[((EV3ColorSensor)this.sensor).getRGBMode().sampleSize()];
            ((EV3ColorSensor)this.sensor).getRGBMode().fetchSample(samples,0);
            act.setValue(samples);
            break;
        default: 
            System.err.println("[" + this.port.name() + "] Asked for Action: " + act.key + " on a ColorSensor...");  
    }
}

Notably, this creates a float array of the desired length, calls fetchSample(), the only other method in that interface is the sampleSize() one we already used. This float array is then returned, and passed to the constructor of Color that takes 3 normalised floats.

The only other lejos method used, getRGBMode(), returns a SensorMode object, this extends SampleProvider (the one that provides the above mentioned sampleSize() and fetchSample()), the only additional method provided is getName().

Hence my original assumption that it’s buggered, unless there’s a standalone example of someone else’s lejos code which actually works. Quick google finds this which provides two differing methods (one being older and less elegant, no sensor close method).


I tried both the code segments from here (as linked at the end of the previous email), away from the ShefRobot library, and they both provided the same (0,0,0) readings.

This recent thread (Oct 2016) on lejos forums shows someone who’s actually getting readings from the sensor, so my next suggestion would be to try updating the version of lejos on both the computer and ev3. If that fails, querying it on the forum might get a response from someone in the know.

bradsharp commented 7 years ago

I've had a quick crawl of the internet and the earliest working version I could find dates back to 2014. I'm happy to have a look at the code and see if I can find the problem.

It's probably worth updating to the most recent version of lejos anyway, unless the API has majorly changed and would require a lot of rewriting on our part.