Skip to content

Commit

Permalink
[api][desktop]: Improve TPPA
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagohm committed May 18, 2024
1 parent d88986d commit ce45d0c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ enum class TPPAState {
SLEWING,
SLEWED,
SETTLING,
EXPOSURING,
SOLVING,
SOLVED,
COMPUTED,
Expand Down
13 changes: 10 additions & 3 deletions api/src/main/kotlin/nebulosa/api/alignment/polar/tppa/TPPATask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -149,7 +151,7 @@ data class TPPATask(

if (cancellationToken.isDone) break

sendEvent(TPPAState.SOLVING)
sendEvent(TPPAState.EXPOSURING)

// CAPTURE.
cameraCaptureTask.execute(cancellationToken)
Expand All @@ -158,6 +160,8 @@ data class TPPATask(
break
}

sendEvent(TPPAState.SOLVING)

// ALIGNMENT.
val radius = if (mount == null) 0.0 else ATTEMPT_RADIUS * (noSolutionAttempts + 1)

Expand Down Expand Up @@ -229,6 +233,9 @@ data class TPPATask(

continue
}
is ThreePointPolarAlignmentResult.Cancelled -> {
break
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion desktop/src/shared/types/alignment.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,9 +24,14 @@ data class ThreePointPolarAlignment(
private val latitude: Angle,
) : Resettable {

private val positions = arrayOfNulls<Position>(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
Expand All @@ -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(
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ sealed interface ThreePointPolarAlignmentResult {
) : ThreePointPolarAlignmentResult

data class NoPlateSolution(@JvmField val exception: PlateSolvingException?) : ThreePointPolarAlignmentResult

data object Cancelled : ThreePointPolarAlignmentResult
}

0 comments on commit ce45d0c

Please sign in to comment.