From aa3ea916a16dc58b65926a2bec78f8648c0c876a Mon Sep 17 00:00:00 2001 From: HSZ_HeSongZhen <210202959@qq.com> Date: Thu, 24 Jul 2025 21:45:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8A=A0=E7=83=AD=E6=A3=92?= =?UTF-8?q?=E6=8C=87=E4=BB=A4=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hardware/command/handlers/ClawHandler.java | 61 -------------- .../hardware/command/handlers/HeatRodHandler.java | 72 +++++++++++++++++ .../command/handlers/MiniServoHandler.java | 61 ++++++++++++++ .../hardware/drivers/TecHeaterRodDriver.java | 93 ++++++++++++++++++++++ .../colortitration/hardware/type/CmdId.java | 8 +- .../iflytop/colortitration/hardware/type/MId.java | 2 + .../colortitration/hardware/type/RegIndex.java | 39 +++++++++ .../hardware/type/TecHeaterRegIndex.java | 33 ++++++++ .../hardware/type/TecHeaterRodMId.java | 16 ++++ 9 files changed, 323 insertions(+), 62 deletions(-) delete mode 100644 src/main/java/com/iflytop/colortitration/hardware/command/handlers/ClawHandler.java create mode 100644 src/main/java/com/iflytop/colortitration/hardware/command/handlers/MiniServoHandler.java create mode 100644 src/main/java/com/iflytop/colortitration/hardware/drivers/TecHeaterRodDriver.java create mode 100644 src/main/java/com/iflytop/colortitration/hardware/type/TecHeaterRegIndex.java create mode 100644 src/main/java/com/iflytop/colortitration/hardware/type/TecHeaterRodMId.java diff --git a/src/main/java/com/iflytop/colortitration/hardware/command/handlers/ClawHandler.java b/src/main/java/com/iflytop/colortitration/hardware/command/handlers/ClawHandler.java deleted file mode 100644 index 1578372..0000000 --- a/src/main/java/com/iflytop/colortitration/hardware/command/handlers/ClawHandler.java +++ /dev/null @@ -1,61 +0,0 @@ -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.MiniServoDriverWrapper; -import com.iflytop.colortitration.hardware.type.MId; -import com.iflytop.colortitration.hardware.type.Servo.MiniServoMId; -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; - -@Slf4j -@Component -@RequiredArgsConstructor -public class ClawHandler extends CommandHandler { - private final MiniServoDriverWrapper driver_; - - private final Map servoMIdMap_ = Map.ofEntries( - Map.entry(Device.CLAW, MiniServoMId.RobotArmClawSV) - ); - private final Map supportCmdDeviceMIdMap = servoMIdMap_.entrySet().stream(). - collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().mid)); - private final Set supportActions = Set.of(Action.SET, Action.ORIGIN, Action.MOVE, Action.STOP); - - @Override - protected Map getSupportCmdDeviceMIdMap() - { - return supportCmdDeviceMIdMap; - } - - @Override - protected Set getSupportActions() { - return supportActions; - } - - @Override - public void handleCommand(DeviceCommand command) throws Exception { - // 发送命令 - MiniServoMId servoMId = servoMIdMap_.get(command.getDevice()); - if(command.getAction() == Action.STOP) { - driver_.stop(servoMId); - } - else if(command.getAction() == Action.MOVE) { - double position = command.getParam().getPosition(); - driver_.moveTo(servoMId, position); - } - else if(command.getAction() == Action.SET) { - Double speed = command.getParam().getSpeed(); - if(speed != null) { - driver_.setSpeed(servoMId, speed); - } - } - } -} diff --git a/src/main/java/com/iflytop/colortitration/hardware/command/handlers/HeatRodHandler.java b/src/main/java/com/iflytop/colortitration/hardware/command/handlers/HeatRodHandler.java index e69de29..3cd8853 100644 --- a/src/main/java/com/iflytop/colortitration/hardware/command/handlers/HeatRodHandler.java +++ b/src/main/java/com/iflytop/colortitration/hardware/command/handlers/HeatRodHandler.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 supportCmdDeviceTecHeaterRodMIdMap = Map.ofEntries( + Map.entry(Device.HEAT_ROD_1, TecHeaterRodMId.HeaterRod1), + Map.entry(Device.HEAT_ROD_2, TecHeaterRodMId.HeaterRod2) + ); + + private final Map supportCmdDeviceMIdMap = Map.ofEntries( + Map.entry(Device.HEAT_ROD_1, MId.HeaterRod1), + Map.entry(Device.HEAT_ROD_2, MId.HeaterRod2) + ); + + private final Set supportActions = Set.of(Action.OPEN, Action.CLOSE); + + @Override + protected Map getSupportCmdDeviceMIdMap() + { + return supportCmdDeviceMIdMap; + } + + @Override + protected Set 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())); + } + } +} diff --git a/src/main/java/com/iflytop/colortitration/hardware/command/handlers/MiniServoHandler.java b/src/main/java/com/iflytop/colortitration/hardware/command/handlers/MiniServoHandler.java new file mode 100644 index 0000000..e0a96bb --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/hardware/command/handlers/MiniServoHandler.java @@ -0,0 +1,61 @@ +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.MiniServoDriverWrapper; +import com.iflytop.colortitration.hardware.type.MId; +import com.iflytop.colortitration.hardware.type.Servo.MiniServoMId; +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; + +@Slf4j +@Component +@RequiredArgsConstructor +public class MiniServoHandler extends CommandHandler { + private final MiniServoDriverWrapper driver_; + + private final Map servoMIdMap_ = Map.ofEntries( + Map.entry(Device.CLAW, MiniServoMId.RobotArmClawSV) + ); + private final Map supportCmdDeviceMIdMap = servoMIdMap_.entrySet().stream(). + collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().mid)); + private final Set supportActions = Set.of(Action.SET, Action.ORIGIN, Action.MOVE, Action.STOP); + + @Override + protected Map getSupportCmdDeviceMIdMap() + { + return supportCmdDeviceMIdMap; + } + + @Override + protected Set getSupportActions() { + return supportActions; + } + + @Override + public void handleCommand(DeviceCommand command) throws Exception { + // 发送命令 + MiniServoMId servoMId = servoMIdMap_.get(command.getDevice()); + if(command.getAction() == Action.STOP) { + driver_.stop(servoMId); + } + else if(command.getAction() == Action.MOVE) { + double position = command.getParam().getPosition(); + driver_.moveTo(servoMId, position); + } + else if(command.getAction() == Action.SET) { + Double speed = command.getParam().getSpeed(); + if(speed != null) { + driver_.setSpeed(servoMId, speed); + } + } + } +} diff --git a/src/main/java/com/iflytop/colortitration/hardware/drivers/TecHeaterRodDriver.java b/src/main/java/com/iflytop/colortitration/hardware/drivers/TecHeaterRodDriver.java new file mode 100644 index 0000000..0d19bd1 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/hardware/drivers/TecHeaterRodDriver.java @@ -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; + } +} diff --git a/src/main/java/com/iflytop/colortitration/hardware/type/CmdId.java b/src/main/java/com/iflytop/colortitration/hardware/type/CmdId.java index ca2a39e..ada8cc4 100644 --- a/src/main/java/com/iflytop/colortitration/hardware/type/CmdId.java +++ b/src/main/java/com/iflytop/colortitration/hardware/type/CmdId.java @@ -110,7 +110,13 @@ public enum CmdId { liquid_valve_stop(0x5704, "LIQUID_VALVE_STOP"), liquid_valve_reset(0x5705, "LIQUID_VALVE_RESET"), liquid_valve_home_reset(0x5706, "LIQUID_VALVE_HOME_RESET"), - liquid_valve_force_stop(0x5707, "LIQUID_VALVE_FORCE_STOP"); + liquid_valve_force_stop(0x5707, "LIQUID_VALVE_FORCE_STOP"), + + tec103_heater_power_on(0x5801, "TEC103_HEATER_POWER_ON"), + tec103_heater_enable(0x5802, "TEC103_HEATER_ENABLE"), + tec103_heater_set_target_temperature(0x5803, "TEC103_HEATER_SET_TARGET_TEMPERATURE"), + tec103_heater_read_temperature(0x5804, "TEC103_HEATER_READ_TEMPERATURE"), + tec103_heater_factory_reset(0x5805, "TEC103_HEATER_FACTORY_RESET"), ; public final static int ATTACH_IS_BYTES = 1; diff --git a/src/main/java/com/iflytop/colortitration/hardware/type/MId.java b/src/main/java/com/iflytop/colortitration/hardware/type/MId.java index f2a930b..c741d11 100644 --- a/src/main/java/com/iflytop/colortitration/hardware/type/MId.java +++ b/src/main/java/com/iflytop/colortitration/hardware/type/MId.java @@ -40,6 +40,8 @@ public enum MId { TitratorStir2M(77, "滴定仪搅拌2泵"), Titrator1M(78, "滴定仪1泵"), Titrator2M(79, "滴定仪2泵"), + HeaterRod1(80, "加热棒1"), + HeaterRod2(81, "加热棒2"), ; final public String description; diff --git a/src/main/java/com/iflytop/colortitration/hardware/type/RegIndex.java b/src/main/java/com/iflytop/colortitration/hardware/type/RegIndex.java index e5477ee..3dbf2c9 100644 --- a/src/main/java/com/iflytop/colortitration/hardware/type/RegIndex.java +++ b/src/main/java/com/iflytop/colortitration/hardware/type/RegIndex.java @@ -155,6 +155,45 @@ public enum RegIndex { kreg_leisai_servo_high_velocity(10707), // 高速 kreg_leisai_servo_acc_time(10708), // 加速度时间 kreg_leisai_servo_dcc_time(10709), // 减速度时间 + kreg_tec103_heater1_target_temp(10800), // 目标温度 + kreg_tec103_heater1_real_temp(10801), // 读取温度 + kreg_tec103_heater1_sensor_r(10802), // 查询传感器电阻值 + kreg_tec103_heater1_polynomial(10803), // 温度求解模型选择 + kreg_tec103_heater1_ntc_b(10804), // NTC B值 + kreg_tec103_heater1_ntc_r0(10805), // NTC R0值 + kreg_tec103_heater1_output_enable(10807), // 使能 + kreg_tec103_heater1_output_max_duty(10808), // 最大输出占空比 + kreg_tec103_heater1_output_mode(10809), // 输出模式 + kreg_tec103_heater1_output_pol(10810), // 输出极性 + kreg_tec103_heater1_output_percent(10811), // 输出电压百分比 + kreg_tec103_heater1_pwm_freq(10812), // PWM引脚输出频率 + kreg_tec103_heater1_over_temp_protect_mode(10813), // 传感器过温保护模式 + kreg_tec103_heater1_p(10814), // P参数 + kreg_tec103_heater1_i(10815), // I参数 + kreg_tec103_heater1_d(10816), // D参数 + kreg_tec103_heater1_pid_self_tune(10817), // PID自整定 + kreg_tec103_heater2_target_temp(10830), // 目标温度 + kreg_tec103_heater2_real_temp(10831), // 读取温度 + kreg_tec103_heater2_sensor_r(10832), // 查询传感器电阻值 + kreg_tec103_heater2_polynomial(10833), // 温度求解模型选择 + kreg_tec103_heater2_ntc_b(10834), // NTC B值 + kreg_tec103_heater2_ntc_r0(10835), // NTC R0值 + kreg_tec103_heater2_output_enable(10837), // 使能 + kreg_tec103_heater2_output_max_duty(10838), // 最大输出占空比 + kreg_tec103_heater2_output_mode(10839), // 输出模式 + kreg_tec103_heater2_output_pol(10840), // 输出极性 + kreg_tec103_heater2_output_percent(10841), // 输出电压百分比 + kreg_tec103_heater2_pwm_freq(10842), // PWM引脚输出频率 + kreg_tec103_heater2_over_temp_protect_mode(10843), // 传感器过温保护模式 + kreg_tec103_heater2_p(10844), // P参数 + kreg_tec103_heater2_i(10845), // I参数 + kreg_tec103_heater2_d(10846), // D参数 + kreg_tec103_heater2_pid_self_tune(10847), // PID自整定 + kreg_tec103_heater_version(10860), // 版本号 + kreg_tec103_heater_address(10861), // 从机地址 + kreg_tec103_heater_baud_rate(10862), // 波特率 + kreg_tec103_heater_e_code(10863), // 错误码 + ; public final int index; diff --git a/src/main/java/com/iflytop/colortitration/hardware/type/TecHeaterRegIndex.java b/src/main/java/com/iflytop/colortitration/hardware/type/TecHeaterRegIndex.java new file mode 100644 index 0000000..7dc4a0d --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/hardware/type/TecHeaterRegIndex.java @@ -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; + } +} diff --git a/src/main/java/com/iflytop/colortitration/hardware/type/TecHeaterRodMId.java b/src/main/java/com/iflytop/colortitration/hardware/type/TecHeaterRodMId.java new file mode 100644 index 0000000..d1e9482 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/hardware/type/TecHeaterRodMId.java @@ -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; + } +}