Skip to content

Commit

Permalink
Fixed cyclical bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Erives committed Aug 20, 2023
1 parent 907fe5e commit c9dbea5
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ class PipelineOpModeSwitchablePanel(val eocvSim: EOCVSim) : JTabbedPane() {
val index = sourceTabbedPane.selectedIndex

if(index == 0) {
opModeSelectorPanel.reset(0)

pipelineSelectorPanel.isActive = true
opModeSelectorPanel.isActive = false

opModeSelectorPanel.reset(0)
} else if(index == 1) {
opModeSelectorPanel.reset()

Expand All @@ -76,12 +76,12 @@ class PipelineOpModeSwitchablePanel(val eocvSim: EOCVSim) : JTabbedPane() {

fun enableSwitching() {
pipelineSelectorPanel.allowPipelineSwitching = true
opModeSelectorPanel.allowOpModeSwitching = true
opModeSelectorPanel.isActive = true
}

fun disableSwitching() {
pipelineSelectorPanel.allowPipelineSwitching = false
opModeSelectorPanel.allowOpModeSwitching = false
opModeSelectorPanel.isActive = false
}

fun enableSwitchingBlocking() = runBlocking {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import io.github.deltacv.vision.internal.opmode.OpModeNotification
import io.github.deltacv.vision.internal.opmode.OpModeState
import java.awt.BorderLayout
import javax.swing.JPanel
import java.awt.GridBagConstraints
import java.awt.GridBagLayout
import javax.swing.JButton
import javax.swing.SwingUtilities

Expand All @@ -21,8 +19,9 @@ class OpModeControlsPanel(val eocvSim: EOCVSim) : JPanel() {
private set

private var currentManagerIndex: Int? = null
private var upcomingIndex: Int? = null

var allowOpModeSwitching = false
var isActive = false

init {
layout = BorderLayout()
Expand All @@ -36,6 +35,8 @@ class OpModeControlsPanel(val eocvSim: EOCVSim) : JPanel() {
eocvSim.pipelineManager.onUpdate.doOnce {
if(eocvSim.pipelineManager.currentPipeline !is OpMode) return@doOnce

eocvSim.pipelineManager.setPaused(false, PipelineManager.PauseReason.NOT_PAUSED)

val opMode = eocvSim.pipelineManager.currentPipeline as OpMode
val state = opMode.notifier.state

Expand All @@ -50,17 +51,16 @@ class OpModeControlsPanel(val eocvSim: EOCVSim) : JPanel() {
}

fun stopCurrentOpMode() {
if(eocvSim.pipelineManager.currentPipeline !is OpMode) return

val opMode = eocvSim.pipelineManager.currentPipeline as OpMode
opMode.notifier.notify(OpModeNotification.STOP)
if(eocvSim.pipelineManager.currentPipeline != currentOpMode || currentOpMode == null) return
currentOpMode!!.notifier.notify(OpModeNotification.STOP)
}

private fun notifySelected() {
if(!allowOpModeSwitching) return
if(!isActive) return

if(eocvSim.pipelineManager.currentPipeline !is OpMode) return
val opMode = eocvSim.pipelineManager.currentPipeline as OpMode
val opModeIndex = currentManagerIndex!!

opMode.notifier.onStateChange {
val state = opMode.notifier.state
Expand All @@ -70,7 +70,10 @@ class OpModeControlsPanel(val eocvSim: EOCVSim) : JPanel() {
}

if(state == OpModeState.STOPPED) {
opModeSelected(currentManagerIndex!!)
if(isActive && opModeIndex == upcomingIndex) {
opModeSelected(currentManagerIndex!!)
}

it.removeThis()
}
}
Expand All @@ -97,12 +100,16 @@ class OpModeControlsPanel(val eocvSim: EOCVSim) : JPanel() {
}

fun opModeSelected(managerIndex: Int, forceChangePipeline: Boolean = true) {
eocvSim.pipelineManager.setPaused(false)
eocvSim.pipelineManager.requestSetPaused(false)

if(forceChangePipeline) {
eocvSim.pipelineManager.requestForceChangePipeline(managerIndex)
}

if(forceChangePipeline) eocvSim.pipelineManager.requestForceChangePipeline(managerIndex)
currentManagerIndex = managerIndex
upcomingIndex = managerIndex

eocvSim.pipelineManager.onUpdate.doOnce {
currentManagerIndex = managerIndex
notifySelected()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,12 @@ class OpModeSelectorPanel(val eocvSim: EOCVSim, val opModeControlsPanel: OpModeC
val autonomousSelector = JList<String>()
val teleopSelector = JList<String>()

var allowOpModeSwitching = false
var isActive = false
set(value) {
opModeControlsPanel.allowOpModeSwitching = value
opModeControlsPanel.isActive = value
field = value
}

var isActive = false
internal set

init {
layout = GridBagLayout()
selectOpModeLabelsPanel.layout = GridBagLayout()
Expand Down Expand Up @@ -168,7 +165,7 @@ class OpModeSelectorPanel(val eocvSim: EOCVSim, val opModeControlsPanel: OpModeC

autonomousSelector.addMouseListener(object: MouseAdapter() {
override fun mouseClicked(e: MouseEvent) {
if (!allowOpModeSwitching) return
if (!isActive) return

val index = (e.source as JList<*>).locationToIndex(e.point)
if(index >= 0) {
Expand All @@ -179,7 +176,7 @@ class OpModeSelectorPanel(val eocvSim: EOCVSim, val opModeControlsPanel: OpModeC

teleopSelector.addMouseListener(object: MouseAdapter() {
override fun mouseClicked(e: MouseEvent) {
if (!allowOpModeSwitching) return
if (!isActive) return

val index = (e.source as JList<*>).locationToIndex(e.point)
if(index >= 0) {
Expand All @@ -189,12 +186,17 @@ class OpModeSelectorPanel(val eocvSim: EOCVSim, val opModeControlsPanel: OpModeC
})

eocvSim.pipelineManager.onPipelineChange {
// we are doing this to detect external pipeline changes
if(!isActive) return@onPipelineChange

// we are doing this to detect external pipeline changes and reflect them
// accordingly in the UI.
//
// in the event that this change was triggered by us, OpModeSelectorPanel,
// we need to hold on a cycle so that the state has been fully updated,
// just to be able to check correctly.
// just to be able to check correctly and, if it was requested by
// OpModeSelectorPanel, skip this message and not do anything.
eocvSim.pipelineManager.onUpdate.doOnce {
if(isActive && opModeControlsPanel.currentOpMode != eocvSim.pipelineManager.currentPipeline) {
if(isActive && opModeControlsPanel.currentOpMode != eocvSim.pipelineManager.currentPipeline && eocvSim.pipelineManager.currentPipeline != null) {
val opMode = eocvSim.pipelineManager.currentPipeline

if(opMode is OpMode) {
Expand All @@ -205,7 +207,7 @@ class OpModeSelectorPanel(val eocvSim: EOCVSim, val opModeControlsPanel: OpModeC
logger.info("External change detected \"$name\"")

opModeSelected(eocvSim.pipelineManager.currentPipelineIndex, name, false)
} else {
} else if(isActive) {
reset(-1)
}
}
Expand All @@ -222,6 +224,8 @@ class OpModeSelectorPanel(val eocvSim: EOCVSim, val opModeControlsPanel: OpModeC
}

private fun opModeSelected(managerIndex: Int, name: String, forceChangePipeline: Boolean = true) {
if(!isActive) return

opModeNameLabel.text = name

textPanel.removeAll()
Expand Down Expand Up @@ -277,9 +281,9 @@ class OpModeSelectorPanel(val eocvSim: EOCVSim, val opModeControlsPanel: OpModeC

opModeControlsPanel.reset()

if(eocvSim.pipelineManager.currentPipeline == opModeControlsPanel.currentOpMode) {
val opMode = opModeControlsPanel.currentOpMode
val opMode = opModeControlsPanel.currentOpMode

if(eocvSim.pipelineManager.currentPipeline == opMode && opMode != null && opMode.notifier.state != OpModeState.SELECTED) {
opMode?.notifier?.onStateChange?.let {
it {
val state = opMode.notifier.state
Expand All @@ -289,7 +293,7 @@ class OpModeSelectorPanel(val eocvSim: EOCVSim, val opModeControlsPanel: OpModeC

if(nextPipeline == null || nextPipeline >= 0) {
eocvSim.pipelineManager.onUpdate.doOnce {
eocvSim.pipelineManager.requestChangePipeline(nextPipeline)
eocvSim.pipelineManager.changePipeline(nextPipeline)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public void update(boolean isPaused) {
Mat m = currentInputSource.update();

if(m != null && !m.empty()) {
lastMatFromSource.release();
m.copyTo(lastMatFromSource);
// add an extra alpha channel because that's what eocv returns for some reason... (more realistic simulation lol)
Imgproc.cvtColor(lastMatFromSource, lastMatFromSource, Imgproc.COLOR_RGB2RGBA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class PipelineManager(var eocvSim: EOCVSim, val pipelineStatisticsCalculator: Pi
return field
}

var pauseOnImages = true

var pauseReason = PauseReason.NOT_PAUSED
private set
get() {
Expand Down Expand Up @@ -615,7 +617,7 @@ class PipelineManager(var eocvSim: EOCVSim, val pipelineStatisticsCalculator: Pi
setPaused(false)

//if pause on images option is turned on by user
if (eocvSim.configManager.config.pauseOnImages) {
if (eocvSim.configManager.config.pauseOnImages && pauseOnImages) {
//pause next frame if current selected input source is an image
eocvSim.inputSourceManager.pauseIfImageTwoFrames()
}
Expand Down

This file was deleted.

Loading

0 comments on commit c9dbea5

Please sign in to comment.