Jan-11-2022, 08:26 PM
i am controlling 4 stepper motors as a mecanum robot in python socket to esp8266 arduino with a limited amount of pins. i would like to add a pair of servos for a camera gimbal but it will have to be controlled by my raspberry pi because i have no spare pins on the esp8266.
i have the server running pygame joystick and will need to split the controller data to a second client. i see two issues here. firstly how do i run a pair of clients on the same server with each their own dedicated tasks? and second how do i differentiate the two clients so they receive the right data?
below is my current server script that only controls the 4 stepper motors. how do i start adding the two servo controls for my raspberry pi? currently using left joystick and both triggers on my gamepad and would like to use the right joystick for the camera gimbal
i have the server running pygame joystick and will need to split the controller data to a second client. i see two issues here. firstly how do i run a pair of clients on the same server with each their own dedicated tasks? and second how do i differentiate the two clients so they receive the right data?
below is my current server script that only controls the 4 stepper motors. how do i start adding the two servo controls for my raspberry pi? currently using left joystick and both triggers on my gamepad and would like to use the right joystick for the camera gimbal
import socket import pygame from pygame.locals import * m1x = 0.0 m2x = 0.0 m3x = 0.0 m4x = 0.0 m1y = 0.0 m2y = 0.0 m3y = 0.0 m4y = 0.0 m1rx = 0.0 m2rx = 0.0 m3rx = 0.0 m4rx = 0.0 value_tl = 0.0 value_tr = 0.0 pause_state = False connected = False m1 = 15000 m2 = 15000 m3 = 15000 m4 = 15000 startline = '<' finishline = '>' pygame.init() clock = pygame.time.Clock() pygame.joystick.init() joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())] for joystick in joysticks: print(joystick.get_name()) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((socket.gethostbyname(socket.gethostname()), 1324)) s.listen() print('listening') client, address = s.accept() print(f'Connected from {address}') connected = True if 'FrSky' in joystick.get_name(): while True: pass else: while True: joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())] for event in pygame.event.get(): if event.type == JOYAXISMOTION: if event.axis == 0: value_x = event.value m1x = value_x m2x = value_x * (-1) m3x = value_x * (-1) m4x = value_x if event.axis == 1: value_y = event.value * (-1) m1y = value_y m2y = value_y m3y = value_y m4y = value_y if event.axis == 2: value_rx = event.value m1rx = value_rx * (-1) m2rx = value_rx * (-1) m3rx = value_rx m4rx = value_rx if event.axis == 4: value_tl = round(event.value + 1, 4) if event.axis == 5: value_tr = round(event.value + 1, 4) if event.type == JOYBUTTONDOWN: if event.button == 7: pause_state = not pause_state value_t = ((value_tr) - (value_tl)) / 2 m1t = value_t * (-1) m2t = value_t * (-1) m3t = value_t m4t = value_t m1 = round((m1x + m1y + m1t + 2) * 15000, 0) m2 = round((m2x + m2y + m2t + 2) * 15000, 0) m3 = round((m3x + m3y + m3t + 2) * 15000, 0) m4 = round((m4x + m4y + m4t + 2) * 15000, 0) if pause_state == False: try: print(f'm 1 {m1} 2 {m2} 3 {m3} 4 {m4}') data = (f'{startline}{m1},{m2},{m3},{m4}{finishline}') data = bytes(data, 'utf-8') client.send(data) except ConnectionAbortedError: print('client aborted') connected = False while not connected: try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((socket.gethostbyname(socket.gethostname()), 1324)) s.listen() print('listening') client, address = s.accept() print(f'Connected from {address}') connected = True except socket.error: clock.tick(0.5) except ConnectionResetError: print('client lost') connected = False while not connected: try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((socket.gethostbyname(socket.gethostname()), 1324)) s.listen() print('listening') client, address = s.accept() print(f'Connected from {address}') connected = True except socket.error: clock.tick(0.5) else: print('pause') pause_data = (f'{startline}{15000},{15000},{15000},{15000}{finishline}') pause_data = bytes(pause_data, 'utf-8') client.send(pause_data) while pause_state: for event in pygame.event.get(): if event.type == JOYBUTTONDOWN: if event.button == 7: pause_state = not pause_state clock.tick(60) client.close()