Skip to content

Commit 15b59a7

Browse files
author
Emma Brand
committed
Reverted style changes, fixed tutorials section and added TOC
1 parent e5af297 commit 15b59a7

14 files changed

+323
-235
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ Thumbs.db
88

99
.rbenv-version
1010
.rmvrc
11+
*.lock
12+
1113

Gemfile.lock

-72
This file was deleted.

_data/navigation.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
- page: Home
22
url: /
3-
3+
44
- page: About
55
url: /about
66

@@ -10,8 +10,8 @@
1010
- page: Sponsors
1111
url: /sponsors
1212

13-
#- page: News
14-
# url: /news
13+
- page: Code
14+
url: /tutorial
1515

1616
- page: Contact
17-
url: /contact
17+
url: /contact

_layouts/posts.html

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
layout: content
3+
---
4+
<article class="post">
5+
<h1 class="post-title">{{ page.title }}</h1>
6+
{{ content }}
7+
</article>

_posts/2018-02-25-test-post-1.markdown

-25
This file was deleted.

_posts/2018-02-25-test-post-2.markdown

-25
This file was deleted.

_posts/2018-02-26-test-post-3.markdown

-25
This file was deleted.
+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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

Comments
 (0)