From 8c20aeb1dfd3f8c15f64b52fd9a962bbcfa59271 Mon Sep 17 00:00:00 2001 From: XZLeo <zi8602xi-s@student.lu.se> Date: Mon, 22 Nov 2021 15:59:32 +0100 Subject: [PATCH] update --- src/creds.py | 2 - src/location.py | 1 + src/main.py | 17 +++++---- src/move.py | 97 +++++++++++++------------------------------------ 4 files changed, 36 insertions(+), 81 deletions(-) delete mode 100644 src/creds.py diff --git a/src/creds.py b/src/creds.py deleted file mode 100644 index ce5142d..0000000 --- a/src/creds.py +++ /dev/null @@ -1,2 +0,0 @@ -device_id="***REMOVED***" -token=***REMOVED*** diff --git a/src/location.py b/src/location.py index 856f8b5..3cde737 100644 --- a/src/location.py +++ b/src/location.py @@ -207,6 +207,7 @@ def cal_location(args: Namespace) -> ndarray: confidence = detection[5] # pixel coordinate to camera coordinate local_coordinate = cam_coordinate(center_x, center_y, K_matrix) + print(local_coordinate) # camera coordinate to global coordinate global_x, global_y = global_coordinate(local_coordinate, diff --git a/src/main.py b/src/main.py index 5d876c2..0cae31e 100644 --- a/src/main.py +++ b/src/main.py @@ -11,6 +11,7 @@ from pathlib import Path from typing import Tuple from numpy import sqrt from pandas import DataFrame +from gripper import gripper_close, gripper_open from move import * from detect import * @@ -61,8 +62,10 @@ def remove_temp(path: Path)-> None: def main(args: Namespace): + # start from the origin + simple_move(0, 0, 0) # scan - scan() + scan(flag=False) # detect detect(args) # calculate locations @@ -80,12 +83,12 @@ def main(args: Namespace): for i in range(num_goals): x, y = goal_class.loc[i, ['x','y']] simple_move(x, y, GRIP_Z, False) - gripper(True) # to ensure the gripper is open - gripper(False) - - # go back to the origin - simple_move(ORIGIN_X, ORIGIN_Y, ORIGIN_Z, False) - gripper(True) + open() + gripper_open() # to make sure the gripper is open before gripping + gripper_close() + # go back to the orgin + simple_move(x, y, GRIP_Z, False) + gripper_open() # clean temporary files if all the previous step work remove_temp(args.input) remove_temp(args.locations) diff --git a/src/move.py b/src/move.py index 77d57d9..77788f6 100644 --- a/src/move.py +++ b/src/move.py @@ -7,15 +7,16 @@ Note: it is for remote server, can ben replaced by a local script ''' from argparse import ArgumentParser from logging import getLogger -from os import path, makedirs -from time import sleep, time +from os import path, makedirs, system +from time import sleep, strftime, time #from serial import Serial, PARITY_NONE, STOPBITS_ONE, EIGHTBITS from requests.api import delete from typing import List from pathlib import Path from logging import basicConfig, DEBUG, INFO, error, getLogger +from urllib import request -from datetime import timezone +from datetime import timezone, datetime from dateutil.parser import parse from requests import get, delete @@ -23,7 +24,7 @@ import creds from client import FarmbotClient -_SWEEEP_HEIGHT = -200 +_SWEEEP_HEIGHT = 0 Logger = getLogger(__name__) @@ -38,7 +39,7 @@ class Opts: self.flag = flag -def scan(min_x=0, max_x=1300, min_y=0, max_y=1000, delta=500, offset=0, flag=True) -> List: #numbers need to be re-measured +def scan(img_path: Path, min_x=0, max_x=1300, min_y=0, max_y=1000, delta=500, offset=0, flag=True) -> List: #里面的数字需要重新测量 ''' scan the bed at a certain height, first move along x axis, then y, like a zig zag; Taking pictures and record the location of the camera that corresponds to the picture @@ -74,56 +75,22 @@ def scan(min_x=0, max_x=1300, min_y=0, max_y=1000, delta=500, offset=0, flag=Tru client.move(0, 0, _SWEEEP_HEIGHT) # ensure moving from original for x, y in pts: client.move(x, y, _SWEEEP_HEIGHT) # move camera - #client #需要添加一个函数,读取当前位置,需要吗? - client.take_photo() # 拍照时,需要记住每张照片对应的相机位置! + take_photo(img_path) client.shutdown() - return pts # 这里应该写入文件 img/location + return pts # location应该写入文件 img/location -def download_images() -> Path: - REQUEST_HEADERS = {'Authorization': 'Bearer ' + creds.token, 'content-type': "application/json"} +def take_photo(img_path: Path): + HERE = path.dirname(__file__) + IMG_DIR = path.join(HERE, img_path) - while True: - response = get('https://my.farmbot.io/api/images', headers=REQUEST_HEADERS) # http rquest - images = response.json() - Logger.info("Download {} Images".format(len(images))) - if len(images) == 0: # cannot receive images - break # leave the loop + with request.urlopen('http://localhost:8080/?action=snapshot') as photo: + filename = datetime.now().strftime("%Y-%m-%dT%H:%M:%S") + ".jpg" + with open(path.join(IMG_DIR, filename), mode="wb") as save_file: + save_file.write(photo.read()) - at_least_one_dup = False - for image_info in images: - if 'placehold.it' in image_info['attachment_url']: - Logger.debug("IGNORE! placeholder", image_info['id']) - continue - - # convert date time of capture from UTC To AEDT and extract - # a simple string version for local image filename - dts = parse(image_info['attachment_processed_at']) - dts = dts.replace(tzinfo=timezone.utc).astimezone(tz=None) - local_img_dir = "imgs/%s" % dts.strftime("%Y%m%d") #the name of local pics 新建图片的位置 - if not path.exists(local_img_dir): - makedirs(local_img_dir) - local_img_name = "%s/%s.jpg" % (local_img_dir, dts.strftime("%H%M%S")) - Logger.debug(">", local_img_name) - - # download image from google storage and save locally - captured_img_name = image_info['meta']['name'] - if captured_img_name.startswith("/tmp/images"): - req = get(image_info['attachment_url'], allow_redirects=True) - open(local_img_name, 'wb').write(req.content) - - # post delete from cloud storage - delete("https://my.farmbot.io/api/images/%d" % image_info['id'], - headers=REQUEST_HEADERS) - - if at_least_one_dup: - Logger.debug("only at least one dup; give DELETEs a chance to work") - sleep(2) - return local_img_dir - - -def simple_move(x: int, y: int, z: int, photo: bool) -> None: +def simple_move(x: int, y: int, z: int) -> None: ''' Move to a place, if flag is true, take a picture Input: x, y,z: destination point @@ -131,31 +98,9 @@ def simple_move(x: int, y: int, z: int, photo: bool) -> None: ''' client = FarmbotClient(creds.device_id, creds.token) client.move(x, y, z) - if photo: - # take a picture - client.take_photo() client.shutdown() return None -def gripper(open: bool) -> None: - ''' - Use a serial port to drive the gripper to open or close - :param open: True: open False: Close - ''' - ser = Serial( - port = 'COM4', # 这里不应为hard code - baudrate = 9600, - parity = PARITY_NONE, - stopbits = STOPBITS_ONE, - bytesize = EIGHTBITS, - timeout = 1 - ) - if open: - ser.write(str.encode("o")) - else: - ser.write(str.encode("c")) - return None - if __name__ == '__main__': parser = ArgumentParser() @@ -172,6 +117,13 @@ if __name__ == '__main__': default='../log/move.log', help='Path to the log file' ) + parser.add_argument( + '-p', + '--photo', + type=Path, + default="../img", + help='Mode for FarmBot, 1 for simple move with an assigned detination, 2 for scaning' + ) parser.add_argument('-v', '--verbose', action='store_true', help='Verbose mode') arguments = parser.parse_args() @@ -186,7 +138,8 @@ if __name__ == '__main__': simple_move(destination_x, destination_y, destination_z, photo) Logger.info(f'time cost {time()-simple_move_start}') elif arguments.mode == 2: - scan() + simple_move(1000,0, -200) + #scan(flag=False) else: Logger.error('Wrong mode number {arguments.mode}') -- GitLab