Dear jsimmondds,
Thank you for your guidance.
I wanted to share an update on the code I am working on. It reads all the plant points and then samples the soil moisture one by one. To do this, I move the robot arm using Python code. I have also attached the implemented sequence; it’s quite simple.
To get the position, I decided to use local_position = fb.read_status(“location_data.position”), as it is the most stable method. The fb.move command is working well.
However, the soil sampling (see def playSequence(sequenceType, fb, plant)) works perfectly the first time but crashes during the second attempt. Specifically, the second time the program executes the sampling command soilSensor_value = fb.read_status(‘pins’)[‘59’][‘value’], it fails.
It’s worth mentioning that when I use debugging mode and manually launch the sampling command twice, it works on the second attempt.
I suspect that the issue is related to MQTT, and I am unsure if it can be resolved. Perhaps I need to introduce a delay or a different approach.
I believe that CeleryScript is not necessary at this stage. Do you agree? Otherwise, I am open to studying it further.
If you are interested, I can keep you updated and share my code for further review.
My ultimate goal is to integrate this code into ROS2.
import json
import time
import math
from tabnanny import check
import requests
import numpy as np
from PIL import Image
from io import BytesIO
import matplotlib.pyplot as plt
from farmbot import Farmbot
def playSequence(sequenceType, fb, plant):
if sequenceType == 'soilSensor':
fb.move(plant['x'], plant['y'], -375)
time.sleep(3)
while True:
try:
fb.sequence("Soil Sensor Stef")
break # Esce dal ciclo se l'input è valido
print("OK WITH Soil Sensor Stef")
except ValueError:
print("ERROR WITH Soil Sensor Stef")
time.sleep(10)
attempt=0
while True:
#fb.listen_for_status_changes(duration=2)
time.sleep(10)
attempt+=1
print("sensor_value Attempt", attempt)
try:
fb.clear_cache()
time.sleep(10)
soilSensor_value=fb.read_status('pins')['59']['value']
print("sensor_value measure, fb.read_status('pins')['59']['value']")
break
except ValueError:
print("ERROR WITH fb.read_status('pins')['59']['value']")
fb.toast("fb.read_status('pins')['59']['value']", message_type="success")
print('soil measure: ', soilSensor_value)
return soilSensor_value
# Initialize the FarmBot class
email = "..."
password = "..."
fb = Farmbot()
token = fb.get_token(email, password)
fb.clear_cache()
fb.set_verbosity(0)
fb.connect_broker()
security_height=-270
fb.listen_for_status_changes(stop_count=3)
points = fb.api_get("points")
# Filter out only the plants
plants = [point for point in points if point["pointer_type"] == "Plant"]
sequenceType = 'soilSensor'
local_position = fb.read_status("location_data.position")
print('Local Position: ', local_position)
print('move to security_height: fb.move(',local_position['x'],',',local_position['y'],',',security_height,')')
fb.move(local_position['x'], local_position['y'], security_height)
fb.listen_for_status_changes(duration=5)
time.sleep(2)
soil_measures = []
i = 0
for plant in plants:
fb.wait(1000)
time.sleep(1)
print('move above the plant: fb.move(', plant['x'], ',', plant['y'], ',', security_height, ')')
fb.toast("move above the plant", message_type="success")
fb.move(plant['x'], plant['y'], security_height)
time.sleep(5)
soil_measures.append(playSequence(sequenceType, fb, plant))
print('soil measure success, move above the plant: fb.move(', plant['x'], ',', plant['y'], ',', security_height, ')')
fb.toast("soil measure success, move above the plant", message_type="success")
fb.move(plant['x'], plant['y'], security_height)
time.sleep(5)
i += 1
fb.sequence("Dismount Tool")
This is the error:
Traceback (most recent call last):
File "F:\pyCharm\FarmBot\farmbot_plantsSoilMeasures.py", line 83, in <module>
soil_measures.append(playSequence(sequenceType, fb, plant))
File "F:\pyCharm\FarmBot\farmbot_plantsSoilMeasures.py", line 34, in playSequence
soilSensor_value=fb.read_status('pins')['59']['value']
File "F:\pyCharm\FarmBot\.venv\lib\site-packages\farmbot\main.py", line 180, in read_status
return self.info.read_status(path)
File "F:\pyCharm\FarmBot\.venv\lib\site-packages\farmbot\functions\information.py", line 169, in read_status
status_tree = status_tree[key]
TypeError: 'NoneType' object is not subscriptable