Home        Store        Learn        Blog

Send Battery status from companion computer

Hello,
I need some some help writing a code to send a battery status from my raspberry pi to my gcs through pixhawk.

I know it sounds simple, but for the last days I have been researching about mavlink on Guide · MAVLink Developer Guide and it seemed overwhelming, I don’t know where to start. I know I need to establish a connection between rpi and pixhawk, and then send/receive heartbeats, since I only want to send a message, I don’t need to request information from pixhawk and then send the message, but i don’t know how to code these steps.Can someone help?

Please, I am at my wits’ end

Hi, from what I understand the communication line is : pixhawk < – > RPI < – > qgc. And, pixhawk usually monitor the battery and send it to qgc.
In this setup, RPI is only a transparent tunnel more or less.
Have you try using qgc setup to see the battery level? This function is implemented in the ardusub version.

That being said, I am not yet expert in this.
Charles

Hi, thank you for answering.
The setup is sensor->rpi->pixhawk->gcs.
I am reading the codes here:Pymavlink · GitBook, but couldn’t find a function that could send a BATTERY_STATUS message using values from the sensor. The only that says something about sending a message to qgc, it only sends a text.

So you want the battery level from a third party sensor on top of the usual battery voltage that QGC <–> Pixhawk exchange.

If so, I can’t help.
In arduino or other kind of microcontroller, we can convert string into numbers. Maybe you can send the battery level in text form and show it in QGC as a text box? Not so nice but it migth do the job?
Not very helpfull I guess! Sorry about this !

Even so, thank you for the answer.

Did you read this?
https://discuss.bluerobotics.com/t/adding-a-sensor-to-mavlink-stream/7985/22

Hi @GRS26, welcome to the forum! :slight_smile:

It’s a bit unclear what you’re trying to do, could you explain a bit more about your setup? In particular:

  1. Are you using a Blue Robotics companion image, or just a plain raspberry pi image with pymavlink installed?
  2. Are you using QGroundControl as your GCS, or something else?
  3. Are you using the Blue Robotics Power Sense Module (as found in the BlueROV2), or some other power sensing board/module?
  4. Are you using ArduSub, or some other autopilot firmware?

If you’re using a standard BlueROV2 with QGroundControl then the battery status is already sent automatically. To provide meaningful help with other options we’d need to know what you’re actually using :slight_smile:

If it is relevant for you to use pymavlink for this, as you mentioned there’s a BATTERY_STATUS mavlink message that’s intended to send the battery info. That can be sent using the method master.mav.battery_status_send(...).

Thank you for the answer, its exactly this method I was looking for. I tried studying the ardusub page about pymavlink, but couldn’t that specific method, or a more general method to send message, the closest was to send commands.
So far my only idea is to find where all the methods of pymavlink are defined and study, but I realize the code is gigantic.Is there another way to learn how to handle mavlink?

Unfortunately our set of pymavlink examples are some of the best documentation there is for using pymavlink, and they’re by no means a comprehensive cover of the available functionality. We’re not the main developers of pymavlink, so while we try to document useful things as we need to use them it’s not really feasible for us to make any kind of complete documentation for it at this stage.

Personally when looking for things in mavlink I first search the relevant message sets (common, ardupilotmega) to find a relevant message (assuming I’m trying to send a command - for parameters I’d search the ArduSub parameter list). Once I have a message name then if I’m trying to use it in pymavlink I’ll basically use Python’s built in commands to help search, using the known message types we specify in our docs:

from pymavlink import mavutil

SEARCH = "battery"
print("searching for:", SEARCH)

# make incoming connection from QGC on top computer
master = mavutil.mavlink_connection('udpin:0.0.0.0:14550')

# search reasonable places for mention of a relevant command
print("searching mavutil.mavlink")
print([result for result in dir(mavutil.mavlink) if SEARCH in result.lower()])

print("searching master.mav")
print([result for result in dir(master.mav) if SEARCH in result.lower()])

If that yields something useful then I’ll generally use Python’s built in help function to at least find out the function signature and see what variables and whatnot it accepts (e.g. help(master.mav.battery_status_send)).

It’s sometimes also worth searching the codebase, e.g. you can CTRL+F in the mavutil.py file, or you can do a github search in the pymavlink or mavlink repositories.

Thank you for the answer, it’s been very helpful.
I don’t know if you already have, but I found this github page that seems to contain all the methods and some description:PX4Firmware/mavlink_px4.py at master · Zubax/PX4Firmware · GitHub

Glad to hear it :slight_smile:

The mavlink protocol is a general specification that can support various different message sets - the actual code to use those messages gets generated based on the desired language and message sets. Autopilot firmwares in the ArduPilot project use the common and ardupilotmega message sets (which I linked you to), and you can find the relevant files for that where pymavlink is installed.

The file you’ve found here is a generated Python file for the common message set, intended for use by the PX4 firmware (not part of the ArduPilot project). The idea of the common message set is that multiple unrelated firmwares can use it without independently needing to replicate each others work. That means that it should generally be fine to refer to that file instead of finding it in your own installation, but you won’t find the relevant file for ardupilotmega on the PX4 github because it’s not related. Since mavlink is updated from time to time it’s also possible the messages and functions found there won’t always match the ones available on your device, if one is less up to date than the other.

Again thank you for the help, your explanation of mavlink is fully structured and easy to follow, I gained a greater understanding of mavlink thanks to you

I generated the dialect in python, and now I am building my code