Lua script for automatic trims
Lua script for automatic trims
Hi,
New to Lua scripting, so sorry in advance if the answer is trivial...
I have a cheap quad-copter that needs to be trimmed at the start of every flight. I want to write the following script to auto-trim it: A momentary switch is held down for a duration of time, and during this time the pilot tries to keep the quad-copter more or less in the same position. When the switch is pressed, the script averages-out (integrates) the four stick positions. When the switch is released, the trims are set to the corresponding averages.
I figured out how to read the stick positions from the snake script (not directly documented in the 2.2 Lua reference guide, as far as I could tell). However, I could not figure out how to set the trim positions. Please help...
New to Lua scripting, so sorry in advance if the answer is trivial...
I have a cheap quad-copter that needs to be trimmed at the start of every flight. I want to write the following script to auto-trim it: A momentary switch is held down for a duration of time, and during this time the pilot tries to keep the quad-copter more or less in the same position. When the switch is pressed, the script averages-out (integrates) the four stick positions. When the switch is released, the trims are set to the corresponding averages.
I figured out how to read the stick positions from the snake script (not directly documented in the 2.2 Lua reference guide, as far as I could tell). However, I could not figure out how to set the trim positions. Please help...
- MikeB
- 9x Developer
- Posts: 17993
- Joined: Tue Dec 27, 2011 1:24 pm
- Country: -
- Location: Poole, Dorset, UK
Re: Lua script for automatic trims
Have you tried the special function "Instant trim"?
Mike
Mike
erskyTx/er9x developer
The difficult we do immediately,
The impossible takes a little longer!
The difficult we do immediately,
The impossible takes a little longer!
Re: Lua script for automatic trims
I did once. It was too instantaneous, and I couldn't coordinate the exact position with reaching for the button to activate instant trim. Averaging over a duration seems better, but of course I would have to try it in order to be sure.
At any rate, thanks for the suggestion!
At any rate, thanks for the suggestion!
-
- Posts: 750
- Joined: Tue Dec 27, 2011 11:22 pm
- Country: United States
- Location: Carson City, Nv
Re: Lua script for automatic trims
You could average the sticks and set the PPM Centers or Subtrim(offset) in OUTUTS to possibly achieve the capability you desire via Lua.
The Trims are stored in Flight Modes and are not available to Lua as best I can determine.
The Trims are stored in Flight Modes and are not available to Lua as best I can determine.
Dean
OldDmbThms: 1. Takeoff, 2. Crash, 3. Repair, GOTO 1
OldDmbThms: 1. Takeoff, 2. Crash, 3. Repair, GOTO 1
-
- Posts: 1844
- Joined: Tue Dec 27, 2011 8:22 pm
- Country: -
- Location: Ottawa
Re: Lua script for automatic trims
Instant Trim works well and doesn’t require critical timing. It just requires you to hold the stick(s) so the models flies level and then throw the switch. It’s easiest with a fixed wing model flown in mode 2 because mainly you’re focused on the right stick.
With other modes and types of model, the choice of the Instant trim switch becomes a major consideration, as you have to be able to press/flip it while holding the stick(s) in level flight position.
I’ve used it with both mode 1 and mode 2 transmitters, but not with a multirotor.
With other modes and types of model, the choice of the Instant trim switch becomes a major consideration, as you have to be able to press/flip it while holding the stick(s) in level flight position.
I’ve used it with both mode 1 and mode 2 transmitters, but not with a multirotor.
Re: Lua script for automatic trims
I have installed 4 push buttons on the back of my radios. Easy peasy for me, even with rotor models
But only useful to thumb flyers like me
João
But only useful to thumb flyers like me
João
My er9x/Ersky9x/eepskye Video Tutorials
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9
Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9
Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
Re: Lua script for automatic trims
Hi all, and thanks for the input!
I don't quite understand the comment by rdeanchurch: I couldn't find OUTUTS in the Lua manual. At any rate, I would really like to set the trims, and not implement a workaround. For now, I think I will probably average out the sticks, and then have the trim settings voiced out. That is "Aileron: two ticks left", etc. I'll see how this compares with instant trims, and report back.
I don't quite understand the comment by rdeanchurch: I couldn't find OUTUTS in the Lua manual. At any rate, I would really like to set the trims, and not implement a workaround. For now, I think I will probably average out the sticks, and then have the trim settings voiced out. That is "Aileron: two ticks left", etc. I'll see how this compares with instant trims, and report back.
-
- Posts: 750
- Joined: Tue Dec 27, 2011 11:22 pm
- Country: United States
- Location: Carson City, Nv
Re: Lua script for automatic trims
OUTPUTS is the name of the page where you set the servo limits on the transmitter, including servo center and sub-trim.
Dean
OldDmbThms: 1. Takeoff, 2. Crash, 3. Repair, GOTO 1
OldDmbThms: 1. Takeoff, 2. Crash, 3. Repair, GOTO 1
- MikeB
- 9x Developer
- Posts: 17993
- Joined: Tue Dec 27, 2011 1:24 pm
- Country: -
- Location: Poole, Dorset, UK
Re: Lua script for automatic trims
The switch for the "instant trim" could be a logical switch, rather than a physical switch.
You could try something like trigger a logical switch when you open the throttle to take off, include a delay in the logical switch so you have time to hold the quad-copter level, then the "instant trim" will occur automatically.
Mike
You could try something like trigger a logical switch when you open the throttle to take off, include a delay in the logical switch so you have time to hold the quad-copter level, then the "instant trim" will occur automatically.
Mike
erskyTx/er9x developer
The difficult we do immediately,
The impossible takes a little longer!
The difficult we do immediately,
The impossible takes a little longer!
Re: Lua script for automatic trims
Mike, isn't the instant trim immediately saved to the Sub-Trims? wouldn't that bring some problems after doing it a few times?
João
João
My er9x/Ersky9x/eepskye Video Tutorials
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9
Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9
Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
Re: Lua script for automatic trims
Not in OpenTX at least.
Re: Lua script for automatic trims
It's been a while since last time I've used instant trim. I remember that at least one of them saved it to the sub trims back then, but I don't remember which fw did that..
João
João
My er9x/Ersky9x/eepskye Video Tutorials
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9
Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9
Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
- MikeB
- 9x Developer
- Posts: 17993
- Joined: Tue Dec 27, 2011 1:24 pm
- Country: -
- Location: Poole, Dorset, UK
Re: Lua script for automatic trims
No problems doing it over and over, it takes the current channel output stores any offset to the sub-trim and sets the trim to zero. You need to release the stick back to centre.
Mike
Mike
erskyTx/er9x developer
The difficult we do immediately,
The impossible takes a little longer!
The difficult we do immediately,
The impossible takes a little longer!
Re: Lua script for automatic trims
Yes, but some flight controllers will take the radio channel positions as neutral at startup, which I think seems to be the case with his multirotor. That means that next time he starts his model, he will have to trim it again, and the radio will add the new trim value to the sub-trim, which was already added in the previous flight.. So, this won't work anymore after a few flights.
What he needs, is something that resets the trims (or sub-trims) to zero after each flight.. This is what I understand it is requested.
But if the trims wouldn't be copied immediately to sub-trims (which I think is the correct way to do it), then, all he has to do is set the trims to center after each flight, and then use the instatrim again on the next flight.
Another option (just thinking loud here) would be the instatrim values being saved to the trims, and then a popup screen coming up asking if we wanted to save the values to the sub-trims, or discard them, in which case the trims would be reset to the previous values, and if saved to the subtrims, the trims would be reset back to center.. And this could even be done in flight without looking at the radio's screen. MENU SHORT to confirm and EXIT SHORT to discard the values... of course, one could just do it after landing.
The other problem, is that the OP wants to achieve all this automatically using a script
João
What he needs, is something that resets the trims (or sub-trims) to zero after each flight.. This is what I understand it is requested.
But if the trims wouldn't be copied immediately to sub-trims (which I think is the correct way to do it), then, all he has to do is set the trims to center after each flight, and then use the instatrim again on the next flight.
Another option (just thinking loud here) would be the instatrim values being saved to the trims, and then a popup screen coming up asking if we wanted to save the values to the sub-trims, or discard them, in which case the trims would be reset to the previous values, and if saved to the subtrims, the trims would be reset back to center.. And this could even be done in flight without looking at the radio's screen. MENU SHORT to confirm and EXIT SHORT to discard the values... of course, one could just do it after landing.
The other problem, is that the OP wants to achieve all this automatically using a script
João
My er9x/Ersky9x/eepskye Video Tutorials
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9
Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9
Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
- MikeB
- 9x Developer
- Posts: 17993
- Joined: Tue Dec 27, 2011 1:24 pm
- Country: -
- Location: Poole, Dorset, UK
Re: Lua script for automatic trims
The OP said his quad-copter needs to be trimmed at the start of every flight.
Note that I said the instant trim function (of ersky9x) STORES the result in the sub-trim, it does not add it in. THis may be done as many times as required.
I recall changing this at some time, it used to add to the sub-trim, now it stores.
Mike
Note that I said the instant trim function (of ersky9x) STORES the result in the sub-trim, it does not add it in. THis may be done as many times as required.
I recall changing this at some time, it used to add to the sub-trim, now it stores.
Mike
erskyTx/er9x developer
The difficult we do immediately,
The impossible takes a little longer!
The difficult we do immediately,
The impossible takes a little longer!
Re: Lua script for automatic trims
Perhaps I am not understanding, sorry Mike..
You mean it is stored and then cleared if we turn the radio OFF or change models, without selecting the option "COPY TRIM" in the Limits menu??
Thanks
João
You mean it is stored and then cleared if we turn the radio OFF or change models, without selecting the option "COPY TRIM" in the Limits menu??
Thanks
João
My er9x/Ersky9x/eepskye Video Tutorials
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9
Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9
Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
- MikeB
- 9x Developer
- Posts: 17993
- Joined: Tue Dec 27, 2011 1:24 pm
- Country: -
- Location: Poole, Dorset, UK
Re: Lua script for automatic trims
On ersky9x, the "instant trim" takes the current channels output, including any stick, trim and sub-trim values, sets the sub-trim to this value, and zeroes the trim. The sub-trim is therefore saved as part of the model, and keeps this new value, for the selected model.
You don't need to use the "COPY TRIM" for this. Instant trim sets the sub-trim as this goes from -100% to +100%, while a trim only goes from -25% to +25% of the full channel output.
Mike
You don't need to use the "COPY TRIM" for this. Instant trim sets the sub-trim as this goes from -100% to +100%, while a trim only goes from -25% to +25% of the full channel output.
Mike
erskyTx/er9x developer
The difficult we do immediately,
The impossible takes a little longer!
The difficult we do immediately,
The impossible takes a little longer!
Re: Lua script for automatic trims
Yes, but as I said above, this won't work with some flight controllers that read what the radio is outputting on every channel at startup and take those values as center. if the instatrim saved the values from the previous flight, the model will read it as center the next flight (startup), meaning that it won't be trimmed anymore.. I believe this is what the OP is saying.. He can of course let us know if this is what happens..
João
João
My er9x/Ersky9x/eepskye Video Tutorials
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9
Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9
Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
- MikeB
- 9x Developer
- Posts: 17993
- Joined: Tue Dec 27, 2011 1:24 pm
- Country: -
- Location: Poole, Dorset, UK
Re: Lua script for automatic trims
Well if the FC needs the sub-trim values in normal operation, then you need to force the Tx outputs to 0% while the FC powers up. This may be done using safety switches, these override the sub-trim.
Mike
Mike
erskyTx/er9x developer
The difficult we do immediately,
The impossible takes a little longer!
The difficult we do immediately,
The impossible takes a little longer!
Re: Lua script for automatic trims
Good idea Mike He could have the safety switches forcing the channels to 0%, and then releasing the channels to the values with the subtrims already added when the throttle cut switch is moved.
Forgot the name of the safety switches in OpenTX Something with "functions" I think..
João
Forgot the name of the safety switches in OpenTX Something with "functions" I think..
João
My er9x/Ersky9x/eepskye Video Tutorials
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9
Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9
Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
Re: Lua script for automatic trims
"Override" Special Function.
As a reference instant trim works very differently on OpenTX, it only adjusts the trims. The first press only allows up to the normal max trim value (25%). If that was still not enough then a 2nd press will allow for 100%.
Transferring trims to subtrims and zeroing trims is an operation you do after flying on the OUTPUTS page.
As a reference instant trim works very differently on OpenTX, it only adjusts the trims. The first press only allows up to the normal max trim value (25%). If that was still not enough then a 2nd press will allow for 100%.
Transferring trims to subtrims and zeroing trims is an operation you do after flying on the OUTPUTS page.
Re: Lua script for automatic trims
I quite like the idea of saving the trim values after. If we are not happy with the result and want to do it again, we just have to reset the trims. If the value is immediately added to the subtrims, we don't know what values were there before. Remember that we used them to center servos when setting up the model. We cannot just zero them without having to configure the model completely once again
I am not sure about letting the trims go to 100% though. If you need more than 25% something might be very wrong with your model..
Just my 2c of course..
I am not sure about letting the trims go to 100% though. If you need more than 25% something might be very wrong with your model..
Just my 2c of course..
My er9x/Ersky9x/eepskye Video Tutorials
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9
Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9
Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
Re: Lua script for automatic trims
Not convinced either but it's been that way forever (looked into it recently following someone's question) and has never been brought up as a problem. As Mike said ersky9x allows 100% directly, we changed that at the very beginning to requiring a double press (a trim already has to be at max for a subsequent instant trim operation to allow going beyond) to somewhat mitigate the risk of a false manipulation giving you an unflyable model. Still possible, but a bit less easily.
Re: Lua script for automatic trims
Yeah, one just have to be careful then.
João
João
My er9x/Ersky9x/eepskye Video Tutorials
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9
Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9
Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW