-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathMotor.py
63 lines (47 loc) · 1.28 KB
/
Motor.py
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
#########################################
# Motor.py
# categories: motor
# more info @: http://myrobotlab.org/service/Motor
#########################################
# uncomment for virtual hardware
virtual = True
# demonstrates the basic motor api
# an Arduino is used as a motor controller
# this dc motor has a simple h-bridge
# 1 pin controls power/speed with pulse width modulation
# the other controls direction
port = "COM99"
# start the services
arduino = Runtime.start("arduino", "Arduino")
m1 = Runtime.start("m1","Motor")
m1.setPwrPin(3)
m1.setDirPin(4)
# start optional virtual arduino service, used for test
if ('virtual' in globals() and virtual):
virtualArduino = Runtime.start("virtualArduino", "VirtualArduino")
virtualArduino.connect(port)
# connect the Arduino - (our motor controller)
arduino.connect(port)
# connect motor m1 with pwm power pin 3, direction pin 4
arduino.attach(m1)
# move both motors forward
# at 50% power
# for 2 seconds
m1.move(0.5)
sleep(2)
# move both motors backward
# at 50% power
# for 2 seconds
m1.move(-0.5)
sleep(2)
# stop and lock m1
m1.stopAndLock()
# after locking
# m1 should not move
m1.move(0.5)
sleep(2)
# unlock m1 and move it
m1.unlock()
m1.move(0.5)
sleep(2)
m1.stop()