|
3 | 3 | from time import sleep
|
4 | 4 | from pololu_drv8835_rpi import motors
|
5 | 5 | import threading
|
6 |
| -import RPi.GPIO as g |
| 6 | +import RPi.GPIO as GPIO |
| 7 | +import PiWarsTurkiyeRobotKiti2019 |
7 | 8 | from PiWarsTurkiyeRobotKiti2019 import HizlandirilmisPiKamera
|
8 |
| -#sudo modprobe bcm2835-v4l2 //this makes picamera visible |
9 | 9 |
|
| 10 | +# sudo modprobe bcm2835-v4l2 //this makes picamera visible |
| 11 | + |
| 12 | + |
| 13 | +motors = PiWarsTurkiyeRobotKiti2019.MotorKontrol () |
| 14 | +controller = PiWarsTurkiyeRobotKiti2019.Kumanda () |
| 15 | +controller.dinlemeyeBasla () |
| 16 | +xy = controller.solVerileriOku () |
| 17 | +xz = controller.sagVerileriOku () |
| 18 | +xx = controller.butonlariOku () |
10 | 19 | commands = []
|
11 | 20 |
|
| 21 | + |
12 | 22 | def setup():
|
13 | 23 | global camera, aruco_dict, parameters
|
14 |
| - camera = HizlandirilmisPiKamera() |
15 |
| - camera.veriOkumayaBasla() |
16 |
| - aruco_dict = aruco.Dictionary_get(aruco.DICT_4X4_250) |
17 |
| - parameters = aruco.DetectorParameters_create() |
18 |
| - motors.setSpeeds(0, 0) |
19 |
| - g.setmode(g.BCM) |
20 |
| - g.setup(26, g.IN, pull_up_down=g.PUD_DOWN) |
21 |
| - print(g.input(26)) |
22 |
| - |
23 |
| -def forward(n): |
24 |
| - motors.setSpeeds(240, 240) #max: 480 |
25 |
| - sleep(n) |
26 |
| - motors.setSpeeds(0, 0) |
27 |
| - |
28 |
| -def back(n): |
29 |
| - motors.setSpeeds(-240, -240) |
30 |
| - sleep(n) |
31 |
| - motors.setSpeeds(0, 0) |
32 |
| - |
| 24 | + camera = HizlandirilmisPiKamera () |
| 25 | + camera.veriOkumayaBasla () |
| 26 | + aruco_dict = aruco.Dictionary_get (aruco.DICT_4X4_250) |
| 27 | + parameters = aruco.DetectorParameters_create () |
| 28 | + motors.hizlariAyarla (0, 0) |
| 29 | + GPIO.setmode (GPIO.BCM) |
| 30 | + GPIO.setup (26, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) |
| 31 | + print(GPIO.input (26)) |
| 32 | + |
| 33 | + |
| 34 | +def forward(duration): |
| 35 | + motors.hizlariAyarla (240, 240) # max: 480 |
| 36 | + sleep (duration) |
| 37 | + motors.hizlariAyarla (0, 0) |
| 38 | + |
| 39 | + |
| 40 | +def back(duration): |
| 41 | + motors.hizlariAyarla (-240, -240) |
| 42 | + sleep (duration) |
| 43 | + motors.hizlariAyarla (0, 0) |
| 44 | + |
| 45 | + |
| 46 | +def right(duration): |
| 47 | + motors.hizlariAyarla (-240, 240) |
| 48 | + sleep (duration) |
| 49 | + motors.hizlariAyarla (0, 0) |
| 50 | + |
| 51 | + |
| 52 | +def left(duration): |
| 53 | + motors.hizlariAyarla (240, -240) |
| 54 | + sleep (duration) |
| 55 | + motors.hizlariAyarla (0, 0) |
| 56 | + |
| 57 | + |
33 | 58 | def takePic():
|
34 | 59 | global commands, camera, aruco_dict, parameters
|
35 |
| - frame = camera.veriOku() |
| 60 | + frame = camera.veriOku () |
36 | 61 |
|
37 |
| - gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) |
| 62 | + gray = cv2.cvtColor (frame, cv2.COLOR_BGR2GRAY) |
| 63 | + |
38 | 64 |
|
39 |
| - ids = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)[1] |
| 65 | + ids = aruco.detectMarkers (gray, aruco_dict, parameters=parameters)[1] |
40 | 66 |
|
41 |
| - if ids is None or len(ids) == 0: |
| 67 | + if ids is None or len (ids) == 0: |
42 | 68 | print("no aruco")
|
43 | 69 | return
|
44 |
| - |
45 |
| - ids = ids.tolist() |
| 70 | + |
| 71 | + ids = ids.tolist () |
46 | 72 | commands = []
|
47 | 73 | for i in ids:
|
48 | 74 | commands += i
|
49 | 75 | print(i)
|
50 |
| - |
51 |
| - #if ids is not None and len(ids) > 0:print(ids.tolist()) #0, 18, 20, 39 |
| 76 | + |
| 77 | + # if ids is not None and len(ids) > 0:print(ids.tolist()) #0, 18, 20, 39 |
| 78 | + |
52 | 79 |
|
53 | 80 | def run():
|
54 | 81 | global commands
|
55 |
| - n = 1 |
56 |
| - for i in commands: |
57 |
| - if i == 11: |
58 |
| - back(n) |
59 |
| - n = 1 |
60 |
| - if i == 0: |
61 |
| - forward(n) |
62 |
| - n = 1 |
63 |
| - if i == 9: n = 3 |
| 82 | + duration = 1 |
| 83 | + if len(commands) >=1: |
| 84 | + print(len(commands)) |
| 85 | + for i in range(len(commands)): |
| 86 | + print(i) |
| 87 | + arucoNumber = commands[i] |
| 88 | + if len(commands)-1>i: |
| 89 | + arucoNumberSecond = commands[i+1] |
| 90 | + else: |
| 91 | + arucoNumberSecond = 1 |
| 92 | + print("no specified seconds") |
| 93 | + if arucoNumber == 12: |
| 94 | + if arucoNumberSecond <= 10: |
| 95 | + duration = arucoNumberSecond |
| 96 | + print ("back", duration) |
| 97 | + back (duration) |
| 98 | + if arucoNumber == 11: |
| 99 | + if arucoNumberSecond <= 10: |
| 100 | + duration = arucoNumberSecond |
| 101 | + print ("forward", duration) |
| 102 | + forward (duration) |
| 103 | + if arucoNumber == 14: |
| 104 | + if arucoNumberSecond <= 10: |
| 105 | + duration = arucoNumberSecond |
| 106 | + left (duration) |
| 107 | + if arucoNumber == 13: |
| 108 | + if arucoNumberSecond <= 10: |
| 109 | + duration = arucoNumberSecond |
| 110 | + print ("right", duration) |
| 111 | + right (duration) |
| 112 | + if arucoNumber == 15: duration = 3 |
| 113 | + |
| 114 | + |
64 | 115 |
|
65 | 116 | def takeInput():
|
66 |
| - |
67 |
| - while(1): |
68 |
| - y = g.input(26) |
| 117 | + while (1): |
| 118 | + y = GPIO.input (26) |
69 | 119 | if (y == True):
|
70 |
| - print("ey") |
71 |
| - takePic() |
72 |
| - run() |
73 |
| - sleep(0.2) |
74 |
| - |
| 120 | + print("button pressed") |
| 121 | + takePic () |
| 122 | + |
| 123 | + run () |
| 124 | + while (xx != controller.butonlariOku () or xy != controller.sagVerileriOku () or xz != controller.solVerileriOku ()): |
| 125 | + lx, ly = controller.solVerileriOku () |
| 126 | + print(lx, ly) |
| 127 | + rightSpeed, leftSpeed = motors.kumandaVerisiniMotorVerilerineCevirme (lx, ly) |
| 128 | + #leftSpeed = motors.kumandaVerisiniMotorVerilerineCevirme (lx, ly, False) |
| 129 | + print(lx, " ", ly, " ", rightSpeed, " ", leftSpeed) |
| 130 | + motors.hizlariAyarla (rightSpeed, leftSpeed) |
| 131 | + sleep (0.3) |
| 132 | + motors.hizlariAyarla (0, 0) |
| 133 | + |
| 134 | + sleep (0.2) |
75 | 135 |
|
76 |
| -setup() |
77 |
| -sleep(1) |
78 |
| -thread = threading.Thread(target=takeInput) |
79 |
| -thread.start() |
80 |
| -while(1): |
81 |
| - frame = camera.veriOku() |
82 |
| - sleep(0.05) |
| 136 | +setup () |
| 137 | + |
| 138 | +sleep (1) |
| 139 | +thread = threading.Thread (target=takeInput) |
| 140 | +thread.start () |
| 141 | + |
| 142 | +while (1): |
| 143 | + |
| 144 | + frame = camera.veriOku () |
| 145 | + camera.kareyiGoster() |
83 | 146 |
|
0 commit comments