# Sending MAVProxy messages from a python program?

**URL:** https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515
**Category:** Blue Robotics Software
**Tags:** positioning
**Created:** [September 21, 2017, 3:31pm UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515 "2017-09-21T15:31:25Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![sbmech17](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@sbmech17](https://discuss.bluerobotics.com/u/sbmech17)
#### Post date: [September 21, 2017, 3:31pm UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/1 "2017-09-21T15:31:26Z")

</div>

So I can send commands to my ROV from the MAVProxy command line on my topside computer… Can I send commands to this link from a python program rather than from the command line? The hope is to send a sequence of commands like arming the vehicle, loading WPs, changing the flight mode, etc.

Thanks!

---

<div class="post-metadata">

### Author: ![jwalser](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.bluerobotics.com/jwalser/32/2234_2.png) [@jwalser](https://discuss.bluerobotics.com/u/jwalser)
#### Post date: [September 21, 2017, 4:25pm UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/2 "2017-09-21T16:25:03Z")

</div>

Yes! Here I arm the vehicle, wait 2 seconds, then disarm the vehicle.

```auto
import time
from pymavlink import mavutil

mavutil.set_dialect("ardupilotmega")

autopilot = mavutil.mavlink_connection('udpin:192.168.2.1:14550')

msg = None

# wait for autopilot connection
while msg is None:
        msg = autopilot.recv_msg()

print msg

# The values of these heartbeat fields is not really important here
# I just used the same numbers that QGC uses
# It is standard practice for any system communicating via mavlink emit the HEARTBEAT message at 1Hz! Your autopilot may not behave the way you want otherwise!
autopilot.mav.heartbeat_send(
6, # type
8, # autopilot
192, # base_mode
0, # custom_mode
4, # system_status
3 # mavlink_version
)

autopilot.mav.command_long_send(
1, # autopilot system id
1, # autopilot component id
400, # command id, ARM/DISARM
0, # confirmation
1, # arm!
0,0,0,0,0,0 # unused parameters for this command
)

time.sleep(2)

autopilot.mav.command_long_send(
1, # autopilot system id
1, # autopilot component id
400, # command id, ARM/DISARM
0, # confirmation
0, # disarm!
0,0,0,0,0,0 # unused parameters for this command
)

```

---

<div class="post-metadata">

### Author: ![sbmech17](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@sbmech17](https://discuss.bluerobotics.com/u/sbmech17)
#### Post date: [September 21, 2017, 7:26pm UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/3 "2017-09-21T19:26:29Z")

</div>

Thanks Jacob, this is just what I was looking for!

When I run this code, I see the following message appear in my MAVProxy console:  
`Got MAVLink msg: COMMAND_ACK {command : 400, result : 0}`

This message appears only once. Is this expected? I see that “result : 0” means the command was accepted/executed but was expecting to see a notification for each sent message.

---

<div class="post-metadata">

### Author: ![jwalser](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.bluerobotics.com/jwalser/32/2234_2.png) [@jwalser](https://discuss.bluerobotics.com/u/jwalser)
#### Post date: [September 21, 2017, 7:39pm UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/4 "2017-09-21T19:39:13Z")

</div>

It is only sent in response to COMMAND\_LONG messages. You should see it twice, once for arm, once for disarm.

---

<div class="post-metadata">

### Author: ![sbmech17](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@sbmech17](https://discuss.bluerobotics.com/u/sbmech17)
#### Post date: [September 27, 2017, 6:24pm UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/5 "2017-09-27T18:24:23Z")

</div>

Hi Jacob,

I’m trying to send a waypoint to the rov using mavlink, so I created another function in the example code you gave me. I referenced [this](http://www.colorado.edu/recuv/2015/05/25/mavlink-protocol-waypoints) link for the command sequence.

```
#mavwp set-up
wp = mavwp.MAVWPLoader()

def set_waypoint_cmd_mavwp(lat,lon,alt):
    wp.add(mavutil.mavlink.MAVLink_mission_item_message(
        1,#sys id
        1,#component id
        0,#seq wp id
        0,#frame id
        16,#mav_cmd 
        0,#current, true/false
        0,#auto continue
        0,0,0,0,#params 1-4: hold time(s),acceptable radius(m), pass/orbit,yaw angle
        lat,lon,alt) #lat/lon/alt
    )

    print "#of waypoints=",wp.count(),"\n"
    print "waypoint is=",wp.wp(0),"\n"
    autopilot.waypoint_clear_all_send()
    autopilot.waypoint_count_send(wp.count())
    msg = autopilot.recv_match(type=['MISSION_REQUEST'],blocking=True)
    print msg
    seq=0
    autopilot.mav.send(wp.wp(msg.seq))

```

then I call my function with the following arguments:  
`set_waypoint_cmd_mavwp(39.003981, -76.244854,0)`

> $ python wpsend\_test2.py  
> #of waypoints= 1
> 
> waypoint is= MISSION\_ITEM {target\_system : 1, target\_component : 1, seq : 0, frame : 0, command : 16, current : 0, autocontinue : 0, param1 : 0, param2 : 0, param3 : 0, param4 : 0, x : 39.003981, y : -76.244854, z : 0}
> 
> MISSION\_REQUEST {target\_system : 255, target\_component : 0, seq : 0}

And on the MAVProxy terminal I see the following messages pop up:

> Got MAVLink msg: MISSION\_ACK {target\_system : 255, target\_component : 0, type : 0}  
> APM: Flight plan received

But when I check the WP list through MAVProxy, I see my home position listed as a wp and nothing about the point i just sent:

> wp list  
> MANUAL\> Requesting 1 waypoints t=Wed Sep 27 14:06:56 2017 now=Wed Sep 27 14:06:56 2017  
> 16 0 39.0066337585 -76.4080886841 0.000000 p1=0.0 p2=0.0 p3=0.0 p4=0.0 cur=0 auto=1  
> Saved 1 waypoints to way.txt  
> Saved waypoints to way.txt

Then in QGC, I see the following “error” message:

 ![wp_error](https://us1.discourse-cdn.com/flex019/uploads/bluerobotics/original/2X/2/2fd30815574b520bbba1b41c22877ba82595821f.png)

If i set up a WP in QGC, I see it on wp list after clicking the “upload” button. Any idea what I’m missing? I also tried sending the waypoint using the mission\_item and mission\_item\_int message types to mirror how we send the arm/disarm commands (which work independently–haven’t figured out how to get them to work one after another with a sleep in between…)

Thanks,  
Stephanie

---

<div class="post-metadata">

### Author: ![jwalser](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.bluerobotics.com/jwalser/32/2234_2.png) [@jwalser](https://discuss.bluerobotics.com/u/jwalser)
#### Post date: [September 27, 2017, 6:47pm UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/6 "2017-09-27T18:47:17Z")

</div>

Stephanie, I recommend you start with the mavproxy waypoint module gui: `wp editor`

You will need to dig into ardusub source code to find out what is happening otherwise (as would I). I can take a look at this next week if you are still struggling.

The QGC error pops up because it got an ack from the autopilot for a command that QGC did not send (unexpected).

---

<div class="post-metadata">

### Author: ![sbmech17](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@sbmech17](https://discuss.bluerobotics.com/u/sbmech17)
#### Post date: [September 27, 2017, 9:51pm UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/7 "2017-09-27T21:51:15Z")

</div>

Thanks. Using wp editor helped! Looks like home position wants to be the initial WP, so I just have to add my WPs after the home position. This will work fine for my application.

Now I am having issues changing flight modes via mavlink. Using the “command\_long” message again, now with MAV\_CMD #176 (do\_set\_mode), I see the following error:

> Got MAVLink msg: COMMAND\_ACK {command : 176, result : 3}

meaning unsupported/unknown command. Has anyone else had success updating the mode via mavlink? I also tried the SET\_MODE message type although the [comment in the source code](https://github.com/ArduPilot/mavlink/blob/master/message_definitions/v1.0/common.xml#L2524) says this message type is outdated. SET\_MODE failed with “result : 4”

p.s. I am able to change the flight mode via MAVProxy command line

---

<div class="post-metadata">

### Author: ![sbmech17](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@sbmech17](https://discuss.bluerobotics.com/u/sbmech17)
#### Post date: [September 28, 2017, 7:15pm UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/8 "2017-09-28T19:15:02Z")

</div>

I’m able to use a [pymavlink function](https://github.com/bluerobotics/pymavlink/blob/master/mavutil.py#L548) to change modes:

`mavutil.mavfile.set_mode(autopilot,mode_mapping_sub,0,0)`

where mode\_mapping\_sub = 0 for stabilize, 19 for manual, etc…

---

<div class="post-metadata">

### Author: ![sbmech17](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@sbmech17](https://discuss.bluerobotics.com/u/sbmech17)
#### Post date: [October 10, 2017, 8:14pm UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/9 "2017-10-10T20:14:38Z")

</div>

@jwalser, can you give an example of how to send commands to the motors?

for example, to dive to a certain depth, should I use the RC\_CHANNELS\_OVERRIDE message to send values to channels 5 and 6? Or should I try to send MANUAL\_CONTROL messages like QGC?

---

<div class="post-metadata">

### Author: ![jwalser](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.bluerobotics.com/jwalser/32/2234_2.png) [@jwalser](https://discuss.bluerobotics.com/u/jwalser)
#### Post date: [October 10, 2017, 8:26pm UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/10 "2017-10-10T20:26:46Z")

</div>

You are confusing input channels and output channels. Those mavlink messages control input channels, so you would want to write to channel 3 (throttle). Either message should work, but I recommend manual\_control.

[https://www.ardusub.com/operators-manual/rc-input-and-output.html](https://www.ardusub.com/operators-manual/rc-input-and-output.html)

---

<div class="post-metadata">

### Author: ![acayulao](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@acayulao](https://discuss.bluerobotics.com/u/acayulao)
#### Post date: [January 23, 2018, 4:20pm UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/11 "2018-01-23T16:20:30Z")

</div>

Hi all!  
I was reading this post and I try test the next after arm the ROV:

_ **# MAV\_CMD\_DO\_SET\_SERVO inside of MAV\_CMD** _  
_**master.mav.command\_long\_send(**_  
_ **1,** _  
_ **1,** _  
_ **183,** _  
_ **0,** _  
_ **1,** _  
_ **1400, 0, 0, 0, 0, 0** _  
_**)**_

and I see that the QGC show this message:

**Set servo command failed**

Then I test change the mode Manual, Stabilize, etc…

_\*\*# MAV\_CMD\_DO\_SET\_MODE inside of MAV\_CMD \*\*_  
_**master.mav.command\_long\_send(**_  
_ **1,** _  
_ **1,** _  
_ **176,** _  
_ **0,** _  
_ **1, # I change this with 0,19, 10 and always have the same output** _  
_ **0, 0, 0, 0, 0, 0** _  
_**)**_

And I receive this output:

**Set Flight mode command not supported**

So, What is wrong with this?  
And also, what is the difference between MAVLink Type Enumerations and MAVLink Messages?

MAVLink Type Enumerations have many sub titles and the ID is not unique, for example, between MAV\_SENSOR\_ORIENTATION and MAV\_CMD.  
What is the idea general? To know how choose one or other and the functions of pymavlink to execute one or other.

Finally, you recommend use the manual\_control, and this command belong to MAV\_CMD,  
I can use this command with command\_long\_send?  
For example:

_**master.mav.command\_long\_send(**_  
_ **1,** _  
_ **1,** _  
_ **manual\_control, # or 69** _  
_ **0,** _  
_ **1,** _  
_ **0, 0, 0, 0, 0, 0** _  
_**)**_

But, this have this parameters:  
**target,**  
**x,**  
**y,**  
**z,**  
**r,**  
**buttons**

Then, I have very confused about how use your suggest.  
Greetings!

---

<div class="post-metadata">

### Author: ![jwalser](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.bluerobotics.com/jwalser/32/2234_2.png) [@jwalser](https://discuss.bluerobotics.com/u/jwalser)
#### Post date: [January 23, 2018, 4:40pm UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/12 "2018-01-23T16:40:24Z")

</div>

> [@acayulao](#):
>
> Set servo command failed

You can not put a setpoint on motor channels. Only non-motor channels are allowed with this command.

> [@acayulao](#):
>
> Set Flight mode command not supported

It’s not supported. Use [SET\_MODE](http://mavlink.org/messages/common#SET_MODE).

An enumeration is a list of possible options, states, related things. The [mavlink message spec](http://mavlink.org/messages/common) will tell you when the items from a particular enumeration are to be used as a parameter to the command.

---

<div class="post-metadata">

### Author: ![patrickelectric](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.bluerobotics.com/patrickelectric/32/2162_2.png) [@patrickelectric](https://discuss.bluerobotics.com/u/patrickelectric)
#### Post date: [January 23, 2018, 10:42pm UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/13 "2018-01-23T22:42:55Z")

</div>

> [@jwalser](#):
>
> It’s not supported. Use SET\_MODE

Here is an example of how to use it:

```python
    def set_mode(self, mode):
        """ Set ROV mode
            http://ardupilot.org/copter/docs/flight-modes.html
        Args:
            mode (str): MAVLink mode
        Returns:
            TYPE: Description
        """
        mode = mode.upper()
        if mode not in self.conn.mode_mapping():
            print('Unknown mode : {}'.format(mode))
            print('Try:', list(self.conn.mode_mapping().keys()))
            return
        mode_id = self.conn.mode_mapping()[mode]
        self.conn.set_mode(mode_id)

```

And [a list](https://github.com/ArduPilot/pymavlink/blob/master/mavutil.py#L1393) with all modes.

---

<div class="post-metadata">

### Author: ![sjpanwar](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@sjpanwar](https://discuss.bluerobotics.com/u/sjpanwar)
#### Post date: [January 25, 2018, 9:56am UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/14 "2018-01-25T09:56:04Z")

</div>

Similar to above,  
How mavproxy command " rc 5 1600" can be implemented in Python i.e to move BlueRov2 forward at 1600 PWM, until commanded to stop.  
Python implementation of depth hold of say 1 meter would be highly appreciated.

---

<div class="post-metadata">

### Author: ![patrickelectric](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.bluerobotics.com/patrickelectric/32/2162_2.png) [@patrickelectric](https://discuss.bluerobotics.com/u/patrickelectric)
#### Post date: [January 25, 2018, 4:43pm UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/15 "2018-01-25T16:43:13Z")

</div>

Hi @sjpanwar,

This will show how you can use mavproxy with any mavlink command.

> [@Getting started with MAVproxy](http://discuss.bluerobotics.com/t/getting-started-with-mavproxy/1082/8):
>
> Hi @sjpanwar, Check the [QGC code](https://github.com/mavlink/qgroundcontrol/blob/9d5bed4842228dc7f247b1af90fd9020632b6d24/src/uas/UAS.cc#L1025) to see what kind of mavlink messages are used, they sounds like exatcly whay you want, take a look in the different modes and what message is used for each one of them. If you want to send mavlink message with python, most messages follow this rules: message\_send. E.g: #start connection conn = mavutil.mavlink\_connection(args\*) #send SET\_ATTITUDE\_TARGET self.conn.mav.set\_attitude\_target\_send(args\*) #send SET\_POSITION\_TARGET\_LOCAL\_NED self.conn.mav.set\_positio…

If you don’t know how to use it, check this python script.

> <https://github.com/patrickelectric/bluerov_ros_playground/blob/master/src/bridge/bridge.py>

---

<div class="post-metadata">

### Author: ![sjpanwar](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@sjpanwar](https://discuss.bluerobotics.com/u/sjpanwar)
#### Post date: [January 25, 2018, 6:56pm UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/16 "2018-01-25T18:56:20Z")

</div>

[quote=“sjpanwar, post:14, topic:1515”]  
Thanks for response  
Is it possible for you send a python script to move BlueROV in forward direction for say 3 seconds and then stop.

---

<div class="post-metadata">

### Author: ![patrickelectric](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.bluerobotics.com/patrickelectric/32/2162_2.png) [@patrickelectric](https://discuss.bluerobotics.com/u/patrickelectric)
#### Post date: [January 27, 2018, 4:51am UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/17 "2018-01-27T04:51:48Z")

</div>

Hi @sjpanwar,

You can use the mavlink message [RC\_CHANNELS\_OVERRIDE](http://mavlink.org/messages/common#RC_CHANNELS_OVERRIDE) to send a forward message, you can check [this line](https://github.com/patrickelectric/bluerov_ros_playground/blob/master/src/bridge/bridge.py#L223) to see an example.

---

<div class="post-metadata">

### Author: ![sjpanwar](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@sjpanwar](https://discuss.bluerobotics.com/u/sjpanwar)
#### Post date: [February 1, 2018, 5:44am UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/18 "2018-02-01T05:44:49Z")

</div>

It is possible to provide python command line, in running a python script  
Rpi of Blue ROV2, to

1. To capture image and video from camera connected to Rpi
2. To rotate camera servo motor, possiblly through manual mavlink command  
or any other command

---

<div class="post-metadata">

### Author: ![jwalser](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.bluerobotics.com/jwalser/32/2234_2.png) [@jwalser](https://discuss.bluerobotics.com/u/jwalser)
#### Post date: [February 1, 2018, 7:12pm UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/19 "2018-02-01T19:12:40Z")

</div>

Hi @sjpanwar. It is possible to do all of these things that you describe, but we cannot write your programs for you.

Please read more about ardusub, mavlink, pymavlink and mavproxy. All of the information that you need is online. If you have more specific questions or need to know where to find certain information, we can help you. There is also a [developer chat](https://gitter.im/bluerobotics/ardusub) that you are welcome to join.

---

<div class="post-metadata">

### Author: ![jwalser](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.bluerobotics.com/jwalser/32/2234_2.png) [@jwalser](https://discuss.bluerobotics.com/u/jwalser)
#### Post date: [February 15, 2018, 4:12pm UTC](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515/20 "2018-02-15T16:12:05Z")

</div>

Here are some examples with pymavlink: [Pymavlink · GitBook](https://www.ardusub.com/developers/pymavlink.html)

[Next page](https://discuss.bluerobotics.com/t/sending-mavproxy-messages-from-a-python-program/1515.md?page=2)
