5338| 0
|
BPI-PicoW-S3 使用双轴摇杆[CircuitPython] |
本帖最后由 RookieC 于 2022-11-21 11:10 编辑 BPI-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 硬件接口示意图使用双轴摇杆这是一个常见的双轴XY摇杆模块,使用了两个电位器最为其核心器件,通过芯片的ADC 模/数转换器 读取它们各自的电压数值,即可将读数转化为其在XY坐标轴上的位置。 [td]
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)
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[0] y_axis = y_axis_pin.value - zero[1] print((x_axis,y_axis)) time.sleep(0.1)
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[0] y_axis = y_axis_pin.value - zero[1] if x_axis >= 10000 and -10000 < y_axis < 10000: return direction_list[0] elif x_axis >= 10000 and y_axis <= -10000: return direction_list[1] elif -10000 < x_axis < 10000 and y_axis <= -10000: return direction_list[2] elif x_axis <= -10000 and y_axis <= -10000: return direction_list[3] elif x_axis <= -10000 and -10000 < y_axis < 10000: return direction_list[4] elif x_axis <= -10000 and y_axis >= 10000: return direction_list[5] elif -10000 < x_axis < 10000 and y_axis >= 10000: return direction_list[6] elif x_axis >=10000 and y_axis >= 10000: return direction_list[7] else : return direction_list[8] zero = get_zero(times =50, sleep = 0.01) print(zero) while True: x_axis = x_axis_pin.value - zero[0] y_axis = y_axis_pin.value - zero[1] print((x_axis,y_axis)) print(get_direction(zero = zero)) time.sleep(0.1)
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[0] - x_extremum[0]) // level x_temp_2 = (x_extremum[1] - zero[0] ) // level y_temp_1 = (zero[1] - y_extremum[0]) // level y_temp_2 = (y_extremum[1] - zero[1] ) // 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[0] y_value = y_axis_pin.value - zero[1] if x_value >= 0: x_axis = x_value // x_spacing[1] else: x_axis = - ((-x_value) // x_spacing[0]) if y_value >= 0: y_axis = y_value // y_spacing[1] else: y_axis = - ((-y_value) // y_spacing[0]) 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 教程聚合链接:https://forum.banana-pi.org.cn/t/topic/3986?u=wind_ BPI-PicoW-S3 wiki 页面:https://wiki.banana-pi.org/BPI-PicoW-S3 购买BPI-PicoW-S3:
|
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed