Celery script export from sequences and compatiblity with cs_eval()

I have been testing the export from a simple WAIT statement to get the celery script to be able to contain it with LUA via the cs_eval() function as some sequence statements are not directly drivable from LUA. My attempts to get the sequence I want failed so I went for something simple.

The celery script export from WAIT is

{
  "kind": "wait",
  "args": {
    "milliseconds": 1000
  }
}

embedding that into

cs_eval({
  "kind": "wait",
  "args": {
    "milliseconds": 1000
  }
}
)

creates this argument

Screenshot 2024-06-13 at 10.57.01

Looking at the examples in the documentation the syntax does not look the same.

Is the issue syntax, structure or something else.
I was expecting to be able to cut paste from the displayed celery script into cs_eval to obtain the same result.

Any ideas?

Please thoroughly read the documentation on the advanced Lua functions page:

I have tried that but it did not work. This was my attempt of code translation

From

{
  "kind": "wait",
  "args": {
    "milliseconds": 1000
  }
}

To

cs_eval({
  kind = "wait",
  args = {
    milliseconds = 1000
  }
})

Whilst I did not see the documentation (Now found), I modelled my code on this example which executes without issue. I have studied the syntax and cannot see any difference in the structure.

cs_eval({
  kind = "rpc_request",
  args = {
    label = "example",
    priority = 500
  },
  body = {
    {
      kind = "move_absolute",
      args = {
        location = {kind = "coordinate", args = {x = 2, y = 2, z = 2}},
        offset = {kind = "coordinate", args = {x = 0, y = 0, z = 0}},
        speed = 100
      }
    }
  }
})

As my code did not work, I asked the question from the beginning as I must be missing something so obvious. The only reason I am attempting to model WAIT is because it is so simple. I am trying to control the LED 3 and LED 4 from LUA which is only available in celery. As I could get that to work I assumed it was because I did not know something so I went for the easiest code possible - and I cannot get that to work either. LOL. I cannot see what I have missed.

I still get the same error and I am totally puzzled.

Screenshot 2024-06-13 at 10.57.01

@mvillion I didn’t try to fix your example tl;dr but here’s how to use the (simpler) rpc() Lua function to control Box Leds :hushed:

2 Likes

Thanks @jsimmonds. That worked perfectly.

rpc() worked
cs_eval() did not work

LED 3 ON

rpc({
  kind = "write_pin",
  args = {
    pin_value = 1,
    pin_mode = 0,
    pin_number = {
      kind = "named_pin",
      args = {
        pin_type = "BoxLed3",
        pin_id = -1
      }
    }
  }
})

I can now control more hardware from within LUA code. :grinning:

The documentation is a little light about the difference between them.
Even now, rereading the documentation cs_eval ‘looks’ like the right one to use, but hey ho.

@roryaronson Are you able to provide some clarity in the documentation about the usage for the two commands?

Sorry for the confusion @mvillion! The issue with this:

cs_eval(  {
    kind = "wait",
    args = {
      milliseconds = 1000
    }
  })

Is that cs_eval() is expecting an rpc_request, not the CeleryScript straight from the sequence editor. So the CeleryScript for the wait must be put into the body of an rpc_request:

cs_eval({
  kind = "rpc_request",
  args = {
    label = "example",
    priority = 500
  },
  body = {
   {
     kind = "wait",
     args = {
       milliseconds = 1000
     }
   }
  }
})

An RPC request is how commands are ultimately formed under the hood in FarmBot OS and used by the CeleryScript Virtual Machine (CSVM). Because it is a bit tedious to write out the full rpc_request syntax every time, we later created rpc() which does the wrapping for you. In taking this shortcut you forego the ability to custom set the label and priority fields of the RPC, but these are really not necessary for 3rd party developers to need to adjust. I would suggest not attempting to use these extra fields or dealing with the added syntax and just sticking with the simpler rpc() command.

I have just updated the developer documentation to point people looking at cs_eval() to use the simpler rpc() function. Hopefully it provides some more clarity.

1 Like

I love the documentation update. Thank you

2 Likes

Now my mission is to make that code use parameterization :slight_smile: