-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrobotcontrol00.py
More file actions
194 lines (147 loc) · 4.2 KB
/
robotcontrol00.py
File metadata and controls
194 lines (147 loc) · 4.2 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""raspberry pi robot control panel with live video feed.
Just like a video game control panel!
*only this time the car/robot is real.
version: 4.f
1. seeing live feed, buttons overlayed
3. show notification on image
4. Control DC motors (making a full mobile robot)
author : Ashraf Minhaj
mail : ashraf_minhaj@yahoo.com
"""
""" import necessary libraries """
import cv2
from tkinter import *
from PIL import Image, ImageTk
import RPi.GPIO as pin
""" variables """
# motor control pins
m11 = 8
m12 = 10
m21 = 12
m22 = 11
msg ='' # message variable
notif = ''
# load button icons
backg_img = 'res/car.gif'
for_img = 'res/f.gif'
left_img = 'res/l.gif'
right_img = 'res/r.gif'
back_img = 'res/b.gif'
quit_img = 'res/close.gif'
# cam feed variables
font = cv2.FONT_HERSHEY_SIMPLEX
org = (30, 20)
fontScale = 0.7
color = (255, 255, 255)
thickness = 2
""" setup things here """
pin.setwarnings(False)
pin.setmode(pin.BOARD)
# setup pins with initial state (LOW)
pin.setup(m11, pin.OUT, initial = pin.LOW)
pin.setup(m12, pin.OUT, initial = pin.LOW)
pin.setup(m21, pin.OUT, initial = pin.LOW)
pin.setup(m22, pin.OUT, initial = pin.LOW)
# read from camera 1
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 600)
#cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 350)
""" initialize things """
root = Tk()
root.title("RPI Robot Control Panel by Ashraf Minhaj")
# main label for showing the feed (imagelabel)
imagel = Label(root)
imagel.pack()
def pin_state_low():
"""default state of certain pins, important for motors."""
pin.output(m11, pin.LOW)
pin.output(m12, pin.LOW)
pin.output(m21, pin.LOW)
pin.output(m22, pin.LOW)
def forward():
"""forward motion"""
global msg
msg = 'Going Forward'
print(msg)
pin.output(m11, pin.HIGH)
pin.output(m21, pin.HIGH)
return
def backward():
"""backward motion"""
global msg
msg = 'Going Backward'
print(msg)
pin.output(m12, pin.HIGH)
pin.output(m22, pin.HIGH)
return
def left():
"""go left"""
global msg
msg = 'Going Left'
print(msg)
pin.output(m12, pin.HIGH)
pin.output(m21, pin.HIGH)
return
def right():
"""go right"""
global msg
msg = 'Going Right'
print(msg)
pin.output(m11, pin.HIGH)
pin.output(m22, pin.HIGH)
return
def clear_msg():
global msg
msg = ''
def get_frame():
"""get a frame from the cam and return it."""
# print("Getting image")
ret, frame = cap.read()
return frame
def update():
"""update frames"""
global msg
frame = get_frame()
cv2.putText(frame, msg, org, font, fontScale, color, thickness, cv2.LINE_AA)
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# manipulate image here
# to perfomr advanced CV operations
# convert opencv image to tk image format
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
imagel.imgtk = imgtk
imagel.configure(image=imgtk)
# clear m
clear_msg()
# motor pins back to low state
pin_state_low()
# update frame after certain delay
imagel.after(15, update)
# read the image for tk
im_f = PhotoImage(file= for_img)
im_l = PhotoImage(file= left_img)
im_r = PhotoImage(file= right_img)
im_b = PhotoImage(file= back_img)
im_quit = PhotoImage(file=quit_img)
# buttons
for_but = Button(root,text="<< Left",repeatdelay=15,repeatinterval=10, command=forward)
for_but.config(image=im_f, fg='gray', border=0, borderwidth=0, bg='black')
for_but.place(x=540, y=250)
left_but = Button(root,repeatdelay=15,repeatinterval=10, command=left)
left_but.config(image=im_l,border=0,borderwidth=0, bg='black' )
left_but.place(x=500, y=300)
right_but = Button(root,repeatdelay=15,repeatinterval=10, command=right)
right_but.config(image=im_r,border=0,borderwidth=0, bg='black')
right_but.place(x=580, y=300)
back_but = Button(root, repeatdelay=15, repeatinterval=10, command=backward)
back_but.config(image=im_b, border=0,borderwidth=0, bg='black')
back_but.place(x=540, y = 350)
quit_but = Button(root, text='Quit', command=root.destroy)
quit_but.config(image = im_quit, bg='red')
quit_but.place(x=0, y=0)
msg = ''
clear_msg()
update()
root.resizable(0, 0)
root.mainloop()
pin.cleanup()