Ejemplo n.º 1
0
    def driveStraightGyro(self, distance, speed=None, brake=False):
        ''' Drive for given distance using gyro sensor.

    Robot goes in straight line in the direction it was facing when this 
    function is called. Uses proportional control.

    Args:
      distance: distance to drive in mm
      speed: speed in mm/s
      brake (bool): brake at end of movement
    '''
        # starting angle for this move
        startAngle = self.gyro.angle

        if speed == None or speed == 0:
            speed = self.defaultSpeed

        if distance < 0:
            distance = abs(distance)
            speed = -speed

        assert (
            distance > 0 or speed > 0
        ), "At least one of distance or speed must be posiitve in driveStraightGyro."

        # calculate motor speeds in percentage units
        rps = speed / (pi * self.wheel_diameter)
        left_speed = rps
        right_speed = rps

        # set encoder counts to zero
        self.rightDriveMotor.position = 0

        # proportional gain (rps/deg)
        kp = 0.01

        # number of wheel rotations in this move
        targetRotations = distance / (pi * self.wheel_diameter)

        while abs(self.rightDriveMotor.rotations) < targetRotations:
            error = self.gyro.angle - startAngle
            left_speed = SpeedRPS(rps - kp * error)
            right_speed = SpeedRPS(rps + kp * error)
            self.on(left_speed, right_speed)
            sleep(0.01)

        # stop both motors
        self.stop(brake=brake)
Ejemplo n.º 2
0
    def test_units(self):
        clean_arena()
        populate_arena([('large_motor', 0, 'outA'),
                        ('large_motor', 1, 'outB')])

        m = Motor()

        self.assertEqual(
            SpeedPercent(35).to_native_units(m), 35 / 100 * m.max_speed)
        self.assertEqual(SpeedDPS(300).to_native_units(m), 300)
        self.assertEqual(SpeedNativeUnits(300).to_native_units(m), 300)
        self.assertEqual(SpeedDPM(30000).to_native_units(m), (30000 / 60))
        self.assertEqual(SpeedRPS(2).to_native_units(m), 360 * 2)
        self.assertEqual(SpeedRPM(100).to_native_units(m), (360 * 100 / 60))

        self.assertEqual(DistanceMillimeters(42).mm, 42)
        self.assertEqual(DistanceCentimeters(42).mm, 420)
        self.assertEqual(DistanceDecimeters(42).mm, 4200)
        self.assertEqual(DistanceMeters(42).mm, 42000)

        self.assertAlmostEqual(DistanceInches(42).mm, 1066.8)
        self.assertAlmostEqual(DistanceFeet(42).mm, 12801.6)
        self.assertAlmostEqual(DistanceYards(42).mm, 38404.8)

        self.assertEqual(DistanceStuds(42).mm, 336)
Ejemplo n.º 3
0
 def speed_mm_s(self, speed):
     ''' Convert speed in mm/s into percent of max speed.
 
 Args:
   speed: speed in mm/s.
   
 Returns:
   speed in SpeedRPS form. '''
     rps = speed / (pi * self.wheel_diameter)
     speed = SpeedRPS(rps)
     return speed
Ejemplo n.º 4
0
    def test_units(self):
        clean_arena()
        populate_arena([('large_motor', 0, 'outA'), ('large_motor', 1, 'outB')])

        m = Motor()

        self.assertEqual(SpeedPercent(35).get_speed_pct(m), 35)
        self.assertEqual(SpeedDPS(300).get_speed_pct(m), 300 / 1050 * 100)
        self.assertEqual(SpeedNativeUnits(300).get_speed_pct(m), 300 / 1050 * 100)
        self.assertEqual(SpeedDPM(30000).get_speed_pct(m), (30000 / 60) / 1050 * 100)
        self.assertEqual(SpeedRPS(2).get_speed_pct(m), 360 * 2 / 1050 * 100)
        self.assertEqual(SpeedRPM(100).get_speed_pct(m), (360 * 100 / 60) / 1050 * 100)
Ejemplo n.º 5
0
    def followLineDistNative(self,
                             distance,
                             sensor,
                             sideToFollow,
                             stopAtEnd,
                             speed,
                             gain_mult=1,
                             brake=False):
        ''' Drive forward following line. 

    Args:
      distance: distance to follow line in mm
      sensor: colorsensor to use for line following
      sideToFollow: which side of line to follow:  LineEdge.LEFT or LineEdge.RIGHT
      stopAtEnd (bool): stop at end of movement
      speed: speed in mm/s
      gain_mult: multiplier for P and D gains
      brake (bool): brake at end of move
    '''
        # calculate the target reflectance, halfway between black ad white
        target = (self.blackThreshold + self.whiteThreshold) / 2

        # set parameters for which side to follow
        if sideToFollow == LineEdge.LEFT:
            followLeft = True
        else:
            followLeft = False

        # calculate the required motor rotation rate in RPS
        rotationRate = speed / (pi * self.wheel_diameter)

        # time to follow line
        timeToFollow_ms = abs(distance / speed * 1000)

        # set which color sensor to use
        self.cs = sensor

        # call line follower built-in function
        self.follow_line(kp=5 * gain_mult,
                         ki=0 * gain_mult,
                         kd=1 * gain_mult,
                         speed=SpeedRPS(rotationRate),
                         target_light_intensity=target,
                         follow_left_edge=followLeft,
                         white=self.whiteThreshold,
                         off_line_count_max=100,
                         sleep_time=0.01,
                         follow_for=follow_for_ms,
                         ms=timeToFollow_ms)

        self.stop(brake=brake)
Ejemplo n.º 6
0
    def test_units(self):
        clean_arena()
        populate_arena([('large_motor', 0, 'outA'),
                        ('large_motor', 1, 'outB')])

        m = Motor()

        self.assertEqual(
            SpeedPercent(35).to_native_units(m), 35 / 100 * m.max_speed)
        self.assertEqual(SpeedDPS(300).to_native_units(m), 300)
        self.assertEqual(SpeedNativeUnits(300).to_native_units(m), 300)
        self.assertEqual(SpeedDPM(30000).to_native_units(m), (30000 / 60))
        self.assertEqual(SpeedRPS(2).to_native_units(m), 360 * 2)
        self.assertEqual(SpeedRPM(100).to_native_units(m), (360 * 100 / 60))
Ejemplo n.º 7
0
def large_motor():
    lm = LargeMotor()
    '''
    This will run the large motor at 50% of its
    rated maximum speed of 1050 deg/s.
    50% x 1050 = 525 deg/s
    '''
    lm.on_for_seconds(50, seconds=3)
    sleep(1)
    '''
    speed and seconds are both POSITIONAL
    arguments which means
    you don't have to include the parameter names as
    long as you put the arguments in this order 
    (speed then seconds) so this is the same as
    the previous command:
    '''
    lm.on_for_seconds(50, 3)
    sleep(1)
    '''
    This will run at 500 degrees per second (DPS).
    You should be able to hear that the motor runs a
    little slower than before.
    '''
    lm.on_for_seconds(SpeedDPS(500), seconds=3)
    sleep(1)

    # 36000 degrees per minute (DPM) (rarely useful!)
    lm.on_for_seconds(SpeedDPM(36000), seconds=3)
    sleep(1)

    # 2 rotations per second (RPS)
    lm.on_for_seconds(SpeedRPS(2), seconds=3)
    sleep(1)

    # 100 rotations per minute(RPM)
    lm.on_for_seconds(SpeedRPM(100), seconds=3)
Ejemplo n.º 8
0
 def move_for_seconds(self, speed, second):  # 각속도
     self.motor.on_for_seconds(SpeedRPS(speed), SpeedRPS(speed), second)
Ejemplo n.º 9
0
#!/usr/bin/env python3
from ev3dev2.motor import LargeMotor, OUTPUT_D, OUTPUT_A
from ev3dev2.motor import MediumMotor, OUTPUT_B
from ev3dev2.motor import MediumMotor, MoveSteering, OUTPUT_A, OUTPUT_D
from ev3dev2.motor import SpeedDPS, SpeedRPM, SpeedRPS, SpeedDPM
from time import sleep


lmA = LargeMotor(OUTPUT_A)
lmD = LargeMotor(OUTPUT_D)
SmB = MediumMotor(OUTPUT_B)
steer_pair = MoveSteering(OUTPUT_D, OUTPUT_A)
steer_pair.on_for_rotations(0,SpeedRPS(2),3)
SmB.on_for_seconds(SpeedRPS(1),1)
steer_pair.on_for_rotations(0,SpeedRPS(2),3)
SmB.on_for_seconds(SpeedRPS(-1),1)
Ejemplo n.º 10
0
#!/usr/bin/env python3
from ev3dev2.motor import MediumMotor, OUTPUT_A
from ev3dev2.motor import SpeedDPS, SpeedRPS, SpeedRPM
from time import sleep
medium_motor = MediumMotor(OUTPUT_A)
# We'll make the motor turn 180 degrees
# at speed 50 (780 dps for the medium motor)
medium_motor.on_for_degrees(speed=50, degrees=180)
# then wait one second
sleep(1)
# then – 180 degrees at 500 dps
medium_motor.on_for_degrees(speed=SpeedDPS(500), degrees=-180)
sleep(1)
# then 0.5 rotations at speed 50 (780 dps)
medium_motor.on_for_rotations(speed=50, rotations=0.5)
sleep(1)
# then – 0.5  rotations at 1 rotation per second (360 dps)
medium_motor.on_for_rotations(speed=SpeedRPS(1), rotations=-0.5)
sleep(1)
medium_motor.on_for_seconds(speed=SpeedRPM(60), seconds=5)
sleep(1)
medium_motor.on(speed=60)
sleep(2)
medium_motor.off()
Ejemplo n.º 11
0
 def move(self,dt):
     
     (vl, vr) = self.get_wheel_speeds()
     
     # actual robot move
     self.on_for_seconds(SpeedRPS(vl/2/pi), SpeedRPS(vr/2/pi), dt, False, False)
Ejemplo n.º 12
0
'''
lm.on_for_seconds(speed=50, seconds=3)
sleep(1)
'''
speed and seconds are both POSITIONAL
arguments which means
you don't have to include the parameter names as
long as you put the arguments in this order 
(speed then seconds) so this is the same as
the previous command:
'''
lm.on_for_seconds(50, 3)
sleep(1)
'''
This will run at 500 degrees per second (DPS).
You should be able to hear that the motor runs a
little slower than before.
'''
lm.on_for_seconds(speed=SpeedDPS(500), seconds=3)
sleep(1)

# 36000 degrees per minute (DPM) (rarely useful!)
lm.on_for_seconds(speed=SpeedDPM(36000), seconds=3)
sleep(1)

# 2 rotations per second (RPS)
lm.on_for_seconds(speed=SpeedRPS(2), seconds=3)
sleep(1)

# 100 rotations per minute(RPM)
lm.on_for_seconds(speed=SpeedRPM(100), seconds=3)
Ejemplo n.º 13
0
 def _SpeedRadPS(self, value):
     return SpeedRPS(value / (2 * pi))
Ejemplo n.º 14
0
#region 1.시간으로 제어
lm.on_for_seconds(speed = 50, seconds=3)    # maximum speed(1050 deg/s)의 50% 속도로 3초 동안 회전
sleep(1)
lm.on_for_seconds(50, 3)   # 매개 변수 이름을 명시하지 않아도 된다. 
sleep(1)
#endregion

#region 2.시간당 각도로 제어
lm.on_for_seconds(speed=SpeedDPS(300), seconds=3)   # DPS(degrees per second) 초당 300도를 도는 속도로 3초 동안 회전
sleep(1)
lm.on_for_seconds(speed=SpeedDPM(36000), seconds=3) # DPM(degrees per minute) 분당 36000도
sleep(1)
#endregion

#region 3.시간당 회전수로 제어 
lm.on_for_seconds(speed=SpeedRPS(1), seconds=2)     # RPS(rotations per second) 초당 1바퀴를 도는 속도로 seconds 초 동안 회전
sleep(1)    
lm.on_for_seconds(speed=SpeedRPM(100), seconds=3)   # RPM(rotations per minute) 분당 100바퀴를 도는 속도 seconds 초 동안 회전
sleep(1)
#endregion
#endregion

#region 바퀴 회전으로 제어 => on_for_rotations(speed, rotations, brake=True, block=True)
#region 1.모터의 정격 스피드의 비율로 동작 
lm.on_for_rotations(speed=50, rotations=3)  # 정격 최대 속도(1050 deg/s)의 50%의 속도로 3바퀴 회전 
sleep(1)
lm.on_for_rotations(50, 3)  # 매개 변수 이름을 명시하지 않아도 된다. 
sleep(1)
#endregion

#region 2.시간당 지정된 스피드로 rotations 바퀴만큼 회전 
lMotorLeft.on_for_seconds(
    speed=50, seconds=2
)  # "speed = 50" - 50% of the max speed of 1050 deg/s for L motor and 1560 for M motor
# or lMotorLeft.on_for_seconds(50, 2)
sleep(0.5)

# Run motors at some speed with use of SpeedDPS/DPM/RPS/RPM funcs.
#  DPS - degrees per second
#  DPM - degress per minute
#  RPS - rotations per second
#  RPM - rotations per minute
lMotorLeft.on_for_seconds(speed=SpeedDPS(500), seconds=2)
sleep(0.5)
lMotorLeft.on_for_seconds(speed=SpeedDPM(36000), seconds=2)
sleep(0.5)
lMotorLeft.on_for_seconds(speed=SpeedRPS(2), seconds=2)
sleep(0.5)
lMotorLeft.on_for_seconds(speed=SpeedRPM(100), seconds=2)
sleep(0.5)

# Run motors for certain speed with implicit 'brake' and 'block' paramters (True by default)
# "brake = False" - don't stop the motor after it finishes the command (use friction to stop)
# "block = False" - don't block the run of script (continue to run script)
lMotorLeft.on_for_seconds(speed=SpeedRPS(2),
                          seconds=3,
                          brake=False,
                          block=False)
lMotorRight.on_for_seconds(speed=SpeedRPS(2),
                           seconds=3,
                           brake=False,
                           block=True)