This repository has been archived on 2025-06-01. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
compLIB/client/docs/source/other/usage.rst
Konstantin Lampalzer 002db9d650 Add linefollower
2022-10-14 00:08:59 +02:00

91 lines
No EOL
1.8 KiB
ReStructuredText

.. _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()