Home        Store        Learn        Blog

Send Battery status from companion computer

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.

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?

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:')

# 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

I was able to send data using the master.mav.battery_status_send and the data is also been sent but it doesnt appear on the status tab for voltage.It is at zero only.Do we need to do some param config for that?I am using mission planner btw.

Hi @shreyassule7738, welcome to the forum :slight_smile:

How are you making the connection? When I was adding a sensor previously, I found the mavlink connection had to be made with source_system=1 to communicate properly to the ground control software.

I have no experience with Mission Planner, and I don’t believe it has support for ArduSub. I would assume connecting is the same as for QGroundControl, but there may be differences I’m not aware of. If the above connection approach doesn’t help, you may wish to ask on the more general ArduPilot forum (this forum is targeted at marine robotics, including ArduSub, and Blue Robotics products/software) :slight_smile:

Hello,Thankyou for the response.
I am using the UART pins on RPiThey are connected to the telem2 port.I also made the source_system=1 in the initialization.
As far as I know,there shouldn’t be any problem if I am using Mission Planner or QGC.Also,I was able to send the rangefinder reading and display it on the mission planner and also display MAV_SEVERITY_WARNING on the Mission planner.
The thing which I observed is that for rangefinder,we set the RNG_fndr param type to mavlink.There is no mavlink option for BATT_MON type in MP.Is there any such option in QGC?

Glad that you were able to establish and confirm the connection :slight_smile:

Yes, QGC supports battery monitor selection :slight_smile: