LeJOS学习(7):Sensor的API研究-ColorSensor
来源:互联网 发布:网络协议软件是什么 编辑:程序博客网 时间:2024/05/22 00:30
继续刚才的,颜色传感器相对比较复杂一些了,它有三个用途:测颜色,测环境光,测表面亮度,对应出四个模式:
getColorIDMode() //测量颜色模式一,
getRGBMode() //测量颜色模式二,采样得到三个数,分别对应红R绿G蓝B,各自取值从0到1。
getRedMode() //反射光模式,采样得到一个数,介于0到1之间,0表示很暗,1表示很亮
getAmbientMode() //环境光模式,采样也是得到一个数,介于0到1之间
整体来看,先看如下代码,然后再分别说。
EV3ColorSensor sensor = new EV3ColorSensor(SensorPort.S1); //模式一:ColorID,一个数 //SensorMode mode = sensor.getColorIDMode(); //模式二:RGB,三个数 //SensorMode mode = sensor.getRGBMode(); //模式三:环境光,一个数,大则亮,小则暗 //SensorMode mode = sensor.getAmbientMode(); //模式四:反射光,一个数,大则亮,0则暗 SensorMode mode= sensor.getRedMode(); float[] samples = new float[mode.sampleSize()]; while( !Button.DOWN.isDown() ){ mode.fetchSample(samples, 0); int k=0; for(float v : samples) log.echo("v["+(k++)+"]="+v); } sensor.close();
测量颜色
getColorIDMode与getRGBMode都是用于测量颜色,但前者直接得到一个表示颜色的常量,后者更精细,可以得到颜色的细节。
ColorID的取值从0到13,含义如下:
public static final int RED = 0; public static final int GREEN = 1; public static final int BLUE = 2; public static final int YELLOW = 3; public static final int MAGENTA = 4; public static final int ORANGE = 5; public static final int WHITE = 6; public static final int BLACK = 7; public static final int PINK = 8; public static final int GRAY = 9; public static final int LIGHT_GRAY = 10; public static final int DARK_GRAY = 11; public static final int CYAN = 12; public static final int BROWN = 13; public static final int NONE = -1;
测量环境光
getAmbientMode能测量传感器周围的光线强度,采样一个数,介于0到1之间,0表示很暗,越大则越亮。我们需要在使用中自己实验并确定合适的阀值。
测量反射光
getRedMode会让传感器发射红色光(所以叫RedMode吧),同样,采样也只有一个数,介于0到1之间,0表示表面很暗,1表示表面很亮,一般用于巡线的判断,可以知道自己是否偏离。它的测量范围很小,所以稍稍超过一定距离就是0了,需要自己在使用中通过实验确定合适的阀值。
补充:使用Adapter
对于颜色传感器,如果你只是拿来测量颜色,那也可以使用Adapter来简化代码:
ColorAdapter adapter = new ColorAdapter(new EV3ColorSensor(SensorPort.S1)); while( !Button.DOWN.isDown() ){ Color c = adapter.getColor(); String str = "r="+c.getRed()+",g="+c.getGreen()+",b="+c.getBlue(); log.echo(str); Delay.msDelay(1000); if( adapter.getColorID()==Color.BLUE ) break; }
0 0
- LeJOS学习(7):Sensor的API研究-ColorSensor
- LeJOS学习(6):Sensor的API研究-TouchSensor
- LeJOS学习(8):Sensor的API研究-InfraRedSensor
- LeJOS学习(4):Motor的API研究
- LeJOS学习(5):Motor的API研究-进阶篇
- LeJOS学习(3):继续打造Maven的开发环境
- LeJOS学习(2):继续 HelloWorld
- LeJOS学习(9):一个整合传感器与马达的简单例子
- LeJOS学习(1):一切从安装开始
- HttpClient学习研究---第五章:流利的API
- HTML5 audio API的研究与学习1
- [sensor] android的sensor架构
- leJOS——LEGO NXT的Java环境
- 最近研究的windows API
- Android 传感器(Sensor)API教程
- Android 传感器(Sensor)API教程
- Android学习之Sensor
- MQTT的学习研究(十二) MQTT moquette 的 Future API 消息发布订阅的实现
- eclipse部署web项目至本地的tomcat但在webapps中找不到
- 2015.11.1 XJOJ(NOIP模拟赛)总结
- 初级基础练习:酒店管理系统的房间号定位:不用for循环.
- hdu 5532 Almost Sorted Array 最长上升子序列
- Maven2使用项目开发规范说明
- LeJOS学习(7):Sensor的API研究-ColorSensor
- LeetCode----Partition List
- struts2 中 Actionsupport 的作用
- hdu 5538 House Building 基础题
- 那是快感还是痛
- snake
- PHP引用(&)详解
- Codestorm:Counting Triangles 查各种三角形的个数
- MySQL-5.6.25免安装版--安装与配置方法