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()
def record(self, filename, target, state): """ Log data to control error, if the filename wasnt used before a new file will be created Args: filename (str): Filename of the log file target (int, float, str, bool): Anything that is considered the target value of a system state (int, float, str, bool): Anything that is considered the current state value of a system """ if filename not in self.log_files_names: self.log_files_names.append(filename) self.log_files.append( DataLog("Target", "State", "Error", name=filename)) self.log_files_names.index(filename).log(target, state, target - state)
def __init__(self, logger_desc, nb_calibrage_classe): self.maxSpeed = 200 #mm self.maxdist = 2 #meters self.speed = 360 self.turnDiameter = 186 #mm self.wheelDiameter = 35 #mm self.motor_left = Motor(Port.B) self.motor_right = Motor(Port.C) self.dist = 0 self.color = [0, 0, 0] self.logger = DataLog("Dist", "Vitesse", name="log_test_" + logger_desc, timestamp=True, extension='txt', append=False)
from pybricks.parameters import Port, Stop, Direction, Button, Color from pybricks.tools import wait, StopWatch, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile, Font # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. # 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', 'sensor_value', name='log', timestamp=False, extension='csv', append=False) colorValues = DataLog('color', 'value', name='color', timestamp=False, extension='csv', append=False) message = Font() # Start a stopwatch to measure elapsed time watch = StopWatch() # Create your objects here. ev3 = EV3Brick()
# Display font, style and size big = Font(family='Helvetica', size=24, bold=True) ev3.screen.set_font(big) if args.predict is None: # Initialize motors platform_motor = Motor(args.motor) # Initialise sensors platform_color_sensor = ColorSensor(args.color) trigger_button = TouchSensor(args.touch) # Initialise DataLog - append target label to filename if supplied filename = args.filename + args.label data = DataLog('angle', 'reflectivity', name=filename) # Initiate platform rotation and measurement on pressing the touch sensor print(trigger_button.pressed()) while True: if trigger_button.pressed(): # Play a sound. ev3.speaker.beep() # Calculate rotation angle total_rotation = int(args.rotations) * 360 rotation_speed = int(args.speed) log_interval = int(args.interval) # Run the platform motor
#!/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)
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()
learning = 0.3 discount = .9 exploration_rate = 1 min_exploration = .0001 exploration_decay = .4 # policy shaping parameters: confidence = .99 # confidence that feedback is optimal likelihood = .5 # likelihood feedback is provided const = 0.3 # constant used in probability of action equation rewards = [] num_moves = [] data = DataLog('Episode #', 'Reward', "Time", "Num_feedbacks", "agent_pos", "is_done", \ "username", name="ep_log", timestamp=True, extension='csv', append=False) q_table_data = DataLog(name="q_table", extension='csv', append=False) child_fdbktable_data = DataLog(name="child_table", extension='csv', append=False) parent_fdbktable_data = DataLog(name="parent_table", extension='csv', append=False) episode_stopwatch = StopWatch() episode_stopwatch.reset() user_color = None user = None fam.say("Please enter user")
learning = 0.3 discount = .9 exploration_rate = 1 min_exploration = .0001 exploration_decay = .8 # policy shaping parameters: confidence = .8 # confidence that feedback is optimal likelihood = .5 # likelihood feedback is provided const = 0.3 # constant used in probability of action equation rewards = [] num_moves = [] data = DataLog('Episode #', 'Reward', "Time", "Num_feedbacks", "agent_pos", "is_done", \ "username", "qtable", name="ep_log", timestamp=True, extension='csv', append=False) episode_stopwatch = StopWatch() episode_stopwatch.reset() user_color = None user = None fam.say("Please enter user") user_color = fam.get_color(3000) if user_color is Color.RED: fam.say("Hello child") user = child elif user_color is Color.GREEN: fam.say("Hello parent") user = parent elif user_color is Color.BLUE or user_color is None:
# Lane change state # step 0: drive on the outer lane # step 1: change lane (from outer to inner) # step 2: drive on the inner lane # step 3: change lane (from inner to outer) step = 0 # Time that state changed previously previousStateChangedTime = 0 # Time that robot has stopped stop_time = 0 # The flag to control robot stop or not stopping = False data = DataLog('time', 'step', 'color', 'speed', 'distance', 'stop', 'deviation', 'integral', 'derivative', 'front_id', 'front_time', 'front_step', 'front_speed', 'front_distance') # Initialize Bluetooth client and mailboxes for message passing. client = BluetoothMailboxClient() mbox_id = NumericMailbox('id2', client) mbox_time = NumericMailbox('time2', client) mbox_lane = NumericMailbox('lane2', client) mbox_speed = NumericMailbox('speed2', client) mbox_distance = NumericMailbox('distance2', client) # Server robot name SERVER = 'ev3-2' print('establishing connection...') client.connect(SERVER) print('ev3-2 connected!')
#!/usr/bin/env pybricks-micropython from pybricks.ev3devices import Motor from pybricks.parameters import Port from pybricks.tools import DataLog, StopWatch, wait # 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
from pybricks.ev3devices import (Motor, ColorSensor, UltrasonicSensor, GyroSensor) from pybricks.robotics import DriveBase # Initialize the EV3 ev3 = EV3Brick() ev3.light.on(Color.RED) left = Motor(Port.A) right = Motor(Port.D) dist = UltrasonicSensor(Port.S4) wheel_dia = 56 wheel_spacing = 114 car = DriveBase(left,right,wheel_dia,wheel_spacing) timer = StopWatch() data = DataLog('output.txt', header = 'iteration,Base time, motor time') Iteration = 1000 def testBase(): timer.reset() 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()
from pybricks.parameters import Port, Button, Color, ImageFile, SoundFile, Stop from pybricks.robotics import DriveBase from ucollections import namedtuple from pybricks.tools import wait, StopWatch, DataLog from threading import Thread import sys # Logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)5s: %(message)s') log = logging.getLogger(__name__) log.info("Starting Program") data = DataLog('time', 'angle', name='my_file', timestamp=False, extension='txt') start_gyro_value = 0 #region robot functions def turn_robot_left_in_place(angle): turn_robot_in_place("LEFT", angle) def turn_robot_right_in_place(angle): turn_robot_in_place("RIGHT", angle) def turn_robot_in_place(direction, angle):
from pybricks.parameters import Port, Stop, Direction, Button, Color from pybricks.tools import wait, StopWatch, DataLog from pybricks.robotics import DriveBase from pybricks.media.ev3dev import SoundFile, ImageFile, Font # This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. # 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', 'sensor_value', name='log', timestamp=False, extension='csv', append=False) # Initialize the EV3 brick. ev3 = EV3Brick() # Initialize the gyro sensor. It is used to provide feedback for balancing the # robot. gyro_sensor = GyroSensor(Port.S2) # The following (UPPERCASE names) are constants that control how the program # behaves. GYRO_CALIBRATION_LOOP_COUNT = 200 GYRO_OFFSET_FACTOR = 0.0005