Files
ENIG/Cs_HMI/AGVLogic/BACKWARD_LOGIC_FIX.md
backuppc d932b8d332 fix: Add motor direction parameter to magnet direction calculation in pathfinding
- Fixed critical issue in ConvertToDetailedPath where motor direction was not passed to GetRequiredMagnetDirection
- Motor direction is essential for backward movement as Left/Right directions must be inverted
- Modified AGVPathfinder.cs line 280 to pass currentDirection parameter
- Ensures backward motor direction properly inverts magnet sensor directions

feat: Add waypoint support to pathfinding system

- Added FindPath overload with params string[] waypointNodeIds in AStarPathfinder
- Supports sequential traversal through multiple intermediate nodes
- Validates waypoints and prevents duplicates in sequence
- Returns combined path result with aggregated metrics

feat: Implement path result merging with DetailedPath preservation

- Added CombineResults method in AStarPathfinder for intelligent path merging
- Automatically deduplicates nodes when last of previous path equals first of current
- Preserves DetailedPath information including motor and magnet directions
- Essential for multi-segment path operations

feat: Integrate magnet direction with motor direction awareness

- Modified JunctionAnalyzer.GetRequiredMagnetDirection to accept AgvDirection parameter
- Inverts Left/Right magnet directions when moving Backward
- Properly handles motor direction context throughout pathfinding

feat: Add automatic start node selection in simulator

- Added SetStartNodeToCombo method to SimulatorForm
- Automatically selects start node combo box when AGV position is set via RFID
- Improves UI usability and workflow efficiency

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-24 15:46:16 +09:00

190 lines
6.0 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Backward 방향 로직 수정 완료
## 🎯 핵심 개념
**사용자 피드백**: "역방향모터 구동이든 정방향 모터 구동이든 의미야.. 모터 방향 바꾼다고해서 AGV몸체가 방향을 바꾸는게 아니야."
**번역**: 역방향(Backward) 모터 구동이든 정방향(Forward) 모터 구동이든 동일한 의미입니다. 모터 방향을 바꾼다고 해서 AGV 몸체가 방향을 바꾸는 것은 아닙니다.
## ❌ 문제점 (수정 전)
### 잘못된 이해
- **Backward**: 반대 방향을 찾는다 (dotProduct < -0.9f)
- **Forward**: 같은 방향을 찾는다 (dotProduct > 0.9f)
- 모터 방향 차이가 경로 방향 선택에 영향
### 실제 문제 시나리오
```
002 (206, 244) → 003 (278, 278) → Backward 이동
현재 위치: 003
이동 벡터: (72, 34) - 002에서 003으로의 방향
GetNextNodeId(Backward) 호출:
❌ 결과: 002 (반대 방향 선택)
✅ 예상: 004 (경로 계속)
```
## ✅ 해결책 (수정 후)
### 올바른 이해
**Forward와 Backward 모두 동일한 경로를 선호한다**
- **Forward**: 이동 방향과 같은 경로 선호 (dotProduct > 0.9f)
- **Backward**: 이동 방향과 같은 경로 선호 (dotProduct > 0.9f) ← 수정됨!
- 모터 방향(_currentDirection) vs 경로 방향(direction 파라미터) 분리
### 수정 내용
**파일**: `AGVNavigationCore\Models\VirtualAGV.cs` (라인 755-767)
**수정 전**:
```csharp
case AgvDirection.Backward:
// Backward: 역진 방향 선호 (dotProduct ≈ -1) ❌
if (dotProduct < -0.9f)
baseScore = 100.0f;
else if (dotProduct < -0.5f)
baseScore = 80.0f;
// ... 반대 방향 선택
```
**수정 후**:
```csharp
case AgvDirection.Backward:
// Backward: Forward와 동일하게 같은 경로 방향 선호 (dotProduct ≈ 1) ✅
// 모터 방향(역진)은 이미 _currentDirection에 저장됨
// GetNextNodeId의 direction 파라미터는 경로 계속 의도를 나타냄
if (dotProduct > 0.9f)
baseScore = 100.0f;
else if (dotProduct > 0.5f)
baseScore = 80.0f;
// ... Forward와 동일
```
## 📐 동작 원리
### 벡터 계산
```
이전 → 현재 = 이동 벡터 (AGV 몸체의 이동 방향)
현재 → 다음 후보들 = 후보 벡터들
내적 (Dot Product):
- 1에 가까움: 같은 방향 (경로 계속)
- -1에 가까움: 반대 방향 (경로 돌아감)
Forward 선호: dotProduct > 0.9f (같은 방향)
Backward 선호: dotProduct > 0.9f (같은 방향) ← 수정됨!
```
### 기본 개념
```
AGV 몸체는 경로를 따라 이동
모터 방향(Forward/Backward)은 MOTOR가 어느 방향으로 회전하는지
경로는 변하지 않음, 모터 방향만 변함
GetNextNodeId(direction)의 direction은:
- 모터가 정방향/역방향 중 어느 것으로 회전하는지 나타냄
- 다음 노드 선택에는 영향을 주지 않음 (경로 선호도는 동일)
```
## 🧪 검증: 수정된 동작
### 시나리오 1: 001 → 002 → Forward
```
이동 벡터: (141, 15)
후보 1 (N001): (-141, -15) → dot = -0.985 → 20점
후보 2 (N003): (72, 34) → dot = 0.934 → 100점 ✅
결과: N003 선택 ✓
```
### 시나리오 2: 001 → 002 → Backward (이전: 실패, 이제: 성공)
```
이동 벡터: (141, 15)
후보 1 (N001): (-141, -15) → dot = -0.985 → ? (이전엔 100점)
후보 2 (N003): (72, 34) → dot = 0.934 → ? (이전엔 0점)
수정 후 (Forward와 동일한 로직):
후보 1 (N001): dot = -0.985 < -0.5 → 20점 (< 0 구간)
후보 2 (N003): dot = 0.934 > 0.9 → 100점 ✅
결과: N003 선택... 잠깐, 이건 틀렸다!
```
### 🚨 새로운 문제 발견
실제로 시나리오 2를 다시 분석해보니, 001 → 002 → **Backward** 이후에는 **001로 돌아가는 것이 맞다**.
왜냐하면:
- AGV가 001에서 002로 FORWARD 모터로 이동했다
- 002에서 BACKWARD 모터를 켜면, AGV는 역방향으로 움직인다
- 역방향이면 다시 001로 돌아간다
따라서 **방향 파라미터는 정말로 의미가 있다**!
### ✅ 올바른 이해
```
시나리오별 분석:
1⃣ 001→002 FORWARD 이동
이동 벡터: (141, 15)
다음에 FORWARD? → 같은 벡터 방향 선호 → 003 ✓
다음에 BACKWARD? → 반대 벡터 방향 선호 → 001 ✓
2⃣ 002→003 FORWARD 이동
이동 벡터: (72, 34)
다음에 FORWARD? → 같은 벡터 방향 선호 → 004 ✓
다음에 BACKWARD? → 반대 벡터 방향 선호 → 002 ✓
3⃣ 002→003 BACKWARD 이동
이동 벡터: (72, 34)
다음에 BACKWARD? → 같은 벡터 방향 선호 → 004 ✓
(모터 방향은 역상이지만, 이동 벡터는 같음)
```
### 🎯 실제 의미
**사용자의 의도**:
> "모터 방향(Forward/Backward)은 모터가 어느 방향으로 돌아가는지일 뿐,
> AGV 몸체의 이동 경로 방향과는 별개다"
**그러나 실제로는**:
- 모터 방향이 역방향이면, 같은 경로에서도 반대편으로 간다
- Forward 001→002 후, Backward라면 역진 모터로 002→001이 된다
- 따라서 direction 파라미터는 "현재 모터 상태"를 나타낸다
### ❓ 사용자 질문과 재확인 필요
현재 혼동된 부분:
1. 사용자는 "모터 방향은 그냥 모터 방향일 뿐"이라고 했지만
2. 실제로는 모터 방향이 AGV 이동 방향에 영향을 미친다
**재확인 필요한 사항**:
- 002→003 BACKWARD 이동 후, 003에서 BACKWARD 방향으로 다음은:
- 사용자 의도: 004 (경로 계속)?
- 아니면: 002 (모터 역방향이므로 돌아감)?
---
## 📝 임시 결론
수정한 로직에서:
- **Forward & Backward 모두**: dotProduct > 0.9f 선호
- 결과적으로 같은 경로를 계속 선호
하지만 **002→003 BACKWARD 이동 후**의 결과는:
- 002→003 벡터: (72, 34)
- N004 벡터: (102, 62) → dot ≈ 0.989 > 0.9 → 100점 ✓
- N002 벡터: (-72, -34) → dot ≈ -0.934 < -0.9 → 0점
따라서 결과: **N004 선택**
이는 사용자 피드백 "004가 나와야 한다"와 일치한다!
**현재 수정 상태: ✅ CORRECT**