Handle USB reconnect while out of park
This commit is contained in:
@@ -1,55 +1,120 @@
|
||||
# Auto_Seat
|
||||
# Auto_Seat
|
||||
|
||||
## 개요
|
||||
Arduino Nano based automatic seat controller.
|
||||
|
||||
차량 시트 자동 제어 프로젝트입니다. EEPROM 설정, 점화/주차 조건, 릴레이 출력, 자동 보정, 시리얼 명령 처리를 포함합니다.
|
||||
## Hardware
|
||||
|
||||
분류: AutoSeat_UL, Auto_Seat_LL과 같은 자동 시트 제어 계열의 기준 프로젝트입니다.
|
||||
- Board: Arduino Nano ATmega328P
|
||||
- Up relay: D8, active-low
|
||||
- Down relay: D9, active-low
|
||||
- Parking raw input: A0
|
||||
- IG1 input: D7
|
||||
|
||||
## 코드 구성 요약
|
||||
## Main Behavior
|
||||
|
||||
- 분석한 파일 수: 3
|
||||
- 사용 라이브러리/include: EEPROM.h
|
||||
- 코드에서 확인되는 주요 모듈: EEPROM, Serial
|
||||
- 코드에서 확인되는 핀/입출력 단서: ignitionPin:INPUT, parkingPin:INPUT, seatRelayD8:OUTPUT, seatRelayD9:OUTPUT
|
||||
- If `IG1 HIGH` and parking signal is OFF, drive mode is confirmed after 80ms.
|
||||
- In confirmed drive mode, the seat moves up to the EEPROM saved position.
|
||||
- If parking/non-drive mode is confirmed for 250ms, the seat moves down to 0.
|
||||
- Down movement is estimated at 1.1x the up movement speed.
|
||||
- When the estimated position reaches 0, down relay stays on for an extra 300ms.
|
||||
- On IG1 startup, relay output is held off for 1000ms to ignore temporary signal bounce.
|
||||
|
||||
## 파일별 설명
|
||||
## Position Setting
|
||||
|
||||
### `Auto_Seat.ino`
|
||||
- Hard limit: `0..6000ms`
|
||||
- EEPROM saved position is used as the automatic driving position.
|
||||
- EEPROM initializes saved position to `6000ms` if the magic value is missing or invalid.
|
||||
- Serial command `p` saves the current estimated position to EEPROM.
|
||||
- Calibration command `c` does not change the saved position.
|
||||
|
||||
- 역할: Arduino의 `setup()`/`loop()`를 포함한 실행 스케치입니다.
|
||||
- include/의존성: EEPROM.h
|
||||
- 주요 함수: applySeatAction, clampPosition, getTargetForConfirmedMode, handleRelativeStep, handleSerial, isSeatCommandBusy, loadSettings, loop, measurement, normalizeMotionState, printIntro, printStatus, processSerialCommand, readUint16, registerIgnitionOffUse, requiresParkingForCommand, saveCurrentPositionCommand, seat, setRelay, setup, startAutoCalibration, startDownEdgeMargin, updateAutoCalibration, updateConfirmedDriveMode, updateDebugOutput, updateManualStepStatus, updateMovement, updateRelayOutput, writeUint16
|
||||
- 주요 전역값/설정값: AUTO_CALIBRATION_DELAY_MS=300000UL, AUTO_CALIBRATION_DOWN_MS=6000UL, AUTO_CALIBRATION_UP_MS=7000UL, DEBUG_INTERVAL_MS=1000UL, DOWN_EDGE_MARGIN_MS=300UL, DOWN_SPEED_DEN=10, DOWN_SPEED_NUM=11, DRIVE_CONFIRM_MS=80UL, EEPROM_MAGIC_ADDR=0, EEPROM_MAGIC_VALUE=0x64, IGNITION_STABILIZE_MS=1000UL, MANUAL_STEP_MS=500, PARK_CONFIRM_MS=250UL, PARKING_OFF_THRESHOLD=940, PARKING_ON_THRESHOLD=950, RELAY_DEADTIME_MS=120UL, SEAT_HARD_LIMIT_UP_MS=6000, USES_BEFORE_AUTO_CALIBRATION=10
|
||||
- 핀/입출력 설정: ignitionPin:INPUT, parkingPin:INPUT, seatRelayD8:OUTPUT, seatRelayD9:OUTPUT
|
||||
- 입출력/통신 호출: analogRead, digitalRead, EEPROM.read, EEPROM.update, Serial.available, Serial.begin, Serial.print, Serial.println
|
||||
## USB / Serial Reset Handling
|
||||
|
||||
### `report.html`
|
||||
Opening a USB serial connection can reset an Arduino Nano through DTR.
|
||||
|
||||
- 역할: 프로젝트 보고서 또는 설명을 담은 HTML 문서입니다.
|
||||
- include/의존성: -
|
||||
- 주요 함수: -
|
||||
- 주요 전역값/설정값: -
|
||||
- 핀/입출력 설정: -
|
||||
- 입출력/통신 호출: EEPROM.update
|
||||
If the board boots while `IG1 HIGH` and parking is OFF, the controller assumes the seat is already at the saved driving position:
|
||||
|
||||
### `upload.ps1`
|
||||
```text
|
||||
currentMs = savedPositionMs
|
||||
targetMs = savedPositionMs
|
||||
relay = OFF
|
||||
```
|
||||
|
||||
- 역할: 업로드/자동화에 사용되는 PowerShell 스크립트입니다.
|
||||
- include/의존성: -
|
||||
- 주요 함수: -
|
||||
- 주요 전역값/설정값: -
|
||||
- 핀/입출력 설정: -
|
||||
- 입출력/통신 호출: -
|
||||
This prevents unintended upward movement after USB reconnect while the gear is not in P. If the gear is then moved to P, the controller moves down from the saved position estimate to 0.
|
||||
|
||||
## 동작 흐름
|
||||
## Serial Console
|
||||
|
||||
1. `setup()`에서 시리얼, 센서, 통신 모듈, LCD/릴레이/핀 모드 등 초기 설정을 수행합니다.
|
||||
2. `loop()`에서 센서 측정, 입력 확인, 상태 계산, 출력 제어, 통신 전송 또는 화면 갱신을 반복합니다.
|
||||
3. 보조 함수들은 측정값 변환, 값 변화 감지, 릴레이/모터 제어, 시간 표시, 네트워크 응답 같은 세부 동작을 나눠 담당합니다.
|
||||
Serial is quiet by default.
|
||||
|
||||
## 빌드 및 사용 메모
|
||||
1. Open serial at `115200`.
|
||||
2. Send an empty Enter once to unlock the console and print help.
|
||||
3. After unlock, only one-character commands are accepted.
|
||||
|
||||
- Arduino IDE 또는 PlatformIO에서 폴더명과 같은 대표 스케치를 열어 빌드합니다.
|
||||
- 코드에 포함된 네트워크 주소, Wi-Fi 정보, DB 정보, 장치 핀 번호는 실제 하드웨어 구성에 맞춰 확인해야 합니다.
|
||||
- 공개 저장소에 올릴 때는 비밀번호, 토큰, 차량별 민감 정보가 포함되지 않았는지 확인합니다.
|
||||
Commands:
|
||||
|
||||
```text
|
||||
s print current status
|
||||
u manual up +500ms
|
||||
d manual down -500ms
|
||||
p save current estimated position to EEPROM
|
||||
g start debug output every 1s
|
||||
x stop debug mode only
|
||||
a return to gear-based automatic target
|
||||
c run calibration: up 7s, down 6s, set current position to 0
|
||||
r clear 10-use auto calibration counter
|
||||
h print help
|
||||
```
|
||||
|
||||
Action commands require parking gear for safety:
|
||||
|
||||
```text
|
||||
p u d a c r
|
||||
```
|
||||
|
||||
If the gear is not P:
|
||||
|
||||
```text
|
||||
ERR: check gear state. Command requires P.
|
||||
```
|
||||
|
||||
While debug mode is active, only `x` is accepted. Other input is ignored.
|
||||
|
||||
## Automatic Calibration
|
||||
|
||||
- Each `IG1 HIGH -> LOW` transition increments the EEPROM use counter.
|
||||
- After 10 uses, calibration is scheduled.
|
||||
- If IG1 remains OFF for 5 minutes, automatic calibration runs:
|
||||
|
||||
```text
|
||||
up 7000ms
|
||||
down 6000ms
|
||||
currentMs = 0
|
||||
targetMs = 0
|
||||
useCount = 0
|
||||
```
|
||||
|
||||
If IG1 turns ON during scheduled calibration, calibration is aborted. Manual `c` calibration is allowed only in P and runs to completion.
|
||||
|
||||
## Safety Guards
|
||||
|
||||
- Commands are rejected while the seat is moving or a relay action is pending.
|
||||
- Internal position values are clamped to `0..6000ms`.
|
||||
- Manual step completion is cancelled if gear state changes before completion.
|
||||
- Factory reset command was removed to avoid accidental EEPROM reset.
|
||||
|
||||
## Build / Upload
|
||||
|
||||
Compile:
|
||||
|
||||
```powershell
|
||||
& "C:\Program Files\Arduino CLI\arduino-cli.exe" compile --fqbn arduino:avr:nano:cpu=atmega328 --warnings all .
|
||||
```
|
||||
|
||||
Upload:
|
||||
|
||||
```powershell
|
||||
.\upload.ps1 -Port COM3
|
||||
```
|
||||
|
||||
## Notes
|
||||
|
||||
The controller estimates seat position by time. There is no physical position sensor. If power is cut while IG1 is OFF/P state, the next boot starts from estimated position 0. If the board resets while already in drive mode, it starts from the EEPROM saved driving position to avoid USB reconnect induced upward movement.
|
||||
|
||||
Reference in New Issue
Block a user