Esempio n. 1
0
def main():
    ledmatrix.rotation(0)
    ledmatrix.clear()  # Clear the display
    draw_battery_outline()  # Draw the battery outline

    battery = Pitop().battery

    while True:
        try:
            charging_state, capacity, _, _ = battery.get_full_state()
            update_battery_state(charging_state,
                                 capacity)  # Fill battery with capacity

        except Exception as e:
            print("Error getting battery info: " + str(e))
Esempio n. 2
0
def assemble_robot():
    robot = Pitop()

    robot.add_component(Camera(resolution=(640, 480), rotate_angle=90))
    robot.add_component(PincerController())
    robot.add_component(
        NavigationController(left_motor_port="M3", right_motor_port="M0")
    )

    return robot
Esempio n. 3
0
def assemble_robot():
    pitop = Pitop()

    pitop.add_component(TiltRollHeadController())
    pitop.head.calibrate()

    pitop.add_component(Camera(resolution=(640, 480), flip_top_bottom=True))

    pitop.add_component(UltrasonicSensor("A1"))

    return pitop
Esempio n. 4
0
    def __init__(self):
        logger.debug("Setting ENV VAR to use miniscreen as system...")
        environ["PT_MINISCREEN_SYSTEM"] = "1"

        logger.debug("Initializing miniscreen...")
        miniscreen = Pitop().miniscreen

        def set_is_user_controlled(user_has_control) -> None:
            if user_has_control:
                self.stop_timers()
            else:
                self.restore_miniscreen()

            logger.info(
                f"User has {'taken' if user_has_control else 'given back'} control of the miniscreen"
            )

        miniscreen.when_user_controlled = lambda: set_is_user_controlled(True)
        miniscreen.when_system_controlled = lambda: set_is_user_controlled(False)
        miniscreen.select_button.when_released = self.create_button_handler(
            self.handle_select_button_release
        )
        miniscreen.cancel_button.when_released = self.create_button_handler(
            self.handle_cancel_button_release
        )
        miniscreen.up_button.when_released = self.create_button_handler(
            self.handle_up_button_release
        )
        miniscreen.down_button.when_released = self.create_button_handler(
            self.handle_down_button_release
        )

        self.dimmed = False
        self.screensaver_timer = None
        self.dimming_timer = None
        self.start_dimming_timer()

        logger.debug("Initialising app...")
        super().__init__(miniscreen, Root=RootComponent)
Esempio n. 5
0
from pitop import Camera, Pitop
from pitop.labs import WebController

photobooth = Pitop()
photobooth.add_component(Camera())


def save_photo(data):
    photobooth.camera.current_frame().save(f'{data["name"]}.jpg')


controller = WebController(get_frame=photobooth.camera.get_frame,
                           message_handlers={"save_photo": save_photo})

controller.serve_forever()
from time import sleep

import cv2

from pitop import Camera, Pitop

miniscreen = Pitop().miniscreen

cam = Camera(format="OpenCV", flip_top_bottom=True)
directory = "images/"
button = miniscreen.select_button
picture_count = 0

while True:
    frame = cam.get_frame()

    cv2.imshow("Frame", frame)
    miniscreen.display_image(frame)
    if button.is_pressed:
        cv2.imwrite(f"{directory}image_{picture_count}.jpg", frame)
        print(f"Frame written to file with ID: {picture_count}\n")
        picture_count += 1
        sleep(0.5)
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

cv2.destroyAllWindows()
Esempio n. 7
0
    else:
        draw_emoji(
            canvas=canvas, emoji="Looking", bounding_box=wide_bounding_box, offset_y=5
        )
        draw_text(canvas=canvas, text="Looking...")

    robot.miniscreen.display_image(image)


def run_emotion_and_face_detector(frame):
    face = face_detector(frame)
    emotion = emotion_classifier(face)
    update_displayed_image(face, emotion)


robot = Pitop()
camera = Camera(resolution=(640, 480), flip_top_bottom=True)
robot.add_component(camera)

image = Image.new(
    robot.miniscreen.mode,
    robot.miniscreen.size,
)

w_ms, h_ms = robot.miniscreen.size

sample_text = "100% Happy"
_, top_section_height = text_font.getsize(sample_text)

left_bounding_box = (0, top_section_height, w_ms // 2, h_ms - top_section_height)
right_bounding_box = (
Esempio n. 8
0
from pitop import Pitop

battery = Pitop().battery

print(f"Battery capacity: {battery.capacity}")
print(f"Battery time remaining: {battery.time_remaining}")
print(f"Battery is charging: {battery.is_charging}")
print(f"Battery is full: {battery.is_full}")
print(f"Battery wattage: {battery.wattage}")


def do_low_battery_thing():
    print("Battery is low!")


def do_critical_battery_thing():
    print("Battery is critically low!")


def do_full_battery_thing():
    print("Battery is full!")


def do_charging_battery_thing():
    print("Battery is charging!")


def do_discharging_battery_thing():
    print("Battery is discharging!")

    face = face_detector(frame)
    robot_view = face.robot_view

    cv2.imshow("Faces", robot_view)
    cv2.waitKey(1)

    if face.found:
        face_angle = face.angle
        robot.head.track_head_angle(face_angle)
        print(f"Face angle: {face.angle}")
    else:
        robot.head.roll.sweep(speed=0)
        print("Cannot find face!")


robot = Pitop()

robot.add_component(
    TiltRollHeadController(servo_roll_port="S0", servo_tilt_port="S3"))
robot.head.calibrate()
robot.head.tilt.target_angle = 70
robot.head.roll.target_angle = 0

robot.add_component(Camera(resolution=(640, 480), flip_top_bottom=True))

face_detector = FaceDetector()

robot.camera.on_frame = track_face

pause()
Esempio n. 10
0
from signal import pause

from pitop import Camera, DriveController, Pitop
from pitop.processing.algorithms.line_detect import process_frame_for_line

# Assemble a robot
robot = Pitop()
robot.add_component(
    DriveController(left_motor_port="M3", right_motor_port="M0"))
robot.add_component(Camera())


# Set up logic based on line detection
def drive_based_on_frame(frame):
    processed_frame = process_frame_for_line(frame)

    if processed_frame.line_center is None:
        print("Line is lost!", end="\r")
        robot.drive.stop()
    else:
        print(f"Target angle: {processed_frame.angle:.2f} deg ", end="\r")
        robot.drive.forward(0.25, hold=True)
        robot.drive.target_lock_drive_angle(processed_frame.angle)
        robot.miniscreen.display_image(processed_frame.robot_view)


# On each camera frame, detect a line
robot.camera.on_frame = drive_based_on_frame

pause()
Esempio n. 11
0
    def __new__(cls, *args, **kwargs):
        from pitop import Pitop

        obj = Pitop.from_config(alex_config)
        obj.__class__ = cls
        return obj
Esempio n. 12
0
from time import sleep

from pitop import Pitop

# Load configuration from a previous session
pitop = Pitop.from_file("/home/pi/my_custom_config.json")

# Check the loaded configuration
print(pitop.config)

# Do something with your device
pitop.red_led.on()
pitop.drive.forward(0.5)
sleep(2)
pitop.red_led.off()
pitop.drive.stop()

# Check the state of all the components attached to the Pitop object
pitop.print_state()
Esempio n. 13
0
from time import sleep

import gevent

from pitop import LED, Button, Pitop, UltrasonicSensor
from pitop.labs import WebController

robot = Pitop()
robot.add_component(Button("D1"))
robot.add_component(LED("D2"))
robot.add_component(UltrasonicSensor("D3"))

dashboard_server = WebController()


def broadcast_state():
    while True:
        sleep(0.1)
        dashboard_server.broadcast(robot.state)


hub = gevent.get_hub()
hub.threadpool.spawn(broadcast_state)

dashboard_server.serve_forever()
from time import sleep

from pitop import LED, Pitop
from pitop.robotics.drive_controller import DriveController

pitop = Pitop()
drive_controller = DriveController()
led = LED("D0", name="red_led")

# Add components to the Pitop object
pitop.add_component(drive_controller)
pitop.add_component(led)

# Do something with the object
pitop.red_led.on()
pitop.drive.forward(0.5)
sleep(2)
pitop.red_led.off()
pitop.drive.stop()

# Store configuration to a file
pitop.save_config("/home/pi/my_custom_config.json")
Esempio n. 15
0
from pitop import Camera, DriveController, Pitop
from pitop.labs import RoverWebController

rover = Pitop()
rover.add_component(DriveController())
rover.add_component(Camera())

rover_controller = RoverWebController(
    get_frame=rover.camera.get_frame,
    drive=rover.drive,
)

rover_controller.serve_forever()
from random import randint
from time import sleep

from pitop import Pitop
from pitop.robotics.drive_controller import DriveController

# Create a basic robot
robot = Pitop()
drive = DriveController(left_motor_port="M3", right_motor_port="M0")
robot.add_component(drive)


# Use miniscreen display
robot.miniscreen.display_multiline_text("hey there!")


def random_speed_factor():
    # 0.01 - 1, 0.01 resolution
    return randint(1, 100) / 100


def random_sleep():
    # 0.5 - 2, 0.5 resolution
    return randint(1, 4) / 2


# Move around randomly
robot.drive.forward(speed_factor=random_speed_factor())
sleep(random_sleep())

robot.drive.left(speed_factor=random_speed_factor())
from PIL import Image, ImageSequence

from pitop import Pitop

pitop = Pitop()
miniscreen = pitop.miniscreen

rocket = Image.open(
    "/usr/lib/python3/dist-packages/pitop/miniscreen/images/rocket.gif")

for frame in ImageSequence.Iterator(rocket):
    miniscreen.display_image(frame)
Esempio n. 18
0

def main():
    square_side_length = 1.0  # m
    square_top_right = (square_side_length / 2, -square_side_length / 2)
    square_top_left = (square_side_length / 2, square_side_length / 2)
    square_bottom_left = (-square_side_length / 2, square_side_length / 2)
    square_bottom_right = (-square_side_length / 2, -square_side_length / 2)

    for _ in range(0, 2):
        set_goal_with_obstacle_detection(position=square_top_right)
        set_goal_with_obstacle_detection(position=square_top_left)
        set_goal_with_obstacle_detection(position=square_bottom_left)
        set_goal_with_obstacle_detection(position=square_bottom_right)

    # go back to start_position
    robot.navigate.go_to(position=(0, 0),
                         angle=0,
                         on_finish=navigation_finished,
                         backwards=True).wait()
    print(f"Goal reached with state: \n{robot.navigate.state_tracker}")


goal_reached = False
robot = Pitop()
robot.add_component(
    NavigationController(left_motor_port="M3", right_motor_port="M0"))
robot.add_component(UltrasonicSensor("D3"))

main()