彩色传感器

python 编程:

创建实例

  • colorlight(port_num)
    创建colorlight实例,port_num 可取值为 thunbot.PORT_1, thunbot.PORT_2,thunbot.PORT_3, thunbot.PORT_4,thunbot.PORT_A,thunbot.PORT_B

方法

  • set_mode(opt_mode)
    设置工作模式,opt_mode的取值如下表:
opt_mode 取值 模式名 功能
MODE_ENV 环境光模式 测量传感器前方光强程度
MODE_REFLECT 反射光模式 获取RGB分量的反射光相对强度
MODE_COLOR 颜色模式 获得颜色编号
注:颜色编号为, 0|无(传感器前方没有色卡)、1|黑色、2|白色、3|红色、4|黄色、5|绿色、6|青色、7|蓝色、8|紫色
  • get_result(result_index)
    获取检测结果,result_index的取值如下表:
result_index 取值 功能
RESULT_ENV 获取前方光强程度
RESULT_REFLECT_R 获取反射光红色分量
RESULT_REFLECT_G 获取反射光绿色分量
RESULT_REFLECT_B 获取反射光蓝色分量
RESULT_COLOR 获取颜色编号
RESULT_COLOR_H 获取颜色的H分量
RESULT_COLOR_S 获取颜色的S分量
RESULT_COLOR_V 获取颜色的V分量
RESULT_COLOR_R 获取颜色的R分量
RESULT_COLOR_G 获取颜色的G分量
RESULT_COLOR_B 获取颜色的B分量
  • set_reflect_led(opt_data)
    设置工作模式为反射光模式,并设置反射光模式的灯光,opt_data的取值如下表:
opt_data 取值 功能
RED_ON 红灯开
GREEN_ON 绿灯开
BLUE_ON 蓝灯开
注:opt_data的取值支持按位或
  • set_extremum(opt_mode,opt_type,opt_data)
    设置指定模式的检测范围,参数的取值如下表:
opt_mode 取值 模式名
MODE_ENV 环境光模式
MODE_REFLECT 反射光模式
opt_type 取值 功能
SET_MINIMUM 设置最小值
SET_MAXIMUM 设置最大值
RESET 复位,恢复默认值
注:opt_data的值在RESET时,不可填写

编程示范

import thunbot
import time

c3 = thunbot.colorlight(thunbot.PORT_1)

# 环境光测试 
c3.set_mode(c3.MODE_ENV)
for i in range(0,50):
    print("env test",c3.get_result(c3.RESULT_ENV))
    time.sleep_ms(200)

# 反射光测试 
c3.set_reflect_led(c3.RED_ON)   
for i in range(0,50):
    print("reflect test,red led on:")
    print("R:",c3.get_result(c3.RESULT_REFLECT_R))
    print("G:",c3.get_result(c3.RESULT_REFLECT_G))
    print("B:",c3.get_result(c3.RESULT_REFLECT_B))
    time.sleep_ms(200)

c3.set_reflect_led(c3.GREEN_ON) 
for i in range(0,50):
    print("reflect test,green led on:")
    print("R:",c3.get_result(c3.RESULT_REFLECT_R))
    print("G:",c3.get_result(c3.RESULT_REFLECT_G))
    print("B:",c3.get_result(c3.RESULT_REFLECT_B))
    time.sleep_ms(200)

c3.set_reflect_led(c3.BLUE_ON) 
for i in range(0,50):
    print("reflect test,blue led on:")
    print("R:",c3.get_result(c3.RESULT_REFLECT_R))
    print("G:",c3.get_result(c3.RESULT_REFLECT_G))
    print("B:",c3.get_result(c3.RESULT_REFLECT_B))
    time.sleep_ms(200)

# 颜色测试
c3.set_mode(c3.MODE_COLOR)
for i in range(0,50):
    print("color test:")
    print("color:",c3.get_result(c3.RESULT_COLOR))
    print("H:",c3.get_result(c3.RESULT_COLOR_H))
    print("S:",c3.get_result(c3.RESULT_COLOR_S))
    print("V:",c3.get_result(c3.RESULT_COLOR_V))
    print("R:",c3.get_result(c3.RESULT_COLOR_R))
    print("G:",c3.get_result(c3.RESULT_COLOR_G))
    print("B:",c3.get_result(c3.RESULT_COLOR_B))
    time.sleep_ms(200)

# 设置范围测试 
# 环境光设置范围测试
print("env set_extremum test")
c3.set_mode(c3.MODE_ENV)
c3.set_extremum(c3.MODE_ENV,c3.SET_MINIMUM,50)
for i in range(0,50):
    print("env SET_MINIMUM = 50:")
    print(c3.get_result(c3.RESULT_ENV))
    time.sleep_ms(200)

c3.set_extremum(c3.MODE_ENV,c3.RESET)
for i in range(0,50):
    print("env SET_MINIMUM  RESET:")
    print(c3.get_result(c3.RESULT_ENV))
    time.sleep_ms(200)

c3.set_extremum(c3.MODE_ENV,c3.SET_MAXIMUM,30)
for i in range(0,50):
    print("env SET_MAXIMUM = 30:")
    print(c3.get_result(c3.RESULT_ENV))
    time.sleep_ms(200)

c3.set_extremum(c3.MODE_ENV,c3.RESET)
for i in range(0,50):
    print("env SET_MINIMUM  RESET:")
    print(c3.get_result(c3.RESULT_ENV))
    time.sleep_ms(200)

# 反射光范围测试
print("reflect set_extremum test")
c3.set_mode(c3.MODE_REFLECT)
c3.set_extremum(c3.MODE_REFLECT,c3.SET_MINIMUM,50)
for i in range(0,50):
    print("reflect SET_MINIMUM = 50:")
    print("R:",c3.get_result(c3.RESULT_REFLECT_R))
    print("G:",c3.get_result(c3.RESULT_REFLECT_G))
    print("B:",c3.get_result(c3.RESULT_REFLECT_B))
    time.sleep_ms(200)

c3.set_extremum(c3.MODE_REFLECT,c3.RESET)
for i in range(0,50):
    print("reflect SET_MINIMUM RESET:")
    print("R:",c3.get_result(c3.RESULT_REFLECT_R))
    print("G:",c3.get_result(c3.RESULT_REFLECT_G))
    print("B:",c3.get_result(c3.RESULT_REFLECT_B))
    time.sleep_ms(200)

c3.set_extremum(c3.MODE_REFLECT,c3.SET_MAXIMUM,30)
for i in range(0,50):
    print("reflect SET_MAXIMUM = 30:")
    print("R:",c3.get_result(c3.RESULT_REFLECT_R))
    print("G:",c3.get_result(c3.RESULT_REFLECT_G))
    print("B:",c3.get_result(c3.RESULT_REFLECT_B))
    time.sleep_ms(200)

c3.set_extremum(c3.MODE_REFLECT,c3.RESET)
for i in range(0,50):
    print("reflect SET_MINIMUM RESET:")
    print("R:",c3.get_result(c3.RESULT_REFLECT_R))
    print("G:",c3.get_result(c3.RESULT_REFLECT_G))
    print("B:",c3.get_result(c3.RESULT_REFLECT_B))
    time.sleep_ms(200)

results matching ""

    No results matching ""