Handle USB reconnect while out of park
This commit is contained in:
@@ -109,6 +109,12 @@ void setup() {
|
||||
confirmedDriveMode = getRawDriveMode();
|
||||
pendingDriveMode = confirmedDriveMode;
|
||||
targetPosMs = getTargetForConfirmedMode();
|
||||
if (confirmedDriveMode == HIGH) {
|
||||
// USB serial reconnect can reset the board while the car is already out of P.
|
||||
// In that case, assume the seat is already at the saved driving position.
|
||||
currentPosMs = storedSeatPositionMs;
|
||||
targetPosMs = storedSeatPositionMs;
|
||||
}
|
||||
if (ignitionState == LOW) {
|
||||
ignitionOffStartTime = millis();
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
+8
-1
@@ -187,6 +187,13 @@ targetPosMs = storedSeatPositionMs
|
||||
relay = UP until currentPosMs reaches targetPosMs</pre>
|
||||
<p>P단 외 상태가 80ms 유지되면 EEPROM에 저장된 위치까지 상승합니다.</p>
|
||||
|
||||
<h3>3.4.1 P단 외 상태에서 USB/시리얼 리셋</h3>
|
||||
<pre>boot with IG1 = HIGH and P = LOW:
|
||||
currentPosMs = storedSeatPositionMs
|
||||
targetPosMs = storedSeatPositionMs
|
||||
relay = OFF</pre>
|
||||
<p>USB 시리얼 연결로 보드가 리셋되더라도 이미 주행 위치에 있다고 가정합니다. 따라서 리셋 직후 오상승하지 않고, 이후 P단으로 바뀌면 저장 위치 기준으로 하강합니다.</p>
|
||||
|
||||
<h3>3.5 P단 복귀 또는 시동 OFF</h3>
|
||||
<pre>confirmedDriveMode = LOW
|
||||
targetPosMs = 0
|
||||
@@ -261,7 +268,7 @@ if useCount >= 10 and IG1 LOW for 5 minutes:
|
||||
<h2>8. 현재 주의 사항</h2>
|
||||
<ul>
|
||||
<li>시트 위치는 실제 센서가 아니라 시간 기반 추정값입니다.</li>
|
||||
<li>전원 차단 후 재부팅하면 <code>currentPosMs</code>는 0에서 시작합니다.</li>
|
||||
<li>전원 차단 후 P단/IG1 OFF 상태로 재부팅하면 <code>currentPosMs</code>는 0에서 시작합니다. 단, IG1 ON + P단 외 상태에서 재부팅하면 저장 위치에서 시작한 것으로 간주합니다.</li>
|
||||
<li><code>u</code>/<code>d</code>는 위치 조절만 하고 EEPROM 저장은 하지 않습니다. 저장은 반드시 <code>p</code>로 합니다.</li>
|
||||
<li><code>d</code>는 하강, <code>g</code>는 debug입니다. debug 충돌을 피하기 위해 <code>d</code>를 debug로 쓰지 않습니다.</li>
|
||||
<li>debug 상태에서는 <code>x</code>만 작동하며, <code>r</code>, <code>a</code>, <code>s</code>, <code>u</code>, <code>d</code>, <code>p</code>, <code>c</code>는 모두 무시됩니다.</li>
|
||||
|
||||
Reference in New Issue
Block a user