init: 第一个版本
这个提交包含在:
二进制文件未显示。
79
mock-server/main.py
普通文件
79
mock-server/main.py
普通文件
@ -0,0 +1,79 @@
|
||||
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
import json
|
||||
from robot_config import SIX_AXIS_ROBOT, SCARA_ROBOT
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
@app.websocket("/ws/{robot_type}")
|
||||
async def websocket_endpoint(websocket: WebSocket, robot_type: str = "six_axis"):
|
||||
await websocket.accept()
|
||||
|
||||
if robot_type == "scara":
|
||||
robot_config = SCARA_ROBOT
|
||||
else:
|
||||
robot_config = SIX_AXIS_ROBOT
|
||||
|
||||
await websocket.send_json(robot_config)
|
||||
|
||||
joint_states = {joint["id"]: joint["default"] for joint in robot_config["joints"]}
|
||||
|
||||
try:
|
||||
while True:
|
||||
message = await websocket.receive_text()
|
||||
data = json.loads(message)
|
||||
|
||||
if "id" in data and "angel" in data:
|
||||
joint_id = data["id"]
|
||||
target_angle = data["angel"]
|
||||
|
||||
if joint_id in joint_states:
|
||||
joint_states[joint_id] = target_angle
|
||||
|
||||
await websocket.send_json(
|
||||
{
|
||||
"type": "move_ack",
|
||||
"joint": joint_id,
|
||||
"angle": target_angle,
|
||||
"status": "success",
|
||||
}
|
||||
)
|
||||
elif "type" in data and data["type"] == "ping":
|
||||
# 响应心跳消息
|
||||
await websocket.send_json(
|
||||
{"type": "pong", "timestamp": data.get("timestamp", 0)}
|
||||
)
|
||||
else:
|
||||
print(f"收到未知消息: {data}")
|
||||
|
||||
except WebSocketDisconnect:
|
||||
print(f"Client disconnected for robot: {robot_type}")
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"JSON解析错误: {e}")
|
||||
except Exception as e:
|
||||
print(f"错误: {e}")
|
||||
|
||||
|
||||
@app.get("/api/robots")
|
||||
async def get_robot_list():
|
||||
return {
|
||||
"robots": [
|
||||
{"id": "six_axis", "name": "6轴工业机械臂"},
|
||||
{"id": "scara", "name": "SCARA机器人"},
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||
3
mock-server/requirements.txt
普通文件
3
mock-server/requirements.txt
普通文件
@ -0,0 +1,3 @@
|
||||
fastapi==0.104.1
|
||||
uvicorn[standard]==0.24.0
|
||||
websockets==12.0
|
||||
90
mock-server/robot_config.py
普通文件
90
mock-server/robot_config.py
普通文件
@ -0,0 +1,90 @@
|
||||
from typing import List, Dict, Any
|
||||
|
||||
SIX_AXIS_ROBOT = {
|
||||
"id": "robot_6dof_001",
|
||||
"joints": [
|
||||
{
|
||||
"id": "base",
|
||||
"range": [-180, 180],
|
||||
"default": 0,
|
||||
"type": "flex_w",
|
||||
"next": "shoulder",
|
||||
},
|
||||
{
|
||||
"id": "shoulder",
|
||||
"range": [-90, 90],
|
||||
"default": 45,
|
||||
"type": "joint",
|
||||
"next": "elbow",
|
||||
},
|
||||
{
|
||||
"id": "elbow",
|
||||
"range": [-135, 135],
|
||||
"default": -30,
|
||||
"type": "joint",
|
||||
"next": "wrist1",
|
||||
},
|
||||
{
|
||||
"id": "wrist1",
|
||||
"range": [-180, 180],
|
||||
"default": 0,
|
||||
"type": "flex_v",
|
||||
"next": "wrist2",
|
||||
},
|
||||
{
|
||||
"id": "wrist2",
|
||||
"range": [-90, 90],
|
||||
"default": 0,
|
||||
"type": "flex_v",
|
||||
"next": "gripper",
|
||||
},
|
||||
{
|
||||
"id": "gripper",
|
||||
"range": [0, 100],
|
||||
"default": 50,
|
||||
"type": "gripper",
|
||||
"next": None,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
SCARA_ROBOT = {
|
||||
"id": "scara_001",
|
||||
"joints": [
|
||||
{
|
||||
"id": "waist",
|
||||
"range": [-170, 170],
|
||||
"default": 0,
|
||||
"type": "flex_w",
|
||||
"next": "arm1",
|
||||
},
|
||||
{
|
||||
"id": "arm1",
|
||||
"range": [-150, 150],
|
||||
"default": 30,
|
||||
"type": "joint",
|
||||
"next": "arm2",
|
||||
},
|
||||
{
|
||||
"id": "arm2",
|
||||
"range": [-150, 150],
|
||||
"default": -45,
|
||||
"type": "joint",
|
||||
"next": "wrist",
|
||||
},
|
||||
{
|
||||
"id": "wrist",
|
||||
"range": [-360, 360],
|
||||
"default": 0,
|
||||
"type": "flex_v",
|
||||
"next": "gripper",
|
||||
},
|
||||
{
|
||||
"id": "gripper",
|
||||
"range": [0, 100],
|
||||
"default": 30,
|
||||
"type": "gripper",
|
||||
"next": None,
|
||||
},
|
||||
],
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户