尝试获取速度变量,TypeError:需要一个整数

时间:2013-11-11 06:07:47

标签: python variables serial-port typeerror

所以我使用来自笔记本电脑的xbox控制器的输入命令使用树莓派编程机器人。使用python 2.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上的部分代码

def joyStickMovement(magnitude, command, joyStickNum):
host = '192.168.1.112'
port = 12345
sock = socket.socket()
sock.connect((host,port))

prefix = ""
if joyStickNum == 1:
    prefix = "bucket"

sock.send(str(prefix + command) + '|' + str(magnitude))

*覆盆子上的部分代码

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)

* sabertooth

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,希望可能会看到变量并将其更改为接收到的任何内容并将其发送到电机但它仍然会出现相同的错误。我真的不知道还能做什么,并希望得到任何帮助

感谢您的时间

1 个答案:

答案 0 :(得分:0)

根据|接收和拆分后,maxSpeed将是一个字符串,您需要在转到intallForward时将其转换为allBack像这样

    frontCon.allBack(int(maxSpeed))
    rearCon.allBack(int(maxSpeed))