package com.iflytop.gd.hardware.controller; import com.iflytop.gd.hardware.drivers.MiniServoDriver.DeviceServoId; import com.iflytop.gd.hardware.drivers.MiniServoDriver.MiniServoRegIndex; import com.iflytop.gd.hardware.exception.HardwareException; import com.iflytop.gd.hardware.service.ServoService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Tag(name = "伺服电机控制") @RestController @RequestMapping("/api/servo") @RequiredArgsConstructor @Slf4j public class ServoController { private final ServoService servoService; // 获取设备列表 @PostMapping("get-device-list") @Operation(summary = "获取设备列表") public Map getDeviceList() { Map map = new HashMap<>(); for(DeviceServoId id : DeviceServoId.values()) { map.put(id.name(), id.getDescription()); } return map; } // 获取寄存器列表 @PostMapping("get-reg-list") @Operation(summary = "获取寄存器列表") public List getRegList() { List list = new ArrayList<>(); for(MiniServoRegIndex reg : MiniServoRegIndex.values()) { list.add(reg.name()); } return list; } // 基础操作 @PostMapping("/enable") @Operation(summary = "电源开启") public void enable(@RequestParam DeviceServoId deviceId) throws HardwareException { servoService.enable(deviceId, true); } @PostMapping("/disable") @Operation(summary = "电源关闭") public void disable(@RequestParam DeviceServoId deviceId) throws HardwareException { servoService.enable(deviceId, false); } @PostMapping("/stop") @Operation(summary = "停止") public void stop(@RequestParam DeviceServoId deviceId) throws HardwareException { servoService.stop(deviceId); } @PostMapping("/move-to") @Operation(summary = "移动(绝对)") public void moveTo(@RequestParam DeviceServoId deviceId, @RequestParam Integer pos) throws HardwareException { servoService.moveTo(deviceId, pos); } @PostMapping("/rotate-forward") @Operation(summary = "正转") public void rotateForward(@RequestParam DeviceServoId deviceId) throws HardwareException { servoService.rotate(deviceId, 1); } @PostMapping("/rotate-backward") @Operation(summary = "反转") public void rotateBackward(@RequestParam DeviceServoId deviceId) throws HardwareException { servoService.rotate(deviceId, 0); } @PostMapping("/rotate-with-torque") @Operation(summary = "旋转(力矩)") public void rotateWithTorque(@RequestParam DeviceServoId deviceId, @RequestParam Integer pos) throws HardwareException { servoService.rotateWithTorque(deviceId, pos); } @PostMapping("/move-to-zero") @Operation(summary = "回Home") public void moveToZero(@RequestParam DeviceServoId deviceId) throws HardwareException { servoService.moveToZero(deviceId); } // 寄存器操作 @PostMapping("/limit-velocity") @Operation(summary = "设置限速") public void setLimitVelocity( @RequestParam DeviceServoId deviceId, @RequestParam Integer val) throws HardwareException { servoService.setLimitVelocity(deviceId, val); } @PostMapping("/limit-torque") @Operation(summary = "设置限力矩") public void setLimitTorque( @RequestParam DeviceServoId deviceId, @RequestParam Integer val) throws HardwareException { servoService.setLimitTorque(deviceId, val); } @PostMapping("/protective-torque") @Operation(summary = "设置保护力矩") public void setProtectiveTorque( @RequestParam DeviceServoId deviceId, @RequestParam Integer val) throws HardwareException { servoService.setProtectiveTorque(deviceId, val); } @PostMapping("/reg") @Operation(summary = "设置寄存器") public void setReg( @RequestParam DeviceServoId deviceId, @RequestParam MiniServoRegIndex reg, @RequestParam Integer val) throws HardwareException { servoService.setReg(deviceId, reg, val); } // 状态查询 @GetMapping("/position") @Operation(summary = "读取位置") public Integer readPos(@RequestParam DeviceServoId deviceId) throws HardwareException { return servoService.readPos(deviceId); } @GetMapping("/regs") @Operation(summary = "获取所有寄存器") public Map getAllReg(@RequestParam DeviceServoId deviceId) { return servoService.getAllReg(deviceId); } }