14 changed files with 226 additions and 153 deletions
-
170src/main/java/com/iflytop/handacid/hardware/command/handlers/CeramicPumpHandler.java
-
4src/main/java/com/iflytop/handacid/hardware/command/handlers/IOCtrlHandler.java
-
8src/main/java/com/iflytop/handacid/hardware/command/handlers/MotorHandler.java
-
62src/main/java/com/iflytop/handacid/hardware/service/BleGamepadService.java
-
15src/main/java/com/iflytop/handacid/hardware/type/BleGamepadMid.java
-
6src/main/java/com/iflytop/handacid/hardware/type/CmdId.java
-
9src/main/java/com/iflytop/handacid/hardware/type/IO/OutputIOMId.java
-
44src/main/java/com/iflytop/handacid/hardware/type/MId.java
-
5src/main/java/com/iflytop/handacid/hardware/type/ModuleType.java
-
2src/main/java/com/iflytop/handacid/hardware/type/Servo/MiniServoId.java
-
2src/main/java/com/iflytop/handacid/hardware/type/Servo/MiniServoMId.java
-
24src/main/java/com/iflytop/handacid/hardware/type/StepMotor/StepMotorMId.java
-
4src/main/java/com/iflytop/handacid/hardware/type/TecHeaterRodMId.java
-
24src/main/java/com/iflytop/handacid/hardware/type/appevent/A8kHardwareReport.java
@ -1,85 +1,85 @@ |
|||
package com.iflytop.handacid.hardware.command.handlers; |
|||
|
|||
import com.iflytop.handacid.app.core.command.DeviceCommand; |
|||
import com.iflytop.handacid.common.enums.Action; |
|||
import com.iflytop.handacid.common.enums.Device; |
|||
import com.iflytop.handacid.common.enums.MotorDirection; |
|||
import com.iflytop.handacid.hardware.command.CommandHandler; |
|||
import com.iflytop.handacid.hardware.drivers.CeramicPumpDriver; |
|||
import com.iflytop.handacid.hardware.type.CeramicPumpDriverSlaveId; |
|||
import com.iflytop.handacid.hardware.type.MId; |
|||
import com.iflytop.handacid.hardware.type.StepMotor.MotorDirect; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.Map; |
|||
import java.util.Set; |
|||
import java.util.stream.Collectors; |
|||
|
|||
import static java.lang.Math.abs; |
|||
|
|||
@Slf4j |
|||
@Component |
|||
@RequiredArgsConstructor |
|||
public class CeramicPumpHandler extends CommandHandler { |
|||
private final CeramicPumpDriver driver_; |
|||
|
|||
private final Map<Device, CeramicPumpDriverSlaveId> stepMotorMIdMap_ = Map.ofEntries( |
|||
Map.entry(Device.PUMP_1, CeramicPumpDriverSlaveId.CERAMIC_PUMP1_ID), |
|||
Map.entry(Device.PUMP_2, CeramicPumpDriverSlaveId.CERAMIC_PUMP2_ID) |
|||
); |
|||
private final Map<Device, MId> supportCmdDeviceMIdMap = stepMotorMIdMap_.entrySet().stream(). |
|||
collect(Collectors.toMap(Map.Entry::getKey, entry -> MId.NotSet)); |
|||
|
|||
private final Set<Action> supportActions = Set.of( |
|||
Action.SET, Action.CLOSE_CLAMP, |
|||
Action.MOVE_BY, |
|||
Action.STOP); |
|||
|
|||
@Override |
|||
protected Map<Device, MId> getSupportCmdDeviceMIdMap() { |
|||
return supportCmdDeviceMIdMap; |
|||
} |
|||
|
|||
@Override |
|||
protected Set<Action> getSupportActions() { |
|||
return supportActions; |
|||
} |
|||
|
|||
public MotorDirect getMotorDirect(MotorDirection cmdDirection) |
|||
{ |
|||
return switch (cmdDirection) { |
|||
case FORWARD -> MotorDirect.FORWARD; |
|||
case BACKWARD -> MotorDirect.BACKWARD; |
|||
}; |
|||
} |
|||
|
|||
@Override |
|||
public void handleCommand(DeviceCommand command) throws Exception { |
|||
// 发送命令 |
|||
CeramicPumpDriverSlaveId slaveId = stepMotorMIdMap_.get(command.getDevice()); |
|||
|
|||
switch (command.getAction()) { |
|||
case MOVE_BY -> { |
|||
double distance = command.getParam().getPosition(); |
|||
log.info("Motor {} Move By Distance {}", slaveId, distance); |
|||
CeramicPumpDriver.Direction direction = distance < 0 ? |
|||
CeramicPumpDriver.Direction.BACKWARD : CeramicPumpDriver.Direction.FORWARD; |
|||
driver_.moveByBlock(slaveId, direction, abs((int)(distance))); |
|||
} |
|||
case STOP -> { |
|||
log.info("Motor {} Stop", slaveId); |
|||
driver_.stopBlock(slaveId); |
|||
} |
|||
case SET -> { |
|||
Double speed = command.getParam().getSpeed(); |
|||
if (speed != null) { |
|||
double d_speed = speed; |
|||
log.info("Motor {} Set Speed {}", slaveId, speed); |
|||
driver_.setSpeed(slaveId, (int)d_speed); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
//package com.iflytop.handacid.hardware.command.handlers; |
|||
// |
|||
//import com.iflytop.handacid.app.core.command.DeviceCommand; |
|||
//import com.iflytop.handacid.common.enums.Action; |
|||
//import com.iflytop.handacid.common.enums.Device; |
|||
//import com.iflytop.handacid.common.enums.MotorDirection; |
|||
//import com.iflytop.handacid.hardware.command.CommandHandler; |
|||
//import com.iflytop.handacid.hardware.drivers.CeramicPumpDriver; |
|||
//import com.iflytop.handacid.hardware.type.CeramicPumpDriverSlaveId; |
|||
//import com.iflytop.handacid.hardware.type.MId; |
|||
//import com.iflytop.handacid.hardware.type.StepMotor.MotorDirect; |
|||
//import lombok.RequiredArgsConstructor; |
|||
//import lombok.extern.slf4j.Slf4j; |
|||
//import org.springframework.stereotype.Component; |
|||
// |
|||
//import java.util.Map; |
|||
//import java.util.Set; |
|||
//import java.util.stream.Collectors; |
|||
// |
|||
//import static java.lang.Math.abs; |
|||
// |
|||
//@Slf4j |
|||
//@Component |
|||
//@RequiredArgsConstructor |
|||
//public class CeramicPumpHandler extends CommandHandler { |
|||
// private final CeramicPumpDriver driver_; |
|||
// |
|||
// private final Map<Device, CeramicPumpDriverSlaveId> stepMotorMIdMap_ = Map.ofEntries( |
|||
// Map.entry(Device.PUMP_1, CeramicPumpDriverSlaveId.CERAMIC_PUMP1_ID), |
|||
// Map.entry(Device.PUMP_2, CeramicPumpDriverSlaveId.CERAMIC_PUMP2_ID) |
|||
// ); |
|||
// private final Map<Device, MId> supportCmdDeviceMIdMap = stepMotorMIdMap_.entrySet().stream(). |
|||
// collect(Collectors.toMap(Map.Entry::getKey, entry -> MId.NotSet)); |
|||
// |
|||
// private final Set<Action> supportActions = Set.of( |
|||
// Action.SET, Action.CLOSE_CLAMP, |
|||
// Action.MOVE_BY, |
|||
// Action.STOP); |
|||
// |
|||
// @Override |
|||
// protected Map<Device, MId> getSupportCmdDeviceMIdMap() { |
|||
// return supportCmdDeviceMIdMap; |
|||
// } |
|||
// |
|||
// @Override |
|||
// protected Set<Action> getSupportActions() { |
|||
// return supportActions; |
|||
// } |
|||
// |
|||
// public MotorDirect getMotorDirect(MotorDirection cmdDirection) |
|||
// { |
|||
// return switch (cmdDirection) { |
|||
// case FORWARD -> MotorDirect.FORWARD; |
|||
// case BACKWARD -> MotorDirect.BACKWARD; |
|||
// }; |
|||
// } |
|||
// |
|||
// @Override |
|||
// public void handleCommand(DeviceCommand command) throws Exception { |
|||
// // 发送命令 |
|||
// CeramicPumpDriverSlaveId slaveId = stepMotorMIdMap_.get(command.getDevice()); |
|||
// |
|||
// switch (command.getAction()) { |
|||
// case MOVE_BY -> { |
|||
// double distance = command.getParam().getPosition(); |
|||
// log.info("Motor {} Move By Distance {}", slaveId, distance); |
|||
// CeramicPumpDriver.Direction direction = distance < 0 ? |
|||
// CeramicPumpDriver.Direction.BACKWARD : CeramicPumpDriver.Direction.FORWARD; |
|||
// driver_.moveByBlock(slaveId, direction, abs((int)(distance))); |
|||
// } |
|||
// case STOP -> { |
|||
// log.info("Motor {} Stop", slaveId); |
|||
// driver_.stopBlock(slaveId); |
|||
// } |
|||
// case SET -> { |
|||
// Double speed = command.getParam().getSpeed(); |
|||
// if (speed != null) { |
|||
// double d_speed = speed; |
|||
// log.info("Motor {} Set Speed {}", slaveId, speed); |
|||
// driver_.setSpeed(slaveId, (int)d_speed); |
|||
// } |
|||
// } |
|||
// } |
|||
// } |
|||
//} |
@ -0,0 +1,62 @@ |
|||
package com.iflytop.handacid.hardware.service; |
|||
|
|||
import com.iflytop.handacid.hardware.comm.can.A8kCanBusService; |
|||
import com.iflytop.handacid.hardware.type.A8kPacket; |
|||
import com.iflytop.handacid.hardware.type.CmdId; |
|||
import com.iflytop.handacid.hardware.type.appevent.A8kCanBusOnConnectEvent; |
|||
import com.iflytop.handacid.hardware.type.appevent.A8kHardwareReport; |
|||
import com.iflytop.handacid.hardware.type.appevent.AppEvent; |
|||
import jakarta.annotation.PostConstruct; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Component; |
|||
import jakarta.annotation.PostConstruct; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
/** |
|||
* 蓝牙手柄服务管理 |
|||
* |
|||
*/ |
|||
|
|||
@Component |
|||
@Slf4j |
|||
@RequiredArgsConstructor |
|||
public class BleGamepadService { |
|||
private final A8kCanBusService canBus; |
|||
private final AppEventBusService eventBus; |
|||
|
|||
@PostConstruct |
|||
synchronized public void init() { |
|||
eventBus.regListener(this::onAppEvent); |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 事件总线事件处理 |
|||
* @param event e |
|||
*/ |
|||
private void onAppEvent(AppEvent event) { |
|||
if (event instanceof A8kHardwareReport canPacket) { |
|||
A8kPacket packet = canPacket.getReportPacket(); |
|||
CmdId cmdId = CmdId.valueOf(packet.getCmdId()); |
|||
if (CmdId.event_ble_gamepad_liquid_acid.equals(cmdId)) { |
|||
log.info("蓝牙手柄 加酸按钮 按下"); |
|||
} else if (CmdId.event_ble_gamepad_liquid_acid_prefilling.equals(cmdId)) { |
|||
log.info("蓝牙手柄 预充按钮 按下"); |
|||
} |
|||
else if (CmdId.event_ble_gamepad_connected.equals(cmdId)) { |
|||
log.info("蓝牙手柄 连接成功"); |
|||
} |
|||
else if (CmdId.event_ble_gamepad_disconnected.equals(cmdId)) { |
|||
log.info("蓝牙手柄 连接断开"); |
|||
} |
|||
|
|||
} else if (event instanceof A8kCanBusOnConnectEvent canPacket) { |
|||
log.info("a8k canbus connect sucess"); |
|||
} |
|||
} |
|||
|
|||
} |
|||
|
@ -0,0 +1,15 @@ |
|||
package com.iflytop.handacid.hardware.type; |
|||
|
|||
import org.springframework.util.Assert; |
|||
public enum BleGamepadMid { |
|||
BleGamePad(MId.BleGamePad), |
|||
; |
|||
|
|||
final public MId mid; |
|||
|
|||
BleGamepadMid(MId mid) { |
|||
Assert.isTrue(this.name().equals(mid.name()), "BleGamepadMid Init fail"); |
|||
this.mid = mid; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,24 @@ |
|||
package com.iflytop.handacid.hardware.type.appevent; |
|||
|
|||
import com.iflytop.handacid.hardware.type.A8kPacket; |
|||
import org.springframework.lang.NonNull; |
|||
|
|||
/** |
|||
* A8k底层硬件上报事件 |
|||
*/ |
|||
public class A8kHardwareReport extends AppEvent{ |
|||
A8kPacket reportPacket; |
|||
public A8kHardwareReport(@NonNull A8kPacket packet) { |
|||
super(A8kHardwareReport.class.getSimpleName()); |
|||
this.reportPacket = packet; |
|||
} |
|||
|
|||
public A8kPacket getReportPacket(){ |
|||
return reportPacket; |
|||
} |
|||
|
|||
public String toString(){ |
|||
return String.format("|Event A8kHardwareReport :%s|", reportPacket.toString()); |
|||
} |
|||
} |
|||
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue