def __initSensors(self): '''Sub-method for initializing Sensors.''' self.logger.debug(self, "Starting sensor initialisation...") try: self.__gyro = GyroSensor(self.__conf2port[self.__config['gyroSensorPort']], Direction.CLOCKWISE if not self.__config['gyroInverted'] else Direction.COUNTERCLOCKWISE) if self.__config['gyroSensorPort'] != 0 else 0 self.logger.debug(self, 'Gyrosensor initialized sucessfully on port %s' % self.__config['gyroSensorPort']) except Exception as exception: self.__gyro = 0 self.logger.error(self, "Failed to initialize the Gyro-Sensor - Are u sure it's connected to Port %s?" % exception, exception) try: self.__rLight = ColorSensor( self.__conf2port[self.__config['rightLightSensorPort']]) if self.__config['rightLightSensorPort'] != 0 else 0 self.logger.debug(self, 'Colorsensor initialized sucessfully on port %s' % self.__config['rightLightSensorPort']) except Exception as exception: self.logger.error(self, "Failed to initialize the right Color-Sensor - Are u sure it's connected to Port %s?" % exception, exception) try: self.__lLight = ColorSensor( self.__conf2port[self.__config['leftLightSensorPort']]) if self.__config['leftLightSensorPort'] != 0 else 0 self.logger.debug(self, 'Colorsensor initialized sucessfully on port %s' % self.__config['leftLightSensorPort']) except Exception as exception: self.logger.error(self, "Failed to initialize the left Color-Sensor - Are u sure it's connected to Port %s?" % exception, exception) try: self.__touch = TouchSensor(self.__conf2port[self.__config['backTouchSensor']]) if self.__config['backTouchSensor'] != 0 else 0 self.logger.debug(self, 'Touchsensor initialized sucessfully on port %s' % self.__config['backTouchSensor']) except Exception as exception: self.logger.error(self, "Failed to initialize the Touch-Sensor - Are u sure it's connected to Port %s?" % exception, exception) self.logger.debug(self, "Sensor initialisation done")
def __init__(self): self.brick = EV3Brick() self.frontMotor = Motor(Port.D) self.rearMotor = Motor(Port.A) self.leftMotor = Motor(Port.C) self.rightMotor = Motor(Port.B) if path.exists('sensorpoints.py'): import sensorpoints self.leftSensor = LightSensor(Port.S3, sensorpoints.leftLow, sensorpoints.leftHigh) self.rightSensor = LightSensor(Port.S2, sensorpoints.rightLow, sensorpoints.rightHigh) else: self.leftSensor = LightSensor(Port.S3, 10, 105) self.rightSensor = LightSensor(Port.S2, 20, 160) self.gyroSensor = GyroSensor(Port.S1) wait(100) self.gyroSensor.speed() self.gyroSensor.angle() wait(500) self.gyroSensor.reset_angle(0.0) wait(200)
def __init__(self, robot): self.robot = robot try: self.gyro = GyroSensor(Port.S2, Direction.CLOCKWISE) except: self.gyro = None print("No Gyro Sensor plugged into Port 2. Cannot run Greyden's Skills. :-(") print()
def __init__(self, left_motor_port, right_motor_port, med_motor_port, gyro_port, wheel_diameter, wheel_base): self.left_motor = Motor(left_motor_port) self.right_motor = Motor(right_motor_port) # self.med_motor = Motor(med_motor_port) self.robot = DriveBase(self.left_motor, self.right_motor, wheel_diameter, wheel_base) self.gyro = GyroSensor(gyro_port)
def fall_check(self): fall_gyro = GyroSensor(Port.S4, Direction.CLOCKWISE) while True: real_angle = fall_gyro.angle() if real_angle > 45 or real_angle < -45: self.brick.speaker.beep() else: wait(30) print(real_angle)
def run(): motor_b = Motor(Port.B) motor_c = Motor(Port.C) Gyro = GyroSensor(Port.S1) while Gyro.angle() <= 65: motor_c.run(900) motor_b.run(-900) motor_c.stop(Stop.BRAKE) motor_b.stop(Stop.BRAKE)
def main(): # sound test log.info('test beep') brick.sound.beep() # color sensor test sensor_color = ColorSensor(ROBOT['sensor_color_port']) log.debug('color sensor: color=%s' % sensor_color.color()) # gyro sensor test sensor_gyro = GyroSensor(ROBOT['sensor_gyro_port']) log.debug('gyro sensor: speed=%d, angle=%d' % (sensor_gyro.speed(), sensor_gyro.angle()))
def run(): motor_b = Motor(Port.B) motor_c = Motor(Port.C) Gyro = GyroSensor(Port.S1) Gyro.reset_angle(0) motor_b.run_angle(500, 720, Stop.BRAKE, False) motor_c.run_angle(500, 720, Stop.BRAKE, True) wait(10) while Gyro.angle() <= 180: motor_c.run(100) motor_b.run(-100) wait(10) motor_b.run_angle(500, 720, Stop.BRAKE, False) motor_c.run_angle(500, 720, Stop.BRAKE, True)
def __init__(self): # Initialize the EV3 Brick. self.ev3 = EV3Brick() self.ev3.speaker.set_speech_options(voice='f3') # large motors self.left_motor = Motor(Port.A, positive_direction=Direction.COUNTERCLOCKWISE, gears=None) self.right_motor = Motor(Port.B, positive_direction=Direction.COUNTERCLOCKWISE, gears=None) # small (medium) motors self.small_motor_left = Motor(Port.C, positive_direction=Direction.CLOCKWISE, gears=None) self.small_motor_right = Motor(Port.D, positive_direction=Direction.CLOCKWISE, gears=None) # gyro sensor self.gyro_sensor = GyroSensor(Port.S3, Direction.CLOCKWISE) # Initialize the drive base. self.drive_base = DriveBase(self.left_motor, self.right_motor, wheel_diameter=93.5, axle_track=120) return
def test_gyro_drift(port=Port.S4): gyro = GyroSensor(port) printMsg("Checking for drift.") numTests = 5 drifting = False for i in range(numTests): angle = gyro.angle() printMsg("Test %i/%i = %d" % (i + 1, numTests, angle)) if angle != 0: drifting = True break # no need to keep testing wait(1000) return drifting
class Robot(): #robot hardware brick = EV3Brick() wheel_left = Motor(Port.A) wheel_right = Motor(Port.B) arm_left = Motor(Port.D) arm_right = Motor(Port.C) gyro = GyroSensor(Port.S1, Direction.COUNTERCLOCKWISE) color_left = ColorSensor(Port.S2) color_right = ColorSensor(Port.S3) chassis = DriveBase(wheel_left, wheel_right, wheel_diameter=49.5, axle_track=150) @classmethod def reset_settings(cls): cls.chassis.stop() cls.chassis.settings(115, 460, 88, 352) @classmethod def settings(cls, straight_speed=None, straight_acceleration=None, turn_rate=None, turn_acceleration=None): cls.chassis.stop() #Settings can only be changed while stopped current_settings = cls.chassis.settings() cls.chassis.settings( straight_speed if straight_speed is not None else current_settings[0], straight_acceleration if straight_acceleration is not None else current_settings[1], turn_rate if turn_rate is not None else current_settings[2], turn_acceleration if turn_acceleration is not None else current_settings[3]) @classmethod def brake(cls): cls.chassis.stop() cls.wheel_left.brake() cls.wheel_right.brake() @classmethod def reset_gyro(cls): cls.gyro.reset_angle(0) cls.gyro.angle() cls.gyro.speed() cls.gyro.angle() wait(10)
def init_brick(): # Create your objects here. ev3 = EV3Brick() # Initilize our motors left_motor = Motor(Port.A) right_motor = Motor(Port.D) front_motor_1 = Motor(Port.C) front_motor_2 = Motor(Port.B) left_motor.reset_angle(0) right_motor.reset_angle(0) front_motor_1.reset_angle(0) front_motor_2.reset_angle(0) # Initialize the color sensor. left_sensor = ColorSensor(Port.S4) right_sensor = ColorSensor(Port.S1) # Speeds right_sensor = ColorSensor(Port.S1) left_sensor = ColorSensor(Port.S4) ARM_MOTOR_SPEED = 400 WHEEL_DIAMETER = 92 AXLE_TRACK = 130 DRIVE_SPEED_FAST = 350 DRIVE_SPEED_NORMAL = 200 DRIVE_SPEED_SLOW = 100 DRIVE_EXTRA_SLOW = 30 CIRCUMFERENCE = 3.14 * WHEEL_DIAMETER # Diameter = 100mm, Circumference = 314.10mm = 1 rotation # Initialize the Gyro sensor gyro = GyroSensor(Port.S2) gyro.reset_angle(0) # All parameters are in millimeters robot = DriveBase(left_motor, right_motor, wheel_diameter=config.WHEEL_DIAMETER, axle_track=config.AXLE_TRACK) # Set the straight speed and turn rate robot.settings(straight_speed=config.DRIVE_SPEED_NORMAL, turn_rate=config.TURN_RATE)
def __init__(self): """Class that represents the robot """ try: self.state = "Port 1: Right Color" self.right_color = ColorSensor(Port.S1) self.state = "Port 2: Center Color" self.center_color = ColorSensor(Port.S2) self.state = "Port 3: Left Color" self.left_color = ColorSensor(Port.S3) self.state = "Port 4: Gyro" self.gyro = GyroSensor(Port.S4, Direction.COUNTERCLOCKWISE) self.state = "Port A: Left Motor" self.left_motor = Motor(Port.A) self.state = "Port B: Right Motor" self.right_motor = Motor(Port.B) self.state = "Port C: Linear Gear" self.linear_attachment_motor = Motor(Port.C) self.state = "Port D: Block Dropper" self.dropper_attachment_motor = Motor(Port.D) self.wheel_diameter = 55 self.axle_track = 123 self.drive_base = DriveBase(self.left_motor, self.right_motor, self.wheel_diameter, self.axle_track) self.state = "OK" self.clock = StopWatch() self.dance_clock = 0 self.sign = 1 except: brick.screen.clear() big_font = Font(size=18) brick.screen.set_font(big_font) brick.screen.draw_text(0, 20, "Error!") brick.screen.draw_text(0, 40, self.state)
def __init__(self, port, left_motor, right_motor, wheel_radius, axis_radius, debug=False): self.port = port self.left_motor = left_motor self.right_motor = right_motor self.wheel_radius = wheel_radius self.axis_radius = axis_radius self.debug = debug # either connect to real hardware, or our dummy sim class self.gyro = GyroSensor(self.port) self.degrees = int(0)
def __init__(self, left_motor=Motor(Port.A), right_motor=Motor(Port.B), attachment_motor=(Motor(Port.C)), wheel_diameter=56, axle_track=145): #wheel_diameter=43, axle_track=110): self.left_motor = left_motor self.right_motor = right_motor self.attachment_motor = attachment_motor self.color_sensor = ColorSensor(Port.S2) self.gyro_sensor = GyroSensor(Port.S1) super().__init__(left_motor, right_motor, wheel_diameter, axle_track)
def rotate_180_by_gyro(self): gyro_sensor = GyroSensor(Port.S4) gyro_sensor.reset_angle(0) self.left_motor.run(self.TRUCK_SPEED) self.right_motor.run(-self.TRUCK_SPEED) while (gyro_sensor.angle() < 135): print("Angle: ", gyro_sensor.angle()) wait(1) self.stop() wait(4000)
def get_robot(): # Initialize two motors and a drive base wheel_diameter_mm=54 axle_track_mm=152 crane_motor=Motor(Port.A) left_motor=Motor(Port.C) right_motor=Motor(Port.B) robot = DriveBase(left_motor, right_motor, wheel_diameter_mm, axle_track_mm) #robot.drive(2000, 0) gyro=GyroSensor(Port.S1) calibrate_gyro(gyro) color_sensor_left = ColorSensor(Port.S3) color_sensor_right = None #ColorSensor(Port.S2) # Initialize the Ultrasonic Sensor. # obstacle_sensor = UltrasonicSensor(Port.S4) return robot, crane_motor, left_motor, right_motor,color_sensor_left, color_sensor_right, gyro
def turn(angle, max_speed, gyro_port, drive_base): gyro = GyroSensor(gyro_port, Direction.COUNTERCLOCKWISE) ev3 = EV3Brick() ev3.screen.clear() initial_error = gyro.angle() - angle while abs(gyro.angle() - angle) > 2: error = gyro.angle() - angle speed = max_speed * (error / initial_error) ev3.screen.print(gyro.angle(), error, speed) drive_base.drive(0, speed) wait(10)
class Greyden_Skills: def __init__(self, robot): self.robot = robot try: self.gyro = GyroSensor(Port.S2, Direction.CLOCKWISE) except: self.gyro = None print("No Gyro Sensor plugged into Port 2. Cannot run Greyden's Skills. :-(") print() def gyro_angle(self): if self.gyro is None: return float("nan") else: return self.gyro.angle() def tell_me_about_your_skills(self): print("SKILLS - I can read the Gyro Sensor!") print("SKILLS - I am able to show you the Gyro angle, see",self.gyro_angle()) print()
def gameLoop(blocks_to_deliver): global brick_state leftMotor = Motor(Port.B) rightMotor = Motor(Port.C) wheels = DriveBase(Motor(Port.B), Motor(Port.C), 56, 114) uSensor = UltrasonicSensor(Port.S4) cSensor = ColorSensor(Port.S3) gSensor = GyroSensor(Port.S2) ts = TouchSensor(Port.S1) while not ts.pressed(): # brick.sound.file(SoundFile.MOTOR_IDLE, 1000) if blocks_delivered >= blocks_to_deliver: brick_state = Status.WAIT if brick_state == Status.WAIT: return elif brick_state == Status.SEARCHING: print('STATE: SEARCHING') t = threading.Thread(target=searchLuggage, args=(wheels, cSensor, gSensor)) t.start() brick_state = Status.COMPUTING elif brick_state == Status.CARRYING_LUGGAGE: print('STATE: CARRYING_LUGGAGE') t = threading.Thread(target=deliverLuggage, args=(wheels, cSensor, )) t.start() brick_state = Status.COMPUTING elif brick_state == Status.PICKING_UP: print('STATE: PICKING_UP') t = threading.Thread(target=doLuggage, args=(wheels, cSensor, True, )) t.start() brick_state = Status.COMPUTING elif brick_state == Status.DEPOSITING: print('STATE: DEPOSITING') t = threading.Thread(target=doLuggage, args=(wheels, cSensor,False, )) t.start() brick_state = Status.COMPUTING
def __init__(self, speed_initializing, max_speed_execution, solution, black_ref=100, white_ref=0, robot_center_parameter=80, robot_rotation_parameter=0, orientation="N", motor_rigth=Motor(Port.B), motor_left=Motor(Port.C), sensor_rigth=ColorSensor(Port.S2), sensor_left=ColorSensor(Port.S1), sensor_stop=ColorSensor(Port.S3), sensor_gyro=GyroSensor(Port.S4), Kp=5, Ki=0, Kd=0): self.speed_initializing = speed_initializing self.max_speed_execution = max_speed_execution self.solution = solution self.black_ref = black_ref self.white_ref = white_ref self.robot_center_parameter = robot_center_parameter self.robot_rotation_parameter = robot_rotation_parameter self.orientation = orientation self.motor_rigth = motor_rigth self.motor_left = motor_left self.sensor_rigth = sensor_rigth self.sensor_left = sensor_left self.sensor_stop = sensor_stop self.sensor_gyro = sensor_gyro self.Kp = Kp self.Ki = Ki self.Kd = Kd self.watch = StopWatch()
#!/usr/bin/env pybricks-micropython from pybricks import ev3brick as brick from pybricks.ev3devices import Motor, ColorSensor, TouchSensor, UltrasonicSensor, GyroSensor from pybricks.parameters import Port, Direction, Color import time #モーター・センサー類の設定 Right = Motor(Port.B) Left = Motor(Port.C, Direction.COUNTERCLOCKWISE) #Mモーター使用のため正の速度方向を逆に設定 CS = ColorSensor(Port.S1) TS = TouchSensor(Port.S2) US = UltrasonicSensor(Port.S3) GS = GyroSensor(Port.S4) #各種関数の定義 class My(): #モーターの角度をAngle度にリセット def Reset(Angle): Right.reset_angle(Angle) Left.reset_angle(Angle) #モーターをそれぞれ速度Right_Speed、Left_Speedで回転 def Run(Right_Speed, Left_Speed): Right.run(Right_Speed) Left.run(Left_Speed) #変数Reflectionの値を境目にライントレース def LineTrace(High_Power, Low_Power): if CS.reflection() > Reflection:
#!/usr/bin/env pybricks-micropython from pybricks import ev3brick as brick from pybricks.ev3devices import Motor, GyroSensor from pybricks.parameters import Port, Stop, Direction, Button from pybricks.tools import print, wait, StopWatch from pybricks.robotics import DriveBase import time left = Motor(Port.B) right = Motor(Port.C) gyro = GyroSensor(Port.S1) gyro.reset_angle(0) data = open("m_angle.data", "w") def rot(spd=40, ang=0): while(True): if(gyro.angle() > ang): right.run(spd) left.run(spd * -1) if(gyro.angle() < ang): left.run(spd) right.run(spd * -1) if(gyro.angle() == ang): left.stop(Stop.HOLD) right.stop(Stop.HOLD) time.sleep(0.5) if(gyro.angle() == ang): data.write(str(gyro.angle()) + "\n") gyro.reset_angle(0) break
AXLE_TRACK_MM = 157 SOUND_VOLUME = 7 #output crane_motor = Motor(Port.D) # side_crane=Motor(Port.C) #drive motors left_motor = Motor(Port.B, Direction.COUNTERCLOCKWISE) # left_motor.duty(75) right_motor = Motor(Port.C, Direction.COUNTERCLOCKWISE) robot = DriveBase(left_motor, right_motor, WHEEL_DIAMETER_MM, AXLE_TRACK_MM) #Sensors gyro = GyroSensor(Port.S1) color_sensor_left = ColorSensor(Port.S2) # color_sensor_right = ColorSensor(Port.S3) # Initialize the Ultrasonic Sensor. # obstacle_sensor = UltrasonicSensor(Port.S4) def sound_happy(): brick.sound.beep(1100, 80, SOUND_VOLUME) brick.sound.beep(900, 80, SOUND_VOLUME) def sound_attention(): brick.sound.beep(700, 80, SOUND_VOLUME) brick.sound.beep(1200, 80, SOUND_VOLUME)
# This program requires LEGO EV3 MicroPython v2.0 or higher. # Click "Open user guide" on the EV3 extension tab for more information. # Variables and constants ev3 = EV3Brick() leftmotor = Motor(Port.C, Direction.CLOCKWISE) rightmotor = Motor(Port.B, Direction.CLOCKWISE) #left medium motor mediummotor = Motor(Port.D, Direction.COUNTERCLOCKWISE) #right medium motor mediummotor2 = Motor(Port.A, Direction.COUNTERCLOCKWISE) robot = DriveBase(leftmotor, rightmotor, 55.85, 108) robot.settings(300, 100, 200, 100) rightcolor = ColorSensor(Port.S1) leftcolor = ColorSensor(Port.S2) gyroSensor = GyroSensor(Port.S3) black = 3 white = 35 threshold = int((black + white) / 2) #19 print(threshold) #seeing white first and then line squaring def linesquare(): lightrange = range(threshold - 2, threshold + 2) #light range for final result #lightrange = range(threshold - 4, threshold + 4) #light rnage for adjustment #First, move the robot straight until one of the sensor sees range. while rightcolor.reflection() > 4 and leftcolor.reflection() > 4: robot.drive(70, 0)
# Initialize the motors. left_motor = Motor(Port.B, positive_direction=Direction.COUNTERCLOCKWISE) right_motor = Motor(Port.C, positive_direction=Direction.COUNTERCLOCKWISE) lift_motor = Motor(Port.A) forklift_motor = Motor(Port.D, positive_direction=Direction.COUNTERCLOCKWISE) robot = DriveBase(left_motor, right_motor, wheel_diameter=94.2, axle_track=94) robot.settings(straight_speed=200, straight_acceleration=50, turn_rate=150, turn_acceleration=200) ev3.screen.draw_text(50, 60, "Alright, let's do this.") ev3.speaker.beep() gyro_sensor = GyroSensor(Port.S2, Direction.COUNTERCLOCKWISE) def gyro_turn(angle, speed=150): gyro_sensor.reset_angle(0) if angle < 0: while gyro_sensor.angle() > angle: left_motor.run(speed=(-1 * speed)) right_motor.run(speed=speed) wait(10) elif angle > 0: while gyro_sensor.angle() < angle: left_motor.run(speed=speed) right_motor.run(speed=(-1 * speed)) wait(10) else:
playerHeight = 50 playerPositionX = int((screenWidth / 2) - (playerWidth / 2)) playerPositionY = screenHeight - playerHeight enemyWidth = 30 enemyHeight = 30 enemy1PositionX = randint(0, screenWidth - enemyWidth) enemy1PositionY = -30 enemy2PositionX = randint(0, screenWidth - enemyWidth) enemy2PositionY = -120 brick.sound.beep() gyro = GyroSensor(Port.S1) touchR = TouchSensor(Port.S4) time1 = StopWatch() #motorL = Motor(Port.B) #motorR = Motor(Port.C) gyro.reset_angle(0) def loadImage(): brick.display.image("Player.png", (200, 200), clear=False) brick.display.image("Enemy.png", (200, 200), clear=False) brick.display.image('SpaceWars.png') brick.sound.file(SoundFile.T_REX_ROAR) wait(2000)
#!/usr/bin/env pybricks-micropython from pybricks import ev3brick as brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import (Port, Stop, Direction, Button, Color, SoundFile, ImageFile, Align) from pybricks.tools import print, wait, StopWatch from pybricks.robotics import DriveBase obstacle_sensor = UltrasonicSensor(Port.S3) gyro_sensor_front = GyroSensor(Port.S4) right_motor = Motor(Port.C) left_motor = Motor(Port.B) wheel_diameter = 56 axle_track = 114 robot = DriveBase(left_motor, right_motor, wheel_diameter, axle_track) def start(): while True: robot.drive(200, 0) while obstacle_sensor.distance() < 250: robot.stop() obstacle_avoidance() def obstacle_avoidance(step = 2000, speed = 200, min_dist = 250, rotation_angle = 90): print('avoid') distance = 0 rotate(rotation_angle) distance += drive_past(step, speed, min_dist, rotation_angle)
This program requires LEGO® EV3 MicroPython v2.0. Download: https://education.lego.com/en-us/support/mindstorms-ev3/python-for-ev3 Building instructions can be found at: https://education.lego.com/en-us/support/mindstorms-ev3/building-instructions#robot """ from pybricks.ev3devices import Motor, ColorSensor, GyroSensor from pybricks.parameters import Port , Direction from pybricks.tools import wait from pybricks.robotics import DriveBase import Dimensions # Initialize the motors. left_motor = Motor(Port.B , positive_direction=Direction.COUNTERCLOCKWISE, gears=[40,8]) right_motor = Motor(Port.C , positive_direction=Direction.COUNTERCLOCKWISE, gears=[40,8]) gyro_sensor = GyroSensor(Port.S3) # Initialize the color sensor. #line_sensor = ColorSensor(Port.S3) left_motor.reset_angle(0) right_motor.reset_angle(0) gyro_sensor.reset_angle(0) fudge=1 speed=100 angle=0 robot = DriveBase(left_motor, right_motor, wheel_diameter=30, axle_track=135) initial_distance = 0 robot.reset() while ((robot.distance() - initial_distance) < 350) :
#!/usr/bin/env pybricks-micropython from pybricks import ev3brick as brick from pybricks.ev3devices import (Motor, TouchSensor, ColorSensor, InfraredSensor, UltrasonicSensor, GyroSensor) from pybricks.parameters import (Port, Stop, Direction, Button, Color, SoundFile, ImageFile, Align) from pybricks.tools import print, wait, StopWatch from pybricks.robotics import DriveBase import utime gg = GyroSensor(Port.S4) touch = TouchSensor(Port.S2) brick.display.text("milan") anglecounter = 0 motor = Motor(Port.D, Direction.COUNTERCLOCKWISE) frontmotor = Motor(Port.A, Direction.COUNTERCLOCKWISE) dist = UltrasonicSensor(Port.S1) def reverse(): motor = Motor(Port.D, Direction.CLOCKWISE) starttime = utime.ticks_ms() b = 0 while (not touch.pressed()) and (b < 3000): motor.run(360) currenttime = utime.ticks_ms() b = currenttime - starttime if motor.stalled(): break motor = Motor(Port.D, Direction.COUNTERCLOCKWISE)