9 changed files with 323 additions and 62 deletions
-
72src/main/java/com/iflytop/colortitration/hardware/command/handlers/HeatRodHandler.java
-
2src/main/java/com/iflytop/colortitration/hardware/command/handlers/MiniServoHandler.java
-
93src/main/java/com/iflytop/colortitration/hardware/drivers/TecHeaterRodDriver.java
-
8src/main/java/com/iflytop/colortitration/hardware/type/CmdId.java
-
2src/main/java/com/iflytop/colortitration/hardware/type/MId.java
-
39src/main/java/com/iflytop/colortitration/hardware/type/RegIndex.java
-
33src/main/java/com/iflytop/colortitration/hardware/type/TecHeaterRegIndex.java
-
16src/main/java/com/iflytop/colortitration/hardware/type/TecHeaterRodMId.java
@ -0,0 +1,72 @@ |
|||
package com.iflytop.colortitration.hardware.command.handlers; |
|||
|
|||
import com.iflytop.colortitration.app.core.command.DeviceCommand; |
|||
import com.iflytop.colortitration.common.enums.Action; |
|||
import com.iflytop.colortitration.common.enums.Device; |
|||
import com.iflytop.colortitration.hardware.command.CommandHandler; |
|||
import com.iflytop.colortitration.hardware.drivers.TecHeaterRodDriver; |
|||
import com.iflytop.colortitration.hardware.type.MId; |
|||
import com.iflytop.colortitration.hardware.type.TecHeaterRodMId; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.security.InvalidParameterException; |
|||
import java.util.Map; |
|||
import java.util.Set; |
|||
|
|||
@Slf4j |
|||
@Component |
|||
@RequiredArgsConstructor |
|||
public class HeatRodHandler extends CommandHandler { |
|||
private final TecHeaterRodDriver driver_; |
|||
|
|||
private final Map<Device, TecHeaterRodMId> supportCmdDeviceTecHeaterRodMIdMap = Map.ofEntries( |
|||
Map.entry(Device.HEAT_ROD_1, TecHeaterRodMId.HeaterRod1), |
|||
Map.entry(Device.HEAT_ROD_2, TecHeaterRodMId.HeaterRod2) |
|||
); |
|||
|
|||
private final Map<Device, MId> supportCmdDeviceMIdMap = Map.ofEntries( |
|||
Map.entry(Device.HEAT_ROD_1, MId.HeaterRod1), |
|||
Map.entry(Device.HEAT_ROD_2, MId.HeaterRod2) |
|||
); |
|||
|
|||
private final Set<Action> supportActions = Set.of(Action.OPEN, Action.CLOSE); |
|||
|
|||
@Override |
|||
protected Map<Device, MId> getSupportCmdDeviceMIdMap() |
|||
{ |
|||
return supportCmdDeviceMIdMap; |
|||
} |
|||
|
|||
@Override |
|||
protected Set<Action> getSupportActions() { |
|||
return supportActions; |
|||
} |
|||
|
|||
@Override |
|||
protected void checkParams(DeviceCommand command) throws Exception { |
|||
// 检查参数 |
|||
if (command.getAction() == Action.OPEN) { |
|||
// 检查参数 |
|||
} |
|||
|
|||
} |
|||
|
|||
MId getMId(Device cmdDevice){ |
|||
return supportCmdDeviceMIdMap.get(cmdDevice); |
|||
} |
|||
|
|||
@Override |
|||
public void handleCommand(DeviceCommand command) throws Exception { |
|||
// 发送命令 |
|||
|
|||
if (command.getAction() == Action.OPEN) { |
|||
Double temp = command.getParam().getTemperature(); |
|||
driver_.open(supportCmdDeviceTecHeaterRodMIdMap.get(command.getDevice()), temp); |
|||
|
|||
} else if (command.getAction() == Action.CLOSE) { |
|||
driver_.close(supportCmdDeviceTecHeaterRodMIdMap.get(command.getDevice())); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,93 @@ |
|||
package com.iflytop.colortitration.hardware.drivers; |
|||
|
|||
import com.fasterxml.jackson.databind.node.ObjectNode; |
|||
import com.iflytop.colortitration.hardware.comm.can.A8kCanBusService; |
|||
import com.iflytop.colortitration.hardware.exception.HardwareException; |
|||
import com.iflytop.colortitration.hardware.type.A8kPacket; |
|||
import com.iflytop.colortitration.hardware.type.CmdId; |
|||
import com.iflytop.colortitration.hardware.type.TecHeaterRegIndex; |
|||
import com.iflytop.colortitration.hardware.type.TecHeaterRodMId; |
|||
import com.iflytop.colortitration.hardware.utils.ZJsonHelper; |
|||
import lombok.RequiredArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
@Component |
|||
@Slf4j |
|||
@RequiredArgsConstructor |
|||
public class TecHeaterRodDriver { |
|||
final private A8kCanBusService canBus; |
|||
static final Double DEFAULT_TARGET_TEMPERATURE = 100.0; |
|||
static final Integer DEFAULT_CHANNEL = 1; |
|||
|
|||
// application API |
|||
public void open(TecHeaterRodMId id, Double temperature) throws HardwareException { |
|||
this.enable(id, true); |
|||
this.setTargetTemperature(id, temperature); |
|||
} |
|||
|
|||
public void close(TecHeaterRodMId id) throws HardwareException { |
|||
this.enable(id, false); |
|||
} |
|||
|
|||
// driver API |
|||
public void openPower(TecHeaterRodMId id, boolean is_open) throws HardwareException { |
|||
log.info("openPower called with id: {}, is_open: {}", id, is_open); |
|||
Integer powerOn = is_open ? 1 : 0; |
|||
canBus.callcmd(id.mid, CmdId.tec103_heater_power_on, powerOn); |
|||
} |
|||
|
|||
public void enable(TecHeaterRodMId id, boolean is_enable) throws HardwareException { |
|||
log.info("enable called with id: {}, is_enable: {}", id, is_enable); |
|||
Integer enable = is_enable ? 1 : 0; |
|||
canBus.callcmd(id.mid, CmdId.tec103_heater_enable, DEFAULT_CHANNEL, enable); |
|||
} |
|||
|
|||
public void setTargetTemperature(TecHeaterRodMId id, Double targetTemperature) throws HardwareException { |
|||
Integer targetTemperatureInt = (int) (targetTemperature * DEFAULT_TARGET_TEMPERATURE); |
|||
log.info("setTargetTemperature called with id: {}, targetTemperature: {}", id, targetTemperature); |
|||
canBus.callcmd(id.mid, CmdId.tec103_heater_set_target_temperature, DEFAULT_CHANNEL, targetTemperatureInt); |
|||
} |
|||
|
|||
public Double readTemperature(TecHeaterRodMId id) throws HardwareException { |
|||
log.info("readTemperature called with id: {}", id); |
|||
A8kPacket packet = canBus.callcmd(id.mid, CmdId.tec103_heater_read_temperature, DEFAULT_CHANNEL); |
|||
return packet.getContentI32(0) / DEFAULT_TARGET_TEMPERATURE; |
|||
} |
|||
|
|||
public void factoryReset(TecHeaterRodMId id) throws HardwareException { |
|||
log.info("factoryReset called with id: {}", id); |
|||
canBus.callcmd(id.mid, CmdId.tec103_heater_factory_reset); |
|||
} |
|||
|
|||
public void reset(TecHeaterRodMId id) throws HardwareException { |
|||
log.info("reset called with id: {}", id); |
|||
canBus.callcmd(id.mid, CmdId.liquid_valve_reset); |
|||
} |
|||
|
|||
|
|||
public void waitForMod(TecHeaterRodMId id, Integer overtime) throws HardwareException { |
|||
canBus.waitForMod(id.mid, overtime); |
|||
} |
|||
|
|||
public void setReg(TecHeaterRodMId id, TecHeaterRegIndex regindex, int val) throws HardwareException { |
|||
log.info("setReg called with id: {}, regindex: {}, val: {}", id, regindex, val); |
|||
canBus.moduleSetReg(id.mid, regindex.regIndex, val); |
|||
} |
|||
|
|||
public Integer getReg(TecHeaterRodMId id, TecHeaterRegIndex regindex) throws HardwareException { |
|||
log.info("getReg called with id: {}, regindex: {}", id, regindex); |
|||
return canBus.moduleGetReg(id.mid, regindex.regIndex); |
|||
} |
|||
|
|||
public Object getAllReg(TecHeaterRodMId id) throws HardwareException { |
|||
log.info("getAllReg called with id: {}", id); |
|||
ObjectNode node = ZJsonHelper.createObjectNode(); |
|||
for (TecHeaterRegIndex regIndex : TecHeaterRegIndex.values()) { |
|||
Integer regVal = getReg(id, regIndex); |
|||
log.info("read reg {} -> {}", regIndex, regVal); |
|||
node.put(regIndex.name(), getReg(id, regIndex)); |
|||
} |
|||
return node; |
|||
} |
|||
} |
@ -0,0 +1,33 @@ |
|||
package com.iflytop.colortitration.hardware.type; |
|||
|
|||
|
|||
public enum TecHeaterRegIndex { |
|||
kreg_tec103_heater1_target_temp(RegIndex.kreg_tec103_heater1_target_temp), |
|||
kreg_tec103_heater1_real_temp(RegIndex.kreg_tec103_heater1_real_temp), |
|||
kreg_tec103_heater1_sensor_r(RegIndex.kreg_tec103_heater1_sensor_r), |
|||
kreg_tec103_heater1_polynomial(RegIndex.kreg_tec103_heater1_polynomial), |
|||
kreg_tec103_heater1_ntc_b(RegIndex.kreg_tec103_heater1_ntc_b), |
|||
kreg_tec103_heater1_ntc_r0(RegIndex.kreg_tec103_heater1_ntc_r0), |
|||
kreg_tec103_heater1_output_enable(RegIndex.kreg_tec103_heater1_output_enable), |
|||
kreg_tec103_heater1_output_max_duty(RegIndex.kreg_tec103_heater1_output_max_duty), |
|||
kreg_tec103_heater1_output_mode(RegIndex.kreg_tec103_heater1_output_mode), |
|||
kreg_tec103_heater1_output_pol(RegIndex.kreg_tec103_heater1_output_pol), |
|||
kreg_tec103_heater1_output_percent(RegIndex.kreg_tec103_heater1_output_percent), |
|||
kreg_tec103_heater1_pwm_freq(RegIndex.kreg_tec103_heater1_pwm_freq), |
|||
kreg_tec103_heater1_over_temp_protect_mode(RegIndex.kreg_tec103_heater1_over_temp_protect_mode), |
|||
kreg_tec103_heater1_p(RegIndex.kreg_tec103_heater1_p), |
|||
kreg_tec103_heater1_i(RegIndex.kreg_tec103_heater1_i), |
|||
kreg_tec103_heater1_d(RegIndex.kreg_tec103_heater1_d), |
|||
kreg_tec103_heater1_pid_self_tune(RegIndex.kreg_tec103_heater1_pid_self_tune), |
|||
kreg_tec103_heater_version(RegIndex.kreg_tec103_heater_version), |
|||
kreg_tec103_heater_address(RegIndex.kreg_tec103_heater_address), |
|||
kreg_tec103_heater_baud_rate(RegIndex.kreg_tec103_heater_baud_rate), |
|||
kreg_tec103_heater_e_code(RegIndex.kreg_tec103_heater_e_code), |
|||
; |
|||
|
|||
public final RegIndex regIndex; |
|||
|
|||
TecHeaterRegIndex(RegIndex regIndex) { |
|||
this.regIndex = regIndex; |
|||
} |
|||
} |
@ -0,0 +1,16 @@ |
|||
package com.iflytop.colortitration.hardware.type; |
|||
|
|||
import org.springframework.util.Assert; |
|||
|
|||
public enum TecHeaterRodMId { |
|||
HeaterRod1(MId.HeaterRod1), |
|||
HeaterRod2(MId.HeaterRod2), |
|||
; |
|||
|
|||
final public MId mid; |
|||
|
|||
TecHeaterRodMId(MId mid) { |
|||
Assert.isTrue(this.name().equals(mid.name()), "IO1_ChimeBuzzer Init fail"); |
|||
this.mid = mid; |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue