5 changed files with 14 additions and 344 deletions
-
161src/main/java/com/iflytop/colortitration/hardware/controller/ServoController.java
-
40src/main/java/com/iflytop/colortitration/hardware/service/GDDeviceStatusService.java
-
129src/main/java/com/iflytop/colortitration/hardware/service/ServoService.java
-
8src/main/java/com/iflytop/colortitration/hardware/service/StepMotorService.java
-
20src/main/java/com/iflytop/colortitration/hardware/type/StepMotor/DeviceStepMotorId.java
@ -1,161 +0,0 @@ |
|||
package com.iflytop.colortitration.hardware.controller; |
|||
|
|||
import com.iflytop.colortitration.app.service.DeviceParamConfigService; |
|||
import com.iflytop.colortitration.common.result.Result; |
|||
import com.iflytop.colortitration.hardware.exception.HardwareException; |
|||
import com.iflytop.colortitration.hardware.service.ServoService; |
|||
import com.iflytop.colortitration.hardware.type.Servo.MiniServoId; |
|||
import com.iflytop.colortitration.hardware.type.Servo.MiniServoRegIndex; |
|||
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; |
|||
private final DeviceParamConfigService deviceParamConfigService; |
|||
|
|||
// 获取设备列表 |
|||
@PostMapping("/get-device-list") |
|||
@Operation(summary = "获取设备列表") |
|||
public Map<String, String> getDeviceList() { |
|||
Map<String, String> map = new HashMap<>(); |
|||
for(MiniServoId id : MiniServoId.values()) { |
|||
map.put(id.name(), id.getDescription()); |
|||
} |
|||
return map; |
|||
} |
|||
|
|||
// 获取寄存器列表 |
|||
@PostMapping("/get-reg-list") |
|||
@Operation(summary = "获取寄存器列表") |
|||
public List<String> getRegList() { |
|||
List<String> list = new ArrayList<>(); |
|||
for(MiniServoRegIndex reg : MiniServoRegIndex.values()) { |
|||
list.add(reg.name()); |
|||
} |
|||
return list; |
|||
} |
|||
|
|||
// 基础操作 |
|||
@PostMapping("/enable") |
|||
@Operation(summary = "电源开启") |
|||
public Result<?> enable(@RequestParam MiniServoId deviceId) throws HardwareException { |
|||
servoService.enable(deviceId, true); |
|||
return Result.success(); |
|||
} |
|||
|
|||
@PostMapping("/disable") |
|||
@Operation(summary = "电源关闭") |
|||
public Result<?> disable(@RequestParam MiniServoId deviceId) throws HardwareException { |
|||
servoService.enable(deviceId, false); |
|||
return Result.success(); |
|||
} |
|||
|
|||
@PostMapping("/stop") |
|||
@Operation(summary = "停止") |
|||
public Result<?> stop(@RequestParam MiniServoId deviceId) throws HardwareException { |
|||
servoService.stop(deviceId); |
|||
return Result.success(); |
|||
} |
|||
|
|||
@PostMapping("/move-to") |
|||
@Operation(summary = "移动(绝对)") |
|||
public Result<?> moveTo(@RequestParam MiniServoId deviceId, @RequestParam Integer pos) throws HardwareException { |
|||
servoService.moveTo(deviceId, pos); |
|||
return Result.success(); |
|||
} |
|||
|
|||
@PostMapping("/rotate-forward") |
|||
@Operation(summary = "正转") |
|||
public Result<?> rotateForward(@RequestParam MiniServoId deviceId) throws HardwareException { |
|||
servoService.rotate(deviceId, 1); |
|||
return Result.success(); |
|||
} |
|||
|
|||
@PostMapping("/rotate-backward") |
|||
@Operation(summary = "反转") |
|||
public Result<?> rotateBackward(@RequestParam MiniServoId deviceId) throws HardwareException { |
|||
servoService.rotate(deviceId, 0); |
|||
return Result.success(); |
|||
} |
|||
@PostMapping("/rotate-with-torque") |
|||
@Operation(summary = "旋转(力矩)") |
|||
public Result<?> rotateWithTorque(@RequestParam MiniServoId deviceId, @RequestParam Integer pos) throws HardwareException { |
|||
servoService.rotateWithTorque(deviceId, pos); |
|||
return Result.success(); |
|||
} |
|||
|
|||
@PostMapping("/move-to-zero") |
|||
@Operation(summary = "回Home") |
|||
public Result<?> moveToZero(@RequestParam MiniServoId deviceId) throws HardwareException { |
|||
servoService.moveToZero(deviceId); |
|||
return Result.success(); |
|||
} |
|||
|
|||
// 寄存器操作 |
|||
@PostMapping("/limit-velocity") |
|||
@Operation(summary = "设置限速") |
|||
public Result<?> setLimitVelocity( |
|||
@RequestParam MiniServoId deviceId, |
|||
@RequestParam Integer val) throws HardwareException { |
|||
servoService.setLimitVelocity(deviceId, val); |
|||
deviceParamConfigService.setModuleAndReg(deviceId.getServoMId().mid.name(), MiniServoRegIndex.kreg_mini_servo_limit_velocity.regIndex.name(),val); |
|||
return Result.success(); |
|||
} |
|||
|
|||
@PostMapping("/limit-torque") |
|||
@Operation(summary = "设置限力矩") |
|||
public Result<?> setLimitTorque( |
|||
@RequestParam MiniServoId deviceId, |
|||
@RequestParam Integer val) throws HardwareException { |
|||
servoService.setLimitTorque(deviceId, val); |
|||
deviceParamConfigService.setModuleAndReg(deviceId.getServoMId().mid.name(), MiniServoRegIndex.kreg_mini_servo_limit_torque.regIndex.name(),val); |
|||
return Result.success(); |
|||
} |
|||
|
|||
@PostMapping("/protective-torque") |
|||
@Operation(summary = "设置保护力矩") |
|||
public Result<?> setProtectiveTorque( |
|||
@RequestParam MiniServoId deviceId, |
|||
@RequestParam Integer val) throws HardwareException { |
|||
servoService.setProtectiveTorque(deviceId, val); |
|||
deviceParamConfigService.setModuleAndReg(deviceId.getServoMId().mid.name(), MiniServoRegIndex.kreg_mini_servo_protective_torque.regIndex.name(),val); |
|||
return Result.success(); |
|||
} |
|||
|
|||
@PostMapping("/reg") |
|||
@Operation(summary = "设置寄存器") |
|||
public Result<?> setReg( |
|||
@RequestParam MiniServoId deviceId, |
|||
@RequestParam MiniServoRegIndex reg, |
|||
@RequestParam Integer val) throws HardwareException { |
|||
servoService.setReg(deviceId, reg, val); |
|||
deviceParamConfigService.setModuleAndReg(deviceId.getServoMId().mid.name(), reg.regIndex.name(),val); |
|||
return Result.success(); |
|||
} |
|||
|
|||
// 状态查询 |
|||
@GetMapping("/position") |
|||
@Operation(summary = "读取位置") |
|||
public Integer readPos(@RequestParam MiniServoId deviceId) throws HardwareException { |
|||
return servoService.readPos(deviceId); |
|||
} |
|||
|
|||
@GetMapping("/regs") |
|||
@Operation(summary = "获取所有寄存器") |
|||
public Map<String, Integer> getAllReg(@RequestParam MiniServoId deviceId) { |
|||
return servoService.getAllReg(deviceId); |
|||
} |
|||
} |
@ -1,129 +0,0 @@ |
|||
package com.iflytop.colortitration.hardware.service; |
|||
|
|||
import com.iflytop.colortitration.hardware.drivers.MiniServoDriver.MiniServoDriver; |
|||
import com.iflytop.colortitration.hardware.exception.HardwareException; |
|||
import com.iflytop.colortitration.hardware.type.Servo.MiniServoId; |
|||
import com.iflytop.colortitration.hardware.type.Servo.MiniServoMId; |
|||
import com.iflytop.colortitration.hardware.type.Servo.MiniServoRegIndex; |
|||
import com.iflytop.colortitration.hardware.type.error.A8kEcode; |
|||
import com.iflytop.colortitration.hardware.type.error.AppError; |
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
@Service |
|||
@RequiredArgsConstructor |
|||
public class ServoService { |
|||
private final MiniServoDriver miniServoDriver; |
|||
|
|||
private void throwDeviceNull(MiniServoId id) throws HardwareException |
|||
{ |
|||
throw new HardwareException(new AppError(A8kEcode.PE_PARAM_OUT_OF_RANGE, "StepMotorMId is null for id: " + id)); |
|||
} |
|||
|
|||
public void enable(MiniServoId id, Boolean enable) throws HardwareException { |
|||
MiniServoMId servoMId = id.getServoMId(); |
|||
if (servoMId == null) { |
|||
throwDeviceNull(id); |
|||
} |
|||
miniServoDriver.miniServoEnable(servoMId, enable ? 1 : 0); |
|||
} |
|||
|
|||
public void stop(MiniServoId id) throws HardwareException { |
|||
MiniServoMId servoMId = id.getServoMId(); |
|||
if (servoMId == null) { |
|||
throwDeviceNull(id); |
|||
} |
|||
miniServoDriver.moduleStop(servoMId); |
|||
} |
|||
|
|||
public void moveTo(MiniServoId id, Integer pos) throws HardwareException { |
|||
MiniServoMId servoMId = id.getServoMId(); |
|||
if (servoMId == null) { |
|||
throwDeviceNull(id); |
|||
} |
|||
miniServoDriver.miniServoMoveToBlock(servoMId, pos); |
|||
} |
|||
|
|||
public void rotate(MiniServoId id, Integer direction) throws HardwareException { |
|||
MiniServoMId servoMId = id.getServoMId(); |
|||
if (servoMId == null) { |
|||
throwDeviceNull(id); |
|||
} |
|||
miniServoDriver.miniServoRotate(servoMId, direction); |
|||
} |
|||
|
|||
public void rotateWithTorque(MiniServoId id, Integer pos) throws HardwareException { |
|||
MiniServoMId servoMId = id.getServoMId(); |
|||
if (servoMId == null) { |
|||
throwDeviceNull(id); |
|||
} |
|||
miniServoDriver.miniServoRotateWithTorque(servoMId, pos); |
|||
} |
|||
|
|||
public void setReg(MiniServoId id, MiniServoRegIndex reg, Integer val) throws HardwareException { |
|||
MiniServoMId servoMId = id.getServoMId(); |
|||
if (servoMId == null) { |
|||
throwDeviceNull(id); |
|||
} |
|||
miniServoDriver.setReg(servoMId, reg, val); |
|||
} |
|||
|
|||
public Map<String, Integer> getAllReg(MiniServoId id) { |
|||
Map<String, Integer> map = new HashMap<>(); |
|||
MiniServoMId servoMId = id.getServoMId(); |
|||
if (servoMId == null) { |
|||
return map; |
|||
} |
|||
|
|||
for (MiniServoRegIndex reg : MiniServoRegIndex.values()) { |
|||
try { |
|||
map.put(reg.name(), miniServoDriver.getReg(servoMId, reg)); |
|||
} catch (HardwareException e) { |
|||
} |
|||
} |
|||
return map; |
|||
} |
|||
|
|||
public void setLimitVelocity(MiniServoId id, Integer val) throws HardwareException { |
|||
MiniServoMId servoMId = id.getServoMId(); |
|||
if (servoMId == null) { |
|||
throwDeviceNull(id); |
|||
} |
|||
miniServoDriver.setReg(servoMId, MiniServoRegIndex.kreg_mini_servo_limit_velocity, val); |
|||
} |
|||
|
|||
public void setLimitTorque(MiniServoId id, Integer val) throws HardwareException { |
|||
MiniServoMId servoMId = id.getServoMId(); |
|||
if (servoMId == null) { |
|||
throwDeviceNull(id); |
|||
} |
|||
miniServoDriver.setReg(servoMId, MiniServoRegIndex.kreg_mini_servo_limit_torque, val); |
|||
} |
|||
|
|||
public void setProtectiveTorque(MiniServoId id, Integer val) throws HardwareException { |
|||
MiniServoMId servoMId = id.getServoMId(); |
|||
if (servoMId == null) { |
|||
throwDeviceNull(id); |
|||
} |
|||
miniServoDriver.setReg(servoMId, MiniServoRegIndex.kreg_mini_servo_protective_torque, val); |
|||
} |
|||
|
|||
public Integer readPos(MiniServoId id) throws HardwareException { |
|||
MiniServoMId servoMId = id.getServoMId(); |
|||
if (servoMId == null) { |
|||
throwDeviceNull(id); |
|||
} |
|||
return miniServoDriver.miniServoReadPos(servoMId); |
|||
} |
|||
|
|||
public void moveToZero(MiniServoId id) throws HardwareException { |
|||
MiniServoMId servoMId = id.getServoMId(); |
|||
if (servoMId == null) { |
|||
throwDeviceNull(id); |
|||
} |
|||
miniServoDriver.miniServoMoveToZeroBlock(servoMId); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue