feat: 摆头
This commit is contained in:
parent
0032d67ea0
commit
ac7d5c283c
@ -53,4 +53,16 @@ public class TeFlowiseController extends BaseController {
|
|||||||
public AjaxResult chat(@RequestBody FlowiseChatRequestVO request) {
|
public AjaxResult chat(@RequestBody FlowiseChatRequestVO request) {
|
||||||
return AjaxResult.ok(flowiseActionService.chat(request));
|
return AjaxResult.ok(flowiseActionService.chat(request));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation("动作(眨眼+动嘴+头部微动)")
|
||||||
|
@GetMapping("/action/start")
|
||||||
|
public AjaxResult actionStart() {
|
||||||
|
return AjaxResult.ok(flowiseActionService.actionStart());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ApiOperation("动作停止")
|
||||||
|
@GetMapping("/action/stop")
|
||||||
|
public AjaxResult actionStop() {
|
||||||
|
return AjaxResult.ok(flowiseActionService.actionStop());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -44,7 +44,10 @@ public class EdgeHumanoidRobotServiceImpl implements EdgeHumanoidRobotService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// log.info("movej start time={}", System.currentTimeMillis());
|
||||||
|
|
||||||
HumanoidRobotCommand.MoveJ.Response response = stub.moveJ(builder.build());
|
HumanoidRobotCommand.MoveJ.Response response = stub.moveJ(builder.build());
|
||||||
|
// log.info("movej success time={}", System.currentTimeMillis());
|
||||||
return JSON.toJSONString(response.getHeader());
|
return JSON.toJSONString(response.getHeader());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -12,7 +12,6 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
|
|||||||
@ -7,7 +7,10 @@ import com.alibaba.fastjson2.JSONObject;
|
|||||||
import com.cmvr.common.exception.GlobalException;
|
import com.cmvr.common.exception.GlobalException;
|
||||||
import com.cmvr.common.utils.http.CallAPIUtil;
|
import com.cmvr.common.utils.http.CallAPIUtil;
|
||||||
import com.cmvr.common.utils.uuid.IdUtils;
|
import com.cmvr.common.utils.uuid.IdUtils;
|
||||||
|
import com.cmvr.edge.client.model.EdgeCommonVO;
|
||||||
|
import com.cmvr.edge.client.model.humanoid.EdgeMoveJVO;
|
||||||
import com.cmvr.edge.client.service.EdgeBioHeadService;
|
import com.cmvr.edge.client.service.EdgeBioHeadService;
|
||||||
|
import com.cmvr.edge.client.service.EdgeHumanoidRobotService;
|
||||||
import com.cmvr.test.enums.FlowiseActionEnum;
|
import com.cmvr.test.enums.FlowiseActionEnum;
|
||||||
import com.cmvr.test.flow.context.TaskContext;
|
import com.cmvr.test.flow.context.TaskContext;
|
||||||
import com.cmvr.test.flow.context.TaskContextManager;
|
import com.cmvr.test.flow.context.TaskContextManager;
|
||||||
@ -22,8 +25,10 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@ -31,6 +36,7 @@ import java.util.Map;
|
|||||||
public class FlowiseActionService {
|
public class FlowiseActionService {
|
||||||
|
|
||||||
private final EdgeBioHeadService edgeBioHeadService;
|
private final EdgeBioHeadService edgeBioHeadService;
|
||||||
|
private final EdgeHumanoidRobotService edgeHumanoidRobotService;
|
||||||
private final TaskContextManager taskContextManager;
|
private final TaskContextManager taskContextManager;
|
||||||
private final FlowControlService flowControlService;
|
private final FlowControlService flowControlService;
|
||||||
|
|
||||||
@ -52,6 +58,11 @@ public class FlowiseActionService {
|
|||||||
@Resource(name = "threadPoolTaskExecutor")
|
@Resource(name = "threadPoolTaskExecutor")
|
||||||
private final ThreadPoolTaskExecutor executor;
|
private final ThreadPoolTaskExecutor executor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 整个动作是否在运行(张嘴 + 摆头)
|
||||||
|
*/
|
||||||
|
private final AtomicBoolean actionRunning = new AtomicBoolean(true);
|
||||||
|
|
||||||
|
|
||||||
private final Object lock = new Object();
|
private final Object lock = new Object();
|
||||||
private String chatId = null;
|
private String chatId = null;
|
||||||
@ -161,4 +172,120 @@ public class FlowiseActionService {
|
|||||||
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String actionStart() {
|
||||||
|
|
||||||
|
// 防止重复启动
|
||||||
|
if (!actionRunning.compareAndSet(false, true)) {
|
||||||
|
log.warn("action 已在运行中");
|
||||||
|
return "already running";
|
||||||
|
}
|
||||||
|
|
||||||
|
String terminalId = "8c7c5d5d8c8b09df9d1e5fc9b76c46ab";
|
||||||
|
// String terminalId = "25d13e8ee0cbc2b1046e190f840b8ab0";
|
||||||
|
String bioId = "hc01";
|
||||||
|
String spkId = "spk1";
|
||||||
|
|
||||||
|
executor.execute(() -> {
|
||||||
|
EdgeCommonVO speakVO = new EdgeCommonVO();
|
||||||
|
speakVO.setTerminalId(terminalId);
|
||||||
|
speakVO.setDeviceId(spkId);
|
||||||
|
try {
|
||||||
|
// 张嘴
|
||||||
|
// edgeBioHeadService.speakStart(speakVO);
|
||||||
|
log.info("张嘴动作触发完成");
|
||||||
|
|
||||||
|
// 摆头循环
|
||||||
|
boolean toRight = true;
|
||||||
|
|
||||||
|
while (actionRunning.get()) {
|
||||||
|
|
||||||
|
double rad = toRight ? 0.1d : -0.1d;
|
||||||
|
|
||||||
|
EdgeMoveJVO moveJVO = buildHeadMoveJVO(
|
||||||
|
terminalId,
|
||||||
|
bioId,
|
||||||
|
rad
|
||||||
|
);
|
||||||
|
|
||||||
|
long t0 = System.currentTimeMillis();
|
||||||
|
log.info(">>> moveJ start, rad={},time={}", rad, t0);
|
||||||
|
String res = edgeHumanoidRobotService.moveJ(moveJVO);
|
||||||
|
|
||||||
|
long t1 = System.currentTimeMillis();
|
||||||
|
log.info(">>> moveJ end, rad={}, cost={}", rad, t1 - t0);
|
||||||
|
|
||||||
|
// 判断返回
|
||||||
|
try {
|
||||||
|
JSONObject resJson = JSON.parseObject(res);
|
||||||
|
if (!resJson.getBooleanValue("success")) {
|
||||||
|
log.warn("moveJ 返回失败,res={}", res);
|
||||||
|
}
|
||||||
|
} catch (Exception ignore) {
|
||||||
|
}
|
||||||
|
|
||||||
|
toRight = !toRight;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("action 执行异常", e);
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
// 回正
|
||||||
|
try {
|
||||||
|
EdgeMoveJVO resetVO = buildHeadMoveJVO(
|
||||||
|
terminalId,
|
||||||
|
bioId,
|
||||||
|
0d
|
||||||
|
);
|
||||||
|
log.info("结束动作,头部回正 rad=0");
|
||||||
|
edgeHumanoidRobotService.moveJ(resetVO);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("头部回正异常", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 停止张嘴
|
||||||
|
try {
|
||||||
|
// edgeBioHeadService.speakStop(speakVO);
|
||||||
|
log.info("speakStop 已调用");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("speakStop 调用失败", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
actionRunning.set(false);
|
||||||
|
log.info("action 线程结束");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return "ok";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String actionStop() {
|
||||||
|
if (!actionRunning.get()) {
|
||||||
|
return "not running";
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("actionStop 调用,准备停止所有动作");
|
||||||
|
actionRunning.set(false);
|
||||||
|
return "ok";
|
||||||
|
}
|
||||||
|
|
||||||
|
private EdgeMoveJVO buildHeadMoveJVO(String terminalId,
|
||||||
|
String deviceId,
|
||||||
|
double rad) {
|
||||||
|
|
||||||
|
EdgeMoveJVO vo = new EdgeMoveJVO();
|
||||||
|
vo.setTerminalId(terminalId);
|
||||||
|
vo.setDeviceId(deviceId);
|
||||||
|
vo.setAcc(1d);
|
||||||
|
vo.setVel(1d);
|
||||||
|
|
||||||
|
EdgeMoveJVO.JointCmd headCmd = new EdgeMoveJVO.JointCmd();
|
||||||
|
headCmd.setJointName("HEAD_R");
|
||||||
|
headCmd.setRad(rad);
|
||||||
|
headCmd.setVel(0d);
|
||||||
|
|
||||||
|
vo.setCmds(Collections.singletonList(headCmd));
|
||||||
|
return vo;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user