Intelino - programovateľný vláčik

3. Úlohy

3.4. Programovací jazyk Python

Úloha 7:

V programovacom jazyku Python napíšte program aby vlak zablikal päťkrát hornou LED diódou, použite náhodné farby.

Riešenie:

import random
import time
from intelino.trainlib import TrainScanner


def main():
    with TrainScanner() as train:
        blink_delay = 0.5  # in seconds

        for _ in range(5):
            red = random.randint(0, 1) * 255
            green = random.randint(0, 1) * 255
            blue = random.randint(0, 1) * 255

            # turn LED on
            train.set_top_led_color(red, green, blue)
            time.sleep(blink_delay)

            # turn LED off
            train.set_top_led_color(0, 0, 0)
            time.sleep(blink_delay)


if __name__ == "__main__":
    main()
  

Úloha 8:

Postavte kruhovú trať a v programovacom jazyku Python napíšte program. Ovládajte rýchlosť vlaku. Najskôr pomocou preddefinovaných úrovní rýchlosti potom s manuálnymi rýchlosťami v cm/s. Po každom kole zmeňte úroveň rýchlosti, potom krátko zastavte a postupne zrýchľujte od 20 do 80 cm/s počas 3 kôl.

Riešenie:

import time

from intelino.trainlib import TrainScanner
from intelino.trainlib.enums import (
    MovementDirection,
    SpeedLevel,
    StopDrivingFeedbackType,
)

# Constant: 1 circle track length is approx. 127 cm (PI * 40.5 cm)
CIRCLE_LENGTH_CM = 127.0


def main():
    with TrainScanner() as train:
        #
        # Run 3 loops to test out the 3 predefined speed levels.
        #
        train.drive_at_speed_level(
            # start slow
            speed_level=SpeedLevel.LEVEL1,
            # optional: direction defaults to forward, if not specified
            direction=MovementDirection.FORWARD,
            # optional: whether to blink the top LED, defaults to True
            play_feedback=True,
        )
        # wait until the train drives 1 circle length
        while train.distance_cm < 1 * CIRCLE_LENGTH_CM:
            time.sleep(0.001)  # 1ms sleep

        # medium speed
        train.drive_at_speed_level(SpeedLevel.LEVEL2)
        while train.distance_cm < 2 * CIRCLE_LENGTH_CM:
            time.sleep(0.001)

        # fast
        train.drive_at_speed_level(SpeedLevel.LEVEL3)
        while train.distance_cm < 3 * CIRCLE_LENGTH_CM:
            time.sleep(0.001)

        # stop and rest for a moment
        train.stop_driving(StopDrivingFeedbackType.END_ROUTE)
        time.sleep(3)

        # reset train distance measurement
        train.distance_cm = 0

        #
        # Run 3 loops on a circle while smoothly increasing train speed.
        #
        slow_cmps = 20
        fast_cmps = 80
        step_count = fast_cmps - slow_cmps
        # calculate how long (in cm) we drive at every speed
        step_length_cm = 3 * CIRCLE_LENGTH_CM / step_count

        # start slow
        current_speed_cmps = slow_cmps
        while train.distance_cm < 3 * CIRCLE_LENGTH_CM:
            start_cm = train.distance_cm
            train.drive_at_speed(current_speed_cmps)
            while (train.distance_cm - start_cm) < step_length_cm:
                time.sleep(0.001)  # 1ms sleep

            # increase the speed for the next iteration
            current_speed_cmps += 1

        train.stop_driving(StopDrivingFeedbackType.END_ROUTE)


if __name__ == "__main__":
    main()