Fix StepDrive parameters
This commit is contained in:
parent
8c7c017ab4
commit
a9d58bfac2
controls-constructor/src/commonMain/kotlin/space/kscience/controls/constructor/devices
demo/constructor/src/jvmMain/kotlin
15
controls-constructor/src/commonMain/kotlin/space/kscience/controls/constructor/devices/StepDrive.kt
15
controls-constructor/src/commonMain/kotlin/space/kscience/controls/constructor/devices/StepDrive.kt
@ -21,21 +21,26 @@ import kotlin.time.DurationUnit
|
|||||||
*/
|
*/
|
||||||
public class StepDrive(
|
public class StepDrive(
|
||||||
context: Context,
|
context: Context,
|
||||||
ticksPerSecond: MutableDeviceState<Double>,
|
ticksPerSecond: Double,
|
||||||
position: MutableDeviceState<Long> = MutableDeviceState(0),
|
position: MutableDeviceState<Long> = MutableDeviceState(0),
|
||||||
target: MutableDeviceState<Long> = MutableDeviceState(0),
|
|
||||||
private val writeTicks: suspend (ticks: Long, speed: Double) -> Unit = { _, _ -> },
|
private val writeTicks: suspend (ticks: Long, speed: Double) -> Unit = { _, _ -> },
|
||||||
) : DeviceConstructor(context) {
|
) : 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)
|
public val position: DeviceState<Long> by property(MetaConverter.long, position)
|
||||||
|
|
||||||
//FIXME round to zero problem
|
//FIXME round to zero problem
|
||||||
private val ticker = onTimer(reads = setOf(target, position), writes = setOf(position)) { prev, next ->
|
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 timeDelta = (next - prev).toDouble(DurationUnit.SECONDS)
|
||||||
val ticksDelta: Long = target.value - position.value
|
val ticksDelta: Long = target.value - position.value
|
||||||
val steps: Long = when {
|
val steps: Long = when {
|
||||||
|
@ -1,24 +1,26 @@
|
|||||||
package space.kscience.controls.demo.constructor
|
package space.kscience.controls.demo.constructor
|
||||||
|
|
||||||
import kotlinx.coroutines.coroutineScope
|
import kotlinx.coroutines.coroutineScope
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.ensureActive
|
import kotlinx.coroutines.ensureActive
|
||||||
import space.kscience.controls.constructor.DeviceConstructor
|
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.device
|
||||||
import space.kscience.controls.constructor.devices.LimitSwitch
|
import space.kscience.controls.constructor.devices.LimitSwitch
|
||||||
import space.kscience.controls.constructor.devices.StepDrive
|
import space.kscience.controls.constructor.devices.StepDrive
|
||||||
import space.kscience.controls.constructor.models.MutableRangeState
|
import space.kscience.controls.constructor.models.MutableRangeState
|
||||||
import space.kscience.controls.manager.DeviceManager
|
import space.kscience.controls.manager.DeviceManager
|
||||||
import space.kscience.dataforge.context.Context
|
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(
|
class LinearStepDrive(
|
||||||
context: Context,
|
context: Context,
|
||||||
drive: StepDrive,
|
drive: StepDrive,
|
||||||
atStart: LimitSwitch,
|
atStart: LimitSwitch,
|
||||||
atEnd: LimitSwitch,
|
atEnd: LimitSwitch,
|
||||||
):DeviceConstructor(context){
|
) : DeviceConstructor(context) {
|
||||||
val drive by device(drive)
|
val drive by device(drive)
|
||||||
val atStart by device(atStart)
|
val atStart by device(atStart)
|
||||||
val atEnd by device(atEnd)
|
val atEnd by device(atEnd)
|
||||||
@ -28,39 +30,48 @@ class LinearStepDrive(
|
|||||||
fun LinearStepDrive(
|
fun LinearStepDrive(
|
||||||
context: Context,
|
context: Context,
|
||||||
position: MutableRangeState<Long>,
|
position: MutableRangeState<Long>,
|
||||||
): LinearStepDrive = LinearStepDrive(
|
): LinearStepDrive = LinearStepDrive(
|
||||||
context = context,
|
context = context,
|
||||||
drive = StepDrive(context, ticksPerSecond, position),
|
drive = StepDrive(context, ticksPerSecond, position),
|
||||||
atStart = LimitSwitch(context, position.atStart),
|
atStart = LimitSwitch(context, position.atStart),
|
||||||
atEnd = LimitSwitch(context, position.atEnd)
|
atEnd = LimitSwitch(context, position.atEnd)
|
||||||
)
|
)
|
||||||
|
|
||||||
suspend fun LinearStepDrive.calibrate(step: Long = 10): ClosedRange<Long> = coroutineScope{
|
suspend fun LinearStepDrive.calibrate(step: Long = 10): ClosedRange<Long> = coroutineScope {
|
||||||
do{
|
do {
|
||||||
ensureActive()
|
|
||||||
drive.target.value += step
|
|
||||||
} while (!atEnd.locked.value)
|
|
||||||
|
|
||||||
val end = drive.position.value
|
|
||||||
|
|
||||||
do{
|
|
||||||
ensureActive()
|
ensureActive()
|
||||||
drive.target.value -= step
|
drive.target.value -= step
|
||||||
|
delay((step / ticksPerSecond).seconds)
|
||||||
} while (!atStart.locked.value)
|
} while (!atStart.locked.value)
|
||||||
|
|
||||||
val start = drive.position.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
|
return@coroutineScope start..end
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun main() = coroutineScope {
|
suspend fun main() = coroutineScope {
|
||||||
val context = Context{
|
val context = Context {
|
||||||
plugin(DeviceManager)
|
plugin(DeviceManager)
|
||||||
}
|
}
|
||||||
|
|
||||||
val positionModel = MutableRangeState<Long>(0L, -1002L..1012L)
|
val positionModel = MutableRangeState<Long>(0L, -1000L..1012L)
|
||||||
|
|
||||||
val linearStepDrive = LinearStepDrive(context, positionModel)
|
val linearStepDrive = LinearStepDrive(context, positionModel)
|
||||||
|
|
||||||
|
val printJob = linearStepDrive.drive.target.collectValuesIn(this){
|
||||||
|
println("Move to $it")
|
||||||
|
}
|
||||||
|
|
||||||
println(linearStepDrive.calibrate())
|
println(linearStepDrive.calibrate())
|
||||||
|
|
||||||
|
printJob.cancel()
|
||||||
}
|
}
|
@ -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 xRange = NumericalValue<Meters>(-0.5)..NumericalValue<Meters>(0.5)
|
||||||
private val yRange = 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)
|
private val step = NumericalValue<Degrees>(1.8)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user