模拟器

master
dongzeliang 2023-11-22 09:41:09 +08:00
parent e9528c8e4b
commit 4c978050f0
3 changed files with 95 additions and 81 deletions

View File

@ -79,4 +79,15 @@ public class VehicleUtils {
num = (double) Math.round(num * 100) / 100; num = (double) Math.round(num * 100) / 100;
return BigDecimal.valueOf(num); return BigDecimal.valueOf(num);
} }
/**
*
* @param start
* @param end
* @return
*/
public static String genValue(int start, int end){
Random rand = new Random();
return String.valueOf(rand.nextInt(start, end));
}
} }

View File

@ -24,6 +24,8 @@ import java.util.Objects;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import static com.muyu.common.SystemConstant.powerConsumption;
/** /**
* @author DongZeLiang * @author DongZeLiang
* @version 1.0 * @version 1.0
@ -36,28 +38,22 @@ import java.util.concurrent.ScheduledFuture;
@AllArgsConstructor @AllArgsConstructor
public class VehicleInstance { public class VehicleInstance {
/**
*
*/
private final LinkedBlockingQueue<PositionModel> positionQueue = new LinkedBlockingQueue<>();
/** /**
* *
*/ */
private Vehicle vehicle; private Vehicle vehicle;
/** /**
* *
*/ */
private VehicleData vehicleData; private VehicleData vehicleData;
/** /**
* *
*/ */
private PositionModel lastPosition; private PositionModel lastPosition;
/**
*
*/
private final LinkedBlockingQueue<PositionModel> positionQueue = new LinkedBlockingQueue<>();
/** /**
* 线 * 线
*/ */
@ -87,11 +83,11 @@ public class VehicleInstance {
* VIN * VIN
* @return VIN * @return VIN
*/ */
public String getVin(){ public String getVin() {
return this.vehicle.getVin(); return this.vehicle.getVin();
} }
public void sendMsg(String msg){ public void sendMsg(String msg) {
// 创建消息并设置 QoS // 创建消息并设置 QoS
MqttMessage message = new MqttMessage(msg.getBytes()); MqttMessage message = new MqttMessage(msg.getBytes());
message.setQos(this.mqttProperties.getQos()); message.setQos(this.mqttProperties.getQos());
@ -103,7 +99,7 @@ public class VehicleInstance {
} }
} }
public void initCline(){ public void initCline() {
try { try {
client = new MqttClient(mqttProperties.getBroker(), mqttProperties.getClientId(), new MemoryPersistence()); client = new MqttClient(mqttProperties.getBroker(), mqttProperties.getClientId(), new MemoryPersistence());
// 连接参数 // 连接参数
@ -128,7 +124,7 @@ public class VehicleInstance {
/** /**
* 线 * 线
*/ */
public void initVehicleThread(){ public void initVehicleThread() {
VehicleThread vehicleThread = new VehicleThread(); VehicleThread vehicleThread = new VehicleThread();
vehicleThread.setVehicleInstance(this); vehicleThread.setVehicleInstance(this);
this.setVehicleThread(vehicleThread); this.setVehicleThread(vehicleThread);
@ -141,28 +137,28 @@ public class VehicleInstance {
/** /**
* 线 * 线
*/ */
public void startSend(){ public void startSend() {
this.vehicleThread.resume(); this.vehicleThread.resume();
} }
/** /**
* 线 * 线
*/ */
public void pauseSend(){ public void pauseSend() {
this.vehicleThread.pause(); this.vehicleThread.pause();
} }
/** /**
* *
*/ */
public void stopSend(){ public void stopSend() {
this.vehicleThread.stop(); this.vehicleThread.stop();
} }
/** /**
* *
*/ */
public void cancelExecution () { public void cancelExecution() {
scheduledFuture.cancel(true); scheduledFuture.cancel(true);
this.vehicleThread = null; this.vehicleThread = null;
this.scheduledFuture = null; this.scheduledFuture = null;
@ -171,7 +167,11 @@ public class VehicleInstance {
/** /**
* *
*/ */
public void imitateData () { public void imitateData() {
String gear = this.vehicleData.getGear();
if (!gear.equals("D")){
// TODO 不为D档则无法启动车辆
}
// 获取上一次定位点 // 获取上一次定位点
PositionModel lastPositionModel = this.lastPosition; PositionModel lastPositionModel = this.lastPosition;
// 获取当前定位点 // 获取当前定位点
@ -192,10 +192,19 @@ public class VehicleInstance {
// 百公里占比 // 百公里占比
BigDecimal hundredKMScale = distance.divide(SystemConstant.hundredKilometers).setScale(3, RoundingMode.HALF_UP); BigDecimal hundredKMScale = distance.divide(SystemConstant.hundredKilometers).setScale(3, RoundingMode.HALF_UP);
// 使用电量 // 使用电量
BigDecimal powerUsage = SystemConstant.powerConsumption.multiply(hundredKMScale) BigDecimal powerUsage = powerConsumption.multiply(hundredKMScale)
.multiply(batteryFloat) .multiply(batteryFloat)
.setScale(2, RoundingMode.HALF_UP); .setScale(2, RoundingMode.HALF_UP);
// 剩余电量
vehicleData.setRemainingBattery(vehicleData.getRemainingBattery().subtract(powerUsage)); vehicleData.setRemainingBattery(vehicleData.getRemainingBattery().subtract(powerUsage));
// 百公里消耗量
vehicleData.setFuelConsumptionRate(
powerConsumption.multiply(batteryFloat).divide(new BigDecimal(1000)).setScale(2, RoundingMode.HALF_UP).toString()
);
// 计算总速度
vehicleData.setSpeed(
distance.divide(new BigDecimal(10)).multiply(new BigDecimal("3.6")).setScale(2, RoundingMode.HALF_UP).toString()
);
} }

View File

@ -2,6 +2,7 @@ package com.muyu.vehicle.model;
import com.muyu.domain.Vehicle; import com.muyu.domain.Vehicle;
import com.muyu.utils.VehicleUtils;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
@ -9,6 +10,8 @@ import lombok.NoArgsConstructor;
import java.math.BigDecimal; import java.math.BigDecimal;
import static com.muyu.utils.VehicleUtils.genValue;
/** /**
* @author * @author
* @Classname VehicleData * @Classname VehicleData
@ -396,74 +399,65 @@ public class VehicleData {
} }
/** /**
VIN *
vin;
线
drivingRoute;
longitude;
latitude;
speed;
mileage;
voltage;
current;
resistance;
gear;
accelerationPedal;
brakePedal;
fuelConsumptionRate;
*/ */
public void imitateBase(){
// 总电压
this.voltage = genValue(110, 750);
// 总电流
this.current = genValue(3, 50);
// 绝缘电阻
this.resistance = genValue(0,30000);
// 加速踏板行程值
this.accelerationPedal = genValue(0, 10);
// 制动踏板行程值
this.brakePedal = genValue(0, 10);
}
/** /**
*
motorControllerTemperature;
motorSpeed;
motorTorque;
motorTemperature;
motorVoltage;
motorCurrent;
*/ */
public void imitateMotor(){
// 电机控制器温度
this.motorControllerTemperature = genValue(0, 100);
// 电机转速
this.motorSpeed = genValue(0, 99999);
// 电机转矩
this.motorTorque = genValue(0, 1000);
// 电机温度
this.motorTemperature = genValue(0, 150);
// 电机电压
this.motorVoltage = genValue(110, 300);
// 电机电流
this.motorCurrent = genValue(0, 15000);
}
/** /**
SOC *
remainingBattery;
maximumFeedbackPower;
maximumDischargePower;
BMS
selfCheckCounter;
totalBatteryCurrent;
V3
totalBatteryVoltage;
singleBatteryMaxVoltage;
singleBatteryMinVoltage;
singleBatteryMaxTemperature;
singleBatteryMinTemperature;
availableBatteryCapacity;
*/ */
public void imitateBatteryPack(){
// 当前状态允许的最大反馈功率
this.maximumFeedbackPower = genValue(0, 100);
// 当前状态允许最大放电功率
this.maximumDischargePower = genValue(0, 100);
// BMS自检计数器
this.selfCheckCounter = genValue(0, 15);
// 动力电池充放电电流
this.totalBatteryCurrent = genValue(0, 15);
// 动力电池负载端总电压V3
this.totalBatteryVoltage = genValue(220, 750);
// 单体电池最高电压
this.singleBatteryMaxVoltage = genValue(3, 5);
// 单体电池最低电压
this.singleBatteryMinVoltage = genValue(3, 5);
// 单体电池最高温度
this.singleBatteryMaxTemperature = genValue(0, 100);
// 单体电池最低温度
this.singleBatteryMinTemperature = genValue(0, 100);
// 动力电池可用容量
this.availableBatteryCapacity = genValue(0,100 );
}
/** /**
vehicleStatus; vehicleStatus;