如何让机器人借助操纵杆斜行?

2024-05-29 02:28:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要写代码来控制一个有操纵杆的两轮机器人的运动。我已经写了向前、向后(asix\u y)、转动(asix\u z)和随着一个引擎的运动转动(asix\u x)的条件。现在我们需要为它沿对角线的运动创造一个条件,这样当y轴和x轴或y轴被激活时,它会同时接受这两个值​​沿着对角线移动。非常感谢你。对不起我的英语

import socket
import pygame
import time

pygame.init()
pygame.display.set_mode((640, 480))

rc_socket = socket.socket()
try:
    rc_socket.connect(('192.168.1.102', 1234)) # connect to robot
except socket.error:
    print("could not connect to socket")

joystick_count = pygame.joystick.get_count()
for i in range(joystick_count):
    joystick = pygame.joystick.Joystick(i)
    joystick.init()

while True:
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                u1 = -1.0
                u2 = 1.0
                print("turn left: ({},{})".format(u1, u2))
            elif event.key == pygame.K_RIGHT:
                u1 = 1.0
                u2 = -1.0
                print("turn right: ({},{})".format(u1, u2))
            elif event.key == pygame.K_UP:
                u1 = -1.0
                u2 = -1.0
                print("forward: ({},{})".format(u1, u2))
            elif event.key == pygame.K_DOWN:
                u1 = 1.0
                u2 = 1.0
                print("forward: ({},{})".format(u1, u2))
            rc_socket.send('({},{})\n'.format(u1, u2).encode())
        `enter code here`elif event.type == pygame.KEYUP:
            u1 = 0
            u2 = 0
            print("key released, resetting: ({},{})".format(u1, u2))
            rc_socket.send('({},{})\n'.format(u1, u2).encode())


        if joystick:
            asix_x = joystick.get_axis(2)
            asix_y = joystick.get_axis(1)
            asix_z = joystick.get_axis(4)


        if asix_x < -0.12:
            u1 = -asix_x
            u2 = 0
            rc_socket.send('({},{})\n'.format(u1, u2).encode())
            time.sleep(0.1)
        elif asix_x and asix_y > 0.15:
            u1 = 0
            u2 = asix_x
            rc_socket.send('({},{})\n'.format(u1, u2).encode())
            pygame.time.delay(100)
            u1 = -asix_y
            u2 = -asix_y
            rc_socket.send('({},{})\n'.format(u1, u2).encode())
            time.sleep(0.1)

        elif asix_x > 0.12:
            u1 = 0
            u2 = asix_x
            rc_socket.send('({},{})\n'.format(u1, u2).encode())
            time.sleep(0.1)
        elif asix_y < -0.12:
            u1 = -asix_y
            u2 = -asix_y
            rc_socket.send('({},{})\n'.format(u1, u2).encode())
            time.sleep(0.1)

        elif asix_y > 0.12:
            u1 = -asix_y
            u2 = -asix_y
            rc_socket.send('({},{})\n'.format(u1, u2).encode())
            time.sleep(0.1)
        elif asix_z > 0.12:
            u1 = asix_z
            u2 = -asix_z
            rc_socket.send('({},{})\n'.format(u1, u2).encode())
            time.sleep(0.1)
        elif asix_z < -0.12:
            u1 = asix_z
            u2 = -asix_z
            rc_socket.send('({},{})\n'.format(u1, u2).encode())
            time.sleep(0.1)
        else:
            u1 = 0.0
            u2 = 0.0
            rc_socket.send('({},{})\n'.format(u1, u2).encode())
            time.sleep(0.1)

Tags: eventsendformattimesleepsocketpygameencode

热门问题