Ejemplo n.º 1
0
def line_follow(colour_sensor, speed, P_multiplier, D_multiplier, I_multiplier,
                sample_time, distance):
    global log
    robot.reset()
    last_error = 0
    iteration = 0
    p = 0
    d = 0
    i = 0
    error = 0
    total = (p + (i * I_multiplier) + d)
    if log == True:
        data = DataLog('total', 'p', 'i', 'd', 'error', 'last error')
    while robot.distance() < distance:
        error = threshold - colour_sensor.reflection()
        p = error * P_multiplier
        d = (error - last_error) / sample_time * D_multiplier
        i = error * sample_time + i
        total = (p + (i * I_multiplier) + d)
        if log == True:
            data.log(total, p, i * I_multiplier, d, error, last_error)
        #this stays on the right side of the line
        robot.drive(speed, total)
        last_error = error
        time.sleep(sample_time)
    robot.stop()
    leftmotor.hold()
    rightmotor.hold()
Ejemplo n.º 2
0
watch = StopWatch()

# Create your objects here.
ev3 = EV3Brick()

# Initialize the color sensor.
left_sensor = ColorSensor(Port.S1)

# Place on black.
ev3.speaker.say("Place on black")

# Write the value of the left color sensor to the screen.
while True:
    # print("Button pressed: " + str(ev3.buttons.pressed()))
    if (Button.CENTER in ev3.buttons.pressed()):
        # Check the value of reflected light
        reflected_light = left_sensor.reflection()
        colorValues.log('black', reflected_light)
        ev3.speaker.say("Recorded black")
        break

# Place on white.
ev3.speaker.say("Place on white")
while True:
    if (Button.CENTER in ev3.buttons.pressed()):
        # Check the value of reflected light
        reflected_light = left_sensor.reflection()
        colorValues.log('white', reflected_light)
        ev3.speaker.say("Recorded white")
        break
Ejemplo n.º 3
0
#!/usr/bin/env pybricks-micropython
from pybricks.parameters import Color
from pybricks.tools import DataLog

# Create a data log file called my_file.txt
data = DataLog('time', 'angle', name='my_file', timestamp=False, extension='txt')

# The log method uses the print() method to add a line of text.
# So, you can do much more than saving numbers. For example:
data.log('Temperature', 25)
data.log('Sunday', 'Monday', 'Tuesday')
data.log({'Kiwi': Color.GREEN}, {'Banana': Color.YELLOW})

# You can upload the file to your computer, but you can also print the data:
print(data)
Ejemplo n.º 4
0
            platform_motor.run_angle(speed=rotation_speed,
                                     rotation_angle=total_rotation,
                                     then=Stop.HOLD,
                                     wait=False)

            wait(100)
            # print('Platform Speed: {0}'.format(platform_motor.speed()))

            # Log the time and the sensor readings 10 times
            while platform_motor.speed() > 0:
                rotation_angle = platform_motor.angle()
                character_reflectivity = platform_color_sensor.reflection()
                print("Angle: {0} degrees, Reflectivity: {1}%".format(
                    platform_motor.angle(),
                    platform_color_sensor.reflection()))
                data.log(rotation_angle, character_reflectivity)

                # Wait some time so the motor can move a bit
                wait(log_interval)

                # Play another beep sound.
            ev3.speaker.beep(frequency=1000, duration=500)
            break

else:
    numbers = [
        'ZERO', 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT',
        'NINE'
    ]
    number_dict = {i: val for i, val in enumerate(numbers)}
    prediction = number_dict[int(args.predict)]
Ejemplo n.º 5
0
                print(feedback)
                if feedback != 0:
                    num_feedbacks += 1
                wait(3000)
                user.update_feedback_table(state, action, feedback)
            if reward == 0 and is_done is False:
                reward = -1  # penalty for each move
        else:
            print(move)
            move -= 1  # does not count as a move
            print(move)
            if reward == 0 and is_done is False:
                reward = -3  # penalty for trying to move out of bounds

        ep_time = episode_stopwatch.time()
        data.log(episode, curr_reward, ep_time, num_feedbacks, fam.agent_pos,
                 is_done, user.username)
        print(state, action)
        qtable.qtable[state][action] = (1 - learning) * qtable.qtable[state][action] + learning * \
                                        (reward + discount * qtable.maxq(next_state))

        state = next_state
        curr_reward += reward
        print(qtable.qtable)
        if is_done:
            fam.unload()
            fam.say("Hooray!", color=Color.GREEN)
            break
    if not is_done:
        fam.say("Let's try again.")
    ep_time = episode_stopwatch.time()
    data.log(episode, curr_reward, ep_time, num_feedbacks, fam.agent_pos,
Ejemplo n.º 6
0
from pybricks.hubs import EV3Brick
from pybricks.tools import wait, StopWatch, DataLog
from pybricks.parameters import Color, Port
from pybricks.ev3devices import Motor
from pybricks.iodevices import AnalogSensor, UARTDevice

# Initialize the EV3
ev3 = EV3Brick()
ev3.speaker.beep()
sense = AnalogSensor(Port.S1, False)
sense.voltage()

watch = StopWatch()
wheel = Motor(Port.A)
data = DataLog('output.txt', header='Time,Angle,Voltage')

# Turn on a red light
ev3.light.on(Color.RED)
ev3.speaker.say("About to take data")
wheel.run(500)

for i in range(10):
    time = watch.time()
    angle = wheel.angle()
    light = sense.voltage()  #This seems to give a EIO error sometimes.
    data.log(time, angle, light)
    wait(100)

# Turn the light off
ev3.light.off()
Ejemplo n.º 7
0
                print(feedback)
                if feedback != 0:
                    num_feedbacks += 1
                wait(3000)
                user.update_feedback_table(state, action, feedback)
            if reward == 0 and is_done is False:
                reward = -1  # penalty for each move
        else:
            print(move)
            move -= 1  # does not count as a move
            print(move)
            if reward == 0 and is_done is False:
                reward = -3  # penalty for trying to move out of bounds

        ep_time = episode_stopwatch.time()
        data.log(episode, curr_reward, ep_time, num_feedbacks, fam.agent_pos,
                 is_done, user.username, qtable.qtable)
        print(state, action)
        qtable.qtable[state][action] = (1 - learning) * qtable.qtable[state][action] + learning * \
                                        (reward + discount * qtable.maxq(next_state))

        state = next_state
        curr_reward += reward
        print(qtable.qtable)
        if is_done:
            fam.say("Hooray!")
            break
    if not is_done:
        fam.say("Let's try again.")
    ep_time = episode_stopwatch.time()
    data.log(episode, curr_reward, ep_time, num_feedbacks, fam.agent_pos,
             is_done)
Ejemplo n.º 8
0
        robot.drive(DRIVE_SPEED, turn_rate)

        last_deviation = deviation
    else:
        robot.stop()

        end_time = watch.time()

    # print("step: "+str(step))
    # print("color: "+str(color))
    # print("speed: "+str(DRIVE_SPEED))
    # print("distance: "+str(distance))
    # print("stopOrNot: "+str(stopping))
    # print("stop time: "+str(stop_time))
    # print("time: "+str(time))
    # print("time difference: "+str(end_time-time))
    # print("deviation: "+str(deviation))
    # print("integral: "+str(integral))
    # print("derivative: "+str(derivative))

    # Store data log.
    data.log(time, step, color, DRIVE_SPEED, distance, stopping, deviation,
             integral, derivative, front_id, front_time, front_lane,
             front_speed, front_distance)

    # Keep time of each loop constant 200ms.
    wait_time = 0
    if (end_time - time) < 200:
        wait_time = 200 - (end_time - time)
    wait(wait_time)
Ejemplo n.º 9
0
# Create a data log file in the project folder on the EV3 Brick.
# * By default, the file name contains the current date and time, for example:
#   log_2020_02_13_10_07_44_431260.csv
# * You can optionally specify the titles of your data columns. For example,
#   if you want to record the motor angles at a given time, you could do:
data = DataLog('time', 'angle')

# Initialize a motor and make it move
wheel = Motor(Port.B)
wheel.run(500)

# Start a stopwatch to measure elapsed time
watch = StopWatch()

# Log the time and the motor angle 10 times
for i in range(10):
    # Read angle and time
    angle = wheel.angle()
    time = watch.time()

    # Each time you use the log() method, a new line with data is added to
    # the file. You can add as many values as you like.
    # In this example, we save the current time and motor angle:
    data.log(time, angle)

    # Wait some time so the motor can move a bit
    wait(100)

# You can now upload your file to your computer
Ejemplo n.º 10
0
     for i in range(Iteration):
          speed = dist.distance()
          car.drive(speed,0)
     duration = timer.time()
     car.stop()
     return duration
     
def testMotor():
     timer.reset()
     for i in range(Iteration):
          speed = dist.distance()
          left.run(speed)
     duration = timer.time()
     left.stop()
     return duration

for i in range(10):
     base = testBase()
     motor = testMotor()
     print('%d  %d' % (base,motor))
     data.log(i,base,motor)

ev3.light.off()







Ejemplo n.º 11
0
        elif step == 3:
            turn_rate = -15

        # Set the drive base speed and turn rate.
        robot.drive(DRIVE_SPEED, turn_rate)

        last_deviation = deviation

    else: 
        robot.stop()

    end_time = watch.time()
    
    # print("step: "+str(step))
    # print("color: "+str(color))
    # print("speed: "+str(DRIVE_SPEED))
    # print("distance: "+str(distance))
    # print("stopOrNot: "+str(stopping))
    # print("stop time: "+str(stop_time))
    # print("time: "+str(time))
    # print("time difference: "+str(end_time-time))
    
    # Store data log.
    data.log(time, step, color, DRIVE_SPEED, distance, stopping, deviation, integral, derivative)

    # Keep time of each loop constant 100ms.
    wait_time = 0
    if (end_time-time) < 100:
        wait_time = 100-(end_time-time)
    wait(wait_time)