How to send command line commands to Mavproxy using Pymavlink or python?

Anyone knows how can I send command line commands to Mavproxy using Pymavlink or python.

For example I like to send a command like this using python

output add 192.168.1.1:14550
or to load a certain module
These are not mavlink messages but mavproxy command.

Anyway to do it using Python and not manually using command prompt?

sorry to bump this but Iā€™m interested in an answer of how to do this. Was this ever solved?

I wanted to document my solution, which diverges slightly from the original intent but accomplished the purpose using a related method. I used the --cmd flag in a .bat file that was called by python to start mavproxy and that allowed me to send command through the --cmd flag. This only allowed commands to be sent upon startup, so after the commands were sent initially no more could be called using this method.

I am also working on a similar problem.
Any solutions?

Found a solution:

I wanted to download logs on a Companion Computer connected to a Flight Controller.
So I created a script that starts the mavproxy on the CC. The contents of the script are:

#!/bin/bash
mavproxy.py

Then I created a python script that would open the mavproxy process and send the required commands. The contents of the python script include:

import subprocess
import time

command_to_send = "log list\n"      # MAVProxy command to list the available logs (running this is important before you use any other log module command)

mavproxy_process = subprocess.Popen(["mavproxy.py", "--console"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)

# Send the command to mavproxy
try:
    # Send the command to mavproxy
    mavproxy_process.stdin.write(command_to_send)
    mavproxy_process.stdin.flush()
    time.sleep(15)
    print("Command sent")

    command_to_send = "log download 300\n"    # MAVProxy command to download the log number 300

    mavproxy_process.stdin.write(command_to_send)
    mavproxy_process.stdin.flush()
    #xtime.sleep(60)
    print("Command sent")

    # Read the response
    response = mavproxy_process.stdout.read()
    print(response)
finally:
    # Close the process and cleanup
    mavproxy_process.stdin.close()
    mavproxy_process.stdout.close()
    mavproxy_process.stderr.close()
    mavproxy_process.terminate()  # Terminate the process

Now just I have to figure out how to get a feedback once the log has been completely downloaded and kill the script.

You can also log directly to the companion computer without any other script.
https://ardupilot.org/mavproxy/docs/modules/dataflash_logger.html
https://ardupilot.org/copter/docs/parameters.html#log-backend-type-ap-logger-backend-storage-type

1 Like

Great! Thanks for the references.