所以,我用树莓皮编程机器人,使用笔记本电脑的xbox控制器输入命令。使用Python2.7编程
我得到了一个错误时,试图使用从轴控制器的幅度,得到速度控制的电机。在
但是如果我声明一个确定的速度。 但是如果我使用这个量值,它会给出一个错误,即使我打印出变量,也会显示得很好。在
这是我得到的错误
executing forward...
max speed is
23
Traceback (most recent call last):
File "driverMain.py", line 103, in <module>
main()
File "driverMain.py", line 69, in main
frontCon.allForward(maxSpeed)
File "/home/pi/xbox/sabretooth.py", line 11, in allForward
self.leftMotor.drive('forward', speed)
File "/home/pi/xbox/sabretooth.py", line 32, in drive
self.port.write(chr(speed))
TypeError: an integer is required
*pc上的部分代码
^{pr2}$*树莓的部分代码
frontCon = controller(serialPort, baudRate, 130)
rearCon = controller(serialPort, baudRate, 129)
#set up socket
host = ''
port = 12345
sock = socket.socket()
sock.bind((host,port))
sock.listen(5)
while True:
c, addr = sock.accept()
command,maxSpeed = c.recv(1024).split('|')
print(command)
print(maxSpeed)
#### this print shows the speed value correctly
#### if i insert this maxspeed code
## maxSpeed = 120
## here for example, the program works correctly, otherwise i get an error
## even tho without it, the print work fine
#### later in the code
elif command == 'forward':
print('executing forward...')
print('max speed is')
print(maxSpeed)
frontCon.allBack(maxSpeed)
rearCon.allBack(maxSpeed)
*剑齿
import serial
class controller(object):
def __init__(self, port, baudRate, address):
self.port = serial.Serial(port, baudRate, timeout=1)
self.address = address
self.leftMotor = motor(self.port, address, 1)
self.rightMotor = motor(self.port, address, 2)
def allForward(self, speed):
self.leftMotor.drive('forward', speed)
self.rightMotor.drive('forward', speed)
def allBack(self, speed):
self.leftMotor.drive('back', speed)
self.rightMotor.drive('back', speed)
class motor(object):
#motorNum is 1 or 2, depending on which motor you wish to control
def __init__(self, serial, controllerAddress, motorNum):
self.port = serial
self.address = controllerAddress
self.motorNum = motorNum
if motorNum == 1:
self.commands = {'forward': 0, 'back': 1}
elif motorNum == 2:
self.commands = {'forward': 4, 'back': 5}
def drive(self, direction, speed):
self.port.write(chr(self.address))
self.port.write(chr(self.commands[direction]))
self.port.write(chr(speed))
self.port.write(chr(int(bin((self.address + self.commands[direction] + speed) & 0b01111111), 2)))
我还尝试在脚本的开头添加一个maxSpeed=50,希望它能看到变量并将其更改为接收到的任何值并将其发送到电机,但仍然会出现相同的错误。我真的不知道还能做什么,谢谢你的帮助
谢谢你抽出时间
根据}时将其转换为
|
接收并拆分后,maxSpeed
将是一个字符串,您需要在传递给allForward
和{int
相关问题 更多 >
编程相关推荐