海凌科HLK-LD116S-24G毫米波雷达传感虽然功能较为单一,但应用在合适的场合,还是非常有用途的。
官方有一个案例,就是灯感应用:
我手头上,正好有一块 带有树莓派pico的开发板,上面有12个WS2812B灯珠,结合HLK-LD116S-24G毫米波雷达传感,正好能做一个灯感应用。
把传感器撞到开发板上,最后加上一个底座,具体如下:
然后,对这个灯感应用,做一点点简单的设计:
1. 如果检测到有人移动,则从白光80,80,80开始点亮;如果持续到检测有人移动,则每秒+30,直到240,240,240为止
2. 如果持续没有检测到移动,则过5秒后,每秒-10,过10秒后,每秒-20,直到0为止,也就是熄灭。这样就实现了延迟熄灭。
上述各亮度,或者时间,都是可以调整的,测试起见,去了上面的值。
因为使用的是树莓派Pico,那么就自然使用了micropython来进行编程。
具体的程序如下:
首先是ws2812b的驱动程序:ws2812b.py
import array, time
from machine import Pin
import rp2
@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=24)
def ws2812():
T1 = 2
T2 = 5
T3 = 3
wrap_target()
label("bitloop")
out(x, 1) .side(0) [T3 - 1]
jmp(not_x, "do_zero") .side(1) [T1 - 1]
jmp("bitloop") .side(1) [T2 - 1]
label("do_zero")
nop() .side(0) [T2 - 1]
wrap()
#delay here is the reset time. You need a pause to reset the LED strip back to the initial LED
#however, if you have quite a bit of processing to do before the next time you update the strip
#you could put in delay=0 (or a lower delay)
class ws2812b:
def __init__(self, num_leds, state_machine, pin, delay=0.001):
self.pixels = array.array("I", [0 for _ in range(num_leds)])
self.sm = rp2.StateMachine(state_machine, ws2812, freq=8000000, sideset_base=Pin(pin))
self.sm.active(1)
self.num_leds = num_leds
self.delay = delay
self.brightnessvalue = 255
# Set the overal value to adjust brightness when updating leds
def brightness(self, brightness = None):
if brightness == None:
return self.brightnessvalue
else:
if (brightness < 1):
brightness = 1
if (brightness > 255):
brightness = 255
self.brightnessvalue = brightness
# Create a gradient with two RGB colors between "pixel1" and "pixel2" (inclusive)
def set_pixel_line_gradient(self, pixel1, pixel2, left_red, left_green, left_blue, right_red, right_green, right_blue):
if pixel2 - pixel1 == 0: return
right_pixel = max(pixel1, pixel2)
left_pixel = min(pixel1, pixel2)
for i in range(right_pixel - left_pixel + 1):
fraction = i / (right_pixel - left_pixel)
red = round((right_red - left_red) * fraction + left_red)
green = round((right_green - left_green) * fraction + left_green)
blue = round((right_blue - left_blue) * fraction + left_blue)
self.set_pixel(left_pixel + i, red, green, blue)
# Set an array of pixels starting from "pixel1" to "pixel2" to the desired color.
def set_pixel_line(self, pixel1, pixel2, red, green, blue):
for i in range(pixel1, pixel2+1):
self.set_pixel(i, red, green, blue)
def set_pixel(self, pixel_num, red, green, blue):
# Adjust color values with brightnesslevel
blue = round(blue * (self.brightness() / 255))
red = round(red * (self.brightness() / 255))
green = round(green * (self.brightness() / 255))
self.pixels[pixel_num] = blue | red << 8 | green << 16
# rotate x pixels to the left
def rotate_left(self, num_of_pixels):
if num_of_pixels == None:
num_of_pixels = 1
self.pixels = self.pixels[num_of_pixels:] + self.pixels[:num_of_pixels]
# rotate x pixels to the right
def rotate_right(self, num_of_pixels):
if num_of_pixels == None:
num_of_pixels = 1
num_of_pixels = -1 * num_of_pixels
self.pixels = self.pixels[num_of_pixels:] + self.pixels[:num_of_pixels]
def show(self):
for i in range(self.num_leds):
self.sm.put(self.pixels[i],8)
time.sleep(self.delay)
def fill(self, red, green, blue):
for i in range(self.num_leds):
self.set_pixel(i, red, green, blue)
time.sleep(self.delay)
然后是主程序:
import time
from machine import Pin
from ws2812b import ws2812b
num_leds = 12
pixels = ws2812b(num_leds, 0,18, delay=0)
radar = Pin(16, Pin.IN)
pixels.brightness(255)
pixels.fill(0,0,0)
pixels.show()
color_min=80
color_max=240
color = 0
leave_time = 0
while True:
now_time = time.time()
val = radar.value()
if val == 0:
if now_time - leave_time > 10:
color -= 20
elif now_time - leave_time > 5:
color -= 10
if color < 0:
color = 0
else:
if color < color_min:
color = color_min
elif color < color_max:
color += 30
if color > color_max:
color = color_max
leave_time = time.time()
print("val=%d color=%d time=%d leave_time=%d diff_time=%d" % (val, color, now_time, leave_time, now_time - leave_time))
# pixels.brightness(brightness)
pixels.fill(color,color,color)
pixels.show()
time.sleep(1)
上述代码的逻辑,比较简单,根据之前的设计进行实际编写的。
执行以上程序后,从串口可以收到如下的数据:
实际效果如下:
3895_1673971842