How to calibrate the Lego NXT color sensor with LeJos NXJ?

寵の児 提交于 2019-12-10 11:29:12

问题


I'm currently writing on a program about detecting different colored balls and sorting them with the Lego NXT and its color sensor. At the beginning it worked quite good but now, the color sensor always returnes the color ID "7" (white), no matter what i do.

On the documentation page i found something about a calibration (calibrateHigh() and calibrateLow()). Does anybody know how to use this calibration or is my color sensor broken?

I tried it with this code:

package com.mydomain;
import lejos.nxt.*;

public class HelloWorld {
  public static void main(String[] args) throws Exception {
        ColorSensor color = new ColorSensor(SensorPort.S2);

    while (true) {
      LCD.drawInt(color.getColorID(), 1, 1, 1);
    }
  }
}

回答1:


First you have to be aware of these conditions:

  1. The color sensor should be about 1cm over the color.
  2. The darker your room is, the more your sensor can detect.

So here is my code to control if the color is white:

public boolean isWhite() {
    //Gives "True" if the color is white
    ColorSensor cs = new ColorSensor(SensorPort.S3);
    cs.setFloodlight(false);
    if (cs.getColor().getColor() == Color.WHITE) {            
      return true;
    }     
    return false;           
    }

Explanation:
The first cs.getColor() in the if - clause gives the rgb value of the color back.
If you call the method cs.getColor() after the first cs.getColor() (so it will be: cs.getColor().getColor) than it will return its color ID.
In this case Color.WHITEis 6.
You can see all numbers here.


Note:
I'm sorry for my bad english and also sorry if this explanation isn't detailed too! I'm not really a java programmer, I just needed that for my school project, therefore sorry that I couldn't explained it more detailed, but I hope this answer can help some people! ;)



来源:https://stackoverflow.com/questions/37641882/how-to-calibrate-the-lego-nxt-color-sensor-with-lejos-nxj

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!