【Raspberry Pi Sense HAT】介绍、环境搭建、工程测试
本文介绍了 Raspberry Pi Sensor HAT 扩展板的相关信息,包括外观、资源、参数特点、原理图等,并硬件连接至树莓派开发板,搭建开发环境和校准,结合 Demo 例程实现板载传感器和外设资源的工程测试。
介绍
Sense HAT 是一款扩展板,为树莓派提供一系列传感器功能。可以监测压力、湿度、温度、颜色、方向和运动。8×8 RGB LED 矩阵实现可视化传感器数据,五键摇杆可实现项目互动。


Sense HAT 最初是为在国际空间站上使用而开发的,是 树莓派基金会 与 欧洲航天局 合作开展的教育 Astro Pi 计划的一部分。它可以帮助任何需要位置、运动、方向或环境传感的项目。
包装


打开包装盒,包括
SENSE-HAT 扩展板
2x20p 排母
M2.5 尼龙螺柱 x 4
M2.5 尼龙螺丝 x 8


开发板
Top view


Bottom view


资源
Sense HAT配备了8x8 RGB LED矩阵、迷你摇杆以及以下传感器:
陀螺仪
加速度计
磁力计
温度
湿度
气压


详见:GitHub - python-sense-hat .
参数特点
压力/温度传感器:STMicro LPS25HB
绝对压力范围 260 至 1260 hPa
压力数据输出 24 位
温度测量范围 0 至 65°C(精度 ±2°C)
温度数据输出 16 位
湿度/温度传感器:STMicro HTS221
相对湿度范围 0 至 100%
温度测量范围 15 至 40°C(精度 ±0.5°C)
数据输出 16 位
IMU:STMicro LSM9DS1 加速度计/陀螺仪/磁力计
加速度测量范围 ±16g
磁力计测量范围 ±16 Gs
陀螺仪测量范围 ±2000 dps
每个测量通道分辨率 16 位
颜色传感器:TCS3400 RGB 颜色与亮度传感器
LED 矩阵:8×8 RGB LED 显示屏
操纵杆:5 按钮迷你操纵杆,包含上、下、左、右及中键点击
原理图


详见:Raspberry Pi Sense HAT - Product Information Portal .
硬件连接
将排母接入 SENSE-HAT 扩展板;
使用尼龙螺柱和螺丝将扩展板固定在树莓派主板;


上电
树莓派上电后,Sense-HAT 板载 LED 矩阵呈现彩虹色。


环境搭建
安装
更新 Raspberry Pi 软件包,安装 sense-hat 软件包,重启系统
sudo apt update sudo apt install sense-hat sudo reboot
校准
安装必要的软件并运行校准程序,终端执行
sudo apt install octave -y cd cp /usr/share/librtimulib-utils/RTEllipsoidFit ./ -a cd RTEllipsoidFit RTIMULibCal
校准程序将显示以下菜单:


键盘输入 m ,按任意键开始校准


以俯仰、滚动等动作移动扩展板,当数字不再变化时停止;
按 s 键保存配置,按 x 退出程序;
将生成的 RTIMULib.ini 配置文件复制到 /etc/ 文件夹下,删除本地副本;
rm ~/.config/sense_hat/RTIMULib.ini sudo cp RTIMULib.ini /etc
工程测试
加载 /usr/src/sense-hat/examples 文件夹下的示例代码。详见:Examples - Sense HAT .
进入 /usr/src/sense-hat/examples/python-sense-hat/ 文件夹路径,依次执行测试程序;


彩虹
代码
#!/usr/bin/python import time from sense_hat import SenseHat sense = SenseHat() pixels = [ [255, 0, 0], [255, 0, 0], [255, 87, 0], [255, 196, 0], [205, 255, 0], [95, 255, 0], [0, 255, 13], [0, 255, 122], [255, 0, 0], [255, 96, 0], [255, 205, 0], [196, 255, 0], [87, 255, 0], [0, 255, 22], [0, 255, 131], [0, 255, 240], [255, 105, 0], [255, 214, 0], [187, 255, 0], [78, 255, 0], [0, 255, 30], [0, 255, 140], [0, 255, 248], [0, 152, 255], [255, 223, 0], [178, 255, 0], [70, 255, 0], [0, 255, 40], [0, 255, 148], [0, 253, 255], [0, 144, 255], [0, 34, 255], [170, 255, 0], [61, 255, 0], [0, 255, 48], [0, 255, 157], [0, 243, 255], [0, 134, 255], [0, 26, 255], [83, 0, 255], [52, 255, 0], [0, 255, 57], [0, 255, 166], [0, 235, 255], [0, 126, 255], [0, 17, 255], [92, 0, 255], [201, 0, 255], [0, 255, 66], [0, 255, 174], [0, 226, 255], [0, 117, 255], [0, 8, 255], [100, 0, 255], [210, 0, 255], [255, 0, 192], [0, 255, 183], [0, 217, 255], [0, 109, 255], [0, 0, 255], [110, 0, 255], [218, 0, 255], [255, 0, 183], [255, 0, 74] ] msleep = lambda x: time.sleep(x / 1000.0) def next_colour(pix): r = pix[0] g = pix[1] b = pix[2] if (r == 255 and g < 255 and b == 0): g += 1 if (g == 255 and r > 0 and b == 0): r -= 1 if (g == 255 and b < 255 and r == 0): b += 1 if (b == 255 and g > 0 and r == 0): g -= 1 if (b == 255 and r < 255 and g == 0): r += 1 if (r == 255 and b > 0 and g == 0): b -= 1 pix[0] = r pix[1] = g pix[2] = b while True: for pix in pixels: next_colour(pix) sense.set_pixels(pixels) msleep(2)
终端执行 python rainbow.py 指令,效果如下


旋转
代码
#!/usr/bin/python import sys import time from sense_hat import SenseHat X = (255, 0, 0) O = (255, 255, 255) question_mark = [ O, O, O, X, X, O, O, O, O, O, X, O, O, X, O, O, O, O, O, O, O, X, O, O, O, O, O, O, X, O, O, O, O, O, O, X, O, O, O, O, O, O, O, X, O, O, O, O, O, O, O, O, O, O, O, O, O, O, O, X, O, O, O, O ] sense = SenseHat() sense.set_pixels(question_mark) sense.set_pixel(0, 0, 255, 0, 0) sense.set_pixel(0, 7, 0, 255, 0) sense.set_pixel(7, 0, 0, 0, 255) sense.set_pixel(7, 7, 255, 0, 255) while True: for r in [0, 90, 180, 270]: sense.set_rotation(r) time.sleep(0.3)
终端执行 python rotation.py 指令,效果如下


罗盘
代码
#!/usr/bin/python import sys from sense_hat import SenseHat # To get good results with the magnetometer you must first calibrate it using # the program in RTIMULib/Linux/RTIMULibCal # The calibration program will produce the file RTIMULib.ini # Copy it into the same folder as your Python code led_loop = [4, 5, 6, 7, 15, 23, 31, 39, 47, 55, 63, 62, 61, 60, 59, 58, 57, 56, 48, 40, 32, 24, 16, 8, 0, 1, 2, 3] sense = SenseHat() sense.set_rotation(0) sense.clear() prev_x = 0 prev_y = 0 led_degree_ratio = len(led_loop) / 360.0 while True: dir = sense.get_compass() dir_inverted = 360 - dir # So LED appears to follow North led_index = int(led_degree_ratio * dir_inverted) offset = led_loop[led_index] y = offset // 8 # row x = offset % 8 # column if x != prev_x or y != prev_y: sense.set_pixel(prev_x, prev_y, 0, 0, 0) sense.set_pixel(x, y, 0, 0, 255) prev_x = x prev_y = y
终端执行 python compass.py 指令,效果如下


渐变色
代码
#!/usr/bin/python import time from sense_hat import SenseHat sense = SenseHat() r = 255 g = 0 b = 0 msleep = lambda x: time.sleep(x / 1000.0) def next_colour(): global r global g global b if (r == 255 and g < 255 and b == 0): g += 1 if (g == 255 and r > 0 and b == 0): r -= 1 if (g == 255 and b < 255 and r == 0): b += 1 if (b == 255 and g > 0 and r == 0): g -= 1 if (b == 255 and r < 255 and g == 0): r += 1 if (r == 255 and b > 0 and g == 0): b -= 1 while True: sense.clear([r, g, b]) msleep(2) next_colour()
终端执行 python colur_cycle.py 指令,效果如下


图片显示
代码
#!/usr/bin/python
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
sense.load_image("space_invader.png")将 space_invader.png 放置在代码同一目录下


终端执行 python space_invader.py 指令,效果如下


文字滚动
代码
#!/usr/bin/python
from sense_hat import SenseHat
sense = SenseHat()
sense.set_rotation(180)
red = (255, 0, 0)
sense.show_message("One small step for Pi!", text_colour=red)终端执行 python text_scroll.py 指令,效果如下


PyGame
代码
#!/usr/bin/python
from sense_hat import SenseHat
import os
import time
import pygame # See http://www.pygame.org/docs
from pygame.locals import *
print("Press Escape to quit")
time.sleep(1)
pygame.init()
pygame.display.set_mode((640, 480))
sense = SenseHat()
sense.clear() # Blank the LED matrix
# 0, 0 = Top left
# 7, 7 = Bottom right
UP_PIXELS = [[3, 0], [4, 0]]
DOWN_PIXELS = [[3, 7], [4, 7]]
LEFT_PIXELS = [[0, 3], [0, 4]]
RIGHT_PIXELS = [[7, 3], [7, 4]]
CENTRE_PIXELS = [[3, 3], [4, 3], [3, 4], [4, 4]]
def set_pixels(pixels, col):
for p in pixels:
sense.set_pixel(p[0], p[1], col[0], col[1], col[2])
def handle_event(event, colour):
if event.key == pygame.K_DOWN:
set_pixels(DOWN_PIXELS, colour)
elif event.key == pygame.K_UP:
set_pixels(UP_PIXELS, colour)
elif event.key == pygame.K_LEFT:
set_pixels(LEFT_PIXELS, colour)
elif event.key == pygame.K_RIGHT:
set_pixels(RIGHT_PIXELS, colour)
elif event.key == pygame.K_RETURN:
set_pixels(CENTRE_PIXELS, colour)
running = True
BLACK = [0, 0, 0]
WHITE = [255, 255, 255]
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
handle_event(event, WHITE)
if event.type == KEYUP:
handle_event(event, BLACK)终端执行 python pygame_joystick.py 指令,效果如下


详见:Pygame Front Page .
传感器
终端执行 touch sensor_print.py 新建程序文件,添加如下代码
from sense_hat import SenseHat
sense = SenseHat()
sense.clear()
# 惯性测量单元
def orientation():
orientation = sense.get_orientation()
pitch = round(orientation["pitch"], 1)
roll = round(orientation["roll"], 1)
yaw = round(orientation["yaw"], 1)
print(f"Pitch: {pitch}, Roll: {roll}, Yaw: {yaw}")
#sense.show_message("Pitch {0}, Roll {1}, Yaw {2}".format(pitch, roll, yaw))
# 温度
def temperature():
temp = sense.get_temperature()
temp = round(temp, 1)
print(f"Temperature: {temp} °C")
#sense.show_message("Temperature: %s degrees Celsius" % temp)
# 湿度
def humidity():
humidity = sense.get_humidity()
humidity = round(humidity, 1)
print(f"Humidity: {humidity} %RH")
#sense.show_message("Humidity: %s percent" % humidity)
# 气压传感器
def pressure():
pressure = sense.get_pressure()
pressure = round(pressure, 1)
print(f"Pressure: {pressure} mbar")
#sense.show_message("Pressure: %s millibars" % pressure)
# 来自磁力计的罗盘读数
def compass():
for i in range(0, 10):
north = sense.get_compass()
north = round(north, 1)
print(f"North: {north} °")
#sense.show_message("North: %s degrees" % north)
# 摇杆控制显示
sense.stick.direction_up = orientation
sense.stick.direction_right = temperature
sense.stick.direction_down = compass
sense.stick.direction_left = humidity
sense.stick.direction_middle = pressure
print("↑姿态 →温度 ↓罗盘 ←湿度 按下:气压")
while True:
pass保存代码。
终端执行 python sensor_print.py 指令;
控制摇杆,打印传感器数据


总结
本文介绍了 Raspberry Pi Sensor HAT 扩展板的相关信息,包括外观、资源、参数特点、原理图等,并硬件连接至树莓派开发板,搭建开发环境和校准,结合 Demo 例程实现板载传感器和外设资源的工程测试,为相关产品的快速开发和应用设计提供了参考。
我要赚赏金
