Difference between revisions of "Team:NAWI Graz/Robot"

(added source code)
Line 11: Line 11:
 
The robot, a Thymio II, is a robot designed for educational purposes. All components (hardware and software) are open source. The robot consists of more than 20 sensors, many LEDs, two motors, a microphone and a loudspeaker.
 
The robot, a Thymio II, is a robot designed for educational purposes. All components (hardware and software) are open source. The robot consists of more than 20 sensors, many LEDs, two motors, a microphone and a loudspeaker.
 
To make the already very powerful tool even more superior we connected a raspberry pi to it, allowing us to not only take full control over every aspect of the robot, but also using python as a programming language for the device.  
 
To make the already very powerful tool even more superior we connected a raspberry pi to it, allowing us to not only take full control over every aspect of the robot, but also using python as a programming language for the device.  
 +
 +
<!-- HTML generated using hilite.me --><div style="background: #f8f8f8; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><table><tr><td><pre style="margin: 0; line-height: 125%">  1
 +
  2
 +
  3
 +
  4
 +
  5
 +
  6
 +
  7
 +
  8
 +
  9
 +
10
 +
11
 +
12
 +
13
 +
14
 +
15
 +
16
 +
17
 +
18
 +
19
 +
20
 +
21
 +
22
 +
23
 +
24
 +
25
 +
26
 +
27
 +
28
 +
29
 +
30
 +
31
 +
32
 +
33
 +
34
 +
35
 +
36
 +
37
 +
38
 +
39
 +
40
 +
41
 +
42
 +
43
 +
44
 +
45
 +
46
 +
47
 +
48
 +
49
 +
50
 +
51
 +
52
 +
53
 +
54
 +
55
 +
56
 +
57
 +
58
 +
59
 +
60
 +
61
 +
62
 +
63
 +
64
 +
65
 +
66
 +
67
 +
68
 +
69
 +
70
 +
71
 +
72
 +
73
 +
74
 +
75
 +
76
 +
77
 +
78
 +
79
 +
80
 +
81
 +
82
 +
83
 +
84
 +
85
 +
86
 +
87
 +
88
 +
89
 +
90
 +
91
 +
92
 +
93
 +
94
 +
95
 +
96
 +
97
 +
98
 +
99
 +
100
 +
101
 +
102
 +
103
 +
104
 +
105
 +
106
 +
107
 +
108
 +
109
 +
110
 +
111
 +
112
 +
113
 +
114
 +
115
 +
116
 +
117
 +
118
 +
119
 +
120
 +
121
 +
122
 +
123
 +
124
 +
125
 +
126
 +
127
 +
128
 +
129
 +
130
 +
131
 +
132
 +
133
 +
134
 +
135
 +
136
 +
137
 +
138
 +
139
 +
140
 +
141
 +
142
 +
143
 +
144
 +
145
 +
146
 +
147
 +
148
 +
149
 +
150
 +
151
 +
152
 +
153
 +
154
 +
155
 +
156
 +
157
 +
158
 +
159
 +
160
 +
161
 +
162
 +
163
 +
164
 +
165
 +
166
 +
167
 +
168
 +
169
 +
170
 +
171
 +
172
 +
173
 +
174
 +
175
 +
176
 +
177
 +
178
 +
179
 +
180
 +
181
 +
182
 +
183
 +
184
 +
185
 +
186
 +
187
 +
188
 +
189
 +
190
 +
191
 +
192
 +
193
 +
194
 +
195
 +
196
 +
197
 +
198
 +
199
 +
200
 +
201
 +
202
 +
203
 +
204
 +
205
 +
206
 +
207</pre></td><td><pre style="margin: 0; line-height: 125%"><span style="color: #8f5902; font-style: italic">#!/usr/bin/python2</span>
 +
<span style="color: #204a87; font-weight: bold">import</span> <span style="color: #000000">dbus</span>
 +
<span style="color: #204a87; font-weight: bold">import</span> <span style="color: #000000">dbus.mainloop.glib</span>
 +
<span style="color: #204a87; font-weight: bold">import</span> <span style="color: #000000">gobject</span>
 +
<span style="color: #204a87; font-weight: bold">import</span> <span style="color: #000000">sys</span>
 +
<span style="color: #204a87; font-weight: bold">import</span> <span style="color: #000000">socket</span>
 +
<span style="color: #204a87; font-weight: bold">import</span> <span style="color: #000000">time</span>
 +
<span style="color: #204a87; font-weight: bold">import</span> <span style="color: #000000">logging</span>
 +
 +
<span style="color: #8f5902; font-style: italic">#from w1thermsensor import W1ThermSensor</span>
 +
 +
<span style="color: #204a87; font-weight: bold">from</span> <span style="color: #000000">optparse</span> <span style="color: #204a87; font-weight: bold">import</span> <span style="color: #000000">OptionParser</span>
 +
 +
<span style="color: #8f5902; font-style: italic"># proximity sensor readaout values</span>
 +
<span style="color: #000000">proxSensorsVal</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">0</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #0000cf; font-weight: bold">0</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #0000cf; font-weight: bold">0</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #0000cf; font-weight: bold">0</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #0000cf; font-weight: bold">0</span><span style="color: #000000; font-weight: bold">]</span>
 +
 +
<span style="color: #8f5902; font-style: italic"># server ip and port</span>
 +
<span style="color: #000000">host</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #4e9a06">&#39;192.168.43.150&#39;</span>
 +
<span style="color: #000000">port</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #0000cf; font-weight: bold">4343</span>
 +
 +
<span style="color: #000000">logger</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">logging</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">getLogger</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&#39;colibot&#39;</span><span style="color: #000000; font-weight: bold">)</span>
 +
 +
<span style="color: #000000">debug</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #3465a4">False</span>
 +
<span style="color: #000000">counter</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #0000cf; font-weight: bold">0</span>
 +
 +
<span style="color: #000000">SENSOR_THRESHOLD</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #0000cf; font-weight: bold">3300</span>
 +
<span style="color: #000000">DRIVE_DIST</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #0000cf; font-weight: bold">400</span>
 +
<span style="color: #000000">TURN_DIST</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #0000cf; font-weight: bold">180</span>
 +
 +
<span style="color: #204a87; font-weight: bold">def</span> <span style="color: #000000">dbus_reply</span><span style="color: #000000; font-weight: bold">():</span>
 +
    <span style="color: #204a87; font-weight: bold">pass</span>
 +
 +
 +
<span style="color: #204a87; font-weight: bold">def</span> <span style="color: #000000">dbus_error</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">e</span><span style="color: #000000; font-weight: bold">):</span>
 +
    <span style="color: #8f5902; font-style: italic"># logger.info &#39;error %s&#39;</span>
 +
    <span style="color: #8f5902; font-style: italic"># logger.info str(e)</span>
 +
    <span style="color: #204a87; font-weight: bold">pass</span>
 +
 +
 +
<span style="color: #204a87; font-weight: bold">def</span> <span style="color: #000000">control</span><span style="color: #000000; font-weight: bold">():</span>
 +
    <span style="color: #8f5902; font-style: italic"># get the values of the sensors</span>
 +
    <span style="color: #000000">network</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">GetVariable</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;thymio-II&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #4e9a06">&quot;prox.horizontal&quot;</span><span style="color: #000000; font-weight: bold">,</span>
 +
                        <span style="color: #000000">reply_handler</span><span style="color: #ce5c00; font-weight: bold">=</span><span style="color: #000000">get_variables_reply</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000">error_handler</span><span style="color: #ce5c00; font-weight: bold">=</span><span style="color: #000000">get_variables_error</span><span style="color: #000000; font-weight: bold">)</span>
 +
 +
    <span style="color: #8f5902; font-style: italic"># send proximity sensor reading to colis</span>
 +
    <span style="color: #000000">reply</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">coli_com</span><span style="color: #000000; font-weight: bold">()</span>
 +
 +
    <span style="color: #204a87; font-weight: bold">if</span> <span style="color: #000000">reply</span> <span style="color: #ce5c00; font-weight: bold">==</span> <span style="color: #0000cf; font-weight: bold">0</span><span style="color: #000000; font-weight: bold">:</span>
 +
        <span style="color: #204a87; font-weight: bold">for</span> <span style="color: #000000">i</span> <span style="color: #204a87; font-weight: bold">in</span> <span style="color: #204a87">range</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #0000cf; font-weight: bold">0</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000">DRIVE_DIST</span><span style="color: #000000; font-weight: bold">):</span>
 +
            <span style="color: #000000">network</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">SetVariable</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;thymio-II&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #4e9a06">&quot;motor.left.target&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">100</span><span style="color: #000000; font-weight: bold">])</span>
 +
            <span style="color: #000000">network</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">SetVariable</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;thymio-II&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #4e9a06">&quot;motor.right.target&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">100</span><span style="color: #000000; font-weight: bold">])</span>
 +
 +
    <span style="color: #204a87; font-weight: bold">if</span> <span style="color: #000000">reply</span> <span style="color: #ce5c00; font-weight: bold">==</span> <span style="color: #0000cf; font-weight: bold">1</span><span style="color: #000000; font-weight: bold">:</span>
 +
        <span style="color: #204a87; font-weight: bold">for</span> <span style="color: #000000">i</span> <span style="color: #204a87; font-weight: bold">in</span> <span style="color: #204a87">range</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #0000cf; font-weight: bold">0</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000">TURN_DIST</span><span style="color: #000000; font-weight: bold">):</span>
 +
            <span style="color: #000000">network</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">SetVariable</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;thymio-II&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #4e9a06">&quot;motor.left.target&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">100</span><span style="color: #000000; font-weight: bold">])</span>
 +
            <span style="color: #000000">network</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">SetVariable</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;thymio-II&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #4e9a06">&quot;motor.right.target&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000; font-weight: bold">[</span><span style="color: #ce5c00; font-weight: bold">-</span><span style="color: #0000cf; font-weight: bold">100</span><span style="color: #000000; font-weight: bold">])</span>
 +
 +
        <span style="color: #000000">network</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">SetVariable</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;thymio-II&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #4e9a06">&quot;motor.left.target&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">0</span><span style="color: #000000; font-weight: bold">])</span>
 +
        <span style="color: #000000">network</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">SetVariable</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;thymio-II&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #4e9a06">&quot;motor.right.target&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">0</span><span style="color: #000000; font-weight: bold">])</span>
 +
 +
    <span style="color: #204a87; font-weight: bold">return</span> <span style="color: #3465a4">True</span>
 +
 +
 +
<span style="color: #204a87; font-weight: bold">def</span> <span style="color: #000000">get_variables_reply</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">r</span><span style="color: #000000; font-weight: bold">):</span>
 +
    <span style="color: #204a87; font-weight: bold">global</span> <span style="color: #000000">proxSensorsVal</span>
 +
    <span style="color: #000000">proxSensorsVal</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">r</span>
 +
 +
 +
<span style="color: #204a87; font-weight: bold">def</span> <span style="color: #000000">get_variables_error</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">e</span><span style="color: #000000; font-weight: bold">):</span>
 +
    <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&#39;error: &#39;</span><span style="color: #000000; font-weight: bold">)</span>
 +
    <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #204a87">str</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">e</span><span style="color: #000000; font-weight: bold">))</span>
 +
    <span style="color: #000000">loop</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">quit</span><span style="color: #000000; font-weight: bold">()</span>
 +
 +
 +
<span style="color: #204a87; font-weight: bold">def</span> <span style="color: #000000">coli_com</span><span style="color: #000000; font-weight: bold">():</span>
 +
    <span style="color: #204a87; font-weight: bold">global</span> <span style="color: #000000">debug</span>
 +
 +
    <span style="color: #000000">sum_prox</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">proxSensorsVal</span><span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">0</span><span style="color: #000000; font-weight: bold">]</span> <span style="color: #ce5c00; font-weight: bold">+</span> <span style="color: #000000">proxSensorsVal</span><span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">1</span><span style="color: #000000; font-weight: bold">]</span> <span style="color: #ce5c00; font-weight: bold">+</span> <span style="color: #000000">proxSensorsVal</span><span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">2</span><span style="color: #000000; font-weight: bold">]</span> <span style="color: #ce5c00; font-weight: bold">+</span> <span style="color: #000000">proxSensorsVal</span><span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">3</span><span style="color: #000000; font-weight: bold">]</span> <span style="color: #ce5c00; font-weight: bold">+</span> <span style="color: #000000">proxSensorsVal</span><span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">4</span><span style="color: #000000; font-weight: bold">]</span>
 +
    <span style="color: #000000">sides</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">proxSensorsVal</span><span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">0</span><span style="color: #000000; font-weight: bold">]</span> <span style="color: #ce5c00; font-weight: bold">+</span> <span style="color: #000000">proxSensorsVal</span><span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">4</span><span style="color: #000000; font-weight: bold">]</span>
 +
 +
    <span style="color: #204a87; font-weight: bold">global</span> <span style="color: #000000">counter</span>
 +
    <span style="color: #000000">counter</span> <span style="color: #ce5c00; font-weight: bold">+=</span> <span style="color: #0000cf; font-weight: bold">1</span>
 +
    <span style="color: #204a87; font-weight: bold">if</span> <span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">counter</span> <span style="color: #ce5c00; font-weight: bold">%</span> <span style="color: #0000cf; font-weight: bold">2</span><span style="color: #000000; font-weight: bold">):</span>
 +
    <span style="color: #8f5902; font-style: italic"># print(&quot;dirrrrty hack to take every second measurement only&quot;)</span>
 +
        <span style="color: #204a87; font-weight: bold">return</span> <span style="color: #0000cf; font-weight: bold">6</span>
 +
 +
    <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&#39;# # # # # # # # # # # # # # # # #&#39;</span><span style="color: #000000; font-weight: bold">)</span>
 +
    <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&#39;                        cycle {}&#39;</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">format</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">counter</span><span style="color: #ce5c00; font-weight: bold">/</span><span style="color: #0000cf; font-weight: bold">2</span><span style="color: #000000; font-weight: bold">))</span>
 +
   
 +
    <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;Proximity sensor sum: &quot;</span> <span style="color: #ce5c00; font-weight: bold">+</span> <span style="color: #204a87">str</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">sum_prox</span><span style="color: #000000; font-weight: bold">))</span>
 +
 +
 +
    <span style="color: #204a87; font-weight: bold">try</span><span style="color: #000000; font-weight: bold">:</span>
 +
        <span style="color: #204a87; font-weight: bold">if</span> <span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">sum_prox</span> <span style="color: #ce5c00; font-weight: bold">&gt;</span> <span style="color: #000000">SENSOR_THRESHOLD</span><span style="color: #000000; font-weight: bold">):</span>
 +
            <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;Obstacle detected!&quot;</span><span style="color: #000000; font-weight: bold">)</span>
 +
            <span style="color: #204a87; font-weight: bold">if</span> <span style="color: #204a87; font-weight: bold">not</span> <span style="color: #000000">debug</span><span style="color: #000000; font-weight: bold">:</span>
 +
                <span style="color: #8f5902; font-style: italic"># send data to server</span>
 +
                <span style="color: #000000">s</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">sendto</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #204a87">str</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">encode</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&#39;obstacle&#39;</span><span style="color: #000000; font-weight: bold">),</span> <span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">host</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000">port</span><span style="color: #000000; font-weight: bold">))</span>
 +
                <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;Sent &#39;obstacle&#39; to reactor.&quot;</span><span style="color: #000000; font-weight: bold">)</span>
 +
            <span style="color: #204a87; font-weight: bold">if</span> <span style="color: #000000">debug</span> <span style="color: #ce5c00; font-weight: bold">==</span> <span style="color: #3465a4">True</span><span style="color: #000000; font-weight: bold">:</span>
 +
                <span style="color: #000000">reply</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #4e9a06">&#39;turn&#39;</span>
 +
        <span style="color: #204a87; font-weight: bold">else</span><span style="color: #000000; font-weight: bold">:</span>
 +
            <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;No obstacle&quot;</span><span style="color: #000000; font-weight: bold">)</span>
 +
            <span style="color: #204a87; font-weight: bold">if</span> <span style="color: #204a87; font-weight: bold">not</span>  <span style="color: #000000">debug</span><span style="color: #000000; font-weight: bold">:</span>
 +
                <span style="color: #8f5902; font-style: italic"># send data to server</span>
 +
                <span style="color: #000000">s</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">sendto</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #204a87">str</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">encode</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&#39;clear&#39;</span><span style="color: #000000; font-weight: bold">),</span> <span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">host</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000">port</span><span style="color: #000000; font-weight: bold">))</span>
 +
                <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;Sent &#39;clear&#39; to reactor.&quot;</span><span style="color: #000000; font-weight: bold">)</span>
 +
            <span style="color: #204a87; font-weight: bold">if</span> <span style="color: #000000">debug</span> <span style="color: #ce5c00; font-weight: bold">==</span> <span style="color: #3465a4">True</span><span style="color: #000000; font-weight: bold">:</span>
 +
                <span style="color: #000000">reply</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #4e9a06">&#39;drive&#39;</span>
 +
 +
 +
        <span style="color: #204a87; font-weight: bold">if</span> <span style="color: #000000">debug</span> <span style="color: #ce5c00; font-weight: bold">==</span> <span style="color: #3465a4">False</span><span style="color: #000000; font-weight: bold">:</span>
 +
            <span style="color: #8f5902; font-style: italic"># receive data</span>
 +
            <span style="color: #000000">d</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">s</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">recvfrom</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #0000cf; font-weight: bold">1024</span><span style="color: #000000; font-weight: bold">)</span>
 +
            <span style="color: #000000">reply</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">d</span><span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">0</span><span style="color: #000000; font-weight: bold">]</span>
 +
            <span style="color: #000000">addr</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">d</span><span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">1</span><span style="color: #000000; font-weight: bold">]</span>
 +
 +
        <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&#39;Server reply : &#39;</span> <span style="color: #ce5c00; font-weight: bold">+</span> <span style="color: #204a87">str</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">reply</span><span style="color: #000000; font-weight: bold">))</span>
 +
<span style="color: #8f5902; font-style: italic">#        logger.info(&#39;Executing ...&#39;)</span>
 +
        <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&#39;                                #&#39;</span><span style="color: #000000; font-weight: bold">)</span>
 +
        <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&#39;# # # # # # # # # # # # # # # # #&#39;</span><span style="color: #000000; font-weight: bold">)</span>
 +
        <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&#39;&#39;</span><span style="color: #000000; font-weight: bold">)</span>
 +
 +
        <span style="color: #204a87; font-weight: bold">if</span> <span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">reply</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">decode</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold">)</span> <span style="color: #ce5c00; font-weight: bold">==</span> <span style="color: #4e9a06">&#39;turn&#39;</span><span style="color: #000000; font-weight: bold">):</span>
 +
<span style="color: #8f5902; font-style: italic">#            s.sendto(str.encode(&#39;thanks&#39;), (host, port))</span>
 +
            <span style="color: #204a87; font-weight: bold">return</span> <span style="color: #0000cf; font-weight: bold">1</span>
 +
        <span style="color: #204a87; font-weight: bold">if</span> <span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">reply</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">decode</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold">)</span> <span style="color: #ce5c00; font-weight: bold">==</span> <span style="color: #4e9a06">&#39;drive&#39;</span><span style="color: #000000; font-weight: bold">):</span>
 +
<span style="color: #8f5902; font-style: italic">#            s.sendto(str.encode(&#39;thanks&#39;), (host, port))            </span>
 +
            <span style="color: #204a87; font-weight: bold">return</span> <span style="color: #0000cf; font-weight: bold">0</span>
 +
        <span style="color: #204a87; font-weight: bold">else</span><span style="color: #000000; font-weight: bold">:</span>
 +
            <span style="color: #204a87; font-weight: bold">return</span> <span style="color: #0000cf; font-weight: bold">6</span>
 +
 +
    <span style="color: #204a87; font-weight: bold">except</span> <span style="color: #000000">socket</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">error</span> <span style="color: #204a87; font-weight: bold">as</span> <span style="color: #000000">msg</span><span style="color: #000000; font-weight: bold">:</span>
 +
        <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&#39;Error Code : &#39;</span> <span style="color: #ce5c00; font-weight: bold">+</span> <span style="color: #204a87">str</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">msg</span><span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">0</span><span style="color: #000000; font-weight: bold">])</span> <span style="color: #ce5c00; font-weight: bold">+</span> <span style="color: #4e9a06">&#39; Message &#39;</span> <span style="color: #ce5c00; font-weight: bold">+</span> <span style="color: #000000">msg</span><span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">1</span><span style="color: #000000; font-weight: bold">])</span>
 +
 +
 +
<span style="color: #204a87; font-weight: bold">def</span> <span style="color: #000000">cam_sync_at_start</span><span style="color: #000000; font-weight: bold">():</span>
 +
    <span style="color: #000000">network</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">SetVariable</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;thymio-II&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #4e9a06">&quot;motor.left.target&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">300</span><span style="color: #000000; font-weight: bold">])</span>
 +
    <span style="color: #000000">network</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">SetVariable</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;thymio-II&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #4e9a06">&quot;motor.right.target&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">300</span><span style="color: #000000; font-weight: bold">])</span>
 +
    <span style="color: #000000">time</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">sleep</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #0000cf; font-weight: bold">1</span><span style="color: #000000; font-weight: bold">)</span>
 +
    <span style="color: #000000">network</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">SetVariable</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;thymio-II&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #4e9a06">&quot;motor.left.target&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">0</span><span style="color: #000000; font-weight: bold">])</span>
 +
    <span style="color: #000000">network</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">SetVariable</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;thymio-II&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #4e9a06">&quot;motor.right.target&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">0</span><span style="color: #000000; font-weight: bold">])</span>   
 +
    <span style="color: #000000">network</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">SetVariable</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;thymio-II&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #4e9a06">&quot;motor.left.target&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000; font-weight: bold">[</span><span style="color: #ce5c00; font-weight: bold">-</span><span style="color: #0000cf; font-weight: bold">300</span><span style="color: #000000; font-weight: bold">])</span>
 +
    <span style="color: #000000">network</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">SetVariable</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;thymio-II&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #4e9a06">&quot;motor.right.target&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000; font-weight: bold">[</span><span style="color: #ce5c00; font-weight: bold">-</span><span style="color: #0000cf; font-weight: bold">300</span><span style="color: #000000; font-weight: bold">])</span>
 +
    <span style="color: #000000">time</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">sleep</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #0000cf; font-weight: bold">1</span><span style="color: #000000; font-weight: bold">)</span>
 +
    <span style="color: #000000">network</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">SetVariable</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;thymio-II&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #4e9a06">&quot;motor.left.target&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">0</span><span style="color: #000000; font-weight: bold">])</span>
 +
    <span style="color: #000000">network</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">SetVariable</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;thymio-II&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #4e9a06">&quot;motor.right.target&quot;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000; font-weight: bold">[</span><span style="color: #0000cf; font-weight: bold">0</span><span style="color: #000000; font-weight: bold">])</span>
 +
 +
<span style="color: #204a87; font-weight: bold">if</span> <span style="color: #000000">__name__</span> <span style="color: #ce5c00; font-weight: bold">==</span> <span style="color: #4e9a06">&#39;__main__&#39;</span><span style="color: #000000; font-weight: bold">:</span>
 +
    <span style="color: #8f5902; font-style: italic"># format</span>
 +
    <span style="color: #000000">FORMAT</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #4e9a06">&quot;[%(asctime)s ]  %(message)s&quot;</span>
 +
    <span style="color: #000000">fileHandler</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">logging</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">FileHandler</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&#39;log.txt&#39;</span><span style="color: #000000; font-weight: bold">)</span>
 +
    <span style="color: #000000">fileHandler</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">setFormatter</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">logging</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">Formatter</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">FORMAT</span><span style="color: #000000; font-weight: bold">))</span>
 +
    <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">addHandler</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">fileHandler</span><span style="color: #000000; font-weight: bold">)</span>
 +
    <span style="color: #000000">logging</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">basicConfig</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">level</span><span style="color: #ce5c00; font-weight: bold">=</span><span style="color: #0000cf; font-weight: bold">20</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000">format</span><span style="color: #ce5c00; font-weight: bold">=</span><span style="color: #000000">FORMAT</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000">datefmt</span><span style="color: #ce5c00; font-weight: bold">=</span><span style="color: #4e9a06">&#39;%d.%m.%Y %H:%M:%S&#39;</span><span style="color: #000000; font-weight: bold">)</span>
 +
 +
 +
    <span style="color: #000000">parser</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">OptionParser</span><span style="color: #000000; font-weight: bold">()</span>
 +
    <span style="color: #000000">parser</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">add_option</span><span style="color: #000000; font-weight: bold">("-s", "--system", action="store_true", dest="system",
 +
                      default=False, help="use the system bus instead of the session bus")</span>
 +
    <span style="color: #000000">parser</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">add_option</span><span style="color: #000000; font-weight: bold">("-d", "--debug", action="store_true", dest="debug",
 +
                      default=False)</span>
 +
 +
    <span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">options</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000">args</span><span style="color: #000000; font-weight: bold">)</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">parser</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">parse_args</span><span style="color: #000000; font-weight: bold">()</span>
 +
 +
    <span style="color: #000000">dbus</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">mainloop</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">glib</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">DBusGMainLoop</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">set_as_default</span><span style="color: #ce5c00; font-weight: bold">=</span><span style="color: #3465a4">True</span><span style="color: #000000; font-weight: bold">)</span>
 +
 +
    <span style="color: #8f5902; font-style: italic"># use system bus instead of session bus if option is passed</span>
 +
    <span style="color: #204a87; font-weight: bold">if</span> <span style="color: #000000">options</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">system</span><span style="color: #000000; font-weight: bold">:</span>
 +
        <span style="color: #000000">bus</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">dbus</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">SystemBus</span><span style="color: #000000; font-weight: bold">()</span>
 +
    <span style="color: #204a87; font-weight: bold">else</span><span style="color: #000000; font-weight: bold">:</span>
 +
        <span style="color: #000000">bus</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">dbus</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">SessionBus</span><span style="color: #000000; font-weight: bold">()</span>
 +
 +
    <span style="color: #204a87; font-weight: bold">if</span> <span style="color: #000000">options</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">debug</span><span style="color: #000000; font-weight: bold">:</span>
 +
        <span style="color: #000000">debug</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #3465a4">True</span>
 +
   
 +
    <span style="color: #8f5902; font-style: italic"># Create Aseba network</span>
 +
    <span style="color: #000000">network</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">dbus</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">Interface</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">bus</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">get_object</span><span style="color: #000000; font-weight: bold">(</span>
 +
        <span style="color: #4e9a06">&#39;ch.epfl.mobots.Aseba&#39;</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #4e9a06">&#39;/&#39;</span><span style="color: #000000; font-weight: bold">),</span> <span style="color: #000000">dbus_interface</span><span style="color: #ce5c00; font-weight: bold">=</span><span style="color: #4e9a06">&#39;ch.epfl.mobots.AsebaNetwork&#39;</span><span style="color: #000000; font-weight: bold">)</span>
 +
 +
    <span style="color: #8f5902; font-style: italic"># logger.info in the terminal the name of each Aseba NOde</span>
 +
    <span style="color: #8f5902; font-style: italic">#logger.info(network.GetNodesList())</span>
 +
 +
    <span style="color: #8f5902; font-style: italic"># to sync camera:</span>
 +
    <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;cam-sync:&quot;</span><span style="color: #000000; font-weight: bold">)</span>
 +
    <span style="color: #000000">cam_sync_at_start</span><span style="color: #000000; font-weight: bold">()</span>
 +
    <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;Start logging&quot;</span><span style="color: #000000; font-weight: bold">)</span>
 +
    <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;###############################&quot;</span><span style="color: #000000; font-weight: bold">)</span>
 +
    <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;  ColiBot Thymio controller  &quot;</span><span style="color: #000000; font-weight: bold">)</span>
 +
    <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;-------------------------------&quot;</span><span style="color: #000000; font-weight: bold">)</span>
 +
    <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;...        starting:        ...&quot;</span><span style="color: #000000; font-weight: bold">)</span>
 +
 +
 +
    <span style="color: #8f5902; font-style: italic"># create dgram udp socket</span>
 +
    <span style="color: #204a87; font-weight: bold">try</span><span style="color: #000000; font-weight: bold">:</span>
 +
        <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&quot;Creating socket.&quot;</span><span style="color: #000000; font-weight: bold">)</span>
 +
        <span style="color: #000000">s</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">socket</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">socket</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #000000">socket</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">AF_INET</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000">socket</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">SOCK_DGRAM</span><span style="color: #000000; font-weight: bold">)</span>
 +
    <span style="color: #204a87; font-weight: bold">except</span> <span style="color: #000000">socket</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">error</span><span style="color: #000000; font-weight: bold">:</span>
 +
        <span style="color: #000000">logger</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">info</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #4e9a06">&#39;Failed to create socket&#39;</span><span style="color: #000000; font-weight: bold">)</span>
 +
        <span style="color: #000000">sys</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">exit</span><span style="color: #000000; font-weight: bold">()</span>
 +
 +
 +
 +
    <span style="color: #8f5902; font-style: italic"># GObject loop</span>
 +
    <span style="color: #8f5902; font-style: italic"># logger.info &#39;starting loop&#39;</span>
 +
    <span style="color: #000000">loop</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">gobject</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">MainLoop</span><span style="color: #000000; font-weight: bold">()</span>
 +
    <span style="color: #8f5902; font-style: italic"># call the callback of control flow</span>
 +
    <span style="color: #000000">handle</span> <span style="color: #ce5c00; font-weight: bold">=</span> <span style="color: #000000">gobject</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">timeout_add</span><span style="color: #000000; font-weight: bold">(</span><span style="color: #0000cf; font-weight: bold">1000</span><span style="color: #000000; font-weight: bold">,</span> <span style="color: #000000">control</span><span style="color: #000000; font-weight: bold">)</span>  <span style="color: #8f5902; font-style: italic"># every 0.1 sec</span>
 +
    <span style="color: #000000">loop</span><span style="color: #ce5c00; font-weight: bold">.</span><span style="color: #000000">run</span><span style="color: #000000; font-weight: bold">()</span>
 +
</pre></td></tr></table></div>
 +
 +
 +
 +
 
<p/>
 
<p/>
  
  
 
{{NAWI_Graz:footer}}
 
{{NAWI_Graz:footer}}

Revision as of 23:12, 30 October 2017

The robot

The robot, a Thymio II, is a robot designed for educational purposes. All components (hardware and software) are open source. The robot consists of more than 20 sensors, many LEDs, two motors, a microphone and a loudspeaker. To make the already very powerful tool even more superior we connected a raspberry pi to it, allowing us to not only take full control over every aspect of the robot, but also using python as a programming language for the device.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/python2
import dbus
import dbus.mainloop.glib
import gobject
import sys
import socket
import time
import logging

#from w1thermsensor import W1ThermSensor

from optparse import OptionParser

# proximity sensor readaout values
proxSensorsVal = [0, 0, 0, 0, 0]

# server ip and port
host = '192.168.43.150'
port = 4343

logger = logging.getLogger('colibot')

debug = False
counter = 0

SENSOR_THRESHOLD = 3300
DRIVE_DIST = 400
TURN_DIST = 180

def dbus_reply():
    pass


def dbus_error(e):
    # logger.info 'error %s'
    # logger.info str(e)
    pass


def control():
    # get the values of the sensors
    network.GetVariable("thymio-II", "prox.horizontal",
                        reply_handler=get_variables_reply, error_handler=get_variables_error)

    # send proximity sensor reading to colis
    reply = coli_com()

    if reply == 0:
        for i in range(0, DRIVE_DIST):
            network.SetVariable("thymio-II", "motor.left.target", [100])
            network.SetVariable("thymio-II", "motor.right.target", [100])

    if reply == 1:
        for i in range(0, TURN_DIST):
            network.SetVariable("thymio-II", "motor.left.target", [100])
            network.SetVariable("thymio-II", "motor.right.target", [-100])

        network.SetVariable("thymio-II", "motor.left.target", [0])
        network.SetVariable("thymio-II", "motor.right.target", [0])

    return True


def get_variables_reply(r):
    global proxSensorsVal
    proxSensorsVal = r


def get_variables_error(e):
    logger.info('error: ')
    logger.info(str(e))
    loop.quit()


def coli_com():
    global debug

    sum_prox = proxSensorsVal[0] + proxSensorsVal[1] + proxSensorsVal[2] + proxSensorsVal[3] + proxSensorsVal[4]
    sides = proxSensorsVal[0] + proxSensorsVal[4]

    global counter
    counter += 1
    if (counter % 2):
    # print("dirrrrty hack to take every second measurement only")
        return 6

    logger.info('# # # # # # # # # # # # # # # # #')
    logger.info('                         cycle {}'.format(counter/2))
    
    logger.info("Proximity sensor sum: " + str(sum_prox))


    try:
        if (sum_prox > SENSOR_THRESHOLD):
            logger.info("Obstacle detected!")
            if not debug:
                # send data to server
                s.sendto(str.encode('obstacle'), (host, port))
                logger.info("Sent 'obstacle' to reactor.")
            if debug == True:
                reply = 'turn' 
        else:
            logger.info("No obstacle")
            if not  debug:
                # send data to server
                s.sendto(str.encode('clear'), (host, port))
                logger.info("Sent 'clear' to reactor.")
            if debug == True:
                reply = 'drive' 


        if debug == False:
            # receive data
            d = s.recvfrom(1024)
            reply = d[0]
            addr = d[1]

        logger.info('Server reply : ' + str(reply))
#        logger.info('Executing ...')
        logger.info('                                #')
        logger.info('# # # # # # # # # # # # # # # # #')
        logger.info('')

        if (reply.decode("utf-8") == 'turn'):
#            s.sendto(str.encode('thanks'), (host, port))
            return 1
        if (reply.decode("utf-8") == 'drive'):
#            s.sendto(str.encode('thanks'), (host, port))            
            return 0
        else:
            return 6

    except socket.error as msg:
        logger.info('Error Code : ' + str(msg[0]) + ' Message ' + msg[1])


def cam_sync_at_start():
    network.SetVariable("thymio-II", "motor.left.target", [300])
    network.SetVariable("thymio-II", "motor.right.target", [300])
    time.sleep(1)
    network.SetVariable("thymio-II", "motor.left.target", [0])
    network.SetVariable("thymio-II", "motor.right.target", [0])    
    network.SetVariable("thymio-II", "motor.left.target", [-300])
    network.SetVariable("thymio-II", "motor.right.target", [-300])
    time.sleep(1)
    network.SetVariable("thymio-II", "motor.left.target", [0])
    network.SetVariable("thymio-II", "motor.right.target", [0]) 

if __name__ == '__main__':
    # format
    FORMAT = "[%(asctime)s ]   %(message)s"
    fileHandler = logging.FileHandler('log.txt')
    fileHandler.setFormatter(logging.Formatter(FORMAT))
    logger.addHandler(fileHandler)
    logging.basicConfig(level=20, format=FORMAT, datefmt='%d.%m.%Y %H:%M:%S')
	

    parser = OptionParser()
    parser.add_option("-s", "--system", action="store_true", dest="system",
                      default=False, help="use the system bus instead of the session bus")
    parser.add_option("-d", "--debug", action="store_true", dest="debug",
                      default=False)

    (options, args) = parser.parse_args()	
	
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

    # use system bus instead of session bus if option is passed
    if options.system:
        bus = dbus.SystemBus()
    else:
        bus = dbus.SessionBus()

    if options.debug:
        debug = True
    
    # Create Aseba network
    network = dbus.Interface(bus.get_object(
        'ch.epfl.mobots.Aseba', '/'), dbus_interface='ch.epfl.mobots.AsebaNetwork')

    # logger.info in the terminal the name of each Aseba NOde
    #logger.info(network.GetNodesList())

    # to sync camera:
    logger.info("cam-sync:")
    cam_sync_at_start()
    logger.info("Start logging")
    logger.info("###############################")
    logger.info("   ColiBot Thymio controller   ")
    logger.info("-------------------------------")
    logger.info("...        starting:        ...")


    # create dgram udp socket
    try:
        logger.info("Creating socket.")
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    except socket.error:
        logger.info('Failed to create socket')
        sys.exit()



    # GObject loop
    # logger.info 'starting loop'
    loop = gobject.MainLoop()
    # call the callback of control flow
    handle = gobject.timeout_add(1000, control)  # every 0.1 sec
    loop.run()

{{NAWI_Graz:footer}}