AGV 控制Python

from adafruit_pca9685 import PCA9685

from board import SCL, SDA

import board

import busio

import time

import digitalio

from inputs import get_gamepad, devices

i2c_bus = busio.I2C(SCL, SDA)

Create a simple PCA9685 class instance.

pca = PCA9685(i2c_bus)

Set the PWM frequency to 60hz.

pca.frequency = 400

class Motor:

def init(self, pca, en_channel, in1_channel, in2_channel):

self.pca = pca

self.en = pca.channels[en_channel]

self.in1 = pca.channels[in1_channel]

self.in2 = pca.channels[in2_channel]

def drive(self, direction, speed):

Convert speed from 0-100 to 0-0xffff

speed = int(speed * 655.35)

self.en.duty_cycle = speed

if direction == 1:

self.in1.duty_cycle = 0xffff

self.in2.duty_cycle = 0

elif direction == 2:

self.in1.duty_cycle = 0

self.in2.duty_cycle = 0xffff

def stop(self):

self.en.duty_cycle = 0

self.in1.duty_cycle = 0

self.in2.duty_cycle = 0

motorFL = Motor(pca, 0, 1, 2)

motorFR = Motor(pca, 6, 8, 7)

motorBL = Motor(pca, 5, 4, 3)

motorBR = Motor(pca, 11, 9, 10)

def process_controller():

while True:

events = get_gamepad()

for event in events:

if event.code == 'ABS_HAT0Y': # Dpad Y-axis (forward/backward)

speed = event.state # Dpad values are -1, 0, or 1

motorFL.drive(1 if speed <= 0 else 2, abs(speed)*100)

motorFR.drive(1 if speed <= 0 else 2, abs(speed)*100)

motorBL.drive(1 if speed <= 0 else 2, abs(speed)*100)

motorBR.drive(1 if speed <= 0 else 2, abs(speed)*100)

elif event.code == 'ABS_HAT0X': # Dpad X-axis (left/right)

strafe_speed = event.state # Dpad values are -1, 0, or 1

if strafe_speed >= 0: # Strafe right

motorFL.drive(1, abs(strafe_speed)*100)

motorBL.drive(2, abs(strafe_speed)*100)

motorFR.drive(2, abs(strafe_speed)*100)

motorBR.drive(1, abs(strafe_speed)*100)

else: # Strafe left

motorFL.drive(2, abs(strafe_speed)*100)

motorBL.drive(1, abs(strafe_speed)*100)

motorFR.drive(1, abs(strafe_speed)*100)

motorBR.drive(2, abs(strafe_speed)*100)

elif event.code == 'ABS_RX': # Right joystick X-axis (turning)

turn_speed = event.state / 330 # Normalize to 0-100

if turn_speed >= 0: # Turn right

motorFL.drive(1, abs(turn_speed))

motorBL.drive(1, abs(turn_speed))

motorFR.drive(2, abs(turn_speed))

motorBR.drive(2, abs(turn_speed))

else: # Turn left

motorFL.drive(2, abs(turn_speed))

motorBL.drive(2, abs(turn_speed))

motorFR.drive(1, abs(turn_speed))

motorBR.drive(1, abs(turn_speed))

Start processing controller input

while len(devices.gamepads) == 0:

print("Waiting for controller...")

time.sleep(1)

process_controller()

相关推荐
大G哥2 分钟前
java提高正则处理效率
java·开发语言
ROBOT玲玉5 分钟前
Milvus 中,FieldSchema 的 dim 参数和索引参数中的 “nlist“ 的区别
python·机器学习·numpy
VBA633712 分钟前
VBA技术资料MF243:利用第三方软件复制PDF数据到EXCEL
开发语言
轩辰~14 分钟前
网络协议入门
linux·服务器·开发语言·网络·arm开发·c++·网络协议
小_太_阳23 分钟前
Scala_【1】概述
开发语言·后端·scala·intellij-idea
向宇it24 分钟前
【从零开始入门unity游戏开发之——unity篇02】unity6基础入门——软件下载安装、Unity Hub配置、安装unity编辑器、许可证管理
开发语言·unity·c#·编辑器·游戏引擎
Kai HVZ1 小时前
python爬虫----爬取视频实战
爬虫·python·音视频
古希腊掌管学习的神1 小时前
[LeetCode-Python版]相向双指针——611. 有效三角形的个数
开发语言·python·leetcode
赵钰老师1 小时前
【R语言遥感技术】“R+遥感”的水环境综合评价方法
开发语言·数据分析·r语言
m0_748244831 小时前
StarRocks 排查单副本表
大数据·数据库·python