feat: 增加base64传入动作指令

feat: 换用 deepseek 大模型
pref: 修改应急避险2停车条件
This commit is contained in:
bmy
2024-08-04 10:04:27 +08:00
parent 55f5b13d8c
commit f3bb720bed
7 changed files with 278 additions and 130 deletions

View File

@@ -3,6 +3,7 @@ from loguru import logger
from utils import label_filter
from utils import tlabel
from utils import LLM
from utils import LLM_deepseek
from utils import CountRecord
import utils
import toml
@@ -12,10 +13,13 @@ import variable as var
import action as act
import re
import math
import json
import json5
# import threading
# import ctypes
cfg = None
cfg_args = None
cfg_move_area = None
by_cmd = None
filter = None
llm_bot = None
@@ -48,9 +52,15 @@ def import_obj(_by_cmd, skip_queue):
global cfg
global cfg_args
global cfg_move_area
global global_skip_queue
cfg = toml.load('/home/evan/Workplace/project_main/cfg_subtask.toml') # 加载任务配置
cfg_args = toml.load('/home/evan/Workplace/project_main/cfg_args.toml')
try:
with open('/home/evan/Workplace/project_main/cfg_move_area.json', 'r') as f:
cfg_move_area = json.load(f)
except:
cfg_move_area = None
by_cmd = _by_cmd
global_skip_queue = skip_queue
@@ -68,7 +78,7 @@ def import_obj(_by_cmd, skip_queue):
filter = label_filter(socket)
if cfg['move_area']['llm_enable']:
llm_bot = LLM()
llm_bot = LLM_deepseek()
def car_stop():
for _ in range(3):
by_cmd.send_speed_x(0)
@@ -467,7 +477,7 @@ class get_block1():
time.sleep(0.1)
by_cmd.send_position_axis_x(1, 100)
time.sleep(1)
by_cmd.send_position_axis_z(30, 90)
by_cmd.send_position_axis_z(30, 100)
time.sleep(0.5)
by_cmd.send_angle_claw(63)
time.sleep(0.5)
@@ -572,7 +582,7 @@ class put_block():
by_cmd.send_angle_claw(85)
# by_cmd.send_angle_storage(0)
time.sleep(1)
by_cmd.send_position_axis_z(30,90)
by_cmd.send_position_axis_z(30,100)
time.sleep(1)
by_cmd.send_angle_claw(25)
by_cmd.send_distance_x(-10, 110)
@@ -703,13 +713,13 @@ class up_tower():
by_cmd.send_distance_x(-10, 120)
time.sleep(1)
# 上古參數
by_cmd.send_distance_y(-10, 80)
by_cmd.send_distance_y(-10, 50) # 80
# 6_9 模型參數
# by_cmd.send_distance_y(-10, 40)
# 7_12_3 模型參數
# by_cmd.send_distance_y(-10, 50)
time.sleep(2)
car_stop()
# time.sleep(2)
# car_stop()
# FIXME 如果下發 distance 後直接 car_stop則 distance 執行時間僅由指令間處理延時決定
# time.sleep(3)
# by_cmd.send_speed_y(-10)
@@ -759,21 +769,21 @@ class get_rball():
# 靠近塔
by_cmd.send_angle_scoop(20)
# 上古參數
by_cmd.send_distance_y(-15, 70) # 50
by_cmd.send_distance_y(-15, 50) # 50 # 70
# 6_9 參數
# by_cmd.send_distance_y(-15, 35)
# time.sleep(2)
# 7_12_3 參數
# by_cmd.send_distance_y(-15, 45)
time.sleep(2)
car_stop()
# time.sleep(2)
# car_stop()
calibrate_new(tlabel.RBALL,offset = 44, run = True)
time.sleep(1)
logger.info("抓红球")
# by_cmd.send_angle_scoop(12)
time.sleep(0.5)
by_cmd.send_position_axis_z(30, 200)
time.sleep(2.5)
time.sleep(3)
by_cmd.send_angle_scoop(12)
time.sleep(0.5)
by_cmd.send_angle_scoop(7)
@@ -1255,33 +1265,36 @@ class move_area1():
# filter_w = (148, 560)
# filter_h = (165, 390)
counts = 0
while True:
ocr_socket.send_string("")
resp = ocr_socket.recv_pyobj()
var.llm_text = ''
counts += 1
if resp.get('code') == 0:
for item in resp.get('content'):
if item['probability']['average'] < 0.80:
continue
# box = item['location']
# center_x = box['left'] + box['width'] / 2
# center_y = box['top'] + box['height'] / 2
# if center_x < filter_w[0] or center_x > filter_w[1] \
# or center_y < filter_h[0] or center_y > filter_h[1]:
# continue
var.llm_text += item['words']
break
if counts >= 2:
if cfg_move_area == None:
counts = 0
while True:
ocr_socket.send_string("")
resp = ocr_socket.recv_pyobj()
var.llm_text = ''
counts += 1
if resp.get('code') == 0:
for item in resp.get('content'):
if item['probability']['average'] < 0.80:
continue
# box = item['location']
# center_x = box['left'] + box['width'] / 2
# center_y = box['top'] + box['height'] / 2
# if center_x < filter_w[0] or center_x > filter_w[1] \
# or center_y < filter_h[0] or center_y > filter_h[1]:
# continue
var.llm_text += item['words']
break
if counts >= 2:
var.skip_llm_task_flag = True
return
logger.error(var.llm_text)
if len(var.llm_text) < 3:
var.skip_llm_task_flag = True
return
logger.error(var.llm_text)
if len(var.llm_text) < 3:
var.skip_llm_task_flag = True
return
else:
# 不需要文字识别 直接使用传入的参数执行 action
pass
var.task_speed = 9 # 12
@@ -1325,17 +1338,18 @@ class move_area2():
pass
def init(self):
logger.info("应急避险第二阶段初始化")
self.offset = 15
self.offset = 60
self.delta_x = 0
self.delta_y = 0
self.delta_omage = 0
def find(self):
if var.skip_llm_task_flag:
if var.skip_llm_task_flag and cfg_move_area == None:
return 5000
ret, box = filter.get(tlabel.SHELTER)
if ret:
error = (box[0][2] + box[0][0] - 320) / 2 + self.offset
if abs(error) < 20:
# 增加了一个宽度过滤
if abs(error) < 30 and abs(box[0][2] - box[0][0]) > 180:
return 5000
return False
def add_item(self, item):
@@ -1472,41 +1486,60 @@ class move_area2():
logger.info(f"回正后最终位置: ({self.abs_y:.2f}, {self.abs_x:.2f}), 角度: {math.degrees(self.abs_w % (2 * math.pi))}")
def exec(self):
var.task_speed = 0
if var.skip_llm_task_flag:
logger.error("ocr 识别出错 直接跳过任务")
if var.skip_llm_task_flag and cfg_move_area == None:
logger.error("ocr 识别出错 直接跳过任务")
return
logger.info("开始寻找停车区域")
car_stop()
calibrate_new(tlabel.SHELTER, offset = 15, run = True)
calibrate_new(tlabel.SHELTER, offset = 30, run = True)
time.sleep(0.5)
# 调用大模型 然后执行动作
try:
resp = llm_bot.get_command_json(var.llm_text)
logger.info(resp)
except:
logger.error("大模型超时,跳过任务")
return
try:
# FIXME 当前仍然可能存在文心一言分段返回和掺杂解释的问题,不确定当前条件足以过滤
resp_commands = eval(re.findall("```json(.*?)```", resp, re.S)[0])
logger.info(resp_commands)
if len(resp_commands) == 0:
if cfg_move_area == None:
# 调用大模型 然后执行动作
try:
resp = llm_bot.get_command_json(var.llm_text)
logger.info(f"llm 返回原数据 {resp}")
except:
logger.error("大模型 llm_bot 超时,跳过任务")
return
action_list = resp_commands
# 进入停车区域
by_cmd.send_distance_y(10, 450)
time.sleep((450 * 5 / 1000) + 0.5)
for action in action_list:
self.add_item(action)
time.sleep(0.1)
time.sleep(0.5)
self.reset()
except:
logger.warning("任务解析失败并退出,文心一言真是废物")
pass
try:
json_text = re.findall("```json(.*?)```", resp, re.S)
if len(json_text) == 0:
# 返回的内容不带'''json
resp_commands = eval(resp)
else:
resp_commands = json5.loads(json_text[0])
logger.info(resp_commands)
if len(resp_commands) == 0:
return
action_list = resp_commands
# 进入停车区域
by_cmd.send_distance_y(10, 450)
time.sleep((450 * 5 / 1000) + 0.5)
for action in action_list:
self.add_item(action)
time.sleep(0.1)
time.sleep(0.5)
self.reset()
except:
logger.warning("任务解析失败并退出,文心一言真是废物")
pass
else:
# 无需调用大模型 直接开始执行传入的参数
try:
by_cmd.send_distance_y(10, 450)
time.sleep((450 * 5 / 1000) + 0.5)
for action in cfg_move_area:
self.add_item(action)
time.sleep(0.1)
time.sleep(0.5)
self.reset()
except:
pass
pass
def nexec(self):
logger.warning("正在跳過大模型任務")
time.sleep(2)