.. _other_usage:

Beispiele
#########

Vorwärts und rückwärts fahren
*****************************

.. code-block:: python

    import time
    from compLib.Motor import *

    def forward():
        Motor.power(0, -30)
        Motor.power(3, 30)


    def backward():
        Motor.power(0, 30)
        Motor.power(3, -30)

    def main():
        print("hallo ich bin ein roboter beep buup")

        forward()
        time.sleep(1)
        backward()
        time.sleep(1)

    if __name__ == '__main__':
        main()


Eine Linie verfolgen
********************

.. code-block:: python

    import time
    from compLib.Motor import Motor
    from compLib.Encoder import Encoder
    from compLib.IRSensor import IRSensor

    COLOR_BREAK = 850
    DRIVE_SPEED = 35

    IRSensor.enable()

    def drive(left, right):
        right *= -1
        Motor.multiple_power((0, right), (3, left))
        print(f"{left} {right}")

    def follow():
        while True:
            sensors = IRSensor.read_all()
            
            if sensors[0] > COLOR_BREAK:
                # turn left
                drive(-DRIVE_SPEED, DRIVE_SPEED)
            elif sensors[4] > COLOR_BREAK:
                # turn right
                drive(DRIVE_SPEED, -DRIVE_SPEED)
            else:
                # straight
                drive(DRIVE_SPEED, DRIVE_SPEED)

            if sensors[0] > COLOR_BREAK and sensors[4] > COLOR_BREAK:
                break
        
        drive(0, 0)
        time.sleep(1)

    def main():
        follow()

        drive(DRIVE_SPEED, DRIVE_SPEED)
        time.sleep(0.5)
        follow()

        drive(DRIVE_SPEED, DRIVE_SPEED)
        time.sleep(0.5)
        follow()

        drive(DRIVE_SPEED, DRIVE_SPEED)
        time.sleep(0.5)
        follow()

    if __name__ == "__main__":
        main()

Funktionalität des Roboters überprüfen 
**************************************

.. code-block:: python

    import time
    from compLib.Motor import Motor
    from compLib.Encoder import Encoder
    from compLib.IRSensor import IRSensor


    def testIR():
        print("Enabling Infrared Sensor")
        IRSensor.enable()
        time.sleep(1)

        print("Writing sensor values...")
        for i in range(0, 50):
            print(IRSensor.read_all())
            time.sleep(0.1)

        print("Disabling Infrared Sensor")
        IRSensor.disable()

    def testEncoders():
        Motor.multiple_pulse_width((0, 50), (3, -50))

        print("Writing encoder positions...")
        for i in range(0, 50):
            print(Encoder.read_all_positions())
            time.sleep(0.1)

        time.sleep(2)
        print("Writing encoder velocities...")
        for i in range(0, 50):
            print(Encoder.read_all_velocities())
            time.sleep(0.1)

        Motor.multiple_pulse_width((0, 0), (3, 0))


    def testMotors():
        print("Setting pulse_with")
        Motor.multiple_pulse_width((0, 50), (3, -50))
        time.sleep(3)

        print("Setting power")
        Motor.multiple_power((0, 50), (3, -50))
        time.sleep(3)

        print("Setting pulse_with")
        Motor.multiple_speed((0, 5), (3, -5))
        time.sleep(3)

        for i in range(0, 100):
            Motor.multiple_power((0, i), (3, -i))
            time.sleep(0.1)


    if __name__ == "__main__":
        print("Make sure robot is turned on it's back!")
        time.sleep(5)

        print()
        print("----------------- Testing Infrared Sensor -----------------")
        testIR()

        print()
        print("----------------- Testing Encoder -----------------")
        testEncoders()

        print()
        print("----------------- Testing Motors -----------------")
        testMotors()