Skip to content

#モータードライバーDRV8835のサンプルコード

プログラム

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# coding: utf-8
import RPi.GPIO as GPIO
import time

MOTORPIN1 = 4
MOTORPIN2 = 5

GPIO.setwarnings(False)
GPIO.setmode( GPIO.BCM )
GPIO.setup(MOTORPIN1, GPIO.OUT )
GPIO.setup(MOTORPIN2, GPIO.OUT )

# PWM/100Hzに設定
ML1 = GPIO.PWM(MOTORPIN1, 100)
ML2 = GPIO.PWM(MOTORPIN2, 100)
# 初期化
ML1.start(0)
ML2.start(0)

if __name__ == '__main__':
    try:
        #モーター開始
        ML1.ChangeDutyCycle(10)
        ML2.ChangeDutyCycle(0)
        time.sleep(3)
        ML1.ChangeDutyCycle(20)
        ML2.ChangeDutyCycle(0)
        time.sleep(3)
        ML1.ChangeDutyCycle(50)
        ML2.ChangeDutyCycle(0)
        time.sleep(3)
        ML1.ChangeDutyCycle(100)
        ML2.ChangeDutyCycle(0)
        time.sleep(3)

        ML1.ChangeDutyCycle(0)
        ML2.ChangeDutyCycle(0)

        ML1.ChangeDutyCycle(0)
        ML2.ChangeDutyCycle(10)
        time.sleep(3)
        ML1.ChangeDutyCycle(0)
        ML2.ChangeDutyCycle(20)
        time.sleep(3)
        ML1.ChangeDutyCycle(0)
        ML2.ChangeDutyCycle(50)
        time.sleep(3)
        ML1.ChangeDutyCycle(0)
        ML2.ChangeDutyCycle(100)
        time.sleep(3)
        ML1.ChangeDutyCycle(0)
        ML2.ChangeDutyCycle(0)

        ML1.ChangeDutyCycle(100)
        ML2.ChangeDutyCycle(100)
        time.sleep(3)

        ML1.stop()
        ML2.stop()
    except KeyboardInterrupt:
        ML1.stop()
        ML2.stop()
        GPIO.cleanup()
        sys.exit(0)