BPI-PicoW-S3 使用双轴摇杆[CircuitPython]
本帖最后由 _f2b6a5 于 2022-11-21 11:07 编辑https://forum.banana-pi.org.cn/uploads/default/optimized/2X/b/bd27a897a352fc58cb42271a597ab207b8b2e012_2_690x174.jpegBPI-Pico-S3 与 Raspberry Pi Pico 板尺寸相同,搭载ESP32S3芯片,8M flash,4层PCB,电镀半孔工艺,陶瓷天线,支持 2.4 GHz Wi-Fi 和 Bluetooth® LE 双模无线通信,是一款专为物联网开发和Maker DIY设计的开发板。出厂内置 tinyUF2 + CircuitPython,推荐使用Mu编辑器上手CircuitPython开发。BiliBili视频 https://www.bilibili.com/video/BV1zt4y1N7Je硬件接口示意图https://forum.banana-pi.org.cn/uploads/default/original/2X/8/8c67557dd471abc554140825684fc4710196ee87.jpeg使用双轴摇杆https://forum.banana-pi.org.cn/uploads/default/optimized/2X/8/84016732154fc6600c571380bd5055a7a5fd2407_2_690x354.jpeg这是一个常见的双轴XY摇杆模块,使用了两个电位器最为其核心器件,通过芯片的ADC 模/数转换器 读取它们各自的电压数值,即可将读数转化为其在XY坐标轴上的位置。
双轴摇杆BPI-PicoW-S3
GNDGND
+5V3V3
VRxGP26_A0
VRYGP27_A1
[*]在CircuitPython中提供的ADC精度是16bit,即最大值的16进制表达为 FFFF,10进制表达为 65535,对应的电压量程为0mv ~ 3300mv。BPI-PicoW-S3所使用的EPS32S3芯片实际ADC电压量程为0mv ~ 3100mv,所以实际应用时仅能测量到3100mv。
[*]基础ADC读数,读取两个电位器的数值,转换为电压数值。
import board,analogio,time
x_axis_pin = analogio.AnalogIn(board.A0)
y_axis_pin = analogio.AnalogIn(board.A1)
while True:
x_axis = x_axis_pin.value
y_axis = y_axis_pin.value
# print((x_axis,y_axis))
x_value = x_axis / 65535 * 3300
y_value = y_axis / 65535 * 3300
print("{0}mv,{1}mv".format(x_value,y_value))
time.sleep(0.1)
[*]在Mu编辑器中,点击Plotter图标即可显示绘图仪,可以实时将REPL输出的数值显示为与时间相关的折线图。
https://forum.banana-pi.org.cn/uploads/default/original/2X/8/878e7b76d3a2628434d0a8c6d047ca86d859595c.png
[*]以下程序可实现校准坐标零点的功能。开始运行的前五秒,请静止摇杆等待获取零点数值。
import board,analogio,time
x_axis_pin = analogio.AnalogIn(board.A0)
y_axis_pin = analogio.AnalogIn(board.A1)
def get_zero(times =500, sleep = 0.01):
x_total = 0
y_total = 0
for i in range (times):
x_axis = x_axis_pin.value
y_axis = y_axis_pin.value
x_total += x_axis
y_total += y_axis
time.sleep(sleep)
x_zero = x_total // times
y_zero = y_total // times
return (x_zero,y_zero)
zero = get_zero(times =500, sleep = 0.01)
print(zero)
while True:
x_axis = x_axis_pin.value - zero
y_axis = y_axis_pin.value - zero
print((x_axis,y_axis))
time.sleep(0.1)
[*]以下程序可获取摇杆方向,这是双轴摇杆最常见的应用。
import board,analogio,time
x_axis_pin = analogio.AnalogIn(board.A0)
y_axis_pin = analogio.AnalogIn(board.A1)
direction_list = ["East","Southeast","South","Southwest","West","Northwest","North","Northeast","Centre"]
def get_zero(times =500, sleep = 0.01):
x_total = 0
y_total = 0
for i in range (times):
x_axis = x_axis_pin.value
y_axis = y_axis_pin.value
x_total += x_axis
y_total += y_axis
time.sleep(sleep)
x_zero = x_total // times
y_zero = y_total // times
return (x_zero,y_zero)
def get_direction(zero = (32767,32767)):
x_axis = x_axis_pin.value - zero
y_axis = y_axis_pin.value - zero
if x_axis >= 10000 and -10000 < y_axis < 10000:
return direction_list
elif x_axis >= 10000 and y_axis <= -10000:
return direction_list
elif -10000 < x_axis < 10000 and y_axis <= -10000:
return direction_list
elif x_axis <= -10000 and y_axis <= -10000:
return direction_list
elif x_axis <= -10000 and -10000 < y_axis < 10000:
return direction_list
elif x_axis <= -10000 and y_axis >= 10000:
return direction_list
elif -10000 < x_axis < 10000 and y_axis >= 10000:
return direction_list
elif x_axis >=10000 and y_axis >= 10000:
return direction_list
else :
return direction_list
zero = get_zero(times =50, sleep = 0.01)
print(zero)
while True:
x_axis = x_axis_pin.value - zero
y_axis = y_axis_pin.value - zero
print((x_axis,y_axis))
print(get_direction(zero = zero))
time.sleep(0.1)
[*]以下程序可设置坐标精度等级,计算每级跨度,可以按需求消除抖动,增强数据的实用性。
import board,analogio,time
x_axis_pin = analogio.AnalogIn(board.A0)
y_axis_pin = analogio.AnalogIn(board.A1)
def get_zero(times =500, sleep = 0.01):
x_total = 0
y_total = 0
for i in range (times):
x_axis = x_axis_pin.value
y_axis = y_axis_pin.value
x_total += x_axis
y_total += y_axis
time.sleep(sleep)
x_zero = x_total // times
y_zero = y_total // times
return (x_zero,y_zero)
def get_extremum(times =500, sleep = 0.01):
x_list = []
y_list = []
for i in range (times):
x_axis = x_axis_pin.value
y_axis = y_axis_pin.value
x_list.append(x_axis)
y_list.append(y_axis)
time.sleep(sleep)
x_extremum = (min(x_list),max(x_list))
y_extremum = (min(y_list),max(y_list))
return (x_extremum,y_extremum)
def get_spacing(level = 16 , zero =(32767,32767) ,x_extremum = (0,65535),y_extremum = (0,65535)):
x_temp_1 = (zero - x_extremum) // level
x_temp_2 = (x_extremum - zero ) // level
y_temp_1 = (zero - y_extremum) // level
y_temp_2 = (y_extremum - zero ) // level
x_spacing = (x_temp_1,x_temp_2)
y_spacing = (y_temp_1,y_temp_2)
return (x_spacing,y_spacing)
def get_coordinates(zero = (32767,32767), x_spacing = (2048,2048),y_spacing = (2048,2048)):
x_value = x_axis_pin.value - zero
y_value = y_axis_pin.value - zero
if x_value >= 0:
x_axis = x_value // x_spacing
else:
x_axis = - ((-x_value) // x_spacing)
if y_value >= 0:
y_axis = y_value // y_spacing
else:
y_axis = - ((-y_value) // y_spacing)
return (x_axis,y_axis)
zero = get_zero(times =500, sleep = 0.01)
print(zero)
(x_extremum,y_extremum) = get_extremum(times = 500, sleep = 0.01)
print((x_extremum, y_extremum))
(x_spacing,y_spacing) = get_spacing(level = 128 , zero = zero, x_extremum = x_extremum,y_extremum = y_extremum)
print((x_spacing, y_spacing))
while True:
coordinates = get_coordinates(zero = zero, x_spacing = x_spacing, y_spacing = y_spacing)
print(coordinates)
time.sleep(0.1)BPI-PicoW-S3 + CircuitPython 教程聚合链接BPI-PicoW-S3 wiki 页面购买BPI-PicoW-S3:
[*]比派科技官方速卖通店铺
[*]SinoVoip 官方速卖通店铺
[*]官方**店铺
[*]OEM&OEM 定制服务: sales@banana-pi.com
页:
[1]