そのため、ラップトップの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)
*セイバートゥース
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 を追加しようとしましたが、変数が表示され、受け取ったものに変更されてモーターに送信されることを期待していますが、それでも同じエラーが発生します。私は本当に他に何をすべきか見当がつかないので、助けていただければ幸いです
お時間をいただきありがとうございます