async def counter(websocket, path): # register(websocket) sends user_event() to websocket await register(websocket) try: await websocket.send(state_event()) async for message in websocket: data = json.loads(message) if data["action"] == "left": STATE["horizontal_angle"] -= 0.5 pantilt.set_horizontal_angle(STATE["horizontal_angle"]) await notify_state() elif data["action"] == "right": STATE["horizontal_angle"] += 0.5 pantilt.set_horizontal_angle(STATE["horizontal_angle"]) await notify_state() elif data["action"] == "up": STATE["vertical_angle"] += 0.5 pantilt.set_vertical_angle(STATE["vertical_angle"]) await notify_state() elif data["action"] == "down": STATE["vertical_angle"] -= 0.5 pantilt.set_vertical_angle(STATE["vertical_angle"]) await notify_state() elif data["action"] == "off": pantilt.power_off() await notify_state() else: logging.error("unsupported event: {}", data) finally: await unregister(websocket)
def circle(self): for x in np.arange(0, 2 * math.pi, math.pi / 180): h_rad = math.sin(x) / 5 v_rad = math.cos(x) / 5 h = h_rad * 180 / math.pi v = v_rad * 180 / math.pi pantilt.set_horizontal_angle(h) pantilt.set_vertical_angle(v) sleep(0.01)
def test_keypoints(self): # On a target, on the wall. Camera distance = 800 mm angle = pantilt.horizontal_position_to_angle(215.9 / 2, 600) # Aiming for the edge of a 8 1/2 x 11 sheet centered at 800mm pantilt.set_vertical_angle(0) pantilt.set_horizontal_angle(0) sleep(1) pantilt.set_horizontal_angle(angle) sleep(1) pantilt.set_horizontal_angle(-angle)
def square(self): pantilt.set_horizontal_angle(-10) pantilt.set_vertical_angle(-10) for h in np.arange(-10, 10): pantilt.set_horizontal_angle(h) sleep(0.1) for v in np.arange(-10, 10): pantilt.set_vertical_angle(v) sleep(0.1) for h in np.arange(10, -10, -1): pantilt.set_horizontal_angle(h) sleep(0.1) for v in np.arange(10, -10, -1): pantilt.set_vertical_angle(v) sleep(0.1)
def test_vertical_sweep(self): for angle in range(-45, 45): pantilt.set_vertical_angle(angle) if angle == 0: sleep(1) sleep(0.1)
last_horizontal_error = horizontal_error data["Horizontal error"] = (horizontal_error, "pixels") data["Horizontal angle"] = (horizontal_angle, "degrees") if abs(horizontal_error) > hysteresis: horizontal_angle = horizontal_angle + P * horizontal_error + D * delta_horizontal_error draw_broadcast_queue.append((image, objs, labels, rtcom)) data["Broadcast time"] = ((time.perf_counter() - start) * 1000, "ms") start = time.perf_counter() rtcom.broadcast_endpoint("perf", perf) rtcom.broadcast_endpoint("data", data) data["Network time"] = ((time.perf_counter() - start) * 1000, "ms") start = time.perf_counter() pantilt.set_horizontal_angle(horizontal_angle) pantilt.set_vertical_angle(0) if "pc" in rtcom and "coordinates" in rtcom["pc"]: pantilt.set_vertical_angle( rtcom["pc"]["coordinates"]["vertical_angle"]) data["Pantilt time"] = ((time.perf_counter() - start) * 1000, "ms") beat += 1 data["Loop runtime"] = ((time.perf_counter() - loop_start_time) * 1000, "ms")