# 遠端鍵盤控制自走車＋WebCam

透過遠端SSH連線，直接執行程式並手動控制自走車的行進與轉彎，有了這個功能，只要網路是通的，遠在天邊也可以控制家裡的自走車並且看見家裡的情況。這次一併介紹getkey這個非常方便使用的鍵盤掃描程式庫。

{% embed url="<https://youtu.be/9Pm5SdLtBHY>" %}

```python
# -*- coding: UTF-8 -*-
# 自走車馬達類別
import time
import RPi.GPIO as GPIO
from getkey import getkey, keys


class Motor:
    def __init__(self, ena, in1, in2, enb, in3, in4):
        self.ENA = ena
        self.IN1 = in1
        self.IN2 = in2
        self.ENB = enb
        self.IN3 = in3
        self.IN4 = in4
        # 設定右輪
        GPIO.setup(self.ENA, GPIO.OUT, initial=GPIO.LOW)
        # 利用PWN來改變車子馬達轉速（改變車子速度），初始值為全速100
        self.ENA_SPEED = GPIO.PWM(self.ENA, 600)
        self.ENA_SPEED.start(0)
        self.ENA_SPEED.ChangeDutyCycle(100)
        GPIO.setup(self.IN1, GPIO.OUT, initial=GPIO.LOW)
        GPIO.setup(self.IN2, GPIO.OUT, initial=GPIO.LOW)
        # 設定左輪
        GPIO.setup(self.ENB, GPIO.OUT, initial=GPIO.LOW)
        self.ENB_SPEED = GPIO.PWM(self.ENB, 600)
        self.ENB_SPEED.start(0)
        self.ENB_SPEED.ChangeDutyCycle(100)
        GPIO.setup(self.IN3, GPIO.OUT, initial=GPIO.LOW)
        GPIO.setup(self.IN4, GPIO.OUT, initial=GPIO.LOW)

    def right_speed(self, speed):
        self.ENA_SPEED.ChangeDutyCycle(speed)

    def left_speed(self, speed):
        self.ENB_SPEED.ChangeDutyCycle(speed)

    def speed(self, speed):
        self.right_speed(speed)
        self.left_speed(speed)

    def forward(self):
        GPIO.output(self.ENA, True)
        GPIO.output(self.ENB, True)
        GPIO.output(self.IN1, True)
        GPIO.output(self.IN2, False)
        GPIO.output(self.IN3, True)
        GPIO.output(self.IN4, False)

    def backward(self):
        GPIO.output(self.ENA, True)
        GPIO.output(self.ENB, True)
        GPIO.output(self.IN1, False)
        GPIO.output(self.IN2, True)
        GPIO.output(self.IN3, False)
        GPIO.output(self.IN4, True)

    def turn_left(self):
        GPIO.output(self.ENA, True)
        GPIO.output(self.ENB, True)
        GPIO.output(self.IN1, True)
        GPIO.output(self.IN2, False)
        GPIO.output(self.IN3, False)
        GPIO.output(self.IN4, False)

    def turn_right(self):
        GPIO.output(self.ENA, True)
        GPIO.output(self.ENB, True)
        GPIO.output(self.IN1, False)
        GPIO.output(self.IN2, False)
        GPIO.output(self.IN3, True)
        GPIO.output(self.IN4, False)

    def stop(self):
        GPIO.output(self.ENA, True)
        GPIO.output(self.ENB, True)
        GPIO.output(self.IN1, False)
        GPIO.output(self.IN2, False)
        GPIO.output(self.IN3, False)
        GPIO.output(self.IN4, False)

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
motor = Motor(13, 19, 16, 20, 21, 26)
motor_speed = 100
while True:
    motor.speed(motor_speed)
    key = getkey()
    if key == keys.UP:
        print('forward')
        motor.forward()
    elif key == keys.DOWN:
        print('backward')
        motor.backward()
    elif key == keys.LEFT:
        print('turn left')
        motor.turn_left()
    elif key == keys.RIGHT:
        print('turn right')
        motor.turn_right()
    elif key == 'l':
        motor_speed -= 10
        if motor_speed <= 30:
            motor_speed = 30
    elif key == 'h':
        motor_speed += 10
        if motor_speed >= 100:
            motor_speed = 100
    elif key == 's':
        motor.stop()
    elif key == keys.ESCAPE:
        motor.stop()
        break

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://s761111.gitbook.io/raspi-sensor/shu-mei-pai-yu-zi-zou-che/yuan-duan-jian-pan-kong-zhi-zi-zou-che-webcam.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
