Hi,
I am trying to establish Server-Client Configuration between my laptop and Rpi. I have three thrusters connected to my Pixhawk(PWM) via the Rpi. The joystick is connected to my laptop. I have already established the connection between the duo via ethernet cable. The changes made in the joystick are read at the RPi. The point where I am struggling to get the output is that I see only one among the three running while executing the following code.
from pymavlink import mavutil
import socket
import pickle
Initialize connection to Pixhawk
master = mavutil.mavlink_connection(‘/dev/ttyACM0’) # Change the port if needed
Initialize UDP socket
UDP_IP = “0.0.0.0” # Listen on all available interfaces
UDP_PORT = 5055 # Example port number
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
Channel assignments
THROTTLE_CHANNEL = 1
ROLL_CHANNEL = 2
PITCH_CHANNEL = 3
YAW_CHANNEL = 4
Function to arm the thrusters
def arm_thrusters():
master.arducopter_arm()
print(“Thrusters armed”)
Function to disarm the thrusters
def disarm_thrusters():
master.arducopter_disarm()
print(“Thrusters disarmed”)
Function to set PWM values for each thruster based on joystick input
def set_thruster_pwm(roll, pitch, yaw, throttle):
# Scale joystick values to PWM range
roll_pwm = int(1520 + 500 * roll) # Adjust scaling factor as needed
pitch_pwm = int(1520 + 500 * pitch) # Adjust scaling factor as needed
yaw_pwm = int(1520 + 500 * yaw) # Adjust scaling factor as needed
throttle_pwm = int(1520 + 500 * throttle) # Adjust scaling factor as needed
# Set PWM values for each thruster
master.mav.rc_channels_override_send(
master.target_system,
master.target_component,
throttle_pwm, # Throttle value
roll_pwm, # Roll value
pitch_pwm, # Pitch value
yaw_pwm, # Yaw value
0, 0, 0, 0 # Neutral position for other channels
)
try:
# Arm the thrusters initially
arm_thrusters()
while True:
# Receive joystick input from the laptop
data, addr = sock.recvfrom(1024) # Buffer size is 1024 bytes
input_data = pickle.loads(data)
# Extract roll, pitch, yaw, and throttle from input data
roll = input_data.get('roll', 0)
pitch = input_data.get('pitch', 0)
yaw = input_data.get('yaw', 0)
throttle = input_data.get('throttle', 0)
# Set PWM values for each thruster based on joystick input
set_thruster_pwm(roll, pitch, yaw, throttle)
# Print joystick input for testing
print("Roll: {:.2f}, Pitch: {:.2f}, Yaw: {:.2f}, Throttle: {:.2f}".format(roll, pitch, yaw, throttle))
except KeyboardInterrupt:
# Disarm the thrusters when exiting the script
disarm_thrusters()
finally:
# Clean up
sock.close()
I understand I have to make changes in the Joystick settings with the mission planner. But I am not aware how to make the changes. I request anyone of you to help me.
The server side coding is here as in below:
import pygame
import socket
import pickle
Initialize Pygame
pygame.init()
pygame.joystick.init()
Initialize joystick
joystick = pygame.joystick.Joystick(0)
joystick.init()
Initialize UDP socket
UDP_IP = ‘172.16.2.107’ # Replace with Raspberry Pi’s IP address
UDP_PORT = 5055 # Example port number
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
while True:
# Read input from the joystick
pygame.event.pump()
throttle = joystick.get_axis(0) # Change the index if necessary
yaw = joystick.get_axis(1) # Change the index if necessary
pitch = joystick.get_axis(2) # Change the index if necessary
roll = joystick.get_axis(3) # Change the index if necessary
# Create a dictionary to hold the input data
input_data = {'throttle': throttle, 'yaw': yaw, 'pitch': pitch,'roll': roll}
# Serialize the data
data_string = pickle.dumps(input_data)
# Send the data to the Raspberry Pi
sock.sendto(data_string, (UDP_IP, UDP_PORT))
except KeyboardInterrupt:
pass
finally:
# Clean up
joystick.quit()