Skip to content

Commit

Permalink
first drone code
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronteamAAR committed Jun 25, 2022
0 parents commit 266582d
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/pythonProject1.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions Keypressmodule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import pygame

def init():
pygame.init()
win = pygame.display.set_mode((400, 400))

def getKey(keyName):
ans = False
for eve in pygame.event.get():pass
keyInput = pygame.key.get_pressed()
myKey = getattr(pygame,'K_{}'.format(keyName))
if keyInput[myKey]:
ans = True
pygame.display.update()
return ans
def main():
if getKey("LEFT"):
print("Left key Pressed")
if getKey("RIGHT"):
print("Right key Pressed")
if __name__ == '__main__':
init()
while True:
main()
14 changes: 14 additions & 0 deletions imagecpature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from djitellopy import tello
import cv2

us = tello.Tello()
us.connect()
print(us.get_battery())

us.streamon()

while True:
pic = us.get_frame_read().frame
pic = cv2.resize(pic, (360, 240))
pic.imshow("image", pic)
cv2.waitKey(2)
51 changes: 51 additions & 0 deletions keyboardcontrol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from djitellopy import tello
import Keypressmodule as kp
import time
import cv2

kp.init()
us = tello.Tello()
us.connect()
print(us.get_battery())


def getKeyIn():
lr, fb, up, yv = 0, 0, 0, 0
speed = 50
if kp.getKey("LEFT"): lr = speed
elif kp.getKey("RIGHT"): lr = speed

if kp.getKey("UP"):
fb = speed
elif kp.getKey("DOWN"):
fb = -speed

if kp.getKey("w"):
up = speed
elif kp.getKey("s"):
up = -speed

if kp.getKey("a"):
yv = -speed
elif kp.getKey("d"):
yv = speed

if kp.getKey("q"):
us.land()
if kp.getKey("e"):
us.takeoff()

if kp.getKey('z'):
cv2.imwrite(f'Resources/Images/{time.time()}.jpg')
##To aviod multiple unwanted shots
time.sleep(0.3)

return [lr, fb, up, yv ]

##Reset drone if error is found
us.takeoff()

while True:
vals = getKeyIn()
us.send_rc_control(vals[0], vals[1], vals[2], vals[3])

18 changes: 18 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from djitellopy import tello
from time import sleep

us = tello.Tello()
us.connect()
print(us.get_battery())
us.takeoff()
us.move_forward(15)
sleep(2)
##After sleep move
us.send_rc_control(0, 50, 0, 0)
##left, right, forward, backward and yaw(twist of drone)
sleep(1)
us.send_rc_control(10, 0, 0, 0)
sleep(4)
us.send_rc_control(0, 0, 0, 0)
## so as to land like a helicopter
us.land()
105 changes: 105 additions & 0 deletions mapping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from djitellopy import tello
import Keypressmodule as kp
from time import sleep
import numpy as np
import cv2
import math
##PARAMETERS

forsp = 117/10 ##forward speed (cm)/s
aspeed = 360/10 ##Angular speed in degrees/s
val = 0.75

dval = forsp * aspeed
aval = aspeed* val ## angular interval

x, y = 500, 500
a = 0
yaw = 0
kp.init()
us = tello.Tello()
us.connect()
print(us.get_battery())

points = []

x, y = 500, 650

def getKeyIn():
lr, fb, up, yv = 0, 0, 0, 0
speed = 15
global yaw
global x
global y
global a
d = 0

if kp.getKey("LEFT"):
lr = speed
d = dval
a = -100
elif kp.getKey("RIGHT"):
lr = speed
d = -dval
a = -180

if kp.getKey("UP"):
fb = speed
d = dval
a = 270
elif kp.getKey("DOWN"):
fb = -speed
d = -dval
a = -19

if kp.getKey("w"):
up = speed
elif kp.getKey("s"):
up = -speed

if kp.getKey("a"):
yv = -speed
yaw -= aval
elif kp.getKey("d"):
yv = speed
yaw += -aval

if kp.getKey("q"):
us.land()
if kp.getKey("e"):
us.takeoff()

sleep(val)
a += yaw
x += int(d*math.cos(math.radians(a)))
y += int(d*math.sin(math.radians(a)))

if kp.getKey('z'):
cv2.imwrite(f'Resources/Images/{time.time()}.jpg')
##To aviod multiple unwanted shots
time.sleep(0.3)



return [lr, fb, up, yv, x, y]

##Reset drone if error is found
us.takeoff()
## DRaw an image using the drone.
def artPoints(img , points):
for point in points:
cv2.circle(img, point , 5, (0, 0, 255), cv2.FILLED)
cv2.putText(img, f'({points[-1][0] - 500/100}, {points[-1][1] - 500/100})m'),\
(points[-1][0] + 10, points[-1][1] + 30 )


while True:
vals = getKeyIn()
us.send_rc_control(vals[0], vals[1], vals[2], vals[3])

img = np.zeros((1000, 1000, 3), np.uint8)
points.append((vals[4], vals[5]))
artPoints(img, points)
cv2.imshow("Resource", img)
cv2.waitkey(1)

0 comments on commit 266582d

Please sign in to comment.