Example #1
0
class Plan():
    border_distance = 200

    def __init__(self) -> None:
        self.logging = Logging("events")
        self.display = DisplayBlock()
        self.rgb = RgbLedBlock()
        self.rgb.set_on()

        self.distance_block = DistanceBlock(measurement_period=0.05)
        self.smooth_distance = SmoothedVariable(3, SmoothingType.progressive,
                                                self.distance_block.value)
        self.short_distance_event = self.smooth_distance.less_than(
            self.border_distance, True, self.short_distance)
        self.smooth_distance.more_than(self.border_distance, True,
                                       self.long_distance)
        self.power_save_state = 0

        Planner.repeat(0.5, self.get_distance)

    def short_distance(self):
        self.logging.info("short_distance")
        self.rgb.set_color(RgbLedBlockColor.red)

    def long_distance(self):
        self.logging.info("long_distance")
        self.rgb.set_color(RgbLedBlockColor.green)

    def get_distance(self):
        raw_distance = self.distance_block.value.get(False)
        self.display.clean()
        self.display.print_text(0, 0, str(raw_distance))
        self.display.print_text(0, 9, str(int(self.smooth_distance.get())))
        self.display.showtime()
Example #2
0
class Plan():
    near_barrier = 300
    far_barrier = 600
    open_space = 900

    def __init__(self) -> None:
        self.logging = Logging("events")
        self.chassis = Chassis(power_measurement_period=0.3)
        self.rgb = RgbLedBlock()
        self.display = DisplayBlock()
        self.distance = DistanceBlock(measurement_period=0.1)
        self.button = ButtonBlock(measurement_period=0.1)
        self.ir_block = IrBlock()
        self.ir_block.add_remote(IrNumericRemote())

        self.button.value.equal_to(True, True, self.change_patrol)

        self.near_barrier_event = None
        self.far_bariier_event = None
        self.enough_space_event = None
        self.open_space_event = None
        self.patrol = False

        self.display.contrast(0)
        Ble.value_remote.equal_to(RemoteKey("a"), True, self.turn_left)
        Ble.value_remote.equal_to(RemoteKey("d"), True, self.turn_right)
        Ble.value_remote.equal_to(RemoteKey("w"), True, self.speed_up)
        Ble.value_remote.equal_to(RemoteKey("s"), True, self.slow_down)
        Ble.value_remote.equal_to(RemoteKey("z"), True, self.stop)
        Ble.value_remote.equal_to(RemoteKey("x"), True, self.reverse)
        Ble.value_remote.equal_to(RemoteKey("p"), True, self.change_patrol)

        self.ir_block.value.equal_to(IrNumericRemote.key_left, True,
                                     self.turn_left)
        self.ir_block.value.equal_to(IrNumericRemote.key_right, True,
                                     self.turn_right)
        self.ir_block.value.equal_to(IrNumericRemote.key_top, True,
                                     self.speed_up)
        self.ir_block.value.equal_to(IrNumericRemote.key_bottom, True,
                                     self.slow_down)
        self.ir_block.value.equal_to(IrNumericRemote.key_ok, True, self.stop)
        self.ir_block.value.equal_to(IrNumericRemote.key_hash, True,
                                     self.reverse)
        self.ir_block.value.equal_to(IrNumericRemote.key_star, True,
                                     self.change_patrol)

        self.counter = 0
        Planner.repeat(0.5, self.print_power_info)
        PowerMgmt.set_plan(PowerPlan.get_max_performance_plan())

        self.current_smoother = SmoothedVariable(
            3, SmoothingType.average, self.chassis.power.battery_current_mA)
        self.current_smoother.more_than(600, True, self.stop_for_a_while, 1)
        self.heart_beat = False

    def define_patrol_events(self):
        self.near_barrier_event = self.distance.value.less_than(
            self.near_barrier, True, self.near_barrier_detected)
        self.far_bariier_event = self.distance.value.in_range(
            self.near_barrier, self.far_barrier, True,
            self.far_barrier_detected)
        self.enough_space_event = self.distance.value.in_range(
            self.far_barrier, self.open_space, True,
            self.enough_space_detected)
        self.open_space_event = self.distance.value.more_than(
            self.open_space - 1, True, self.open_space_detected)

    def cancel_patrol_events(self):
        self.distance.value.remove_trigger(self.near_barrier_event)
        self.distance.value.remove_trigger(self.far_bariier_event)
        self.distance.value.remove_trigger(self.enough_space_event)
        self.distance.value.remove_trigger(self.open_space_event)

    def near_barrier_detected(self):
        self.logging.info("near_barrier_detected")
        self.rgb.set_color(RgbLedBlockColor.red)
        self.chassis.set_speed(Speed.slow)
        self.chassis.set_direction(Direction.backward)
        self.chassis.set_manoeuver(
            Manoeuver.sharply_right)  #with reverse will turn left

    def far_barrier_detected(self):
        self.logging.info("far_barrier_detected")
        self.rgb.set_color(RgbLedBlockColor.orange)
        self.chassis.set_speed(Speed.slow)
        self.chassis.set_direction(Direction.forward)
        self.chassis.set_manoeuver(Manoeuver.straight)

    def enough_space_detected(self):
        self.logging.info("enough_space")
        self.rgb.set_color(RgbLedBlockColor.blue)
        self.chassis.set_speed(Speed.normal)
        self.chassis.set_direction(Direction.forward)
        self.chassis.set_manoeuver(Manoeuver.straight)

    def open_space_detected(self):
        self.logging.info("open_space")
        self.rgb.set_color(RgbLedBlockColor.green)
        self.chassis.set_speed(Speed.normal)
        self.chassis.set_direction(Direction.forward)
        self.chassis.set_manoeuver(
            Manoeuver.slightly_right)  #can rotate another wat

    def change_patrol(self):
        if self.patrol:
            self.logging.info("patrol mode canceled")
            self.cancel_patrol_events()
            self.chassis.set_speed(Speed.stop)
        else:
            self.logging.info("patrol mode activated")
            self.define_patrol_events()
            self.chassis.set_speed(Speed.slow)
        self.patrol = not self.patrol

    def print_power_info(self):
        voltage = self.chassis.power.battery_voltage_V.get()

        self.display.clean()
        self.heart_beat = not self.heart_beat
        if self.heart_beat:
            self.display.print_text(55, 0, "*")
        self.display.print_text(0, 9, "%2.3f V" % voltage)

        self.display.print_text(0, 18,
                                "%3.2f mA" % self.current_smoother.get())
        self.display.print_text(0, 36, str(self.distance.value.get()))
        self.counter += 1
        self.display.showtime()

    def slow_down(self):
        self.chassis.set_speed(self.chassis.speed - 1)

    def speed_up(self):
        self.chassis.set_speed(self.chassis.speed + 1)

    def turn_left(self):
        self.chassis.set_manoeuver(self.chassis.manoeuver - 1)

    def turn_right(self):
        self.chassis.set_manoeuver(self.chassis.manoeuver + 1)

    def reverse(self):
        self.logging.info(self.chassis.direction == Direction.forward)
        self.chassis.set_direction(Direction.backward if self.chassis.direction
                                   == Direction.forward else Direction.forward)

    def stop_for_a_while(self, delay):
        self.stop()  #take a rest
        Planner.postpone(delay, self.chassis.set_speed,
                         Speed.slow)  #... and slowly continue

    def stop(self):
        self.chassis.set_speed(Speed.stop)
        self.chassis.set_manoeuver(Manoeuver.straight)
Example #3
0
class Plan:
    def __init__(self):
        self.started = False
        self.display = DisplayBlock()
        self.logging = Logging("plan")
        self.button = ButtonBlock()
        self.rgb = RgbLedBlock()
        self.led_default()
        self.dim_x, self.dim_y = self.display.get_dimensions()
        self.center_x = int(self.dim_x / 2)
        self.center_y = int(self.dim_y / 2)
        self.point = None
        self.map = None
        self.score = 0

        self.wait_for_start()
        self.redraw()

    def wait_for_start(self):
        self.started = False
        self.score = 0
        self.point = Point(self.center_y)
        self.map = Map(self.dim_x, self.dim_y)
        self.display.invert(True)
        self.button.value.changed(False, self.start_button)
        gc.collect()  #force garbage collection before game starts

    def start_button(self):
        self.started = True
        self.logging.info("started")
        self.redraw()
        self.led_default()

    def redraw(self):
        speed = int(self.map.current_x / 400) + 1
        if speed > 10:
            speed = 10

        if not self.map.is_enough_data():
            remove_step_count = speed
            self.map.remove_first_layers(remove_step_count)
            self.map.add_random_layers(remove_step_count)

        self.display.clean()
        self.point.draw(self.display)
        if self.started:
            if self.button.value.get():
                self.point.y -= 2
            else:
                self.point.y += 2

        border_top, border_bottom = self.map.get_borders(self.point.x)

        touch = False
        if self.point.y < border_top + self.point.r - 1:
            self.point.y = border_top + self.point.r
            touch = True

        if self.point.y > border_bottom - self.point.r:
            self.point.y = border_bottom - self.point.r
            touch = True

        if not touch:
            diamonds = self.map.get_diamonds(self.point.x)
            for diamond in diamonds:
                if diamond.is_in_area(self.point.y - self.point.r,
                                      self.point.y + self.point.r):
                    self.map.remove_diamond(diamond)
                    self.score += 1
                    self.rgb.set_color(RgbLedBlockColor.green)
                    Planner.postpone(0.05, self.led_default)

        self.map.draw(self.display)
        if self.started:
            self.display.invert(False if touch else True)
            self.map.current_x += speed
        #score = int(self.map.current_x / 10)
        self.display.print_text(0, 0, str(self.score), color=0)
        self.display.showtime()
        if not touch:
            Planner.postpone(0.05, self.redraw)
        else:
            self.started = False
            self.rgb.set_color(RgbLedBlockColor.red)
            Planner.postpone(
                1, self.wait_for_reset_pressed
            )  #wait a while to prevent random press and wait for starting new game

    def led_default(self):
        self.rgb.set_color(RgbLedBlockColor.aquamarine)

    def wait_for_reset_pressed(self):
        self.button.value.equal_to(True, False, self.wait_for_reset_released)

    def wait_for_reset_released(self):
        self.button.value.equal_to(False, False, self.wait_for_start)