def move(rect): (x,y,w,h) = rect center = (320,240) rect_center = (x+w//2, y+h//2) cv2.circle(img, center, 1, (0, 0, 255), 2); cv2.circle(img, rect_center, 1, (0, 0, 255), 2); go_back = -1 right_left = -1 if w*h > 45000 or y < 50: go_back = 2 elif w*h < 35000 or y > 430: go_back = 1 else: go_back = 0 if rect_center[0] > center[0]+100: right_left = 2 elif rect_center[0] < center[0]-100: right_left = 1 else: right_left = 0 location = (('stop','go','back'), ('left','go_left','back_left'), ('right','go_right','back_right')) loc = location[right_left][go_back] if loc == 'stop': m.stop() elif loc == 'go': m.go() elif loc == 'back': m.back() elif loc == 'left': m.left() elif loc == 'right': m.right() elif loc == 'go_left': m.go_left() elif loc == 'go_right': m.go_right() elif loc == 'back_left': m.back_left() elif loc == 'back_right': m.back_right() print(loc)
if distance[front] > clearance and distance[ frontLeft] > clearance and distance[frontRight] > clearance: motor.front(m1, m2) elif distance[frontRight] > distance[frontLeft]: if distance[frontRight] > clearance: motor.turnRight(m1, m2) sleep(2.5) motor.front(m1, m2) elif distance[frontLeft] > clearance: motor.turnLeft(m1, m2) sleep(2.5) motor.front(m1, m2) else: motor.back(m1, m2) sleep(5) if (distance[backRight] > distance[backLeft]) and distance[backRight] > clearance: motor.turnLeft(m1, m2) sleep(2.5) motor.front(m1, m2) elif distance[backLeft] > clearance: motor.turnRight(m1, m2) sleep(2.5) motor.front(m1, m2) loop()
def back(): data = json.loads(request.data) sleepTime = data['time'] motor.back(sleepTime) result = {'status': 'back'} return Response(json.dumps(result), mimetype='application/json')
def move(rect): (x, y, w, h) = rect center = (320, 240) rect_center = (x + w // 2, y + h // 2) cv2.circle(img, center, 1, (0, 0, 255), 2) cv2.circle(img, rect_center, 1, (0, 0, 255), 2) go_back = -1 right_left = -1 if w * h > 45000 or y < 50: go_back = 2 elif w * h < 35000 or y > 430: go_back = 1 else: go_back = 0 if rect_center[0] > center[0] + 100: right_left = 2 elif rect_center[0] < center[0] - 100: right_left = 1 else: right_left = 0 location = (('stop', 'go', 'back'), ('left', 'go_left', 'back_left'), ('right', 'go_right', 'back_right')) loc = location[right_left][go_back] ''' x= np.linspace(0,640,8) x2 = np.linspace(0,640,6) degree = np.array([4.5,5.5,6.5,7.5,8.5,9.5,10.5]) degree2 = np.array([4.5,6,7.5,9,10.5]) for i in range(0,len(degree)): if x[i] < rect_center[0] and x[i+1] > rect_center[0]: m.setServo(degree[i]) time.sleep(0.3) break ''' if loc == 'stop': m.stop() elif loc == 'go': m.go() elif loc == 'back': m.back() elif loc == 'left': m.left() elif loc == 'right': m.right() elif loc == 'go_left': m.go_left() elif loc == 'go_right': m.go_right() elif loc == 'back_left': m.back_left() elif loc == 'back_right': m.back_right() print(loc)
time.sleep(0.05) key = stdscr.getch() # Parse input if key == ord('m') or key == ord('M'): manual = not manual elif manual and (key == ord('w') or key == ord('W')): output.addstr("Going forwards\n") motor.stop() motor.forward() # elif manual and (key == ord('a') or key == ord('A')): # motor.left(90) elif manual and (key == ord('s') or key == ord('S')): output.addstr("Going backwards\n") motor.stop() motor.back() # elif manual and (key == ord('d') or key == ord('D')): # motor.right(90) elif key == ord(' '): output.addstr("Stopping\n") motor.stop() elif key == ord('r') or key == ord('R'): # Reset all variables to initial state read = 999 in_front = False in_front_count = 0 crossed = False # Closing tasks brick.close() curses.endwin()