{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "

\n", " \n", "

\n", "\n", "# `random_images`\n", "Display a set of random images in the hub\n", "\n", "# Required robot\n", "* Charlie (head)\n", "\n", "\n", "\n", "# Source code\n", "You can find the code in the accompanying [`.py` file](https://github.com/arturomoncadatorres/lego-mindstorms/blob/main/examples/programs/random_images.py). To get it running, simply copy and paste it in a new Mindstorms project.\n", "\n", "# Imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from mindstorms import MSHub, Motor, MotorPair, ColorSensor, DistanceSensor, App\n", "from mindstorms.control import wait_for_seconds, wait_until, Timer\n", "from mindstorms.operator import greater_than, greater_than_or_equal_to, less_than, less_than_or_equal_to, equal_to, not_equal_to\n", "import math" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random # Needed to generate random numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Initialization" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hub = MSHub()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Define the list of possible images" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "images = ['ANGRY', 'ARROW_E', 'ARROW_N', 'ARROW_NE', 'ARROW_NW',\n", "'ARROW_S', 'ARROW_SE', 'ARROW_SW', 'ARROW_W', 'ASLEEP',\n", "'BUTTERFLY', 'CHESSBOARD', 'CLOCK1', 'CLOCK10', 'CLOCK11', \n", "'CLOCK12', 'CLOCK2', 'CLOCK3', 'CLOCK4', 'CLOCK5', \n", "'CLOCK6', 'CLOCK7', 'CLOCK8', 'CLOCK9', 'CONFUSED', \n", "'COW', 'DIAMOND', 'DIAMOND_SMALL', 'DUCK', 'FABULOUS', \n", "'GHOST', 'GIRAFFE', 'GO_RIGHT', 'GO_LEFT', 'GO_UP', \n", "'GO_DOWN', 'HAPPY', 'HEART', 'HEART_SMALL', 'HOUSE',\n", "'MEH', 'MUSIC_CROTCHET', 'MUSIC_QUAVER', 'MUSIC_QUAVERS', 'NO', \n", "'PACMAN', 'PITCHFORK', 'RABBIT', 'ROLLERSKATE', 'SAD', \n", "'SILLY', 'SKULL', 'SMILE', 'SNAKE', 'SQUARE', \n", "'SQUARE_SMALL', 'STICKFIGURE', 'SURPRISED', 'SWORD', 'TARGET', \n", "'TORTOISE', 'TRIANGLE', 'TRIANGLE_LEFT', 'TSHIRT', 'UMBRELLA', \n", "'XMAS', 'YES']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n_images = len(images)\n", "print(\"Total number of images: \" + str(n_images))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Display images\n", "To choose the images to display, we will generate a random index\n", "using [`random.randint`](https://docs.python.org/3/library/random.html#random.randint)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for ii in range(1, 4):\n", " \n", " # Generate a random number\n", " idx = random.randint(0, n_images-1)\n", "\n", " # Display the random image\n", " image = images[idx]\n", " print(\"Displaying image \" + str(ii) + \" with index \" + str(idx) + \" ('\" + image + \"')...\")\n", " hub.light_matrix.show_image(image)\n", " print(\"DONE!\")\n", "\n", " # Insert a pause so we can see the image\n", " wait_for_seconds(2.5)" ] } ], "metadata": { "jupytext": { "formats": "ipynb,py:percent" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }