创意任务二的任务是做一个章鱼哥,章鱼哥的触角根据环境声音的大小舒展或者收缩。
为了让触角的舒张和收缩幅度相同,我没有用360度舵机,改用ULN2003驱动的28BYJ48步进电机。
为了能操作省事,先写了一个文件Stepper28BYJ48_ULN2003.py,里面定义了操作步进电机的类。
初始化的时候,把控制的引脚传进去,之后每次走一步,都只要传方向和速度。
import board,digitalio
import time
'''
EXAMPLE:
import Stepper28BYJ48_ULN2003
stepper = Stepper28BYJ48_ULN2003( board.A3 , board.A2 , board.A1 , board.A0 )
for i in range(4076):
stepper.steper_one_step(1,100000)
'''
class Stepper28BYJ48_ULN2003:
_DELAY_T = 0.15
_POS_ARRY =(
(False , True ,True ,True ),# A
(False , False ,True ,True ),# AB
(True , False ,True ,True ),# B
(True , False ,False ,True ),# BC
(True , True ,False ,True ),# C
(True , True ,False ,False),# CD
(True , True ,True ,False),# D
(False , True ,True ,False),# DA
)
# init
def __init__(self,pin_a,pin_b,pin_c,pin_d):
#self.speed = 1000
#self.direction = 1
self.pos = -1
self.pins = [ digitalio.DigitalInOut(p) for p in (pin_a,pin_b,pin_c,pin_d) ]
for p in self.pins:
p.switch_to_output()
# step
def steper_one_step(self,d=1,speed=1000):
self.pos += d #self.direction
self.pos %= len(self._POS_ARRY)
print(self.pos)
#ctr_a.value , ctr_b.value , ctr_c.value , ctr_d.value = self._POS_ARRY[pos]
for i in range(len(self.pins)):
self.pins[i].value = self._POS_ARRY[self.pos][i]
#time.sleep(self._DELAY_T/self.speed)
time.sleep(self._DELAY_T/abs(speed))
检测声音大小,可以用audiobusio库,喂给它一个缓冲区,就可以返回一系列模拟值。
精简了一下官网代码,就可以实现检测声音大小:
import audiobusio,board,array,math,time
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16)
samples = array.array("H", [0] * 320)
while True:
mic.record(samples, len(samples))
minbuf = int( sum(samples) / len(samples) )
samples_sum = sum([ float(sample - minbuf) * (sample - minbuf) for sample in samples] )
print( sum(samples) , minbuf , math.sqrt(samples_sum / len(samples)) )
time.sleep(0.5)
章鱼触手收回去之后,按一下开关,就会又伸展开来,因为用的是步进电机,所以只要没有失步,反向相同的步数后,会回到原位。
最后完整的代码是这样的:
import board,time
from Stepper28BYJ48_ULN2003 import Stepper28BYJ48_ULN2003 as st
import audiobusio,board,array,math
import digitalio
from adafruit_circuitplayground import cp
N = 1/4
stepper = st( board.A3 , board.A2 , board.A1 , board.A6 )
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16)
samples = array.array("H", [0] * 320)
button = digitalio.DigitalInOut(board.A5)
button.switch_to_input(pull=digitalio.Pull.DOWN)
while True:
mic.record(samples, len(samples))
minbuf = int( sum(samples) / len(samples) )
samples_sum = sum([ float(sample - minbuf) * (sample - minbuf) for sample in samples] )
val = math.sqrt(samples_sum / len(samples))
print( sum(samples) , minbuf , val )
if val > 300 :
print('danger!')
cp.play_file('danger.wav')
for i in range(4076 * N):
if i%100==0:
print(i,end=' :-> ')
stepper.steper_one_step(-1,100000)
time.sleep(2)
while button.value :
time.sleep(0.05)
print('safe.')
cp.play_file('shoping.wav')
for i in range(4076 * N):
if i%100==0:
print(i,end=' :-> ')
stepper.steper_one_step( 1,100000)
time.sleep(2)
time.sleep(0.1)
章鱼的身体是用杯子改的,把杯壁翦成8条,折叠打孔。然后把步进电机固定在杯子底部,在轴上装一个瓶子盖,在瓶盖周围打上8个孔,用棉线穿过空之后,再穿过折叠的杯壁的孔,绕到触手末端,就完成了。
运行效果是这样的:
本帖最后由 nemon 于 2024-8-31 19:14 编辑
不错