How do I use MAVLink Endpoints to get gamepad data?

Hey there!

For a custom project, I’m running BlueOS on a raspberry pi 4B, with no other hardware attached. Using the cockpit extension, I have a fantastic camera stream coming from the picamera, and I’m very impressed!

I want to write a python program on the PI to read the joystick and button data coming from cockpit. I also want to write python program on the pi that populates fields on the cockpit such as the depth, battery, compass etc.

Is this possible?

It seems like MAVLink endpoints with pymavlink are the way to go, but I’ve been having trouble getting them to work. The MAVLink inspector doesn’t show a MANUAL_CONTROL message, despite the docs saying here that “MAVLink messages are automatically sent to the vehicle at 25Hz…” How can I listen to those?

In other words, how can I listen to joystick data on the PI, and publish data from the PI to display in cockpit? (Preferably using python)

Thank you!

2 Likes

Hi Mark,

Iam not familiar with Cocpit, since I use ROS. To control your vehicle with a joystick, you will need to set (by Joystick)some PWM values using Mavlink. ROS, along with MAVROS packages, provides a great interface to move your vehicle using joystick.

I highly recommend using the latest achievement from @evan-palmer, which you can find here (ROS 2):

blue
documentation - joystick

For ROS:

bluerov_ros_playground

Regards,
Markus

1 Like

Hi Markus,

Thank you for your reply. Using ROS may be an alternative I look into, although I would love to take advantage of the features Cockpit offers.

@rafael.lehmkuhl Do you have any insight into how I might use pymavlink to read joystick data coming from Cockpit?

When I navigate to the Mavlink2rest webpage, I see there is a MANUAL_CONTROL message being received/sent in some way:

When I click the watcher link in a separate tab, and focus on the cockpit window while moving the joysticks, nothing changes. The values are all 0. The watcher for MANUAL_CONTROL doesn’t seem to update at all, despite reporting a frequency of 2.53Hz.

I created a MAVLink endpoint, a UDP Server at 0.0.0.0:9000 in hopes I could capture the MANUAL_CONTROL message via python, but when I connect to it, the only packets I receive are those sent by the SITL autopilot ArduSub firmware.

In my custom project, it doesn’t make sense to have autopilot and it’s associated firmware started/active. Turning it off of course stops the heartbeat and SITL telemetry coming over MAVLink, which is okay, because really the python programs I wish to write should provide those.

If you could help me understand the specifics of using pymavlink on the Raspberry PI to send/receive data from cockpit I would really appreciate that!

This is possible and a very interesting idea!
I’m myself am building a non-ardupilot vehicle and using cockpit for seeing the video stream. I was planning on using an Iframe widget to capture input (mostly keyboard for now), but getting the joystick input is a cool idea, too!.

You are looking at the right place. MANUAL_CONTROL will contain 4 of the joystick axis and the button presses as a bitmask.

Mavlink2Rest had a bug recently where the watcher was broken it seems fine on the latest master.

You should be able to capture GCS data (MANUAL_CONTROL) messages if you listen from vehicle ID 255 and component ID 240

IF you inject mavlink data into the stream (as vehicle 1, component 1) you should be able to populate the widgets, though I haven’t tried. I’m also not sure if a heartbeat is required.

1 Like

Hi Mary,

The MAVLink Protocol used by MAVROS and other Robot Operational Systems is very flexible and easy to use. There are several stardard MAVLink commands present on field MessageID. If the MessageID(Command) you need is not present on default list, you can create your own and then defines the payload data you want. Thos protocol was created to control AirPlanes and standard commands are present on them. Check if there are Joystick present on remote control equipments and use them. If not, Its possible you have to create your own MessageID(Command) and use one that is not present on standard MessageID(Command List) to avoid conflits.

1 Like

Hey Mark!

You’re on the right direction.

The MANUAL_CONTROL message is indeed being updated, and you should be able to consume from it.

Ignore the watcher page. It is not working for messages being sent from anybody but the vehicle. It’s a problem on mavlink2rest and @patrickelectric is working on it (issue to track).

About the problems with pymavlink, I think @EliotBR is a better person to help you, as it uses it frequently.

1 Like

Thank you all so much! This is super helpful, I really appreciate it.

1 Like