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.channelsen_channel

self.in1 = pca.channelsin1_channel

self.in2 = pca.channelsin2_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()

相关推荐
2zcode3 分钟前
免费开源项目文档:基于MATLAB同态滤波的医学图像增强系统设计与实现
开发语言·matlab
REDcker8 分钟前
Android 15 16KB 内存页适配详解
android·开发语言·python
2zcode8 分钟前
基于MATLAB同态滤波的医学图像增强系统设计与实现
开发语言·计算机视觉·matlab
脏脏a8 分钟前
飞牛NAS部署EasyNVR实操:摄像头RTSP接入、本地录像与公网回看
开发语言·php
爱吃苹果的梨叔10 分钟前
KVM和分布式坐席参数怎么看?端口、通道、节点都是什么意思
python
量化吞吐机16 分钟前
近期AI量化开发,先跑最小流程再谈复杂功能
人工智能·python
杰佛史彦明122 分钟前
PDF 表格转 CSV:Python 实现方法与代码解析
开发语言·python·pdf
gugucoding27 分钟前
39. 【C语言】经典 C 项目实战:迷你 HTTP 服务器
c语言·开发语言·http
智写-AI31 分钟前
推荐一下好用的降英文AI工具工具
人工智能·python
什巳35 分钟前
JAVA练习277- 找到字符串中所有字母异位
java·开发语言·算法·leetcode