Lua sequence block questions

I’m assisting a colleague with setting up a very unconventional use of Farmbot hardware for a landscape architecture-related installation, and I have a few questions about the Lua functionality in sequences.

  1. This applies to sequences in general, but if I schedule an event to run a sequence containing a Lua block and for some reason contact with the web app is interrupted (e.g. connectivity loss or server down) or a power loss causes a reboot, will the event still run? I haven’t found anything in the documentation about this, only offhand mention on this forum.

  2. We are looking at using move_absolute() to move the machine through a long series of coordinates, but there’s no mention of the speed at which those motions will run. Is there any way to set the speed of a move from a Lua block? Or use the variance feature now available on a regular Move block?

  3. I’ve seen suggested on this forum that developers wanting additional functionality should look at adding it to the Lua sandbox themselves. I assume this would mean forking FBOS and writing Elixir code? Where in the code base would I need to look if I were interested in attempting this?

Thanks in advance for your help!

1 Like

I might be able to answer some of my own questions here after just a bit more digging.

Is there any way to set the speed of a move from a Lua block?

I found the following spot in the code that suggests that a speed argument to move_absolute() is already implemented and available in 14.2.2. Is that correct?

https://github.com/FarmBot/farmbot_os/blob/v14.2.2/farmbot_os/lib/farmbot_os/lua/ext/firmware.ex

I assume this also answers #3 in my original post?

@Deisengard

I’m assisting a colleague with setting up a very unconventional use of Farmbot hardware.

We appreciate hearing about these sorts of projects! We enjoy seeing pictures of unique FarmBot installations if you are at liberty to share with us.

if I schedule an event to run a sequence containing a Lua block and for some reason contact with the web app is interrupted (e.g. connectivity loss or server down) or a power loss causes a reboot, will the event still run?

The answer is “Yes”, but as you might have seen in that forum post you linked, it is not a design goal of FBOS to work fully offline. We never let our bots go more than a few days without internet and it is unlikely that we will fix issues relating to internet loss of more than a few days. That’s not to say it can’t be done, rather we don’t have that goal in mind for the project. If you have a connection that has a 60% uptime throughout the day, plus a WiFi signal strength above 70% as reported in the app, you should be able to carry out most usecases.

We are looking at using move_absolute() to move the machine through a long series of coordinates, but there’s no mention of the speed at which those motions will run.

It looks like you found the answer to this question already, but please let me know if you need more help. One tip I would like to add is that if you are doing very slow movements you may need to increase your movement timeout threshold in the device settings page.

I assume this would mean forking FBOS and writing Elixir code?

Correct. You would need to modify this file. This will be a very involved task if you have not worked with Elixir in the past, but I would be happy to answer any questions you may have. We have added new Lua features to the sandbox upon request in the past. Adding a feature to the Lua sandbox usually does not take long for us. The only caveat I would add here is that the sandbox must remain sandboxed, so we can’t add features that would create a security risk for end users or allow users to accidentally brick their devices. For example, we do not have plans to give shell access to the Lua sandbox.

I found the following spot in the code that suggests that a speed argument to move_absolute() is already implemented and available in 14.2.2. Is that correct?

That is correct! The full Lua API docs can be found here: Lua | FarmBot Software Development Documentation

As you explore the source code, you may notice that there are some undocumented features. Please let us know if and how you use these features if you chose to use them, since undocumented features are sometimes used internally and changed without notice. Conversely, we try our best to not apply breaking changes to any Lua functions that have been publicly announced and documented.

One last note: Since it sounds like you have a technical background, you may want to take a look at the developer documentation. I have written the guide in a “handbook” format, meaning that the documentation can be read from start to finish. It explains many aspects of the system and also provides historical context about certain features (such as the sequence editor).

Please let us know if you require further assistance. I hope you can share more information about this intriguing project!

1 Like

Thank you @RickCarlino and @Marc for your responses! I do have a developer background (though not with Erlang or Elixir) and I know from experience that these things can get complicated.

In order to keep things as simple as possible (and to leverage the maturity of your code base) I was hoping to work with the web app as much as possible rather than write my own scheduling logic, etc. Hence the questions about events, sequences and the Lua implementation.

My plan has been to make use of the HTTP request support in Lua and provide an endpoint (either on a local device or in the cloud) that it can query for a new list of coordinates (and other variables) on a regular basis through repeating events.

Of course providing the endpoint would require a its own code for feeding coordinates to the Farmbot, and maybe it makes sense to skip the web app and just use the APIs as Marc suggested in his PM to me.

Would there be gotchas or drawbacks to the web app approach that would suggest a cloud-hosted remote control approach would be better?

Thanks again for your help!

@Deisengard

This is actually the most optimal solution that I recommend to users:

  • No need to merge upstream changes- custom code stays in one codebase, all other code is maintained by FarmBot (will not complicate the firmware update process).
  • No need to understand FarmBot internals or deal with merge conflicts.
  • No need to run a self-hosted web app or burn custom firmware updates.

It’s hard to say without knowing your use case well.

Hosting in the cloud:

  • Less setup required than hosting on the LAN, since most cloud providers can provision a base OS, handle automated backups, etc…
  • Less resilient to network outages. You will need to have a good error handler on the Lua side to deal with this (not impossible, but does add some extra steps).

Hosting locally:

  • Cheaper, since you can host the whole thing on an inexpensive old computer or raspberry pi (no hosting fees).
  • (Assuming the server has a read/write data partition) You will need to provision the base OS and figure out a solution for data backups.

The bottom line is, hosting a custom web server and communicating with it via Lua is one of the easiest ways to do custom automation. The decision to host on the LAN vs. cloud will depend heavily on the use case. We’re happy to answer any other questions you may have.

1 Like

Thanks again for your feedback! It sounds like we’re on a reasonable path. I’ll be happy to share as we work out the details!

I want to test the events, sequences and Lua code without being connected to our actual machine, so I am working on setting up a test platform for this using an RPi3 and Arduino Mega as described elsewhere on this forum. I’ve got it connected to the web app. When it comes to setup, can I answer yes to all the motor/movement related questions in the wizard and expect to be able to simulate the operation of the machine in a meaningful way? Right now if I attempt to jog the gantry it always jumps back home after animating the movement. Are there any steps I need to take for the web app to think the movements have completed? Or am I missing something?

EDIT: I think maybe I’ll answer my own question again. Looks like turning off the encoders did the trick.

1 Like

Yes! That is how you fix the problem.

I am glad that you’ve got everything up and running.

If you see any places where we can improve the Lua sandbox, please let me know. Lua additions are often very quick to add and we are open to feature requests.

The main issue I’m encountering with the Lua sandbox is the 1500 character limit on the Lua sequence blocks. Can that be increased? While I can get around the limit to an extent by breaking things up and using env() to store/pass things along, this is not very elegant. It would be nice if there were a way to scope a given variable at the sequence-level for these blocks such that a variable or function defined in one block is available in the next within a given sequence. Would that be a possibility? Is there another way around this that I’m missing?

Or could there be a feature allowing us to build our own function library that would/could be imported at the beginning of every Lua sequence block? If the 1500 character limit is an RPC/CeleryScript-related limitation, maybe the functions would still have to be subject to it, but as long as they could be included in a sequence block that would be manageable.

@Deisengard You bring up some very good ideas. In the interim, I can increase the limit (I hope to deploy this in the next few days), but I do like the idea you have of maintaining Lua state across runs for the same sequence. I will need to talk to the team about this to make sure there aren’t any issues with doing it that way, but it could be very useful.

Alternatively, in my own use of the FarmBot, I have considered the idea of having a “function library” of sorts where there is a Lua source file that runs before all sequences and which can be used to define shared functionality.

Thanks for your response @RickCarlino and for considering my suggestions.

Is the character limit just imposed on the web app end? In other words, could I use the API to modify my sequence and upload longer chunks of code before your changes to the web app are pushed out?

@Deisengard It is imposed at the column level of the database, as I remember, so probably not. Do you use FarmBotJS or FarmBotPy by chance? You could possibly work around the limitation using those libraries if so. Let me know if that is the case and I can write a code sample for you.

@Deisengard Quick update:

  • Rory and I had a discussion about your idea for maintaining scope between Lua block runs. We will try to implement this in v14.3.1.
  • I am deploying a change to the web app that will extend the Lua code limit to 3,000 characters. Hopefully, this helps until v14.3.1 is released. We have not updated the UI yet, so you will see warnings when you exceed 1,500 characters, but they can be ignored. The true limit will be 3,000 (we will update the UI in an upcoming release)
2 Likes

Awesome, thank you so much! I will give it a try this afternoon.

1 Like

@RickCarlino Running into what might be a bug with the JSON support. The following causes an error despite the JSON being valid:

result = json.decode("[[ 0.148, 0.302 ], [ -10, 10 ]]")

Am I missing something? Is array-only JSON not supported?

@Deisengard That is indeed a bug. I’ve been tracking it for a while but have not started working on a fix yet. The temporary workaround is to wrap top-level JSON arrays in an object. Example:

Instead of:

[[ 0.148, 0.302 ], [ -10, 10 ]]

send:

{
  "data": [[ 0.148, 0.302 ], [ -10, 10 ]]
}

Hope that helps in the mean time. Please let me know.

@Deisengard Actually, I might be able to get this in to the next RC on the beta channel. I will let you know when a fix is ready.

Awesome, thanks!

@Deisengard Did the fix from yesterday help?

Yes, it works on my test environment; however, I don’t know if we want to run the beta on the actual machine. I will proceed with the workaround you provided for now.

1 Like