|
| 1 | +--- |
| 2 | +layout: posts |
| 3 | +title: "Robotics while loop" |
| 4 | +toc: true |
| 5 | +catagotries: [tutorial, code] |
| 6 | +--- |
| 7 | + |
| 8 | +# python tutorial 1 |
| 9 | + |
| 10 | +The while loop is a convenient way to do the same task over and over again. This |
| 11 | +comes up a lot in robotics, where it is often desirable to let something run in |
| 12 | +a tight loop forever. A good example of this is code which interfaces with an |
| 13 | +instrument. In this case, it is common to write code that simply spends all of |
| 14 | +its time checking the instrument for updates, and then publishing that |
| 15 | +information. |
| 16 | + |
| 17 | +A simple example of this behavior is demonstrated below using the system clock. |
| 18 | +Out of the box, computers are all very good at keeping time, and this code |
| 19 | +shows how to publish that information at a constant rate. |
| 20 | + |
| 21 | +{% highlight python linenos %} |
| 22 | +import time |
| 23 | + |
| 24 | +starttime = time.time() |
| 25 | + |
| 26 | +while True: |
| 27 | + currenttime = time.time() |
| 28 | + print("Time is %.02f seconds"%currenttime) |
| 29 | + time.sleep(0.1) |
| 30 | + |
| 31 | +{% endhighlight %} |
| 32 | + |
| 33 | +This code is an example of an infinite loop. It will run forever doing |
| 34 | +something of questionable importance. To stop the loop, either close the |
| 35 | +terminal running python or push <kbd>CTLR</kbd> + <kbd>c</kbd>. |
| 36 | + |
| 37 | +The time printed to the screen is in seconds since January 1st, 1970. This is |
| 38 | +known as [Unix epoch time](http://en.wikipedia.org/wiki/Unix_time). |
| 39 | + |
| 40 | +There are several things going on in this code, and since this is a tutorial |
| 41 | +they will be broken down line by line. |
| 42 | + |
| 43 | +The first line is an [import](http://docs.python.org/3/reference/import.html) |
| 44 | +statement. Quoting from the first line of the python documentation, "Python |
| 45 | +code in one module gains access to the code in another module by the process |
| 46 | +of importing it." This is a fairly precise definition, but in essence this |
| 47 | +allows code to use the requested module. |
| 48 | +[time](http://docs.python.org/3/library/time.html?highlight=time#module-time) |
| 49 | +is a built in python module, and if python is running this import will |
| 50 | +work. This is not always the case, and sometimes getting imports to work is a |
| 51 | +real challenge :) |
| 52 | + |
| 53 | +The time module has within it a number of functions that relate to computer |
| 54 | +time. The relevant functions are [time](https://docs.python.org/3/library/time.html?highlight=time#time.time) |
| 55 | +and [sleep](https://docs.python.org/3/library/time.html?highlight=time#time.sleep). |
| 56 | +Though a bit weird perhaps, it is not uncommon in python for a word to be |
| 57 | +repeated twice with a period in between, as in line 3. |
| 58 | + |
| 59 | +The [while](https://docs.python.org/3/reference/compound_stmts.html#while) |
| 60 | +loop begins on line 5. A particular characteristic of python appears in |
| 61 | +this statement. Code blocks which can extend for an arbitrary number of lines |
| 62 | +are first ended with the colon character. The code associated with this code |
| 63 | +block (in this case a while loop) is then indicated by *indentation*. All |
| 64 | +statements below the while loop moved one or more tab spaces in from the while |
| 65 | +loop are a part of the while loop. |
| 66 | + |
| 67 | +The [print](https://docs.python.org/3/library/functions.html#print) function |
| 68 | +is how the information moves from inside the program to the computer screen. |
| 69 | +This is a relatively complicated issue in general for robotics, where |
| 70 | +information is moved between many programs frequently. In this example the |
| 71 | +simplest method is used, simply printing the information to the terminal. More |
| 72 | +complicated information transfer is a topic for a later tutorial, but the print |
| 73 | +function is a frequently used first step to understanding a new output. |
| 74 | + |
| 75 | +The print line uses [sting formatting](https://docs.python.org/3/library/stdtypes.html#string-formatting) |
| 76 | +to insert a variable into the printed statement. It is possible to simply print |
| 77 | +the variable by itself as |
| 78 | + |
| 79 | +{% highlight python linenos %} |
| 80 | +print(currenttime) |
| 81 | +{% endhighlight %} |
| 82 | + |
| 83 | +This is often simpler to code, but the output is harder to read. Reviewing the |
| 84 | +documentation the % character is discouraged, but force of habit is hard to break. |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +The last line of the while loop is a sleep statement. This is used to free up |
| 89 | +the CPU for 1/10 of a second, a long time to a computer. Like the |
| 90 | +print statement, the sleep statement is a simple approach to a complicated |
| 91 | +topic, in this case [process](https://en.wikipedia.org/wiki/Process_(computing)) |
| 92 | +management. This method will be extensively |
| 93 | +used in the robot and covered further in future tutorials. This design decision |
| 94 | +is essentially equivalent to the statement *Nothing needs to be |
| 95 | +addressed within 1/10 of a second*. |
| 96 | + |
| 97 | +--- |
| 98 | + |
| 99 | +The second example in this tutorial is a slight extension of the first. The |
| 100 | +template for a mission task has a few distinctions from the instrument reading |
| 101 | +loop show above. In general, these tasks are designed to complete, and good |
| 102 | +practice further ensures that all mission tasks terminate by adding a |
| 103 | +timeout condition. |
| 104 | + |
| 105 | +{% highlight python linenos %} |
| 106 | +import time |
| 107 | + |
| 108 | +isrun = True |
| 109 | +starttime = time.time() |
| 110 | + |
| 111 | +while isrun: |
| 112 | + currenttime = time.time() |
| 113 | + |
| 114 | + # mission completion check |
| 115 | + if False: |
| 116 | + isrun = False |
| 117 | + |
| 118 | + # timeout condition |
| 119 | + if (currenttime - starttime) > 3: |
| 120 | + break |
| 121 | + |
| 122 | + print("Time is %.02f seconds"%currenttime) |
| 123 | + time.sleep(0.1) |
| 124 | + |
| 125 | +print("Goodbye World") |
| 126 | +{% endhighlight %} |
| 127 | + |
| 128 | +The while loop in this example checks a variable before it begins execution. If |
| 129 | +isrun is False, the while loop exits before execution. As written, the while |
| 130 | +loop will never terminate in this way, but it might be fun to try and change |
| 131 | +this behavior. |
| 132 | + |
| 133 | +The timeout behavior takes place on line 14, which checks the current time and |
| 134 | +exits if it has been more than 3 seconds from the start of the loop. The loop |
| 135 | +is then immediately terminated with the break statement. The if loop, and the |
| 136 | +break statement are examples of [flow control](https://docs.python.org/3.7/tutorial/controlflow.html?highlight=break). |
| 137 | + |
| 138 | +The last action of this code is to print the line "Goodbye World". This shows |
| 139 | +how to write code both inside and outside of the while loop. |
| 140 | + |
| 141 | +Well, that is it for now. Hopefully the basic topics covered here will continue |
| 142 | +to be useful into the future. |
0 commit comments