Пример #1
0
class AutonomousCar:
    def __init__(self, settings):
        self.settings = settings
        self.go_ahead = True
        self.turn = False
        self.turn_angle = 30
        if bool(random.getrandbits(1)):
            self.turn_angle *= -1
        self.initiate_objects()
        if settings['run_test']:
            self.test_function()
        else:
            self.calibrate()
            self.the_loop()

    def initiate_objects(self):
        self.hub = MSHub()
        self.hub.status_light.on('orange')
        self.motor_front = Motor(self.settings['motor_front_port'])
        self.motor_rear = Motor(self.settings['motor_rear_port'])
        self.dist_sens = DistanceSensor(self.settings['dist_sens_port'])

    def calibrate(self):
        self.motor_front.run_to_position(self.settings['motor_front_angle'])

    def test_function(self):
        self.hub.light_matrix.show_image('PACMAN')
        sounds_and_colors = [(65, "blue"), (70, "azure"), (60, "cyan")]
        for i in sounds_and_colors:
            self.hub.speaker.beep(i[0], 0.3)
            self.hub.status_light.on(i[1])
        self.hub.light_matrix.off()

    def end_turn(self):
        self.turn = False
        self.motor_front.run_for_degrees(self.turn_angle * -1)

    def the_loop(self):
        self.hub.light_matrix.write('GO')
        self.hub.status_light.on('green')
        loop_time_diff = 0
        loop_start_time = time.time()
        turn_time_diff = 0
        turn_start_time = None
        while loop_time_diff < self.settings['how_long_run']:
            how_far = self.dist_sens.get_distance_cm()
            rear_speed = self.settings.get('motor_rear_speed')
            if self.turn:
                self.hub.status_light.on('yellow')
                turn_time_control = time.time()
                turn_time_diff = turn_time_control - turn_start_time
                if isinstance(
                        how_far, int
                ) and how_far <= self.settings['dist_threshold_low']:
                    self.go_ahead = False
                    self.end_turn()
                elif turn_time_diff >= self.settings['turn_in_seconds']:
                    self.end_turn()
            elif self.go_ahead:
                self.hub.status_light.on('green')
                rear_speed = rear_speed * -1
                if isinstance(
                        how_far, int
                ) and how_far <= self.settings['dist_threshold_low']:
                    self.go_ahead = False
            else:
                self.hub.status_light.on('blue')
                if isinstance(
                        how_far, int
                ) and how_far >= self.settings['dist_threshold_high']:
                    self.turn = True
                    turn_start_time = time.time()
                    self.motor_front.run_for_degrees(self.turn_angle)
                    self.go_ahead = True
            self.motor_rear.start(rear_speed)
            wait_for_seconds(0.25)
            loop_time_control = time.time()
            loop_time_diff = loop_time_control - loop_start_time
        self.motor_rear.stop()
        self.calibrate()
        self.hub.light_matrix.write('STOP')
Пример #2
0
print("DONE!")

# %%
# Turn on the lights of the distance sensor.
print("Turning on the distance sensor...")
distance_sensor.light_up_all(100)
print("DONE!")

# %% [markdown]
# Define Charlie's shy reaction.

# %%
while True:

    # Get distance measurement.
    distance = distance_sensor.get_distance_cm()
    print("Distance measurement: " + str(distance) + " cm")

    # We need to make sure that Charlie reacts only when he perceives a distance.
    # To do so, we check what distance Charlie perceived. If he didn't perceive
    # a distance, distance_sensor.get_distance_cm() returns None.
    if not distance == None:

        # Charlie will get embarrassed if you get closer than 30 cm
        # For now, I defined this distance as 15 cm for testing purposes.
        distance_threshold = 15
        if distance < distance_threshold:

            print("Charlie is embarrassed! (distance = " + str(distance) + " cm)")

            # Turn off the lights of the distance sensor.