From b1d9d8e8badb8a0e7722255c0f3632323aa49767 Mon Sep 17 00:00:00 2001 From: Atos1337 Date: Wed, 20 Oct 2021 16:59:30 +0300 Subject: [PATCH] Add location property in virtual car --- .../controls/demo/virtual_car/VirtualCar.kt | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/demo/src/main/kotlin/ru/mipt/npm/controls/demo/virtual_car/VirtualCar.kt b/demo/src/main/kotlin/ru/mipt/npm/controls/demo/virtual_car/VirtualCar.kt index c8a8fc8..b886d6b 100644 --- a/demo/src/main/kotlin/ru/mipt/npm/controls/demo/virtual_car/VirtualCar.kt +++ b/demo/src/main/kotlin/ru/mipt/npm/controls/demo/virtual_car/VirtualCar.kt @@ -18,6 +18,13 @@ class VirtualCar : DeviceBySpec(VirtualCar) { return this._speedState } + private var _locationState: Pair = Pair(0.0, 0.0) + private val locationState: Pair + get() { + updateSpeedLocationTime() + return this._locationState + } + private var accelerationState: Pair = Pair(0.0, 0.0) set(value) { updateSpeedLocationTime() @@ -28,6 +35,7 @@ class VirtualCar : DeviceBySpec(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) { 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> { @@ -53,6 +69,8 @@ class VirtualCar : DeviceBySpec(VirtualCar) { companion object : DeviceSpec(::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) { 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) { launch { speed.read() acceleration.read() + location.read() } doRecurring(Duration.seconds(1)){ carProperties.read()