Browse Source

feat:实现伺服电机定义及虚拟伺服电机实现

tags/freeze
黄翔 3 months ago
parent
commit
eea06a0bcf
  1. 65
      src/main/java/com/iflytop/gd/infrastructure/devices/StandardServoMotor.java
  2. 75
      src/main/java/com/iflytop/gd/infrastructure/devices/VirtualServoMotor.java
  3. 50
      src/main/java/com/iflytop/gd/infrastructure/devices/VirtualStepMotor.java
  4. 11
      src/main/java/com/iflytop/gd/system/devices/Door.java
  5. 62
      src/main/java/com/iflytop/gd/system/devices/ServoMotor.java
  6. 12
      src/main/java/com/iflytop/gd/system/models/ServoMotorStatus.java
  7. 2
      src/main/java/com/iflytop/gd/system/models/StepMotorStatus.java

65
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) {
}
}

75
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<RegIndex, Integer> registers = new HashMap<RegIndex, Integer>();
@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);
}
}

50
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.devices.StepMotor;
import com.iflytop.gd.system.exceptions.CommandExecTimeoutException; import com.iflytop.gd.system.exceptions.CommandExecTimeoutException;
import com.iflytop.gd.system.exceptions.HardwareErrorException; 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 lombok.extern.slf4j.Slf4j;
import java.io.IOException; import java.io.IOException;
@ -20,7 +20,7 @@ import java.util.Map;
*/ */
@Slf4j @Slf4j
public class VirtualStepMotor implements StepMotor { public class VirtualStepMotor implements StepMotor {
private MotorStatus motorStatus = new MotorStatus();
private StepMotorStatus stepMotorStatus = new StepMotorStatus();
private Map<RegIndex, Integer> registers = new HashMap<RegIndex, Integer>(); private Map<RegIndex, Integer> registers = new HashMap<RegIndex, Integer>();
private final ModuleId moduleId; private final ModuleId moduleId;
@ -30,50 +30,50 @@ public class VirtualStepMotor implements StepMotor {
@Override @Override
public void easyMoveBy(Integer value, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { 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 @Override
public void easyMoveTo(Integer value, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { public void easyMoveTo(Integer value, DistanceUnit unit) throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException {
log.debug("Moving to {}", value); 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 @Override
public void easyMoveToZero() throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { public void easyMoveToZero() throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException {
log.debug("Easy move to zero."); 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 @Override
public void easyMoveToZeroPointQuick() { public void easyMoveToZeroPointQuick() {
log.debug("Easy move to zero quick."); 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 @Override
public void enable() { public void enable() {
log.debug("Enable step motor."); 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 @Override
public void disable() { public void disable() {
log.debug("Disable step motor."); 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 @Override
@ -89,8 +89,8 @@ public class VirtualStepMotor implements StepMotor {
@Override @Override
public void stop() throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException { public void stop() throws HardwareErrorException, CommandExecTimeoutException, IOException, InterruptedException {
log.debug("Stop step motor."); 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 @Override
@ -113,12 +113,12 @@ public class VirtualStepMotor implements StepMotor {
@Override @Override
public Integer readPosition() { public Integer readPosition() {
return this.motorStatus.getCurrentPosition();
return this.stepMotorStatus.getCurrentPosition();
} }
@Override @Override
public Integer readEncoderPosition() { public Integer readEncoderPosition() {
return this.motorStatus.getEncoderPosition();
return this.stepMotorStatus.getEncoderPosition();
} }
@Override @Override

11
src/main/java/com/iflytop/gd/system/devices/Door.java

@ -1,7 +1,18 @@
package com.iflytop.gd.system.devices; package com.iflytop.gd.system.devices;
public interface Door { public interface Door {
/**
* 开门
*/
void open(); void open();
/**
* 关门
*/
void close(); void close();
/**
* 停止
*/
void stop(); void stop();
} }

62
src/main/java/com/iflytop/gd/system/devices/ServoMotor.java

@ -1,20 +1,82 @@
package com.iflytop.gd.system.devices; package com.iflytop.gd.system.devices;
import com.iflytop.gd.infrastructure.drivers.RegIndex;
/** /**
* 伺服电机 * 伺服电机
*/ */
public interface ServoMotor { public interface ServoMotor {
/**
* 使能
*/
void enable(); void enable();
/**
* 失能
*/
void disable(); void disable();
/**
* 停止
*/
void stop(); void stop();
/**
* 获取当前位置
* @return
*/
Integer getCurrentPosition(); Integer getCurrentPosition();
/**
* 移动到零点
*/
void moveToZero(); void moveToZero();
/**
* 移动到指定位置
* @param position
*/
void moveTo(Integer 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);
/****************寄存器配置结束*****************/
} }

12
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;
}

2
src/main/java/com/iflytop/gd/system/models/MotorStatus.java → src/main/java/com/iflytop/gd/system/models/StepMotorStatus.java

@ -5,7 +5,7 @@ import lombok.Setter;
@Getter @Getter
@Setter @Setter
public class MotorStatus {
public class StepMotorStatus {
private boolean stopped = false; private boolean stopped = false;
private boolean isEnabled = false; private boolean isEnabled = false;
private Integer currentSpeed; private Integer currentSpeed;
Loading…
Cancel
Save