def move_and_capture(self): """Move the bot along x and y axes, take photos, and detect circles.""" for i, move in enumerate(RELATIVE_MOVEMENTS): if i > 0: log('Moving to next camera calibration photo location.', message_type='info', title='camera-calibration') if USE_FARMWARE_TOOLS: device.move_relative(move['x'], move['y'], move['z'], 100) else: CeleryPy.move_relative((move['x'], move['y'], move['z']), speed=100) log('Taking camera calibration photo. ({}/3)'.format(i + 1), message_type='info', title='camera-calibration') img_filename = self.capture() if USE_FARMWARE_TOOLS: coordinates = device.get_current_position() for axis, coordinate in coordinates.items(): coordinates[axis] = float(coordinate) else: coordinates = {'z': 0} img = cv2.imread(img_filename, 1) os.remove(img_filename) ret, centers = self.find_pattern(img) if not self.success_flag: self.save_image(img, str(i + 1)) return self.success_flag self.dot_images[i]['circles'] = centers self.dot_images[i]['found'] = ret self.dot_images[i]['image'] = img self.dot_images[i]['coordinates'] = coordinates return self.success_flag
def _api_info(api): """API requests setup.""" api_info = {} if api == 'app': try: api_info['token'] = os.environ['API_TOKEN'] except KeyError: api_info['token'] = 'x.{}.x'.format( 'eyJpc3MiOiAiLy9zdGFnaW5nLmZhcm1ib3QuaW86NDQzIn0') try: encoded_payload = api_info['token'].split('.')[1] encoded_payload += '=' * (4 - len(encoded_payload) % 4) json_payload = base64.b64decode(encoded_payload).decode( 'utf-8') server = json.loads(json_payload)['iss'] except: # noqa pylint:disable=W0702 server = '//my.farmbot.io:443' api_info['url'] = 'http{}:{}/api/'.format( 's' if ':443' in server else '', server) elif api == 'farmware': try: api_info['token'] = os.environ['FARMWARE_TOKEN'] except KeyError: api_info['token'] = 'NA' try: os.environ['FARMWARE_URL'] except KeyError: api_info['url'] = 'NA' else: api_info['url'] = CeleryPy.farmware_api_url() api_info['headers'] = { 'Authorization': 'Bearer {}'.format(api_info['token']), 'content-type': "application/json" } return api_info
def log(message, message_type='info', title='plant-detection'): """Send a log message with a title prefix.""" log_message = '[{title}] {message}'.format(title=title, message=message) if USE_FARMWARE_TOOLS: device.log(log_message, message_type) else: print(CeleryPy.send_message(log_message, message_type))
def save(name, value, its_json=True): """Save an environment variable to env and, if available, redis.""" if its_json: value = json.dumps(value) unsent_cs = CeleryPy.set_user_env(name, value) os.environ[name] = value return unsent_cs
def upload_plants(self): """Add plants to FarmBot Web App Farm Designer.""" point_ids = [] for plant in self.plants['remove']: point_ids = self.upload_point(plant, 'Weed', point_ids) for plant in self.plants['save']: point_ids = self.upload_point(plant, 'Detected Plant', point_ids) # for plant in self.plants['known']: # point_ids = self.upload_point(plant, 'Known Plant', point_ids) for plant in self.plants['safe_remove']: point_ids = self.upload_point(plant, 'Safe-Remove Weed', point_ids) self.api_response_error_printer() if point_ids: # Points have been added to the web app # Indicate that a sync is required for the points CeleryPy.data_update('points', point_ids)
def output_celery_script(self): """Output JSON with identified plant coordinates and radii.""" unsent_cs = [] # Encode to CS for mark in self.plants['remove']: plant_x, plant_y = round(mark['x'], 2), round(mark['y'], 2) plant_r = round(mark['radius'], 2) unsent = CeleryPy.add_point(plant_x, plant_y, 0, plant_r) unsent_cs.append(unsent) return unsent_cs
def log(message, message_type='info', title='plant-detection', channels=None, no_prefix=False): """Send a log message with a title prefix.""" log_message = '[{title}] {message}'.format(title=title, message=message) if no_prefix: log_message = message if USE_FARMWARE_TOOLS: device.log(log_message, message_type, channels) else: print(CeleryPy.send_message(log_message, message_type, channels))
def _move(amount): if USE_FARMWARE_TOOLS: device.move_relative(amount['x'], amount['y'], amount['z'], 100) else: CeleryPy.move_relative((amount['x'], amount['y'], amount['z']), speed=100)
def setUp(self): # add_point self.add_point = CeleryPy.add_point( point_x=1, point_y=2, point_z=3, point_r=4) self.add_point_static = { "kind": "add_point", "args": { "radius": 4, "location": { "kind": "coordinate", "args": { "x": 1, "y": 2, "z": 3 } } }, "body": [ { "kind": "pair", "args": { "label": "created_by", "value": "plant-detection" } } ] } # set_user_env self.set_env_var = CeleryPy.set_user_env( label='PLANT_DETECTION_options', value=json.dumps({"in": "puts"})) self.set_env_var_static = { "kind": "set_user_env", "args": {}, "body": [ { "kind": "pair", "args": { "label": "PLANT_DETECTION_options", "value": "{\"in\": \"puts\"}" } } ] } # move_absolute: coordinate self.move_absolute_coordinate = CeleryPy.move_absolute( location=[10, 20, 30], offset=[40, 50, 60], speed=800) self.move_absolute_coordinate_static = { "kind": "move_absolute", "args": { "location": { "kind": "coordinate", "args": { "x": 10, "y": 20, "z": 30 } }, "offset": { "kind": "coordinate", "args": { "x": 40, "y": 50, "z": 60 } }, "speed": 800 } } # move_absolute: tool self.move_absolute_tool = CeleryPy.move_absolute( location=['tool', 1], offset=[40, 50, 60], speed=800) self.move_absolute_tool_static = { "kind": "move_absolute", "args": { "location": { "kind": "tool", "args": { "tool_id": 1 } }, "offset": { "kind": "coordinate", "args": { "x": 40, "y": 50, "z": 60 } }, "speed": 800 } } # move_absolute: plant self.move_absolute_plant = CeleryPy.move_absolute( location=['Plant', 1], offset=[40, 50, 60], speed=800) self.move_absolute_plant_static = { "kind": "move_absolute", "args": { "location": { "kind": "point", "args": { "pointer_type": "Plant", "pointer_id": 1 } }, "offset": { "kind": "coordinate", "args": { "x": 40, "y": 50, "z": 60 } }, "speed": 800 } } # move_absolute: point self.move_absolute_point = CeleryPy.move_absolute( location=['GenericPointer', 1], offset=[40, 50, 60], speed=800) self.move_absolute_point_static = { "kind": "move_absolute", "args": { "location": { "kind": "point", "args": { "pointer_type": "GenericPointer", "pointer_id": 1 } }, "offset": { "kind": "coordinate", "args": { "x": 40, "y": 50, "z": 60 } }, "speed": 800 } } # move_relative self.move_relative = CeleryPy.move_relative( distance=(100, 200, 300), speed=800) self.move_relative_static = { "kind": "move_relative", "args": { "x": 100, "y": 200, "z": 300, "speed": 800 } } # data_update: all self.data_update = CeleryPy.data_update(endpoint='points') self.data_update_static = { "kind": "data_update", "args": { "value": "update" }, "body": [ { "kind": "pair", "args": { "label": "points", "value": "*" } } ] } # data_update: one self.data_update_id = CeleryPy.data_update(endpoint='points', ids_=101) self.data_update_id_static = { "kind": "data_update", "args": { "value": "update" }, "body": [ { "kind": "pair", "args": { "label": "points", "value": "101" } } ] } # data_update: ids self.data_update_list = CeleryPy.data_update( endpoint='points', ids_=[123, 456, 789]) self.data_update_list_static = { "kind": "data_update", "args": { "value": "update" }, "body": [ { "kind": "pair", "args": { "label": "points", "value": "123" } }, { "kind": "pair", "args": { "label": "points", "value": "456" } }, { "kind": "pair", "args": { "label": "points", "value": "789" } } ] } # send_message: logs self.send_message = CeleryPy.send_message( message='Hello', message_type='fun') self.send_message_static = { "kind": "send_message", "args": { "message": "Hello", "message_type": "fun" } } # send_message: toast self.send_message_toast = CeleryPy.send_message( message='Hello', message_type='fun', channel='toast') self.send_message_toast_static = { "kind": "send_message", "args": { "message": "Hello", "message_type": "fun" }, "body": [ { "kind": "channel", "args": { "channel_name": "toast" } } ] } # send_message: channels self.send_message_channels = CeleryPy.send_message( message='Hello', message_type='fun', channel=['toast', 'email']) self.send_message_channels_static = { "kind": "send_message", "args": { "message": "Hello", "message_type": "fun" }, "body": [ { "kind": "channel", "args": { "channel_name": "toast" } }, { "kind": "channel", "args": { "channel_name": "email" } } ] } # find_home self.find_home = CeleryPy.find_home(axis='all', speed=100) self.find_home_static = { "kind": "find_home", "args": { "axis": "all", "speed": 100 } } # _if: execute self.if_statement = CeleryPy.if_statement( lhs='x', op='is', rhs=0, _then=1, _else=2) self.if_statement_static = { "kind": "_if", "args": { "lhs": "x", "op": "is", "rhs": 0, "_then": { "kind": "execute", "args": { "sequence_id": 1 } }, "_else": { "kind": "execute", "args": { "sequence_id": 2 } } } } # _if: nothing self.if_statement_nothing = CeleryPy.if_statement( lhs='x', op='is', rhs=0) self.if_statement_nothing_static = { "kind": "_if", "args": { "lhs": "x", "op": "is", "rhs": 0, "_then": { "kind": "nothing", "args": {} }, "_else": { "kind": "nothing", "args": {} } } } # write_pin self.write_pin = CeleryPy.write_pin(number=0, value=1) self.write_pin_static = { "kind": "write_pin", "args": { "pin_number": 0, "pin_value": 1, "pin_mode": 0 } } # read_pin self.read_pin = CeleryPy.read_pin(number=0, mode=0, label='pin') self.read_pin_static = { "kind": "read_pin", "args": { "pin_number": 0, "pin_mode": 0, "label": "pin" } } # execute_sequence self.execute_sequence = CeleryPy.execute_sequence(sequence_id=1) self.execute_sequence_static = { "kind": "execute", "args": { "sequence_id": 1 } } # execute_script self.execute_script = CeleryPy.execute_script(label='plant-detection') self.execute_script_static = { "kind": "execute_script", "args": { "label": "plant-detection" } } # take_photo self.take_photo = CeleryPy.take_photo() self.take_photo_static = { "kind": "take_photo", "args": {} } # wait self.wait = CeleryPy.wait(milliseconds=100) self.wait_static = { "kind": "wait", "args": { "milliseconds": 100 } }