From ce45d0cfb8dd4ea3d089d8ce1f2396924a316731 Mon Sep 17 00:00:00 2001 From: tiagohm Date: Sat, 18 May 2024 16:52:12 -0300 Subject: [PATCH] [api][desktop]: Improve TPPA --- .../api/alignment/polar/tppa/TPPAState.kt | 1 + .../api/alignment/polar/tppa/TPPATask.kt | 13 +++- desktop/src/shared/types/alignment.types.ts | 2 +- .../point/three/ThreePointPolarAlignment.kt | 71 ++++++++++++------- .../three/ThreePointPolarAlignmentResult.kt | 2 + 5 files changed, 60 insertions(+), 29 deletions(-) diff --git a/api/src/main/kotlin/nebulosa/api/alignment/polar/tppa/TPPAState.kt b/api/src/main/kotlin/nebulosa/api/alignment/polar/tppa/TPPAState.kt index 8a98cb721..0e1a5c0ef 100644 --- a/api/src/main/kotlin/nebulosa/api/alignment/polar/tppa/TPPAState.kt +++ b/api/src/main/kotlin/nebulosa/api/alignment/polar/tppa/TPPAState.kt @@ -5,6 +5,7 @@ enum class TPPAState { SLEWING, SLEWED, SETTLING, + EXPOSURING, SOLVING, SOLVED, COMPUTED, diff --git a/api/src/main/kotlin/nebulosa/api/alignment/polar/tppa/TPPATask.kt b/api/src/main/kotlin/nebulosa/api/alignment/polar/tppa/TPPATask.kt index b8815cd09..3ec3c020a 100644 --- a/api/src/main/kotlin/nebulosa/api/alignment/polar/tppa/TPPATask.kt +++ b/api/src/main/kotlin/nebulosa/api/alignment/polar/tppa/TPPATask.kt @@ -130,13 +130,15 @@ data class TPPATask( // SLEWING. if (mount != null) { - if (alignment.state in 1..2 && !mountMoveState[alignment.state]) { + if (alignment.state.ordinal in 1..2 && !mountMoveState[alignment.state.ordinal]) { MountMoveTask(mount, mountMoveRequest).use { sendEvent(TPPAState.SLEWING) it.execute(cancellationToken) - mountMoveState[alignment.state] = true + mountMoveState[alignment.state.ordinal] = true } + if (cancellationToken.isDone) break + rightAscension = mount.rightAscension declination = mount.declination sendEvent(TPPAState.SLEWED) @@ -149,7 +151,7 @@ data class TPPATask( if (cancellationToken.isDone) break - sendEvent(TPPAState.SOLVING) + sendEvent(TPPAState.EXPOSURING) // CAPTURE. cameraCaptureTask.execute(cancellationToken) @@ -158,6 +160,8 @@ data class TPPATask( break } + sendEvent(TPPAState.SOLVING) + // ALIGNMENT. val radius = if (mount == null) 0.0 else ATTEMPT_RADIUS * (noSolutionAttempts + 1) @@ -229,6 +233,9 @@ data class TPPATask( continue } + is ThreePointPolarAlignmentResult.Cancelled -> { + break + } } } diff --git a/desktop/src/shared/types/alignment.types.ts b/desktop/src/shared/types/alignment.types.ts index e142de096..3b48be378 100644 --- a/desktop/src/shared/types/alignment.types.ts +++ b/desktop/src/shared/types/alignment.types.ts @@ -7,7 +7,7 @@ export type Hemisphere = 'NORTHERN' | 'SOUTHERN' export type DARVState = 'IDLE' | 'INITIAL_PAUSE' | 'FORWARD' | 'BACKWARD' -export type TPPAState = 'IDLE' | 'SLEWING' | 'SLEWED' | 'SETTLING' | 'SOLVING' | 'SOLVED' | 'COMPUTED' | 'PAUSING' | 'PAUSED' | 'FINISHED' | 'FAILED' +export type TPPAState = 'IDLE' | 'SLEWING' | 'SLEWED' | 'SETTLING' | 'EXPOSURING' | 'SOLVING' | 'SOLVED' | 'COMPUTED' | 'PAUSING' | 'PAUSED' | 'FINISHED' | 'FAILED' export type AlignmentMethod = 'DARV' | 'TPPA' diff --git a/nebulosa-alignment/src/main/kotlin/nebulosa/alignment/polar/point/three/ThreePointPolarAlignment.kt b/nebulosa-alignment/src/main/kotlin/nebulosa/alignment/polar/point/three/ThreePointPolarAlignment.kt index 4793bd3c1..3f0f5c33c 100644 --- a/nebulosa-alignment/src/main/kotlin/nebulosa/alignment/polar/point/three/ThreePointPolarAlignment.kt +++ b/nebulosa-alignment/src/main/kotlin/nebulosa/alignment/polar/point/three/ThreePointPolarAlignment.kt @@ -1,5 +1,6 @@ package nebulosa.alignment.polar.point.three +import nebulosa.alignment.polar.point.three.ThreePointPolarAlignmentResult.* import nebulosa.common.Resettable import nebulosa.common.concurrency.cancel.CancellationToken import nebulosa.constants.DEG2RAD @@ -23,9 +24,14 @@ data class ThreePointPolarAlignment( private val latitude: Angle, ) : Resettable { - private val positions = arrayOfNulls(2) + enum class State { + FIRST_POSITION, + SECOND_POSITION, + THIRD_POSITION, + CONTINUOUS_SOLVE, + } - @Volatile var state = 0 + @Volatile var state = State.FIRST_POSITION private set @Volatile var initialAzimuthError: Angle = 0.0 @@ -40,6 +46,8 @@ data class ThreePointPolarAlignment( @Volatile var currentAltitudeError: Angle = 0.0 private set + private lateinit var firstPosition: Position + private lateinit var secondPosition: Position private lateinit var polarErrorDetermination: PolarErrorDetermination fun align( @@ -48,43 +56,56 @@ data class ThreePointPolarAlignment( compensateRefraction: Boolean = false, cancellationToken: CancellationToken = CancellationToken.NONE, ): ThreePointPolarAlignmentResult { + if (cancellationToken.isDone) { + return Cancelled + } + val solution = try { solver.solve(path, null, rightAscension, declination, radius, cancellationToken = cancellationToken) } catch (e: PlateSolvingException) { - return ThreePointPolarAlignmentResult.NoPlateSolution(e) + return NoPlateSolution(e) } - if (!solution.solved || cancellationToken.isDone) { - return ThreePointPolarAlignmentResult.NoPlateSolution(null) + if (cancellationToken.isDone) { + return Cancelled + } else if (!solution.solved) { + return NoPlateSolution(null) } else { val time = UTC.now() - if (state > 2) { - val (azimuth, altitude) = polarErrorDetermination - .update(time, initialAzimuthError, initialAltitudeError, solution, compensateRefraction) - currentAzimuthError = azimuth - currentAltitudeError = altitude - return ThreePointPolarAlignmentResult.Measured(solution.rightAscension, solution.declination, azimuth, altitude) - } else if (state == 2) { - state++ - val position = solution.position(time, compensateRefraction) - polarErrorDetermination = PolarErrorDetermination(solution, positions[0]!!, positions[1]!!, position, longitude, latitude) - val (azimuth, altitude) = polarErrorDetermination.compute() - initialAzimuthError = azimuth - initialAltitudeError = altitude - return ThreePointPolarAlignmentResult.Measured(solution.rightAscension, solution.declination, azimuth, altitude) - } else { - positions[state] = solution.position(time, compensateRefraction) - state++ + when (state) { + State.CONTINUOUS_SOLVE -> { + val (azimuth, altitude) = polarErrorDetermination + .update(time, initialAzimuthError, initialAltitudeError, solution, compensateRefraction) + currentAzimuthError = azimuth + currentAltitudeError = altitude + return Measured(solution.rightAscension, solution.declination, azimuth, altitude) + } + State.THIRD_POSITION -> { + val position = solution.position(time, compensateRefraction) + polarErrorDetermination = PolarErrorDetermination(solution, firstPosition, secondPosition, position, longitude, latitude) + val (azimuth, altitude) = polarErrorDetermination.compute() + state = State.CONTINUOUS_SOLVE + initialAzimuthError = azimuth + initialAltitudeError = altitude + return Measured(solution.rightAscension, solution.declination, azimuth, altitude) + } + State.SECOND_POSITION -> { + secondPosition = solution.position(time, compensateRefraction) + state = State.THIRD_POSITION + } + State.FIRST_POSITION -> { + firstPosition = solution.position(time, compensateRefraction) + state = State.SECOND_POSITION + } } - return ThreePointPolarAlignmentResult.NeedMoreMeasurement(solution.rightAscension, solution.declination) + return NeedMoreMeasurement(solution.rightAscension, solution.declination) } } override fun reset() { - state = 0 - positions.fill(null) + state = State.FIRST_POSITION } private fun PlateSolution.position(time: UTC, compensateRefraction: Boolean): Position { diff --git a/nebulosa-alignment/src/main/kotlin/nebulosa/alignment/polar/point/three/ThreePointPolarAlignmentResult.kt b/nebulosa-alignment/src/main/kotlin/nebulosa/alignment/polar/point/three/ThreePointPolarAlignmentResult.kt index 6496728d5..ad76ecf00 100644 --- a/nebulosa-alignment/src/main/kotlin/nebulosa/alignment/polar/point/three/ThreePointPolarAlignmentResult.kt +++ b/nebulosa-alignment/src/main/kotlin/nebulosa/alignment/polar/point/three/ThreePointPolarAlignmentResult.kt @@ -13,4 +13,6 @@ sealed interface ThreePointPolarAlignmentResult { ) : ThreePointPolarAlignmentResult data class NoPlateSolution(@JvmField val exception: PlateSolvingException?) : ThreePointPolarAlignmentResult + + data object Cancelled : ThreePointPolarAlignmentResult }