Merge pull request 'fix: 무게 수신 버퍼 및 안정성 임계값 기준 재측정 로직으로 수정' (#10) from fix/weight-timing-control into master

Reviewed-on: #10
This commit is contained in:
kje97 2025-06-26 11:20:09 +09:00
commit a7379e7ebf
3 changed files with 93 additions and 14 deletions

View File

@ -61,6 +61,11 @@
<HintPath>..\..\..\..\..\..\..\..\Program Files\Brother bPAC3 SDK\Samples\VBNET\Badge\bin\Release\Interop.bpac.DLL</HintPath> <HintPath>..\..\..\..\..\..\..\..\Program Files\Brother bPAC3 SDK\Samples\VBNET\Badge\bin\Release\Interop.bpac.DLL</HintPath>
<EmbedInteropTypes>True</EmbedInteropTypes> <EmbedInteropTypes>True</EmbedInteropTypes>
</Reference> </Reference>
<Reference Include="Ivi.Visa.Interop, Version=5.11.0.0, Culture=neutral, PublicKeyToken=a128c98f1d7717c1, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<EmbedInteropTypes>True</EmbedInteropTypes>
<HintPath>..\..\..\..\..\..\..\Windows\assembly\GAC_64\Ivi.Visa.Interop\5.11.0.0__a128c98f1d7717c1\Ivi.Visa.Interop.dll</HintPath>
</Reference>
<Reference Include="K4os.Compression.LZ4, Version=1.1.11.0, Culture=neutral, PublicKeyToken=2186fa9121ef231d, processorArchitecture=MSIL"> <Reference Include="K4os.Compression.LZ4, Version=1.1.11.0, Culture=neutral, PublicKeyToken=2186fa9121ef231d, processorArchitecture=MSIL">
<HintPath>..\packages\K4os.Compression.LZ4.1.1.11\lib\net46\K4os.Compression.LZ4.dll</HintPath> <HintPath>..\packages\K4os.Compression.LZ4.1.1.11\lib\net46\K4os.Compression.LZ4.dll</HintPath>
</Reference> </Reference>

View File

@ -7,7 +7,7 @@ Imports Newtonsoft.Json.Linq
Public Class mainForm Public Class mainForm
Public projectName As String = "DUALSONIC SalesPacking MES Program Ver." Public projectName As String = "DUALSONIC SalesPacking MES Program Ver."
Public projectVer As String = "1.0.1" Public projectVer As String = "1.0.2"
Private apiItemOutURL As String = "item-outflows/page" Private apiItemOutURL As String = "item-outflows/page"

View File

@ -18,6 +18,10 @@ Public Class weightForm
Private weightResult2 As Double = 0 Private weightResult2 As Double = 0
Private zeroSet As Boolean = True Private zeroSet As Boolean = True
Private weightBuffer As New List(Of Double) ' 무게 버퍼
Private Const BUFFER_SIZE As Integer = 3 ' 버퍼 크기 (3개로 축소)
Private Const STABILITY_THRESHOLD As Double = 5.0 ' 안정성 임계값 (5g로 완화)
Private weight(2) Private weight(2)
Private weight2(1) As String Private weight2(1) As String
@ -70,16 +74,25 @@ Public Class weightForm
End If End If
ElseIf mainForm.rdbWeightQW.Checked = True Then ElseIf mainForm.rdbWeightQW.Checked = True Then
If weightResult2 > 5 Then ' 5g 이상이면 측정 시작 If weightResult2 > 5 Then ' 5g 이상이면 측정 시작
statusTimer.Enabled = False ' 실시간 무게 표시
lbNowWeight.Text = Math.Round(weightResult2, 0).ToString()
lbWeight.BackColor = Color.Yellow lbWeight.BackColor = Color.Yellow
lbWeight.Text = "측정중" lbWeight.Text = "측정중 (" & Math.Round(weightResult2, 0).ToString() & "g)"
playTimer.Enabled = False
If AutoMeasureScale() Then ' 무게 버퍼에 추가 (안정성 확인용)
lbNowWeight.Text = weightResult2 AddToWeightBuffer(weightResult2)
If weight_compare() Then ' 버퍼가 충분히 쌓이면 안정성 검사
If weightBuffer.Count >= 3 AndAlso IsWeightStable() Then
statusTimer.Enabled = False
playTimer.Enabled = False
Dim stableWeight As Double = GetStableWeight()
lbNowWeight.Text = Math.Round(stableWeight, 0).ToString()
If weight_compare_improved(stableWeight) Then
lbWeight.BackColor = Color.Green lbWeight.BackColor = Color.Green
lbWeight.Text = "무게 일치" lbWeight.Text = "무게 일치"
delay(2000) delay(2000)
@ -93,9 +106,6 @@ Public Class weightForm
lbWeight.Text = "무게 불일치" lbWeight.Text = "무게 불일치"
ZeroTimer.Enabled = True ZeroTimer.Enabled = True
End If End If
Else
lbWeight.Text = "무게 측정 불안정"
lbWeight.BackColor = Color.Red
End If End If
End If End If
Else Else
@ -133,6 +143,47 @@ Public Class weightForm
End Sub End Sub
' 무게 버퍼에 추가하는 함수
Private Sub AddToWeightBuffer(weight As Double)
weightBuffer.Add(weight)
' 버퍼 크기 초과 가장 오래된 제거
If weightBuffer.Count > BUFFER_SIZE Then
weightBuffer.RemoveAt(0)
End If
End Sub
' 무게가 안정한지 확인하는 함수
Private Function IsWeightStable() As Boolean
If weightBuffer.Count < BUFFER_SIZE Then
Return False
End If
Dim maxWeight As Double = weightBuffer.Max()
Dim minWeight As Double = weightBuffer.Min()
' 최대값과 최소값의 차이가 임계값보다 작으면 안정
Return (maxWeight - minWeight) <= STABILITY_THRESHOLD
End Function
' 안정된 무게값을 얻는 함수 (평균값 사용)
Private Function GetStableWeight() As Double
If weightBuffer.Count = 0 Then
Return 0
End If
Return weightBuffer.Average()
End Function
' 개선된 무게 비교 함수
Private Function weight_compare_improved(currentWeight As Double) As Boolean
Dim targetWeight As Double = weightSum
Dim upperLimit As Double = targetWeight + weightErrorPlus
Dim lowerLimit As Double = targetWeight - weightErrorMinus
Return currentWeight >= lowerLimit And currentWeight <= upperLimit
End Function
Private Sub Status_timer_Tick(sender As Object, e As EventArgs) Handles statusTimer.Tick Private Sub Status_timer_Tick(sender As Object, e As EventArgs) Handles statusTimer.Tick
If switchMotion = True Then If switchMotion = True Then
lbWeight.BackColor = Color.MediumSlateBlue lbWeight.BackColor = Color.MediumSlateBlue
@ -159,10 +210,33 @@ Public Class weightForm
ElseIf mainForm.rdbWeightQW.Checked = True Then ElseIf mainForm.rdbWeightQW.Checked = True Then
lbNowWeight.Text = weightResult2 lbNowWeight.Text = Math.Round(weightResult2, 0).ToString()
' 무게 불일치 재측정 모드를 먼저 체크
If zeroSet = False Then
' 무게가 변경되었는지 확인 (이전 측정값과 5g 이상 차이나면 재측정 시작)
Static previousWeight As Double = 0
' 실제 시리얼 데이터 직접 사용 If Math.Abs(weightResult2 - previousWeight) >= 5 Then
If weightResult2 <= 0 Then ' 1g 이하면 비어있음 ' 무게 변화 감지 - 재측정 시작
statusTimer.Enabled = True
playTimer.Enabled = True
ZeroTimer.Enabled = False
lbWeightEX.Text = weightSum
lbWeight.Text = "재측정 중..."
lbWeight.BackColor = Color.Yellow
' 버퍼 초기화
weightBuffer.Clear()
previousWeight = weightResult2
Else
' 무게 변화 없음 - 계속 대기
lbWeight.Text = "무게를 조정해주세요"
lbWeight.BackColor = Color.Orange
ZeroTimer.Enabled = True
previousWeight = weightResult2
End If
ElseIf weightResult2 <= 0 Then
statusTimer.Enabled = True statusTimer.Enabled = True
playTimer.Enabled = True playTimer.Enabled = True
ZeroTimer.Enabled = False ZeroTimer.Enabled = False
@ -286,7 +360,7 @@ Public Class weightForm
Private Sub QWSerialPort_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles QWSerialPort.DataReceived Private Sub QWSerialPort_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles QWSerialPort.DataReceived
Try Try
System.Threading.Thread.Sleep(100) ' 100ms 딜레이 System.Threading.Thread.Sleep(300) '딜레이
Dim incoming As String = QWSerialPort.ReadExisting() Dim incoming As String = QWSerialPort.ReadExisting()
'Debug.Print("수신 데이터: " & incoming) 'Debug.Print("수신 데이터: " & incoming)