如何用LeJos NXJ校准Lego NXT颜色传感器? [英] How to calibrate the Lego NXT color sensor with LeJos NXJ?

查看:163
本文介绍了如何用LeJos NXJ校准Lego NXT颜色传感器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个程序,用于检测不同的彩色球,并使用Lego NXT及其颜色传感器对其进行分类。
刚开始时效果很好,但现在不管我做什么,颜色传感器始终返回颜色ID 7(白色)。

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.

在文档页面上,我找到了有关校准的内容(calibrateHigh()和calibrateLow())。
有人知道如何使用此校准,还是我的色彩传感器损坏了?

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?

我用以下代码尝试过:

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);
    }
  }
}


推荐答案

首先,您必须了解以下情况:

First you have to be aware of these conditions:


  1. 颜色传感器应在颜色上方约1厘米处。

  2. 房间越黑,传感器可以检测到的越多。

所以这是我的代码来控制颜色是否为白色:

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;           
    }

说明:

第一个 cs.getColor()给出颜色的rgb值。

如果在第一个 cs.getColor() cs.getColor() >(因此它将是: cs.getColor()。getColor ),它将返回其颜色ID。

在这种情况下, Color.WHITE 是6。

您可以看到所有数字此处


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.


注意:

对于我的英语不好,我也感到抱歉。太详细了!我不是真正的Java程序员,我只是在学校的项目中需要它,因此对不起无法详细解释它,但我希望这个答案可以对某些人有所帮助! ;)

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! ;)

这篇关于如何用LeJos NXJ校准Lego NXT颜色传感器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆