# Is it possible to adapt at runtime the motors contribution to the 6DOF?

**URL:** https://discuss.bluerobotics.com/t/is-it-possible-to-adapt-at-runtime-the-motors-contribution-to-the-6dof/13079
**Category:** ArduSub
**Tags:** ardusub
**Created:** [October 21, 2022, 9:41am UTC](https://discuss.bluerobotics.com/t/is-it-possible-to-adapt-at-runtime-the-motors-contribution-to-the-6dof/13079 "2022-10-21T09:41:34Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![rezenders](https://avatars.discourse-cdn.com/v4/letter/r/97f17d/32.png) [@rezenders](https://discuss.bluerobotics.com/u/rezenders)
#### Post date: [October 21, 2022, 9:41am UTC](https://discuss.bluerobotics.com/t/is-it-possible-to-adapt-at-runtime-the-motors-contribution-to-the-6dof/13079/1 "2022-10-21T09:41:34Z")

</div>

Hello,

I am working on a project to enable underwater robots to adapt at runtime, and I am using a BlueROV as testbed.

My goal now is to simulate thruster failures. For this, my plan is to deactivate thrusters at runtime, and to react to it by changing the robot frame configuration. I have two questions related to this:

1. Is it possible to change at runtime the motors contribution to the 6DOF? For instance, can I set [motor 1](https://github.com/ArduPilot/ardupilot/blob/725b7b30ae3a8c530ed5556c306177f379da8946/libraries/AP_Motors/AP_Motors6DOF.cpp#L170) to have 0 contribution to all factors?
2. If the awnser to question 1 is no, can I change the frame that is being used at runtime? For instance, I could create extra custom frames with motors having 0 contribution to all 6DOF and switch to them when a thruster fail.
3. If the awnser to question 1 and 2 are no, is it possible to easily switch the frame and reboot ArduSub for the changes to take effect?

PS: Initially I will be doing this with SITL + Ignition, so I am open to hacks for now. But in the future the goal is to actually apply this to the real BlueROV

Thanks in advance!

---

<div class="post-metadata">

### Author: ![EliotBR](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.bluerobotics.com/eliotbr/32/6937_2.png) [@EliotBR](https://discuss.bluerobotics.com/u/EliotBR)
#### Post date: [October 21, 2022, 11:02am UTC](https://discuss.bluerobotics.com/t/is-it-possible-to-adapt-at-runtime-the-motors-contribution-to-the-6dof/13079/2 "2022-10-21T11:02:48Z")

</div>

Hi @rezenders, interesting question 🙂

There is currently no general way to dynamically change the frame, although it is something [we’re interested in making possible](https://discuss.bluerobotics.com/t/vectored-thruster-orientation/7060/23).

That said, given you’re specifically trying to simulate failures, you should be able to achieve an equivalent result by just disabling the output for the motor you want to “fail”. You can do that by setting the relevant [`SERVOn_FUNCTION`](https://www.ardusub.com/developers/full-parameter-list.html#servonfunction-servo-output-function) parameter from `Motor{n}` to `Disabled` (0), which you can either set in QGC, or set directly with MAVLink (e.g. using [a Pymavlink script](https://www.ardusub.com/developers/pymavlink.html#read-and-write-parameters)). If you have a separate [MAVLink endpoint](https://docs.bluerobotics.com/ardusub-zola/software/onboard/BlueOS-1.0/advanced-usage/#endpoints) set up for it you can even use a pymavlink script while QGroundControl is connected to the standard endpoint (e.g. to simulate a sudden live failure).

> [@rezenders](#):
>
> 1. If the awnser to question 1 and 2 are no, is it possible to easily switch the frame and reboot ArduSub for the changes to take effect?

Just for completeness, yes, it is possible to switch between frames in ArduSub via QGroundControl (or just setting the [`FRAME_CONFIG`](https://www.ardusub.com/developers/full-parameter-list.html#frameconfig-frame-configuration) parameter) and then rebooting, but the parameter is only set up to have a single custom frame, so to have several options you can switch between you would need to either replace some of the other existing frames or go to some extra effort enabling a larger number of frame options.

---

<div class="post-metadata">

### Author: ![rezenders](https://avatars.discourse-cdn.com/v4/letter/r/97f17d/32.png) [@rezenders](https://discuss.bluerobotics.com/u/rezenders)
#### Post date: [October 21, 2022, 11:49am UTC](https://discuss.bluerobotics.com/t/is-it-possible-to-adapt-at-runtime-the-motors-contribution-to-the-6dof/13079/3 "2022-10-21T11:49:35Z")

</div>

Thanks for the reply @EliotBR

> You can do that by setting the relevant [`SERVOn_FUNCTION`](https://www.ardusub.com/developers/full-parameter-list.html#servonfunction-servo-output-function) parameter from `Motor{n}` to `Disabled` (0),

Thanks, I will do that. One more question, does the motion controller takes this into account in order to calculate the required thrust? Probably not, right? For my use case, it would be better if it does not take into account and the movement goes “bad”. Then I can switch to a different frame and ideally the movement would be “better”.

> Just for completeness, yes, it is possible to switch between frames in ArduSub via QGroundControl (or just setting the [`FRAME_CONFIG`](https://www.ardusub.com/developers/full-parameter-list.html#frameconfig-frame-configuration) parameter) and then rebooting, but the parameter is only set up to have a single custom frame, so to have several options you can switch between you would need to either replace some of the other existing frames or go to some extra effort enabling a larger number of frame options.

Ok. I will go with one frame for now, considering that only one specific thruster can fail and then when I get it working I can check how to expand to more scenarios.

> There is currently no general way to dynamically change the frame, although it is something [we’re interested in making possible](https://discuss.bluerobotics.com/t/vectored-thruster-orientation/7060/23).

Is this something that would require a lot of knowledge about how Ardupilot/ArduSub works? I could pottentialy help with this, but so far I only have knowledge from the user side.

Just to let you know, I am not using QGC, I am using ROS + mavros to command the robot. The idea is that this whole process happens autonomously.

---

<div class="post-metadata">

### Author: ![williangalvani](https://avatars.discourse-cdn.com/v4/letter/w/6f9a4e/32.png) [@williangalvani](https://discuss.bluerobotics.com/u/williangalvani)
#### Post date: [October 21, 2022, 1:45pm UTC](https://discuss.bluerobotics.com/t/is-it-possible-to-adapt-at-runtime-the-motors-contribution-to-the-6dof/13079/4 "2022-10-21T13:45:06Z")

</div>

Hi @rezenders,

Check out the new [lua motors backend](https://github.com/ArduPilot/ardupilot/blob/master/libraries/AP_Motors/AP_MotorsMatrix_Scripting_Dynamic.h) that Peter Hall has been pushing. I suspect it would suit your needs.  
I don’t really know how much work it would take to get it running, but it should suit your needs.

---

<div class="post-metadata">

### Author: ![rezenders](https://avatars.discourse-cdn.com/v4/letter/r/97f17d/32.png) [@rezenders](https://discuss.bluerobotics.com/u/rezenders)
#### Post date: [October 25, 2022, 12:50pm UTC](https://discuss.bluerobotics.com/t/is-it-possible-to-adapt-at-runtime-the-motors-contribution-to-the-6dof/13079/5 "2022-10-25T12:50:49Z")

</div>

Thanks for the reply @williangalvani.

I guess the lua script should be something like [this](https://github.com/ArduPilot/ardupilot/blob/master/libraries/AP_Scripting/examples/Motor_mixer_dynamic_setup.lua), right?

I have a few questions:

1. Does this [line](https://github.com/ArduPilot/ardupilot/blob/51481eff2480d58cfafd74e0a2a9e0354a6d8c28/libraries/AP_Scripting/generator/description/bindings.desc#L466) mean that the Motor\_dynamic methods can only be used with ArduPlane, Copter or Heli?
2. If I use a Lua script like this to set the motors contributions, do I need to somehow deactivate the original setup that is done [here](https://github.com/ArduPilot/ardupilot/blob/725b7b30ae3a8c530ed5556c306177f379da8946/libraries/AP_Motors/AP_Motors6DOF.cpp#L168)?
3. I have some doubts about using lua scripts (not sure if this is the right place for this), are the lua scripts executed automatically when ardupilot starts?
4. Is it possible to interact with the lua scripts from different codes? For instance, can I send a msg with information to the lua script from a python/c++ code?

By the way, if anyone has come across this post and is looking for more information, check this [link](https://ardupilot.org/copter/docs/common-lua-scripts.html)

---

<div class="post-metadata">

### Author: ![williangalvani](https://avatars.discourse-cdn.com/v4/letter/w/6f9a4e/32.png) [@williangalvani](https://discuss.bluerobotics.com/u/williangalvani)
#### Post date: [October 25, 2022, 5:42pm UTC](https://discuss.bluerobotics.com/t/is-it-possible-to-adapt-at-runtime-the-motors-contribution-to-the-6dof/13079/6 "2022-10-25T17:42:40Z")

</div>

> [@rezenders](#):
>
> Does this [line](https://github.com/ArduPilot/ardupilot/blob/51481eff2480d58cfafd74e0a2a9e0354a6d8c28/libraries/AP_Scripting/generator/description/bindings.desc#L466) mean that the Motor\_dynamic methods can only be used with ArduPlane, Copter or Heli?

Yes. this is not currently supported in Sub, but I’m keen on having this feature.

> [@rezenders](#):
>
> If I use a Lua script like this to set the motors contributions, do I need to somehow deactivate the original setup that is done [here](https://github.com/ArduPilot/ardupilot/blob/725b7b30ae3a8c530ed5556c306177f379da8946/libraries/AP_Motors/AP_Motors6DOF.cpp#L168)?

That is most likely correct. There is a new Motors6dof\_scripting backend that we’ll need to use.  
I think this is they Key PR implementing the feature (I also highlighted the area responsible for enabling the backend):

> <https://github.com/ArduPilot/ardupilot/pull/16105/files#diff-dcfa1ee5c9a787df36883976aee05a21ca4188c6445619c2211475bcb1f7f0e8R487-R492>
>
> This adds support for copters that can fly in any direction and at any orientati…on.
> 
> https://youtu.be/CXIlZ8mCfGM
> 
> This adds a new attitude control and motor back-end and scripting bindings for each. Scripting can be used to set the true vehicle attitude while the existing navigation 'attitude' is used for the thrust vector. Scripting is also used to set the 6DoF matrix for the mixer. I did spend a some time trying to calculate the mixer directly from motor positions and trust vectors, but it all got too complex. The ideal mixer can be found and then loaded with the script. For my demo I used a PSO in MATLAB to find the mix matrix, the mix is not included.
> 
> Fixes #10117

> [@rezenders](#):
>
> Is it possible to interact with the lua scripts from different codes? For instance, can I send a msg with information to the lua script from a python/c++ code?

Yes, I think there are a couple ways. The easier one is to add parameters created by the scripting interface itself.  
You can, for example, create a param MY\_ROLL to control roll target.

---

<div class="post-metadata">

### Author: ![rezenders](https://avatars.discourse-cdn.com/v4/letter/r/97f17d/32.png) [@rezenders](https://discuss.bluerobotics.com/u/rezenders)
#### Post date: [November 1, 2022, 5:23pm UTC](https://discuss.bluerobotics.com/t/is-it-possible-to-adapt-at-runtime-the-motors-contribution-to-the-6dof/13079/7 "2022-11-01T17:23:31Z")

</div>

Hi @williangalvani,

Sorry for the delay to reply. I had other tasks in hand and I wanted to spend some time checking ArduPilot code to better understand what you sent.

I made some simplified Class and Activity diagrams with the relevant information for both ArduSub and ArduCopter to better communicate what I understood.

ArduSub:

 ![DIagramsArduSub](https://us1.discourse-cdn.com/flex019/uploads/bluerobotics/original/2X/9/94e324cae86495576bbeb8566f8273d0f6881bb5.png)

ArduCopter:

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

So, in order to add this feature to Sub the steps are:

1. Change AP\_Motors6DOF motors to AP\_MotorsMultiCopter \*motors
2. Add an allocate\_motors method to Sub
3. Call allocate\_motors method before init\_rc\_out
4. Add lua script to add the motors

In 3, the allocate\_motors should set motors to be an AP\_Motors6DOF when scripting is not enabled, and AP\_MotorsMatrix\_6DoF\_Scripting when scripting is enabled.

Does this seem correct? Did I miss anything? I would like to implement this, should I open a draft PR in ArduPilot, and we continue the discussion there?

---

<div class="post-metadata">

### Author: ![rezenders](https://avatars.discourse-cdn.com/v4/letter/r/97f17d/32.png) [@rezenders](https://discuss.bluerobotics.com/u/rezenders)
#### Post date: [November 3, 2022, 3:23pm UTC](https://discuss.bluerobotics.com/t/is-it-possible-to-adapt-at-runtime-the-motors-contribution-to-the-6dof/13079/8 "2022-11-03T15:23:54Z")

</div>

I have been trying to work in this feature but I got stuck.  
I opened a draft PR to discuss it: [[ArduSub] Add scripting support to motors by Rezenders · Pull Request #22114 · ArduPilot/ardupilot · GitHub](https://github.com/ArduPilot/ardupilot/pull/22114)

Do you have any idea/tips for me?

---

<div class="post-metadata">

### Author: ![williangalvani](https://avatars.discourse-cdn.com/v4/letter/w/6f9a4e/32.png) [@williangalvani](https://discuss.bluerobotics.com/u/williangalvani)
#### Post date: [November 3, 2022, 5:11pm UTC](https://discuss.bluerobotics.com/t/is-it-possible-to-adapt-at-runtime-the-motors-contribution-to-the-6dof/13079/9 "2022-11-03T17:11:47Z")

</div>

Hi @rezenders ,

I’m not that familiar with that section of the code, but I’ll give it a go, too.

---

<div class="post-metadata">

### Author: ![williangalvani](https://avatars.discourse-cdn.com/v4/letter/w/6f9a4e/32.png) [@williangalvani](https://discuss.bluerobotics.com/u/williangalvani)
#### Post date: [November 3, 2022, 5:59pm UTC](https://discuss.bluerobotics.com/t/is-it-possible-to-adapt-at-runtime-the-motors-contribution-to-the-6dof/13079/10 "2022-11-03T17:59:02Z")

</div>

@rezenders I’ll get in touch with you privately so we can work on this together
