Return notifications about pid and drive updates. Introduce debounce

This commit is contained in:
Alexander Nozik 2023-11-08 22:28:26 +03:00
parent fe98a836f8
commit 74301afb42
4 changed files with 74 additions and 43 deletions

View File

@ -74,6 +74,7 @@ public class VirtualDrive(
// compute new value based on velocity and acceleration from the previous step
positionState.value += velocity * dtSeconds + force / mass * dtSeconds.pow(2) / 2
propertyChanged(Drive.position, positionState.value)
// compute new velocity based on acceleration on the previous step
velocity += force / mass * dtSeconds

View File

@ -62,6 +62,7 @@ public class PidRegulator(
lastPosition = drive.position
drive.force = pidParameters.kp * delta + pidParameters.ki * integral + pidParameters.kd * derivative
propertyChanged(Regulator.position, drive.position)
}
}
}

View File

@ -1,7 +1,11 @@
@file:OptIn(FlowPreview::class)
package space.kscience.controls.vision
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.transform
@ -25,6 +29,7 @@ import space.kscience.plotly.models.TraceValues
import space.kscience.plotly.scatter
import kotlin.time.Duration
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.milliseconds
private var TraceValues.values: List<Value>
get() = value?.list ?: emptyList()
@ -88,12 +93,13 @@ public fun Plot.plotDeviceProperty(
maxAge: Duration = 1.hours,
maxPoints: Int = 800,
minPoints: Int = 400,
debounceDuration: Duration = 10.milliseconds,
coroutineScope: CoroutineScope = device.context,
configuration: Scatter.() -> Unit = {},
): Job = scatter(configuration).run {
val clock = device.context.clock
val data = TimeData()
device.propertyMessageFlow(propertyName).transform {
device.propertyMessageFlow(propertyName).debounce(debounceDuration).transform {
data.append(it.time ?: clock.now(), it.value.extractValue())
data.trim(maxAge, maxPoints, minPoints)
emit(data)
@ -109,10 +115,11 @@ private fun <T> Trace.updateFromState(
maxAge: Duration = 1.hours,
maxPoints: Int = 800,
minPoints: Int = 400,
): Job{
debounceDuration: Duration = 10.milliseconds,
): Job {
val clock = context.clock
val data = TimeData()
return state.valueFlow.transform<T, TimeData> {
return state.valueFlow.debounce(debounceDuration).transform<T, TimeData> {
data.append(clock.now(), it.extractValue())
data.trim(maxAge, maxPoints, minPoints)
}.onEach {
@ -127,9 +134,10 @@ public fun <T> Plot.plotDeviceState(
maxAge: Duration = 1.hours,
maxPoints: Int = 800,
minPoints: Int = 400,
debounceDuration: Duration = 10.milliseconds,
configuration: Scatter.() -> Unit = {},
): Job = scatter(configuration).run {
updateFromState(context, state, extractValue, maxAge, maxPoints, minPoints)
updateFromState(context, state, extractValue, maxAge, maxPoints, minPoints, debounceDuration)
}
@ -139,9 +147,10 @@ public fun Plot.plotNumberState(
maxAge: Duration = 1.hours,
maxPoints: Int = 800,
minPoints: Int = 400,
debounceDuration: Duration = 10.milliseconds,
configuration: Scatter.() -> Unit = {},
): Job = scatter(configuration).run {
updateFromState(context, state, { asValue() }, maxAge, maxPoints, minPoints)
updateFromState(context, state, { asValue() }, maxAge, maxPoints, minPoints, debounceDuration)
}
@ -151,7 +160,8 @@ public fun Plot.plotBooleanState(
maxAge: Duration = 1.hours,
maxPoints: Int = 800,
minPoints: Int = 400,
debounceDuration: Duration = 10.milliseconds,
configuration: Bar.() -> Unit = {},
): Job = bar(configuration).run {
updateFromState(context, state, { asValue() }, maxAge, maxPoints, minPoints)
): Job = bar(configuration).run {
updateFromState(context, state, { asValue() }, maxAge, maxPoints, minPoints, debounceDuration)
}

View File

@ -3,17 +3,28 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"USE(ControlsJupyter())"
"//import space.kscience.controls.jupyter.ControlsJupyter\n",
"\n",
"//USE(ControlsJupyter())\n",
"USE{\n",
" repositories{\n",
" maven(\"https://repo.kotlin.link\")\n",
" }\n",
" dependencies{\n",
" implementation(\"space.kscience:controls-jupyter-jvm:0.3.0-dev-2\")\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class LinearDrive(\n",
@ -34,14 +45,14 @@
" val position by property(state)\n",
" var target by mutableProperty(pid.mutablePropertyAsState(Regulator.target, 0.0))\n",
"}\n"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import kotlin.time.Duration.Companion.milliseconds\n",
@ -57,14 +68,14 @@
")\n",
"\n",
"val device = context.install(\"device\", LinearDrive(context, state, 0.005, pidParameters))"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\n",
@ -80,14 +91,14 @@
" sin(2 * PI * 21 * freq * t + 0.02 * (timeFromStart / pidParameters.timeStep))\n",
" }\n",
"}"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"val maxAge = 10.seconds\n",
@ -96,16 +107,18 @@
"VisionForge.fragment {\n",
" vision {\n",
" plotly {\n",
" \n",
" plotDeviceProperty(device.pid, Regulator.target.name, maxAge = maxAge) {\n",
" name = \"target\"\n",
" }\n",
" \n",
" plotNumberState(context, state, maxAge = maxAge) {\n",
" name = \"real position\"\n",
" }\n",
" \n",
" plotDeviceProperty(device.pid, Regulator.position.name, maxAge = maxAge) {\n",
" name = \"read position\"\n",
" }\n",
"\n",
" plotDeviceProperty(device.pid, Regulator.target.name, maxAge = maxAge) {\n",
" name = \"target\"\n",
" }\n",
" }\n",
" }\n",
"\n",
@ -122,23 +135,29 @@
" }\n",
" }\n",
"}"
],
"metadata": {
"collapsed": false
}
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import kotlinx.coroutines.cancel\n",
"\n",
"job.cancel()"
],
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
}
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
@ -156,21 +175,21 @@
"language": "kotlin",
"name": "kotlin"
},
"language_info": {
"name": "kotlin",
"version": "1.9.0",
"mimetype": "text/x-kotlin",
"file_extension": ".kt",
"pygments_lexer": "kotlin",
"codemirror_mode": "text/x-kotlin",
"nbconvert_exporter": ""
},
"ktnbPluginMetadata": {
"projectDependencies": [
"controls-kt.controls-jupyter.jvmMain"
]
},
"language_info": {
"codemirror_mode": "text/x-kotlin",
"file_extension": ".kt",
"mimetype": "text/x-kotlin",
"name": "kotlin",
"nbconvert_exporter": "",
"pygments_lexer": "kotlin",
"version": "1.8.20"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 4
}