RPM:get_rpm lua script - stuck with attemp to index nil value (global rpm)

Not sure if this is a rover or scripting questions :slight_smile: but hoping for a little nudge or pointer.
I have the hall effect sensor all working nicely, and RPM1 is populated on the Quickscreen - so thats all good. The plan is - that when the (very slow) mower hits long grass, we notice the rpm’s drop - so Slow down until they recover. This is very similar to Yuri’s rough terrain script (thanks) which i’ll use to build this out. Other scripts are running fine also.

My problem - how to get the rpm:get_rpm(instance) values sent back to the GCS / mission planner.

Once i can reference the values from the script - i can bash my way through lua and not mow the rest of the lawns with the petrol motor stalled out…

I found a working snipped and added my rpm:get_rpm() into it - but its complaining.

21/12/2023 8:59:59 pm : to index a nil value (global ‘rpm’)
21/12/2023 8:59:59 pm : Lua: /APM/scripts/active_source_set.lua:7: attempt
21/12/2023 8:59:59 pm : current source set in use :0

> function update() -- this is the loop which periodically runs
>   gcs:send_text(0, string.format("current source  set in use :%d", ahrs:get_posvelyaw_source_set()))
>   gcs:send_text(0, string.format("rpm(1)                     :%d", rpm:get_rpm(1)))
>   return update, 1000 -- 1000ms reschedules the loop (1Hz)
> end
> return update() -- run immediately before starting to reschedule

update: I’ve moved from arducopter 4.4 to the latest ardurover 4.4 beta with the same results. Im getting similar errors copy/pasting some of the examples without mod. Bit stuck help please !

Capitalization matters. The binding you are attempting to use is RPM.

Also, if you only have one sensor, the instance index is most likely 0, not 1.

Thanks - that made the difference, that also the RPM:get_rpm(0) was returning nil when no rpms were detected (the quick panel shows -1) Without doing a check for nil resulted in the script failing. The following script now works fine - much appreciated and a step along my learning curve

function update() -- this is the loop which periodically runs
  local revs=RPM:get_rpm(0)
  if revs == nil 
  then
     revs=0
  end
  gcs:send_text(0, revs)
    return update, 1000 -- 1000ms reschedules the loop (1Hz)
  end
return update() -- run immediately before starting to reschedule

Instances are zero indexed. Users tend to hate that, so AP adds one to the UI displayed text.

The following code should implement the same nil check in fewer statements (and decrease the severity of your debug message):

function update() -- this is the loop which periodically runs
  local revs = RPM:get_rpm(0) or 0
  gcs:send_text(6, revs)
  return update, 1000 -- 1000ms reschedules the loop (1Hz)
end
return update() -- run immediately before starting to reschedule