forked from pimoroni/inventorhatmini-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle_motor.py
More file actions
55 lines (43 loc) · 1.29 KB
/
single_motor.py
File metadata and controls
55 lines (43 loc) · 1.29 KB
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
import time
import math
from inventorhatmini import InventorHATMini, MOTOR_A
"""
Demonstrates how to control a motor on Inventor HAT Mini.
"""
# Create a new InventorHATMini
board = InventorHATMini(init_leds=False)
# Access the motor from Inventor and enable it
m = board.motors[MOTOR_A]
m.enable()
time.sleep(2)
# Drive at full positive
m.full_positive()
time.sleep(2)
# Stop moving
m.stop()
time.sleep(2)
# Drive at full negative
m.full_negative()
time.sleep(2)
# Coast to a gradual stop
m.coast()
time.sleep(2)
SWEEPS = 2 # How many speed sweeps of the motor to perform
STEPS = 10 # The number of discrete sweep steps
STEPS_INTERVAL = 0.5 # The time in seconds between each step of the sequence
SPEED_EXTENT = 1.0 # How far from zero to drive the motor when sweeping
# Do a sine speed sweep
for j in range(SWEEPS):
for i in range(360):
m.speed(math.sin(math.radians(i)) * SPEED_EXTENT)
time.sleep(0.02)
# Do a stepped speed sweep
for j in range(SWEEPS):
for i in range(0, STEPS):
m.to_percent(i, 0, STEPS, 0.0 - SPEED_EXTENT, SPEED_EXTENT)
time.sleep(STEPS_INTERVAL)
for i in range(0, STEPS):
m.to_percent(i, STEPS, 0, 0.0 - SPEED_EXTENT, SPEED_EXTENT)
time.sleep(STEPS_INTERVAL)
# Disable the motor
m.disable()