0

好的,所以我在两台计算机之间设置了一个 UDP 套接字。一台计算机从操纵杆获取坐标,并将它们作为数组通过套接字发送。在另一端接收数组,然后将值发送到伺服。问题是,这在大约 10 秒内工作得很好,然后整个连接在接收端崩溃(在这种情况下是 RPI),我必须重新启动它。如果有人能找到一个会膨胀的解决方案。代码如下。

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)
4

1 回答 1

0

我看到你有睡眠,但它们不匹配。发送者休眠 0.05 秒,接收者休眠 1 秒。这可能会造成缓冲区溢出。

于 2016-07-09T15:36:03.520 回答