Adding a custom control loop / Mode with a set of input variables

I want to add a custom control loop (similar to cruise speed in the Cruise mode), but not necessarily with the same input variables as already used in that mode, or possibly for a different variablethan cruise speed. An example is to set a specific airspeed or ground speed, then flip into that custom mode, and have the throttle controlled by the autopilot, based on airspeed readings, as a negative feedback PID control loop, so if airspeed input value goes down, AP increases the throttle pwm output command, or if airspeed input value goue up, then throttle pwm value is decreased by AP, based on the throttle PID tuning and sensitivity that you set, for instance to keep a set airspeed, so very similar to roll, pitch or heading stabilization, but for a different variable, and with different input variables. I might need to add in a couple of more sensor values down the road. I have seen a similar custom version of Plane running this for throttle control loop, but with a range finder as the input variable.

Does this require custom programming and modification of the Plane source code, or can it be accomplished by manipulating some settings or possibly with lua scripting? I have looked around a little, and know that in Cruise mode, a set cruise speed can be set by inputting a given cruise throttle value, then fine-tuning that throttle value until the specific cruise speed that you want is achieved, but that is not what I am looking for. I am looking to possibly create a new mode (if it is needed for this) that will take any given speed value (read it from the airspeed sensor), then adjust the throttle value in real time to hold that airspeed. Kinda similar to alt_hold mode, where the current altitude is the controlled variable, and pitch is the adjusted pwm variable, but instead for airspeed, so like a spd_hold mode.

Can anyone help me figure out how to go about this, or explain if I need to write and compile a custom version of the code, or if this can be achieved by modifying settings of the available standard version of Plane or other Ardupilot code?

Thanks for any input or assistance

How does this differ from the airspeed control provided by FBWB and CRUISE modes?

I’m not very familiar with the airspeed control in the FBWB mode, that’s partially why I was asking if this is already available or can be achieved in some way, but I know that the airspeed control in the Cruise mode won’t work, because it’s a predetermined constant airspeed based on the cruise throttle you choose in settings in advance, it’s not ANY random airspeed you choose in real-time based on the exact speed you have at the moment you flip the switch to hold that speed.
I want it to be adjustable based on the speed you are traveling at that time, just like when you flip the switch into Altitude-Hold mode, you hold that exact altitude, more or less +/- an error range, not some predetermined altitude you might have set in the settings at some earlier time. I don’t know if FBWB mode is able to do that or not though.

Also, later, I might look into adding more inputs to be factors to control the throttle pwm value to control airspeed, such as altitude reading from a range finder, or an airflow angle sensor to act as an angle of attack sensor, so it can calculate the actual airspeed vector value and not just the indicated airspeed, which is not the true airspeed any longer if the angle of attack isn’t 0 degrees, but it is a component of it, since the pitot tube won’t be pointing directly into the airflow if the angle of attack is different.

I just wanted to know if it’s possible to create your own custom control loop for one of the servo or esc outputs, such as for throttle, based on custom or other available sensor readings, if it’s doable by minor changes, if it requires a whole custom rewriting of the code, or if it can be achieved by lua scripts to be executed on the fc board if not by rewriting and compiling the whole firmware code.

Mostly it’s air or ground speed I want to control, which seems to be very easily done in the Copter code compared to Plane, but in general my question was in regard to any out servo or esc pwm stabilized output, based on custom input variables. Is it as simple as going into the code files and rewriting the calculation algorithm in the PID loop for that specific output variable to include other or custom input variables?

I’d recommend that you first read the developer wiki and take a look at the source code yourself:
https://ardupilot.org/dev/index.html
There are sections on learning the codebase and details of how to add new modes.

1 Like

Awesome, I appreciate it.

I think that’s the correct path. I spent some time earlier reading details of all the Plane modes, it seems that if you do have an airspeed sensor installed and calibrated, FBWB mode does allow adjusting the sought-after airspeed between min and max airspeed limits by manipulating the throttle input below or above 50%, respectively, and even without an airspeed sensor, it allows the throttle value to be nudged up or down, and FBWB also allows for altitude hold. However, it does not allow for adding more sensor input variables like I might need to do later, so reading the developer wiki is probably an excellent idea. I appreciate your suggestion. I’ll try reading it, and if I have any specific questions, I’ll try to ask here again later.

1 Like

I was reading the through the file radio.cpp,most of it makes sense, but there’s a part that reads “if rc failsafe is active, set all radio primary channels to the trim value and throttle to min”. The code reads

…channel_roll/pitch->get_radio_trim()

What exactly is this get_radio_trim() or “trim value”?

I thought the rc input channel values (except for throttle) are ignored once failsafe is triggered, and the mode changes to RTL, auto or just glide with throttle off? What exactly is the trim value? It’s not the servo trim that makes the AP go straight if it’s slightly off center. I searched the entire parameter list, but couldn’t find any variable named radio_trim.

Would appreciate it if a dev could shed some light on this.

Thanks