Exemplo n.º 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')
Exemplo n.º 2
0
# # Set arm motors to starting position

# %%
print("Setting arm motors to position 0...")
motor_left_arm.run_to_position(0)
motor_right_arm.run_to_position(0)
print("DONE!")

# %% [markdown]
# # Configure sensor

# %%
print("Configuring sensors...")
color_sensor = ColorSensor('C')

distance_sensor = DistanceSensor('D')
distance_sensor.light_up_all()
print("DONE!")

# %% [markdown]
# # Make Charlie roam around

# %%
print("Roaming...")

# While testing, it can be quite annoying having Charlie moving around.
# If you wish, you can just comment the following line. This way,
# Charlie will stay still while you test your program.
motors_wheels.start()

# %% [markdown]
Exemplo n.º 3
0
 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'])
Exemplo n.º 4
0
# https://antonsmindstorms.com

# This code is meant to run on a LEGO MINDSTORMS Robot Inventor Snake
# Connect to it using a steering wheel or app:
# https://play.google.com/store/apps/details?id=com.antonsmindstorms.mindstormsrc&hl=nl&gl=US
# Building instructions for the snake:
# https://antonsmindstorms.com/product/robot-snake-with-multiple-51515-inventor-sets/

from hub import port
from time import sleep_ms
import math
from projects.mpy_robot_tools.rc import RCReceiver, R_STICK_VER, L_STICK_HOR, SETTING2
from projects.mpy_robot_tools.motor_sync import Mechanism, AMHTimer
from mindstorms import DistanceSensor

ds = DistanceSensor('A')
rcv = RCReceiver(name="snake")

motors = [
    port.C.motor,
    port.D.motor,
    port.E.motor,
    port.F.motor,
]


def sine_wave_w_params(amplitude=100, period=1000, offset_factor=0):
    def function(x, delay_setting=0, baseline=0):
        # global baseline, delay_setting
        return baseline + math.sin((x - delay_setting * offset_factor) /
                                   period * 2 * math.pi) * amplitude
Exemplo n.º 5
0
motor_turret = Motor('B')  # Turrent spinning

# %%
print("Setting motors to position 0...")
motor_steer.run_to_position(45, speed=100)
motor_steer.run_to_position(0, speed=100)

motor_turret.run_to_position(0, speed=75)
print("DONE!")

# %% [markdown]
# # Initialize distance sensor

# %%
print("Initializing distance sensor...")
distance_sensor = DistanceSensor('D')
print("DONE!")

# %% [markdown]
# # Put the AAT MS5 in motion
#
# The tank will move until the distance sensor detects an obstacle.
#
# The steering is given by `POSITION`.
# * A value between `0` and `90` will steer the tank to the left.
#     - A value closer to `0` will make the tank turn wider.
#     - A value closer to `90` will make the tank turn tighter.
#
# * A value between `270` and `360` will steer the tank to the right.
#     - A value closer to `270` will make the tank turn tighter.
#     - A value closer to `360` will make the tank turn wider.
Exemplo n.º 6
0
# Radar car - this car drives only north<->south or east<->west, when hitting a wall rotating to where is the most space.
from mindstorms import MSHub, Motor, MotorPair, DistanceSensor
import math
import sys

# Setup hardware
hub = MSHub()
mradar = Motor("C")
sradar = DistanceSensor("F")
mdrive = MotorPair("A", "B")


# Lights up 5x5 matrix.
# Parameter `bits` is a 25 bits vector: a 1 switches on that led.
# Bit 24 is upper left, bit 0 is lower right.
def set_image(bits):
    hub.light_matrix.off()
    cur = 1 << 24
    for y in range(5):
        for x in range(5):
            if bits & cur: hub.light_matrix.set_pixel(x, y, 100)
            cur >>= 1


# Emits 3 tone fancy beep
def beep():
    hub.speaker.beep(60, 0.1)
    hub.speaker.beep(70, 0.1)
    hub.speaker.beep(62, 0.1)

Exemplo n.º 7
0
# %% [markdown]
# # Set arm motors to starting position

# %%
print("Setting arm motors to position 0...")
motor_left_arm.run_to_position(0)
motor_right_arm.run_to_position(0)
print("DONE!")

# %% [markdown]
# # Make Charlie shy

# %%
# Initialize the distance sensor.
print("Initializing the distance sensor...")
distance_sensor = DistanceSensor('D')
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.
Exemplo n.º 8
0
# %% [markdown]
# # Set arm motors to starting position

# %%
print("Setting arm motors to position 0...")
motor_left_arm.run_to_position(0)
motor_right_arm.run_to_position(0)
print("DONE!")

# %% [markdown]
# # Configure sensor

# %%
print("Configuring distance sensor...")
distance_sensor = DistanceSensor('D')
distance_sensor.light_up_all()
print("DONE!")

# %% [markdown]
# # High five!

# %%
print("Waiting for high five intention...")
distance_sensor.wait_for_distance_closer_than(12, unit='cm')
distance_sensor.light_up_all(0)
print("DONE!")

# %%
print("Let's high five!")
motor_right_arm.run_for_degrees(-120)