2016-07-09 6 views
0

これで、2台のコンピュータ間にUDPソケット設定ができました。 1台のコンピュータがジョイスティックから座標を取得し、それらをソケット上の配列として送信します。もう一方の端では、配列を受け取り、その値をサーボに送ります。問題は、これが約10秒間大きく働いてから、受信側(この場合はRPI)で接続がクラッシュし、再起動する必要があるということです。誰かが膨らんでしまうような解決策を見つけることができたら。以下のコード。Pythonソケットを使用しているとイーサネット接続がクラッシュする

import socket 
import time 
import pygame 
try: 
    import cPickle as pickle 
except: 
    import pickle 
from pygame.locals import * 
pygame.init() 

#Initiate Some Variables 
IP = "x.x.x.x" 
Port = "5000" 
crashed = False 
connectionID = "123456789" 
clock = pygame.time.Clock() 
ready = True 

#Initalize Joystick 
pygame.init() 
pygame.joystick.init() # main joystick device system 

try: 
    j = pygame.joystick.Joystick(0) # create a joystick instance 
    j.init() # init instance 
    print ('Enabled joystick: ' + j.get_name()) 
except pygame.error: 
    print ('no joystick found.') 

m = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 

while True: 
    if ready: 
     pycontrol = pygame.event.get() 

     #Get Joystick Pos 
     x = j.get_axis(0) 
     y = j.get_axis(1) 
     s = j.get_axis(2) 
     z = j.get_axis(3) 

     #Set Buttons To 0 
     JoyButton_0 = 0 
     JoyButton_1 = 0 
     JoyButton_2 = 0 
     JoyButton_3 = 0 
     JoyButton_4 = 0 
     JoyButton_5 = 0 
     JoyButton_6 = 0 

     #Lets do some math 
     #Create cubic graph y=x^3 
     #And a linear 
     cubic_x = (x**3)*100 
     cubic_y = (y**3)*100 
     linear_s = ((-0.5*s+1)-0.5)*100 
     linear_z = (z**3)*100 
     data_x = round(cubic_x, 0) 
     data_y = round(cubic_y, 0) 
     data_s = round(linear_s, 0) 
     data_z = round(linear_z, 0) 

     #Get Joystick Key Events 
     for event in pycontrol: 
      if event.type == pygame.QUIT: 
       crashed = True 

      ############################ 
      if event.type == pygame.JOYBUTTONDOWN: 
       if 1 == j.get_button(0): 
        JoyButton_0 = 1 
       if 1 == j.get_button(1): 
        JoyButton_1 = 1 
       if 1 == j.get_button(2): 
        JoyButton_2 = 1 
      ###################### 

     #Turn into array and steralize it 
     array = (data_x, data_y, data_s, data_z, JoyButton_0, JoyButton_1, JoyButton_2) 
     print (array) 
     #Pickle Array and set Protocol to 2 to be read by RPI 
     send_array = pickle.dumps(array, protocol=2) 
     m.sendto(send_array, (IP,5000)) 
     time.sleep(0.05) 

が今では、送信側のためだった、ここで私はあなたが眠るを持っていますが、それらが一致していない参照の受信側

#!/usr/bin/python 

from Adafruit_PWM_Servo_Driver import PWM 
import time 

import socket 
try: 
    import cPickle as pickle 
except: 
    import pickle 

# Setup Variables & Socket 
IP = '192.168.0.122' 
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
s.bind((IP, 5000)) 
print ("Ready") 
servoMin = 150 # Min pulse length out of 4096 
servoMax = 600 # Max pulse length out of 4096 

# Initialise the PWM device using the default address 
pwm = PWM(0x40) 
# Note if you'd like more debug output you can instead run: 
#pwm = PWM(0x40, debug=True) 
pwm.setPWMFreq(60) # Set the Frequency to 60hz 

while True: 
    raw_message,data = s.recvfrom(1024) 
    (data_x, data_y, data_s, data_z, JoyButton_0, JoyButton_1, JoyButton_2) = pickle.loads(raw_message) 
    if JoyButton_0 == 1: 
     print("heelo") 
    if JoyButton_1 == 1: 
     print("Potato") 
    if JoyButton_2 == 1: 
     print("Dog") 

    Aileron_Servo = (2.25*data_x)+375 
    Elevator_Servo = (2.25*data_y)+375 
    Throttle_Servo = (4.5*data_s)+150 
    Rudder_Servo = (2.25*data_z)+375 


    if 150 <= Aileron_Servo <= 600: 
     Servo0 = Alieron_Servo 
     pwm.setPWM(0, 0, Servo0) 
    time.sleep(1) 
+0

受信者が送信者からのデータでいっぱいになっている可能性があります。送信しているデータを絞ったり、いくつかのパケットをドロップしてみてください。取得しているエラーに関する詳細情報が必要です。システムログが役に立つかもしれません。 –

+0

1分でシステムログを取得できるかどうか確認してください。しかし、サーボを動かそうとするまで接続が開いているように見えるので、サーボドライバが問題になる可能性があります。しかし、私はそれが接続をクラッシュする方法を見ていない。接続をクラッシュさせると、IPアドレスやすべてがなくなって再起動するまで戻ってこないことを意味します。それをクリアするだけです。 – ferret249

答えて

0

です。送信者は0.05秒寝ていて、受信者は1秒寝ています。おそらくバッファオーバーフローが発生しています。

+0

(ハードウェアレベルでイーサネットを使用しない場合)すべてのTCP接続は何の宣言もなしに50msの遅延を得ました。これは正しい点です(オーバーフロー)、彼はsend-recvガベージデータを必要とします(片方向のソケット使用)。 – dsgdfg

+0

接続はUDP接続なので、おそらくTCPよりも高速です。そして、ガベージデータの送信は何をしますか? – ferret249

関連する問題