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()

相关推荐
爱说实话1 分钟前
C# DependencyObject类、Visual类、UIElement类
开发语言·c#
智码未来学堂3 分钟前
C语言指针:打开通往内存世界的大门
c语言·开发语言
黎雁·泠崖5 分钟前
Java面向对象:对象数组核心+综合实战
java·开发语言
过期的秋刀鱼!10 分钟前
机器学习-带正则化的成本函数-
人工智能·python·深度学习·算法·机器学习·逻辑回归
郝学胜-神的一滴11 分钟前
机器学习数据预处理:归一化与sklearn的MinMaxScaler详解
人工智能·python·程序人生·机器学习·性能优化·sklearn
野生技术架构师12 分钟前
2026最新最全Java 面试题大全(整理版)2000+ 面试题附答案详解
java·开发语言
南村群童欺我老无力.14 分钟前
Flutter 框架跨平台鸿蒙开发 - 打造表情包制作器应用
开发语言·javascript·flutter·华为·harmonyos
weixin_4624462316 分钟前
Python 使用 Chainlit + Ollama 快速搭建本地 AI 聊天应用
人工智能·python·ollama·chainlit
小北方城市网17 分钟前
SpringBoot 集成 MinIO 实战(对象存储):实现高效文件管理
java·spring boot·redis·分布式·后端·python·缓存
UR的出不克19 分钟前
Python实现SMZDM数据处理系统:从爬虫到数据分析的完整实践
爬虫·python·数据分析