-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.py
44 lines (36 loc) · 1.39 KB
/
robot.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
import turtle
from mouse import Mouse
# experimentally determined average of values with lots of deviation
MM_PER_P = 300 / 3830
class Robot(turtle.Turtle):
"""Robot inherits from Turtle, so we can make it work with mice and
things."""
def __init__(self):
# call super to initialize a Robot as a Turtle
super(Robot, self).__init__()
self.start_x, self.start_y = self.position()
self.screen = turtle.Screen()
# only draw every 5 updates with a delay of 16 milliseconds (100 hz)
self.screen.tracer(5, 10)
def track_motion(self):
"""starts tracking the motion of a mouse. (moution?)"""
with Mouse() as mouse:
while True:
try:
self.update(mouse)
except KeyboardInterrupt:
break
def update(self, mouse):
"""perform a single update to the Robot's position"""
status, dx, dy = mouse.update()
# left mouse click resets the robot position and heading
if status[-1]:
self.reset()
self.start_x, self.start_y = self.position()
x, y = self.position()
x += dx
y += dy
self.setposition(x, y)
radius = self.distance(self.start_x, self.start_y) * MM_PER_P
print('x: {} y: {}'.format(x, y))
print('\tdistance from the origin: {:.2f} mm'.format(radius))