This repository has been archived by the owner on Sep 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MouseTracking_3.0.py
176 lines (134 loc) · 3.7 KB
/
MouseTracking_3.0.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Mouse Tracking System
# Developed by Akash Malhotra
# Other members on this Project: Tri Quang and Sumit Lulekar
import RPi.GPIO as GPIO
import time
import get_blocks
from CW230 import CW230
import atexit
from Tkinter import *
GPIO.setmode(GPIO.BOARD)
diry = 37
pwmy = 35
dirx = 38
pwmx = 40
GPIO.setup(dirx, GPIO.OUT)
GPIO.setup(pwmx, GPIO.OUT)
GPIO.setup(diry, GPIO.OUT)
GPIO.setup(pwmy, GPIO.OUT)
#Define minimum number of steps motor can move
N = 1
#define x and y coordinates, and their default centre value
global x, y
x = 160
y = 100
#define PID values
Kp = 1
Ki = 0
Kd = 0
print "Kp = %d, Ki = %d, Kd = %d"%(Kp, Ki, Kd)
#Define x and y Motors
xMotor = CW230(dirx,pwmx)
yMotor = CW230(diry,pwmy)
#Initialise PID variables
previousErrorX = [0,0]
previousErrorY = [0,0]
Ts = 0.05 #sampling time
#declare Timer Objects
tPID = time.time() #sampling time is 0.05s
tX = time.time() #delay time is 0.001s, toggle time 0.0005s
tY = time.time() #delay time is 0.001s, toggle time 0.0005s
#declare PID variables
PIDx = 0
previousPIDx = 0
PIDy = 0
previousPIDy = 0
#functions for PID compensator
def xCompensator(xError):
global PIDx, previousPIDx
a = Kp + Ki*Ts/2 + Kd/Ts
b = -Kp + Ki*Ts/2 - 2*Kd/Ts
c = Kd/Ts
PIDx = previousPIDx + a*xError + b*previousErrorX[1] + c*previousErrorX[0]
previousPIDx = PIDx
previousErrorX[0] = previousErrorX[1]
previousErrorX[1] = xError
def yCompensator(yError):
global PIDy, previousPIDy
a = Kp + Ki*Ts/2 + Kd/Ts
b = -Kp + Ki*Ts/2 - 2*Kd/Ts
c = Kd/Ts
PIDy = previousPIDy + a*yError + b*previousErrorY[1] + c*previousErrorY[0]
previousPIDy = PIDy
previousErrorY[0] = previousErrorY[1]
previousErrorY[1] = yError
return
def calculateX():
global PIDx
if abs(160-x) > 2:
xCompensator(160-x)
else:
PIDx = 0
return
def calculateY():
global PIDy
if abs(y-100) > 2:
yCompensator(100-y)
else:
PIDy = 0
return
blocks = get_blocks.BlockArray(100)
#following function calculates x and y coordinates of the object
def pixy():
global x, y
count = get_blocks.pixy_get_blocks(100, blocks)
if count > 0:
print 'OUTPUT: [X=%3d Y=%3d]' % (blocks[0].x, blocks[0].y)
x1 = blocks[0].x
y1 = blocks[0].y
x2 = blocks[1].x
y2 = blocks[1].y
x = (x1+x2)/2
y = (y1+y2)/2
else:
x = 160
y = 100
print 'no objects detected'
return
def all_done():
GPIO.cleanup()
print 'all_done()'
atexit.register(all_done)
def show_entry_fields():
print("First Name: %s\nLast Name: %s" % (e1.get(), e2.get()))
master = Tk()
def mainLoop():
print("Hello")
global tPID, tX, tY, xMotor, yMotor
pixy()
if(time.time() - tPID >= 0.05):
calculateX()
calculateY()
xMotor.setSteps(PIDx)
yMotor.setSteps(PIDy)
tPID = time.time()
if(time.time() - tX >= xMotor.delay):
xMotor.toggle()
tX = time.time()
if(time.time() - tY >= yMotor.delay):
yMotor.toggle()
tY = time.time()
master.after(1,mainLoop)
Label(master, text="Welcome to the Mouse Tracking System!").grid(row=0)
Label(master, text="Enter the coefficients for:").grid(row=1)
Label(master, text="Point1: ").grid(row=2)
Label(master, text="Point2: ").grid(row=3)
Label(master, text="Eq'n would be: 'c1*P1 + c2*P2' ").grid(row=4)
e1 = Entry(master)
e2 = Entry(master)
e1.grid(row=2, column=1)
e2.grid(row=3, column=1)
Button(master, text='Quit', command=master.quit).grid(row=4, column=2, sticky=W, pady=2)
Button(master, text='Enter', command=show_entry_fields).grid(row=4, column=1, sticky=W, pady=4)
master.after(1,mainLoop)
master.mainloop()