Fixed part of motionMaster

This commit is contained in:
Alexander Nozik 2024-05-12 14:14:31 +03:00
parent 44514cd477
commit a9592d0372
4 changed files with 28 additions and 24 deletions

View File

@ -100,6 +100,7 @@ public abstract class AbstractAsynchronousPort(
* Send a data packet via the port
*/
override suspend fun send(data: ByteArray) {
check(isOpen){"The port is not opened"}
outgoing.send(data)
}

View File

@ -45,20 +45,22 @@ fun ColumnScope.piMotionMasterAxis(
axisName: String,
axis: PiMotionMasterDevice.Axis,
) {
var min by remember { mutableStateOf(0f) }
var max by remember { mutableStateOf(1f) }
var targetPosition by remember { mutableStateOf(0f) }
val position: Double by axis.composeState(PiMotionMasterDevice.Axis.position, 0.0)
val scope = rememberCoroutineScope()
LaunchedEffect(axis) {
min = axis.read(PiMotionMasterDevice.Axis.minPosition).toFloat()
max = axis.read(PiMotionMasterDevice.Axis.maxPosition).toFloat()
targetPosition = axis.read(PiMotionMasterDevice.Axis.position).toFloat()
}
Row {
Text(axisName)
var min by remember { mutableStateOf(0f) }
var max by remember { mutableStateOf(0f) }
var targetPosition by remember { mutableStateOf(0f) }
val position: Double by axis.composeState(PiMotionMasterDevice.Axis.position, 0.0)
val scope = rememberCoroutineScope()
LaunchedEffect(axis) {
min = axis.read(PiMotionMasterDevice.Axis.minPosition).toFloat()
max = axis.read(PiMotionMasterDevice.Axis.maxPosition).toFloat()
targetPosition = axis.read(PiMotionMasterDevice.Axis.position).toFloat()
}
Column {
Slider(
@ -70,10 +72,10 @@ fun ColumnScope.piMotionMasterAxis(
Slider(
value = targetPosition,
onValueChange = { newPosition ->
targetPosition = newPosition
scope.launch {
axis.move(newPosition.toDouble())
}
targetPosition = newPosition
},
valueRange = min..max
)
@ -158,13 +160,13 @@ fun PiMotionMasterApp(device: PiMotionMasterDevice) {
if (!connected) {
device.launch {
device.connect(host, port)
axes = device.axes
}
axes = device.axes
} else {
device.launch {
device.disconnect()
axes = null
}
axes = null
}
},
modifier = Modifier.fillMaxWidth()

View File

@ -165,12 +165,12 @@ class PiMotionMasterDevice(
}
//Update port
//address = portSpec.node
port = portFactory(portSpec, context)
propertyChanged(connected, true)
port = portFactory(portSpec, context).apply { open() }
// connector.open()
//Initialize axes
val idn = read(identity)
failIfError { "Can't connect to $portSpec. Error code: $it" }
propertyChanged(connected, true)
logger.info { "Connected to $idn on $portSpec" }
val ids = request("SAI?").map { it.trim() }
if (ids != axes.keys.toList()) {

View File

@ -8,6 +8,8 @@ import io.ktor.util.InternalAPI
import io.ktor.util.moveToByteArray
import io.ktor.utils.io.writeAvailable
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import space.kscience.dataforge.context.Context
import space.kscience.dataforge.context.Global
@ -28,13 +30,12 @@ fun Context.launchPiDebugServer(port: Int, axes: List<String>): Job = launch(exc
val input = socket.openReadChannel()
val output = socket.openWriteChannel()
val sendJob = launch {
virtualDevice.subscribe().collect {
//println("Sending: ${it.decodeToString()}")
output.writeAvailable(it)
output.flush()
}
}
val sendJob = virtualDevice.subscribe().onEach {
//println("Sending: ${it.decodeToString()}")
output.writeAvailable(it)
output.flush()
}.launchIn(this)
try {
while (isActive) {