无法让打印语句工作 PS4 模拟棒

Posted

技术标签:

【中文标题】无法让打印语句工作 PS4 模拟棒【英文标题】:Can't get print statements to work PS4 analog stick 【发布时间】:2019-11-27 22:34:13 【问题描述】:

一切正常,但我无法让程序打印('完全向后')和打印(“完全正确”)。我很确定我的值在 if 语句中是正确的。

我有另一个程序可以显示模拟摇杆所在位置的值并且我的值是正确的。我试过切换大于/小于语句,但什么也没做,我仔细检查了这些值。

        joystick = pygame.joystick.Joystick(i)###########
        joystick.init()

        for i in range( 0, 2 ):
            axis = joystick.get_axis( i )
            #print('Axis  value: :>6.3f'.format(i, axis))
            axis0 = joystick.get_axis(0)
            axis1 = joystick.get_axis(1)
            #backward totally
            if axis1 == 1.000:
                print("backward totally")
            #Nothing GOOD
            if -.100 < axis0 < .100 and -.100 < axis1 < .100:
                print('centered')
            #forward totally GOOD
            if axis1 == -1.000:
                print('forward totally')

            #left totally GOOD
            if axis0 == -1.000 and -.599 < axis1 < 0.200:
                print("left totally")
            #right totallly 
            if axis0 == 1.000 and -.599 < axis1 < 0.200:
                print('Right totally')

它不会给出错误它只是不打印任何东西,我不知道为什么,我希望它完全正确或完全向后打印。

【问题讨论】:

如果只删除 if 语句并打印出轴 0 和轴 1 的值会发生什么?当你向右猛击操纵杆时,它真的会变成 1 吗? 是的,它显示 1,但是当我将它放入 if 语句时,它似乎不起作用。 【参考方案1】:

我修改了代码中的条件语句,添加了“死区”和“边缘区”的概念。您可以将“死区”视为一个内半径,在该内半径内,运动被记录为“居中”,而在较大外半径之外(即在边缘区域)的运动是在一个承诺方向上的运动。这是该代码的样子;考虑到我的 xbox 360 控制器的布局,每个摇杆的复制肯定有改进的余地:

import pygame

pygame.init()

"""
Just a check to ensure a joystick is plugged in.
Only going to retrieve data for the 0th joystick anyways...
"""
num_joysticks = pygame.joystick.get_count()
if num_joysticks == 0:
    raise ValueError("No joysticks attached!")

joystick = pygame.joystick.Joystick(0)
joystick.init()

#Just some info about the controller
j_id = joystick.get_id()
j_name = joystick.get_name()
num_axes = joystick.get_numaxes()
num_balls = joystick.get_numballs()
num_buttons = joystick.get_numbuttons()
num_hats = joystick.get_numhats()
print(j_id, j_name)
print(num_axes, num_balls, num_buttons, num_hats)

#Define the inner and outer radii that are considered 0 and 100% movement
dead_zone = 0.2#inner radius
edge_zone = 0.9#outer radius

while True:
    for event in pygame.event.get():
        pass

    """
    Only have an xbox 360 controller to work with
    There, axes 0 and 1 correspond the left stick x and y
    And axes 3 and 4 correspond to the right stick x and y
    Not sure what axes 2 and 5 are listed for...
    For me, the ranges only go from [-1.0, 1.0), so 1.0 is not included for right and bottom.
    """
    left_x = joystick.get_axis(0)
    left_y = joystick.get_axis(1)
    right_x = joystick.get_axis(3)
    right_y = joystick.get_axis(4)

    if (-dead_zone < left_x < dead_zone) and (-dead_zone < left_y < dead_zone):
        print("Left stick centered")
    elif left_x < -edge_zone:
        print("Left stick is totally left")
    elif left_x > edge_zone:
        print("Left stick is totally right")
    elif left_y < -edge_zone:
        print("Left stick is totally up")
    elif left_y > edge_zone:
        print("Left stick is totally down")
    else:
        print("Left stick is somewhere")

    if (-dead_zone < right_x < dead_zone) and (-dead_zone < right_y < dead_zone):
        print("Right stick centered")
    elif right_x < -edge_zone:
        print("Right stick is totally left")
    elif right_x > edge_zone:
        print("Right stick is totally right")
    elif right_y < -edge_zone:
        print("Right stick is totally up")
    elif right_y > edge_zone:
        print("Right stick is totally down")
    else:
        print("Right stick is somewhere")

【讨论】:

有效!!!!!!非常感谢,我真的很感激,我显然从来没有想过死区的概念 :-) 但这太棒了,稍后当我完成设计我的机器人时会使用它,我将在其中使用这个程序。

以上是关于无法让打印语句工作 PS4 模拟棒的主要内容,如果未能解决你的问题,请参考以下文章

让 printf 语句在 openCL 内核代码中工作

在 OS X 上模拟 DS4 控制器以实现 PS4 远程播放

如何在每次循环迭代时向用户打印不同的语句?

如何在 python 3 的打印语句中上标?

从控制台 C++ 获取打印的语句

打印错误类型、错误语句和自己的语句