模拟控制复盆子pi使用pyPS4控制器



我有一个带机器人套件的树莓派,我想使用PS4控制器但使用模拟输入来控制它。我已经成功地连接了控制器,我可以读取事件并对电机进行编程以回答二进制输入。然而,文档(pyPS4Controller(并没有明确说明使用模拟值(例如:50%按下R2输出0.5正向(。

有人能帮我想办法做这个吗
提前感谢!

# This is the example for binary buttons
from pyPS4Controller.controller import Controller
class MyController(Controller):
def __init__(self, **kwargs):
Controller.__init__(self, **kwargs)
def on_x_press(self):
print("Forward")
def on_x_release(self):
print("Stop")
# --> The objective is having some way to capture the "intensity" of the button press to use the motors accordingly
controller = MyController(interface="/dev/input/js0", connecting_using_ds4drv=False)
controller.listen()

经过一些调整和反复试验,我最终找到了解决方案。模拟函数接收称为value的输入,该输入包含给定给控制器的输入的模拟值。

from pyPS4Controller.controller import Controller
from gpiozero import CamJamKitRobot as camjam
# Translate controller input into motor output values
def transf(raw):
temp = (raw+32767)/65534
# Filter values that are too weak for the motors to move
if abs(temp) < 0.25:
return 0
# Return a value between 0.3 and 1.0
else:
return round(temp, 1)
class MyController(Controller):
def __init__(self, **kwargs):
Controller.__init__(self, **kwargs)
self.robot = camjam()

def on_R2_press(self, value):
# 'value' becomes 0 or a float between 0.3 and 1.0
value = transf(value)
self.robot.value = (value, value)
print(f"R2 {value}")

def on_R2_release(self):
self.robot.value = (0,0)
print(f"R2 FREE")

def on_L2_press(self, value):
# 'value' becomes 0 or a float between -1.0 and -0.3
value = -transf(value)
self.robot.value = (value, value)
print(f"L2 {value}")

def on_L2_release(self):
self.robot.value = (0,0)
print(f"L2 FREE")

# Press OPTIONS (=START) to stop and exit
def on_options_press(self):
print("nExiting (START)")
self.robot.value = (0,0)
exit(1)

controller = MyController(interface="/dev/input/js0", connecting_using_ds4drv=False)
controller.listen()

相关内容

  • 没有找到相关文章

最新更新