Fix StepDrive parameters

This commit is contained in:
Alexander Nozik 2024-06-18 20:18:18 +03:00
parent 8c7c017ab4
commit a9d58bfac2
3 changed files with 37 additions and 21 deletions

View File

@ -21,21 +21,26 @@ import kotlin.time.DurationUnit
*/
public class StepDrive(
context: Context,
ticksPerSecond: MutableDeviceState<Double>,
ticksPerSecond: Double,
position: MutableDeviceState<Long> = MutableDeviceState(0),
target: MutableDeviceState<Long> = MutableDeviceState(0),
private val writeTicks: suspend (ticks: Long, speed: Double) -> Unit = { _, _ -> },
) : DeviceConstructor(context) {
public val target: MutableDeviceState<Long> by property(MetaConverter.long, target)
public val target: MutableDeviceState<Long> by property(
MetaConverter.long,
MutableDeviceState<Long>(position.value)
)
public val speed: MutableDeviceState<Double> by property(MetaConverter.double, ticksPerSecond)
public val speed: MutableDeviceState<Double> by property(
MetaConverter.double,
MutableDeviceState<Double>(ticksPerSecond)
)
public val position: DeviceState<Long> by property(MetaConverter.long, position)
//FIXME round to zero problem
private val ticker = onTimer(reads = setOf(target, position), writes = setOf(position)) { prev, next ->
val tickSpeed = ticksPerSecond.value
val tickSpeed = speed.value
val timeDelta = (next - prev).toDouble(DurationUnit.SECONDS)
val ticksDelta: Long = target.value - position.value
val steps: Long = when {

View File

@ -1,24 +1,26 @@
package space.kscience.controls.demo.constructor
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.ensureActive
import space.kscience.controls.constructor.DeviceConstructor
import space.kscience.controls.constructor.MutableDeviceState
import space.kscience.controls.constructor.collectValuesIn
import space.kscience.controls.constructor.device
import space.kscience.controls.constructor.devices.LimitSwitch
import space.kscience.controls.constructor.devices.StepDrive
import space.kscience.controls.constructor.models.MutableRangeState
import space.kscience.controls.manager.DeviceManager
import space.kscience.dataforge.context.Context
import kotlin.time.Duration.Companion.seconds
private val ticksPerSecond = MutableDeviceState(3000.0)
private val ticksPerSecond = 3000.0
class LinearStepDrive(
context: Context,
drive: StepDrive,
atStart: LimitSwitch,
atEnd: LimitSwitch,
):DeviceConstructor(context){
) : DeviceConstructor(context) {
val drive by device(drive)
val atStart by device(atStart)
val atEnd by device(atEnd)
@ -28,39 +30,48 @@ class LinearStepDrive(
fun LinearStepDrive(
context: Context,
position: MutableRangeState<Long>,
): LinearStepDrive = LinearStepDrive(
): LinearStepDrive = LinearStepDrive(
context = context,
drive = StepDrive(context, ticksPerSecond, position),
atStart = LimitSwitch(context, position.atStart),
atEnd = LimitSwitch(context, position.atEnd)
)
suspend fun LinearStepDrive.calibrate(step: Long = 10): ClosedRange<Long> = coroutineScope{
do{
ensureActive()
drive.target.value += step
} while (!atEnd.locked.value)
val end = drive.position.value
do{
suspend fun LinearStepDrive.calibrate(step: Long = 10): ClosedRange<Long> = coroutineScope {
do {
ensureActive()
drive.target.value -= step
delay((step / ticksPerSecond).seconds)
} while (!atStart.locked.value)
val start = drive.position.value
do {
ensureActive()
drive.target.value += step
delay((step / ticksPerSecond).seconds)
} while (!atEnd.locked.value)
val end = drive.position.value
return@coroutineScope start..end
}
suspend fun main() = coroutineScope {
val context = Context{
val context = Context {
plugin(DeviceManager)
}
val positionModel = MutableRangeState<Long>(0L, -1002L..1012L)
val positionModel = MutableRangeState<Long>(0L, -1000L..1012L)
val linearStepDrive = LinearStepDrive(context, positionModel)
val printJob = linearStepDrive.drive.target.collectValuesIn(this){
println("Move to $it")
}
println(linearStepDrive.calibrate())
printJob.cancel()
}

View File

@ -83,7 +83,7 @@ private suspend fun Plotter.square(xRange: IntRange, yRange: IntRange) {
private val xRange = NumericalValue<Meters>(-0.5)..NumericalValue<Meters>(0.5)
private val yRange = NumericalValue<Meters>(-0.5)..NumericalValue<Meters>(0.5)
private val ticksPerSecond = MutableDeviceState(3000.0)
private const val ticksPerSecond = 3000.0
private val step = NumericalValue<Degrees>(1.8)