Extending X-axis/redefining plant coordinates

I two Farmbot Express 1.0, I decided to add rails (using 20mmx20mm extrusion) on top of my farm bed. I decided to extend the rails about 15cm on each end to allow the bot to cover the length of the bed. However, I have not yet extended the travel range but have the timing belts to do so.
When/if I do extend the travel, then all my (already planted) plant’s coordinates will be off in the x-coordinate by the X home displacement. (I assume I can’t define a non-zero X home.)
Since changing the coordinates of individual plants is rather tedious using the Farmbot app, I thought perhaps I could write some Python or LUA script to modify the coordinates for plants and points.
I have been looking for the FarmBot OS API but haven’t yet found the link to figure out if this is even possible. Nor do any of the documentation examples have examples that seem to cover modifying coordinates. So, I need some guidance getting started.

@dlmcshan I can help!

Here’s a link to our developer documentation.

Specifically, here is the developer documentation for the REST API.

I keep a list of example requests for every API endpoint if you a curious about the “shape” of the JSON data returned by each endpoint (or which endpoints exist). It’s a big list so you may want to use ctrl + f to search the document for keywords.

Here is what your code would need to do (in pseudo-code):

  1. Create a Web App API token
  2. Download all points (JSON) by performing a GET request to /api/points.
  3. Modify the JSON of each point (EG: point["x"] = point["x"] + 1)
  4. Update each point individually by doing a PUT request to /api/points/:ID (don’t do this too quickly as you may get rate limited by the API. Putting a 750ms sleep between requests is a good idea).

Does that make sense? I’m happy to keep explaining if something is not clear or if you hit errors along the way.

1 Like

@dlmcshan Additionally, there are some scripts from past users that might be helpful:

Hope that helps and happy to answer any other questions.

Okay, I tried this out but the “put” doesn’t seem to be putting.

Here is my “put” code and the output for the first few plants. No response text printed and no actual modification of the “x” values on the server. (This is for “dlmcshan@umich.edu” farm bot.). I must be doing something wrong but no idea what. (I am using Anaconda/Jupyter and performed the getting token and “get” points in prior code. I am using MPastedGraphic-4.tiff (369.9 KB) acBook Pro 13” M1 with Big Sur OS.)

PastedGraphic-4.tiff

Not sure why the screenshot is not showing up.
Here is the code snipet:

import time

headers = {‘Authorization’: 'Bearer ’ + TOKEN,
‘content-type’: “application/json”}
xoffset=150.

for point in points:
if point[‘pointer_type’]==‘Plant’:
print( point[‘name’] )
print( "x = “, point[‘x’],” → ", point[‘x’] + xoffset)

    data = "{\'x\': %.1f}" % (point.get("x",0) + xoffset)
    print ("Data: ", data)
    
    URL = "https://my.farmbot.io/api/points/{0}".format( point['id']) 
    print("URL=",URL)
    response = requests.put(URL, data=data, headers=headers)
    
    print("response: ",response.text)
    print(" ")
    
    time.sleep(1)

print(“Done!”)

And here is the output:

Compact Cherry Tomato
x = 1980 → 2130.0
Data: {‘x’: 2130.0}
URL= https://my.farmbot.io/api/points/413814
response:

Spinach
x = 400 → 550.0
Data: {‘x’: 550.0}
URL= https://my.farmbot.io/api/points/413853
response:

Carrot
x = 150 → 300.0
Data: {‘x’: 300.0}
URL= https://my.farmbot.io/api/points/413801
response:

@dlmcshan Before I take a deeper look, I’d like to remind you that you can’t use 'single quotes' in JSON. Can you retry, but this time, send the data in "double quotes" rather than 'single quotes'?

Example:

# This like was previous using 'single quotes',
# which is not valid JSON and will be rejected by the server.
data = "{\"x\": %.1f}" % (point.get("x",0) + xoffset)

An even better idea would be to use Python’s included JSON library. You can learn more about it here. That way, you can just call json.dumps and not worry about text encoding at all.

Okay, using the double quotes worked! Thanks!

One other question: My code was modifying just the “Plants”. I figured I would likely redefine the camera points but there are other point types defined. Are there any of these that I should modify or not modify with the X-Axis origin offset modification?

@dlmcshan There are 4 types of points:

  • “GenericPointer”
  • “Plant”
  • “ToolSlot”
  • “Weed”

You can modify them any way you would like, but I would caution against modifying meta attributes that were placed there by the web application. The web app may insert special attributes into the meta attribute and then use that data to render the web app differently. Yyou can see this in action when you work on pint grids- the web app assigns each grid a uniqe ID for grouping purposes.

X,Y,Z,Radius, etc… are all safe to modify.

If you see an attribute that you did not add, or it has a name you do not understand, it is best to not modify it. I am happy to answer questions about any specific attributes if you are curious.