| ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ |
|---|
Vernier 适配器
- class VernierAdapter(port, conversion=None)
LEGO® MINDSTORMS® Education NXT/EV3 Vernier 传感器适配器。
- 参数:
port (Port) – 传感器所连接的端口。
- conversion(callable)– 格式为 conversion() 的函数。此函数用于将原始模拟电压转换为传感器特定的输出值。每个 Vernier 传感器都有自己的转换函数。下面给出的示例演示了表面温度传感器的转换。
- voltage() int: mV
测量模拟传感器的原始电压。
- 返回:
模拟电压。
示例
使用表面温度传感器
from math import log
from pybricks.nxtdevices import VernierAdapter
from pybricks.parameters import Port
# 表面温度传感器的换算公式
def convert_raw_to_temperature(voltage):
# 按照 Vernier Adapter EV3 积木块的要求,
# 将原始电压转换为 NTC 电阻值。
counts = voltage / 5000 * 4096
ntc = 15000 * (counts) / (4130 - counts)
# 安全处理 log(0):确保 ntc 值为正。
if ntc <= 0:
ntc = 1
# 按照传感器文档中给出的 Steinhart-Hart 方程进行计算。
K0 = 1.02119e-3
K1 = 2.22468e-4
K2 = 1.33342e-7
return 1 / (K0 + K1 * log(ntc) + K2 * log(ntc) ** 3)
# 在端口 1 初始化适配器
thermometer = VernierAdapter(Port.S1, convert_raw_to_temperature)
# 获取测量值并打印它
temp = thermometer.value()
print(temp)