石墨消解仪后端服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.8 KiB

3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
  1. package com.iflytop.gd.hardware.controller;
  2. import com.iflytop.gd.hardware.drivers.MiniServoDriver.DeviceServoId;
  3. import com.iflytop.gd.hardware.drivers.MiniServoDriver.MiniServoRegIndex;
  4. import com.iflytop.gd.hardware.exception.HardwareException;
  5. import com.iflytop.gd.hardware.service.ServoService;
  6. import io.swagger.v3.oas.annotations.Operation;
  7. import io.swagger.v3.oas.annotations.tags.Tag;
  8. import lombok.RequiredArgsConstructor;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.web.bind.annotation.*;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. import java.util.List;
  14. import java.util.Map;
  15. @Tag(name = "伺服电机控制")
  16. @RestController
  17. @RequestMapping("/api/servo")
  18. @RequiredArgsConstructor
  19. @Slf4j
  20. public class ServoController {
  21. private final ServoService servoService;
  22. // 获取设备列表
  23. @PostMapping("get-device-list")
  24. @Operation(summary = "获取设备列表")
  25. public Map<String, String> getDeviceList() {
  26. Map<String, String> map = new HashMap<>();
  27. for(DeviceServoId id : DeviceServoId.values()) {
  28. map.put(id.name(), id.getDescription());
  29. }
  30. return map;
  31. }
  32. // 获取寄存器列表
  33. @PostMapping("get-reg-list")
  34. @Operation(summary = "获取寄存器列表")
  35. public List<String> getRegList() {
  36. List<String> list = new ArrayList<>();
  37. for(MiniServoRegIndex reg : MiniServoRegIndex.values()) {
  38. list.add(reg.name());
  39. }
  40. return list;
  41. }
  42. // 基础操作
  43. @PostMapping("/enable")
  44. @Operation(summary = "电源开启")
  45. public void enable(@RequestParam DeviceServoId deviceId) throws HardwareException {
  46. servoService.enable(deviceId, true);
  47. }
  48. @PostMapping("/disable")
  49. @Operation(summary = "电源关闭")
  50. public void disable(@RequestParam DeviceServoId deviceId) throws HardwareException {
  51. servoService.enable(deviceId, false);
  52. }
  53. @PostMapping("/stop")
  54. @Operation(summary = "停止")
  55. public void stop(@RequestParam DeviceServoId deviceId) throws HardwareException {
  56. servoService.stop(deviceId);
  57. }
  58. @PostMapping("/move-to")
  59. @Operation(summary = "移动(绝对)")
  60. public void moveTo(@RequestParam DeviceServoId deviceId, @RequestParam Integer pos) throws HardwareException {
  61. servoService.moveTo(deviceId, pos);
  62. }
  63. @PostMapping("/rotate-forward")
  64. @Operation(summary = "正转")
  65. public void rotateForward(@RequestParam DeviceServoId deviceId) throws HardwareException {
  66. servoService.rotate(deviceId, 1);
  67. }
  68. @PostMapping("/rotate-backward")
  69. @Operation(summary = "反转")
  70. public void rotateBackward(@RequestParam DeviceServoId deviceId) throws HardwareException {
  71. servoService.rotate(deviceId, 0);
  72. }
  73. @PostMapping("/rotate-with-torque")
  74. @Operation(summary = "旋转(力矩)")
  75. public void rotateWithTorque(@RequestParam DeviceServoId deviceId, @RequestParam Integer pos) throws HardwareException {
  76. servoService.rotateWithTorque(deviceId, pos);
  77. }
  78. @PostMapping("/move-to-zero")
  79. @Operation(summary = "回Home")
  80. public void moveToZero(@RequestParam DeviceServoId deviceId) throws HardwareException {
  81. servoService.moveToZero(deviceId);
  82. }
  83. // 寄存器操作
  84. @PostMapping("/limit-velocity")
  85. @Operation(summary = "设置限速")
  86. public void setLimitVelocity(
  87. @RequestParam DeviceServoId deviceId,
  88. @RequestParam Integer val) throws HardwareException {
  89. servoService.setLimitVelocity(deviceId, val);
  90. }
  91. @PostMapping("/limit-torque")
  92. @Operation(summary = "设置限力矩")
  93. public void setLimitTorque(
  94. @RequestParam DeviceServoId deviceId,
  95. @RequestParam Integer val) throws HardwareException {
  96. servoService.setLimitTorque(deviceId, val);
  97. }
  98. @PostMapping("/protective-torque")
  99. @Operation(summary = "设置保护力矩")
  100. public void setProtectiveTorque(
  101. @RequestParam DeviceServoId deviceId,
  102. @RequestParam Integer val) throws HardwareException {
  103. servoService.setProtectiveTorque(deviceId, val);
  104. }
  105. @PostMapping("/reg")
  106. @Operation(summary = "设置寄存器")
  107. public void setReg(
  108. @RequestParam DeviceServoId deviceId,
  109. @RequestParam MiniServoRegIndex reg,
  110. @RequestParam Integer val) throws HardwareException {
  111. servoService.setReg(deviceId, reg, val);
  112. }
  113. // 状态查询
  114. @GetMapping("/position")
  115. @Operation(summary = "读取位置")
  116. public Integer readPos(@RequestParam DeviceServoId deviceId) throws HardwareException {
  117. return servoService.readPos(deviceId);
  118. }
  119. @GetMapping("/regs")
  120. @Operation(summary = "获取所有寄存器")
  121. public Map<String, Integer> getAllReg(@RequestParam DeviceServoId deviceId) {
  122. return servoService.getAllReg(deviceId);
  123. }
  124. }