forked from pollen-robotics/rosa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-cube-and-freeze.py
52 lines (37 loc) · 1.47 KB
/
get-cube-and-freeze.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
from __future__ import division
import cv2 as cv
from rosa import Rosa
from rosa.vision import detect_objects
def look_around(rosa, speed=0.2):
rosa.left_wheel.speed = speed
rosa.right_wheel.speed = 0
def follow_cube(rosa, center, gain=0.25):
dx, _ = center
ls = gain * (0.5 * dx + 0.5)
rs = gain * (0.5 * -dx + 0.5)
rosa.left_wheel.speed = ls
rosa.right_wheel.speed = rs
if __name__ == '__main__':
rosa = Rosa('rosa.local')
while True:
img = rosa.camera.last_frame
if img is None:
continue
found_obj = detect_objects(img, render=True)
cubes = [obj for obj in found_obj if obj.label == 'cube']
if not cubes: # We can't find a cube so we have to look around
look_around(rosa)
else:
has_gathered_cube = any([c for c in cubes if c.center[1] > 220 and 100 < c.center[0] < 200])
if has_gathered_cube: # We got a cube, so we freeze!
rosa.left_wheel.speed = 0
rosa.right_wheel.speed = 0
else: # We haven't grabbed the cube yet so we move towards it
# We arbitrarly decide that the first cube is our target.
(x, y) = cubes[0].center
height, width = 256, 320
# height, width = 480, 640
target = (((x / width) * 2 - 1), -((y / height) * 2 - 1))
follow_cube(rosa, target)
cv.imshow('get cube', img)
cv.waitKey(1)