Page 1 of 4

Lua interpreter in openTx

Posted: Wed Jun 26, 2013 8:33 pm
by bertrand35
I am thinking about a lua interpreter embedded in Taranis. Well there will be some work though...

Re: Antennatracking with opentx

Posted: Wed Jun 26, 2013 9:01 pm
by Rob Thomson
Interesting. You will make Tilman happy!

Sent from my GT-I9300 using Tapatalk 2

Re: Antennatracking with opentx

Posted: Wed Jun 26, 2013 9:13 pm
by tilmanb
I was brainstorming the lua idea myself over the last few days.

Hell yea!
Should be quite easy. Except that lua needs malloc() or rather realloc()
I don't think we have that right now at all, right?
I have seen some malloc implementations that use a static byte array as a memory pool. Easy way out I guess.

The current lua version can yield in an out of lua via coroutines. That might be a good way of feeding data in and out...

Bertrand, we only use threads from that mini os right? Any plans of going to dynamic memory or so?
At least for auxiliary tasks.

Re: Sv: Antennatracking with opentx

Posted: Wed Jun 26, 2013 10:01 pm
by dvogonen
Dynamic memory management + Hard Real Time Requirements + Small Processor => Doom and Destruction

Been there, done that. Several times actually

Re: Antennatracking with opentx

Posted: Wed Jun 26, 2013 10:20 pm
by tilmanb
I don't see the connection. It's already got tasks, which should take care of the real time aspect.
I would say dynamic memory is a reliability issue if not handled right. But not a timing issue.
As long as all that fancy scripted stuff can be easily preempted. I mean lua will be nowhere near real time, I just say garbage collection. :)

Re: Antennatracking with opentx

Posted: Thu Jun 27, 2013 5:16 am
by bertrand35
Yes we already have malloc on Taranis / sky9x. But only used in DEBUG mode (printf functions family).

Re: Antennatracking with opentx

Posted: Thu Jun 27, 2013 7:33 am
by tilmanb
Oh, I thought that was only used in the sim and on s PC host.
Cool, well then I see little problems.

Re: Antennatracking with opentx

Posted: Thu Jun 27, 2013 8:01 am
by bertrand35
Do you want to give a try at integrating lua before I go deeper in this topic?

Re: Antennatracking with opentx

Posted: Thu Jun 27, 2013 2:02 pm
by tilmanb
Well, I think I will do some more testing and brainstorming about it. But I don't manage my free time very well and I'm not very well focused on the problem.
I will try some out of opentx lua integrations first. And then we should perhaps think about how we integrate this and where we put hooks.
I will have a few questions I'm sure...

Generally, I would say, let's add another task for Lua or put it into the mixer task. I suppose we don't want to risk locking up the mixer task?

As for the lua api. I don't know yet. Passing data in and out can be done in different ways, and I don't grokk it quite yet.
But I suppose we could have a input and output coroutine which yields back into C. (Yield is a bit like a pipe. Python has a similar concept)
But maybe global variables would work better...

As like grand scheme goes and what to expose to lua and how to integrate it, I don't really know.
It will do mostly what mixes do right now. I don't want to replace mixes, but I would also like to bypass them in a way that is not confusing.
I would like to do something like writing in ex_chans[], but either it should be clear that channels that are being written to either have no mix, or it is integrated with that mix in a sane way.

End goal would be to have a lua script that has a setup script that presents the user with a setup screen and a functional part that runs in the background.
The setup screen should allow very high level template like configuration.
But there are basic questions to answer first of course...

Re: Antennatracking with opentx

Posted: Thu Jun 27, 2013 6:51 pm
by bobosch
Do a stock 9x have enough memory for the lua interpreter?
thomas9x wrote:The calibration could be off-loaded to a PC app, but it would be more convenient if it was a Tx menu feature.
The calibration could be done in the MIXER menu (with Weight/Offset), if it is defined as a new Source.
And the "Set home" could be configured over a switch in the CUSTOM FUNCTIONS menu.
So no extra menu screen is needed.

Re: Antennatracking with opentx

Posted: Thu Jun 27, 2013 6:55 pm
by tilmanb
bobosch wrote:Do a stock 9x have enough memory for the lua interpreter?
No way.
Also lua integers are 16 bit AFAIK.

Re: Antennatracking with opentx

Posted: Fri Jun 28, 2013 6:17 am
by bertrand35
I have my first lua script working on Taranis :)

Re: Antennatracking with opentx

Posted: Fri Jun 28, 2013 7:25 am
by tilmanb
Oh wow. You are fast.

Re: Antennatracking with opentx

Posted: Fri Jun 28, 2013 9:56 am
by Rob Thomson
tilmanb wrote:Oh wow. You are fast.
You have no idea! Why do you think I don't code too much anymore.

Bertrand wipes my a$$ with speed and ability!

Sent from my GT-I9300 using Tapatalk 2

Re: Antennatracking with opentx

Posted: Fri Jun 28, 2013 7:36 pm
by tilmanb
Rob Thomson wrote:
tilmanb wrote:Oh wow. You are fast.
You have no idea! Why do you think I don't code too much anymore.

Bertrand wipes my a$$ with speed and ability!

Sent from my GT-I9300 using Tapatalk 2
Seriously. I was thinking in the area weeks for this project.
Well Bertrand, I will contribute scripts and other improvements. I work quite will existing code. Getting something off the ground is always a drag, though I love doing it.

Re: Antennatracking with opentx

Posted: Fri Jun 28, 2013 8:04 pm
by bertrand35
I did something quick and dirty just to check that the concept will work. And it did. Now I will create a task with a low priority for the lua interpreter. It will fills some values in an array that will be accessible through the mixer (no lua scripts will run outside this task). The main thread of the program will check the duration of the lua routine. If too long, the task will be killed with an error displayed on screen.

It will not be the definitive solution, because I see these scripts as inputs to the mixer. So at the end of the day, the values coming from the lua interpreter will be accessible on the DR/Expos screen (which will be named INPUTS).

That's what I have in mind right now. I think I need a whole week to have something ready for beta testing.

Meanwhile don't hesitate to share your scripts so that we can discuss around them and see if the concept is ok!

Bertrand.

Re: Antennatracking with opentx

Posted: Fri Jun 28, 2013 9:09 pm
by thomas9x
That's what I have in mind right now. I think I need a whole week to have something ready for beta testing.
That is amazing!

- Thomas

Re: Lua interpreter in openTx

Posted: Fri Jun 28, 2013 9:31 pm
by Kilrah
Thread split.

Yes, Bertrand is insanely efficient at integrating libraries, always impresses me!

Re: Lua interpreter in openTx

Posted: Fri Jun 28, 2013 10:30 pm
by Rob Thomson
Will the scripts just go in a folder on the sd card?

Sent from my GT-I9300 using Tapatalk 2

Re: Lua interpreter in openTx

Posted: Sat Jun 29, 2013 12:05 am
by tilmanb
Eventually certainly.
On the topic of storage. I see a need for those lua scripts to store configuration data. (Lua data structures can be serialized, kind of like json)
That will have to go on SD in some smart way add well, as I can't really see that fit in the well structured model memory blocks.

Re: Lua interpreter in openTx

Posted: Sat Jun 29, 2013 5:49 am
by bertrand35
Rob Thomson wrote:Will the scripts just go in a folder on the sd card?

Sent from my GT-I9300 using Tapatalk 2
Right. SCRIPTS folder. You will choose one script per model, the same way you select a bitmap.

Re: Lua interpreter in openTx

Posted: Sat Jun 29, 2013 5:52 am
by bertrand35
tilmanb wrote:Eventually certainly.
On the topic of storage. I see a need for those lua scripts to store configuration data. (Lua data structures can be serialized, kind of like json)
That will have to go on SD in some smart way add well, as I can't really see that fit in the well structured model memory blocks.
Right! And no for the EEPROM, I won't give an access to EEPROM read/write from the interpreter, that's too dangerous.

Re: Lua interpreter in openTx

Posted: Sat Jun 29, 2013 7:00 am
by Kilrah
bertrand35 wrote:Right. SCRIPTS folder. You will choose one script per model, the same way you select a bitmap.
That's where I would see my dedicated page:
http://openrcforums.com/forum/viewtopic ... 176#p45056

The goal being to allow using several different scripts you have downloaded on the same model without having to modify / merge them, as well as also providing a way to map the script's input/outputs without editing the script (which you can't do on the radio, remember!), and adjusting script settings. For example the example antenna tracking script will require telling it in which direction you've oriented your antenna mount, which may need to be different for every flight.

Re: Lua interpreter in openTx

Posted: Tue Jul 02, 2013 2:08 pm
by algo
I hope you considered LuaJIT instead of vanilla Lua. It's like Lua but better, a lot better. For many things like math algorithms and such it's as fast as compiled C code and it has one of the best the FFI libraries I have ever seen for accessing the native code systems. It runs on arm processors although I don't know about the one used on the Taranis specifically.

Or maybe that is what you are already using. :) I think not since you said "interpreter" though.

In fact I have been considering the possibility of developing an entire Taranis firmware based on LuaJIT.

Re: Lua interpreter in openTx

Posted: Mon Jul 08, 2013 1:13 pm
by bertrand35
I will have a look to it once the first (simple) integration is done.

Re: Lua interpreter in openTx

Posted: Mon Jul 08, 2013 1:42 pm
by algo
Just be aware there are some differences in the API between Lua 5.2 and LuaJIT because LuaJIT is more like Lua 5.1 (although it does have some 5.2 features). This could make "upgrading" to LuaJIT more difficult if you are using Lua 5.2.

Re: Lua interpreter in openTx

Posted: Wed Jul 17, 2013 7:29 am
by bertrand35
LUA interpreter is now in openTX in the branches/lua branch! You have to compile with LUA=YES. With a variable number of inputs / outputs.
I will provide some examples later today.

Re: Lua interpreter in openTx

Posted: Wed Jul 17, 2013 9:54 am
by Rob Thomson
Wow! Quite keen to see how this will work!

Sent from my GT-I9300 using Tapatalk 2

Re: Lua interpreter in openTx

Posted: Wed Jul 17, 2013 10:16 am
by ReSt
Will this also work on the standard board with a M128 ?

Reinhard

Re: Lua interpreter in openTx

Posted: Wed Jul 17, 2013 3:23 pm
by bertrand35
No! It needs more vitamines.