diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/StandardServoMotor.java b/src/main/java/com/iflytop/gd/infrastructure/devices/StandardServoMotor.java new file mode 100644 index 0000000..a8c723e --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/StandardServoMotor.java @@ -0,0 +1,65 @@ +package com.iflytop.gd.infrastructure.devices; + +import com.iflytop.gd.infrastructure.drivers.RegIndex; +import com.iflytop.gd.system.devices.ServoMotor; + + +/** + * 标准物理伺服电机 + */ +public class StandardServoMotor implements ServoMotor { + @Override + public void enable() { + + } + + @Override + public void disable() { + + } + + @Override + public void stop() { + + } + + @Override + public Integer getCurrentPosition() { + return 0; + } + + @Override + public void moveToZero() { + + } + + @Override + public void moveTo(Integer position) { + + } + + @Override + public void setMaxVelocity(Integer maxVelocity) { + + } + + @Override + public void setMaxTorque(Integer maxTorque) { + + } + + @Override + public void setProtectiveTorque(Integer protectiveTorque) { + + } + + @Override + public Integer readReg(RegIndex regIndex) { + return 0; + } + + @Override + public void writeReg(RegIndex regIndex, Integer value) { + + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualServoMotor.java b/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualServoMotor.java new file mode 100644 index 0000000..67715fe --- /dev/null +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualServoMotor.java @@ -0,0 +1,75 @@ +package com.iflytop.gd.infrastructure.devices; + +import com.iflytop.gd.infrastructure.drivers.RegIndex; +import com.iflytop.gd.system.devices.ServoMotor; +import com.iflytop.gd.system.models.ServoMotorStatus; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; + + +/** + * 虚拟伺服电机 + */ +@Component +@Profile("dev") +public class VirtualServoMotor implements ServoMotor { + private final ServoMotorStatus servoMotorStatus = new ServoMotorStatus(); + private final Map registers = new HashMap(); + @Override + public void enable() { + this.servoMotorStatus.setEnabled(true); + } + + @Override + public void disable() { + this.servoMotorStatus.setEnabled(false); + } + + @Override + public void stop() { + this.servoMotorStatus.setStopped(true); + } + + @Override + public Integer getCurrentPosition() { + return this.servoMotorStatus.getCurrentPosition(); + } + + @Override + public void moveToZero() { + this.servoMotorStatus.setCurrentPosition(0); + } + + @Override + public void moveTo(Integer position) { + this.servoMotorStatus.setCurrentPosition(position); + } + + @Override + public void setMaxVelocity(Integer maxVelocity) { + //TODO 等待硬件给出寄存器索引 + } + + @Override + public void setMaxTorque(Integer maxTorque) { + //TODO 等待硬件给出寄存器索引 + } + + @Override + public void setProtectiveTorque(Integer protectiveTorque) { + //TODO 等待硬件给出寄存器索引 + } + + @Override + public Integer readReg(RegIndex regIndex) { + return this.registers.get(regIndex); + } + + @Override + public void writeReg(RegIndex regIndex, Integer value) { + this.registers.put(regIndex, value); + } +} diff --git a/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualStepMotor.java b/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualStepMotor.java index 035f378..add4875 100644 --- a/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualStepMotor.java +++ b/src/main/java/com/iflytop/gd/infrastructure/devices/VirtualStepMotor.java @@ -8,7 +8,7 @@ import com.iflytop.gd.system.constants.RotationDirection; import com.iflytop.gd.system.devices.StepMotor; import com.iflytop.gd.system.exceptions.CommandExecTimeoutException; import com.iflytop.gd.system.exceptions.HardwareErrorException; -import com.iflytop.gd.system.models.MotorStatus; +import com.iflytop.gd.system.models.StepMotorStatus; import lombok.extern.slf4j.Slf4j; import java.io.IOException; @@ -20,7 +20,7 @@ import java.util.Map; */ @Slf4j public class VirtualStepMotor implements StepMotor { - private MotorStatus motorStatus = new MotorStatus(); + private StepMotorStatus stepMotorStatus = new StepMotorStatus(); private Map registers = new HashMap(); private final ModuleId moduleId; @@ -30,50 +30,50 @@ public class VirtualStepMotor implements StepMotor { @Override public void easyMoveBy(Integer value, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { - int position = this.motorStatus.getCurrentPosition() + unit.toMM(value); - this.motorStatus.setCurrentPosition(position); - this.motorStatus.setStopped(true); + int position = this.stepMotorStatus.getCurrentPosition() + unit.toMM(value); + this.stepMotorStatus.setCurrentPosition(position); + this.stepMotorStatus.setStopped(true); } @Override public void easyMoveTo(Integer value, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { log.debug("Moving to {}", value); - this.motorStatus.setCurrentPosition(unit.toMM(value)); - this.motorStatus.setZeroPosition(this.motorStatus.getCurrentPosition() == 0); - this.motorStatus.setStopped(true); - log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.motorStatus)); + this.stepMotorStatus.setCurrentPosition(unit.toMM(value)); + this.stepMotorStatus.setZeroPosition(this.stepMotorStatus.getCurrentPosition() == 0); + this.stepMotorStatus.setStopped(true); + log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.stepMotorStatus)); } @Override public void easyMoveToZero() throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { log.debug("Easy move to zero."); - this.motorStatus.setCurrentPosition(0); - this.motorStatus.setZeroPosition(this.motorStatus.getCurrentPosition() == 0); - this.motorStatus.setStopped(true); - log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.motorStatus)); + this.stepMotorStatus.setCurrentPosition(0); + this.stepMotorStatus.setZeroPosition(this.stepMotorStatus.getCurrentPosition() == 0); + this.stepMotorStatus.setStopped(true); + log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.stepMotorStatus)); } @Override public void easyMoveToZeroPointQuick() { log.debug("Easy move to zero quick."); - this.motorStatus.setCurrentPosition(0); - this.motorStatus.setZeroPosition(this.motorStatus.getCurrentPosition() == 0); - this.motorStatus.setStopped(true); - log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.motorStatus)); + this.stepMotorStatus.setCurrentPosition(0); + this.stepMotorStatus.setZeroPosition(this.stepMotorStatus.getCurrentPosition() == 0); + this.stepMotorStatus.setStopped(true); + log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.stepMotorStatus)); } @Override public void enable() { log.debug("Enable step motor."); - this.motorStatus.setEnabled(true); - log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.motorStatus)); + this.stepMotorStatus.setEnabled(true); + log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.stepMotorStatus)); } @Override public void disable() { log.debug("Disable step motor."); - this.motorStatus.setEnabled(false); - log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.motorStatus)); + this.stepMotorStatus.setEnabled(false); + log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.stepMotorStatus)); } @Override @@ -89,8 +89,8 @@ public class VirtualStepMotor implements StepMotor { @Override public void stop() throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { log.debug("Stop step motor."); - this.motorStatus.setStopped(true); - log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.motorStatus)); + this.stepMotorStatus.setStopped(true); + log.debug("MotorStatus {}", JSONUtil.toJsonStr(this.stepMotorStatus)); } @Override @@ -113,12 +113,12 @@ public class VirtualStepMotor implements StepMotor { @Override public Integer readPosition() { - return this.motorStatus.getCurrentPosition(); + return this.stepMotorStatus.getCurrentPosition(); } @Override public Integer readEncoderPosition() { - return this.motorStatus.getEncoderPosition(); + return this.stepMotorStatus.getEncoderPosition(); } @Override diff --git a/src/main/java/com/iflytop/gd/system/devices/Door.java b/src/main/java/com/iflytop/gd/system/devices/Door.java index 0fc417a..aa9b14f 100644 --- a/src/main/java/com/iflytop/gd/system/devices/Door.java +++ b/src/main/java/com/iflytop/gd/system/devices/Door.java @@ -1,7 +1,18 @@ package com.iflytop.gd.system.devices; public interface Door { + /** + * 开门 + */ void open(); + + /** + * 关门 + */ void close(); + + /** + * 停止 + */ void stop(); } diff --git a/src/main/java/com/iflytop/gd/system/devices/ServoMotor.java b/src/main/java/com/iflytop/gd/system/devices/ServoMotor.java index a262005..1915a16 100644 --- a/src/main/java/com/iflytop/gd/system/devices/ServoMotor.java +++ b/src/main/java/com/iflytop/gd/system/devices/ServoMotor.java @@ -1,20 +1,82 @@ package com.iflytop.gd.system.devices; +import com.iflytop.gd.infrastructure.drivers.RegIndex; + /** * 伺服电机 */ public interface ServoMotor { + /** + * 使能 + */ void enable(); + /** + * 失能 + */ void disable(); + /** + * 停止 + */ void stop(); + + /** + * 获取当前位置 + * @return + */ Integer getCurrentPosition(); + + /** + * 移动到零点 + */ void moveToZero(); + + /** + * 移动到指定位置 + * @param position + */ void moveTo(Integer position); + + /****************寄存器配置开始*****************/ + + /** + * 设置最大速度 + * @param maxVelocity + */ + void setMaxVelocity(Integer maxVelocity); + + /** + * 设置最大力矩 + * @param maxTorque + */ + void setMaxTorque(Integer maxTorque); + + /** + * 设置保护力矩 + * @param protectiveTorque + */ + void setProtectiveTorque(Integer protectiveTorque); + + + /** + * 读取寄存器值 + * @param regIndex + * @return + */ + Integer readReg(RegIndex regIndex); + + + /** + * 设置寄存器 + * @param regIndex + * @param value + */ + void writeReg(RegIndex regIndex, Integer value); + /****************寄存器配置结束*****************/ } diff --git a/src/main/java/com/iflytop/gd/system/models/ServoMotorStatus.java b/src/main/java/com/iflytop/gd/system/models/ServoMotorStatus.java new file mode 100644 index 0000000..2e63103 --- /dev/null +++ b/src/main/java/com/iflytop/gd/system/models/ServoMotorStatus.java @@ -0,0 +1,12 @@ +package com.iflytop.gd.system.models; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class ServoMotorStatus { + private boolean stopped = false; + private boolean isEnabled = false; + private Integer currentPosition; +} diff --git a/src/main/java/com/iflytop/gd/system/models/MotorStatus.java b/src/main/java/com/iflytop/gd/system/models/StepMotorStatus.java similarity index 92% rename from src/main/java/com/iflytop/gd/system/models/MotorStatus.java rename to src/main/java/com/iflytop/gd/system/models/StepMotorStatus.java index d4a882a..e1648c2 100644 --- a/src/main/java/com/iflytop/gd/system/models/MotorStatus.java +++ b/src/main/java/com/iflytop/gd/system/models/StepMotorStatus.java @@ -5,7 +5,7 @@ import lombok.Setter; @Getter @Setter -public class MotorStatus { +public class StepMotorStatus { private boolean stopped = false; private boolean isEnabled = false; private Integer currentSpeed;