|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "3d88442e", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Dig Deeper\n", |
| 9 | + "\n", |
| 10 | + "This page includes concepts that are bit more advanced but might come in handy when dealing with ALPACA.\n", |
| 11 | + "\n", |
| 12 | + "\n", |
| 13 | + "## Live Plotting\n", |
| 14 | + "\n", |
| 15 | + ":::{admonition} Background: 'yield'\n", |
| 16 | + ":class: note, dropdown\n", |
| 17 | + "\n", |
| 18 | + "The `yield` keyword is a new concept. It is simillar to the `return` keyword but it doesn't stop the function. Instead it **yields (adds) the value to the output and continues**. This can be compared to lists.\n", |
| 19 | + "\n", |
| 20 | + "```python\n", |
| 21 | + "def foo():\n", |
| 22 | + " l = []\n", |
| 23 | + " for i in range(5):\n", |
| 24 | + " l.append(i + 1)\n", |
| 25 | + " \n", |
| 26 | + " return l\n", |
| 27 | + "\n", |
| 28 | + "output = foo()\n", |
| 29 | + "```\n", |
| 30 | + "\n", |
| 31 | + "gives the exact same result as\n", |
| 32 | + "\n", |
| 33 | + "```python\n", |
| 34 | + "def foo():\n", |
| 35 | + " for i in range(5):\n", |
| 36 | + " yield i + 1\n", |
| 37 | + "\n", |
| 38 | + "# foo() returns a 'generator' which is essentially\n", |
| 39 | + "# something that can be used to generate a list\n", |
| 40 | + "output = list(foo())\n", |
| 41 | + "```\n", |
| 42 | + "\n", |
| 43 | + "Where they start to differ is when you start to use them in loops:\n", |
| 44 | + "\n", |
| 45 | + "```python\n", |
| 46 | + "def foo():\n", |
| 47 | + " for i in range(5):\n", |
| 48 | + " print(\"Here \", i)\n", |
| 49 | + " yield i + 1\n", |
| 50 | + "\n", |
| 51 | + "for j in foo():\n", |
| 52 | + " print(\"There \", j)\n", |
| 53 | + "\n", |
| 54 | + "### OUTPUT\n", |
| 55 | + "# Here 1\n", |
| 56 | + "# There 2\n", |
| 57 | + "# Here 2\n", |
| 58 | + "# There 3\n", |
| 59 | + "# ...\n", |
| 60 | + "```\n", |
| 61 | + "\n", |
| 62 | + "```python\n", |
| 63 | + "def foo():\n", |
| 64 | + " l = []\n", |
| 65 | + " for i in range(5):\n", |
| 66 | + " print(\"Here \", i)\n", |
| 67 | + " l.append(i + 1)\n", |
| 68 | + " \n", |
| 69 | + " return l\n", |
| 70 | + "\n", |
| 71 | + "for j in foo():\n", |
| 72 | + " print(\"There \", j)\n", |
| 73 | + "\n", |
| 74 | + "### OUTPUT\n", |
| 75 | + "# Here 1\n", |
| 76 | + "# Here 2\n", |
| 77 | + "# Here ..\n", |
| 78 | + "# There 1\n", |
| 79 | + "# There 2\n", |
| 80 | + "# There 3\n", |
| 81 | + "# ...\n", |
| 82 | + "```\n", |
| 83 | + "\n", |
| 84 | + "As you can see the main difference is with `yield` the function doesn't stop it continues. Where as returning needs the whole function the be finished.\n", |
| 85 | + ":::\n", |
| 86 | + "\n", |
| 87 | + "\n", |
| 88 | + "When acquiring data, standard plots fall short as they show you the results at the very end. With **live plotting**, you can update an active graph. From the below example you can see how to \"animate\" matplotlib graphs to match your output.\n", |
| 89 | + "\n", |
| 90 | + "```python\n", |
| 91 | + "import matplotlib.pyplot as plt\n", |
| 92 | + "\n", |
| 93 | + "@pico.task\n", |
| 94 | + "def measure_samples(N, dur):\n", |
| 95 | + " '''\n", |
| 96 | + " Take measurements for dur seconds and N samples for each measurement\n", |
| 97 | + " '''\n", |
| 98 | + " from utime import ticks_add, ticks\n", |
| 99 | + "\n", |
| 100 | + " deadline = ticks_add(ticks(), dur) # dur in the future\n", |
| 101 | + "\n", |
| 102 | + " while ticks_diff(deadline, ticks()) =< 0:\n", |
| 103 | + " total = 0\n", |
| 104 | + " for i in range(N):\n", |
| 105 | + " total += adc0.read_u16()\n", |
| 106 | + " \n", |
| 107 | + " yield total * 3 / 2**16 / N # Add average to the list\n", |
| 108 | + "\n", |
| 109 | + " return # Stop\n", |
| 110 | + "\n", |
| 111 | + "plt.ion() # Enable matplotlib\n", |
| 112 | + "fig, ax = plt.subplots() # Create figure and an axis\n", |
| 113 | + "\n", |
| 114 | + "t = [] # Time\n", |
| 115 | + "y = [] # Values\n", |
| 116 | + "# Initialize an empty line object\n", |
| 117 | + "(line,) = ax.plot([], [])\n", |
| 118 | + "\n", |
| 119 | + "# This for loop will run as long as 'yield' is 'ran'\n", |
| 120 | + "for val in measure_samples(50, 5):\n", |
| 121 | + " t.append(time.time()) # Add current time\n", |
| 122 | + " y.append(val) # Add measurement\n", |
| 123 | + "\n", |
| 124 | + " ax.relim() # Adjust the x-axis and y-axis\n", |
| 125 | + " ax.autoscale_view()\n", |
| 126 | + "\n", |
| 127 | + " # Update the line with current data\n", |
| 128 | + " line.set_ydata(y)\n", |
| 129 | + " line.set_xdata(t)\n", |
| 130 | + "\n", |
| 131 | + "plt.ioff()\n", |
| 132 | + "plt.show()\n", |
| 133 | + "```" |
| 134 | + ] |
| 135 | + } |
| 136 | + ], |
| 137 | + "metadata": { |
| 138 | + "language_info": { |
| 139 | + "name": "python" |
| 140 | + } |
| 141 | + }, |
| 142 | + "nbformat": 4, |
| 143 | + "nbformat_minor": 5 |
| 144 | +} |
0 commit comments