Add location property in virtual car

This commit is contained in:
Atos1337 2021-10-20 16:59:30 +03:00
parent 24c5188c30
commit bd23ca1129

View File

@ -18,6 +18,13 @@ class VirtualCar : DeviceBySpec<VirtualCar>(VirtualCar) {
return this._speedState
}
private var _locationState: Pair<Double, Double> = Pair(0.0, 0.0)
private val locationState: Pair<Double, Double>
get() {
updateSpeedLocationTime()
return this._locationState
}
private var accelerationState: Pair<Double, Double> = Pair(0.0, 0.0)
set(value) {
updateSpeedLocationTime()
@ -28,6 +35,7 @@ class VirtualCar : DeviceBySpec<VirtualCar>(VirtualCar) {
private fun updateSpeedLocationTime() {
val previousSpeed = this._speedState
val previousLocation = this._locationState
val currentAcceleration = this.accelerationState
val now = Instant.now().toEpochMilli().toDouble()
val timeDifference = now - this.timeState
@ -36,6 +44,14 @@ class VirtualCar : DeviceBySpec<VirtualCar>(VirtualCar) {
previousSpeed.first + timeDifference * currentAcceleration.first * 1e-3,
previousSpeed.second + timeDifference * currentAcceleration.second * 1e-3
)
val locationDifference = Pair(
timeDifference * 1e-3 * (previousSpeed.first + currentAcceleration.first * timeDifference * 1e-3 / 2),
timeDifference * 1e-3 * (previousSpeed.second + currentAcceleration.second * timeDifference * 1e-3 / 2)
)
this._locationState = Pair(
previousLocation.first + locationDifference.first,
previousLocation.second + locationDifference.second
)
}
object DoublePairMetaConverter : MetaConverter<Pair<Double, Double>> {
@ -53,6 +69,8 @@ class VirtualCar : DeviceBySpec<VirtualCar>(VirtualCar) {
companion object : DeviceSpec<VirtualCar>(::VirtualCar) {
val speed by property(DoublePairMetaConverter) { this.speedState }
val location by property(DoublePairMetaConverter) { this.locationState }
val acceleration by property(DoublePairMetaConverter, VirtualCar::accelerationState)
val carProperties by metaProperty {
@ -60,6 +78,7 @@ class VirtualCar : DeviceBySpec<VirtualCar>(VirtualCar) {
val time = Instant.now()
"time" put time.toEpochMilli()
"speed" put DoublePairMetaConverter.objectToMeta(read(speed))
"location" put DoublePairMetaConverter.objectToMeta(read(location))
"acceleration" put DoublePairMetaConverter.objectToMeta(read(acceleration))
}
}
@ -69,6 +88,7 @@ class VirtualCar : DeviceBySpec<VirtualCar>(VirtualCar) {
launch {
speed.read()
acceleration.read()
location.read()
}
doRecurring(Duration.seconds(1)){
carProperties.read()