Rev Author Line No. Line
3596 kaklik 1 {
2 "metadata": {
3 "name": ""
4 },
5 "nbformat": 3,
6 "nbformat_minor": 0,
7 "worksheets": [
8 {
9 "cells": [
10 {
11 "cell_type": "markdown",
12 "metadata": {},
13 "source": [
14 "Uk\u00e1zka pou\u017eit\u00ed n\u00e1stroje IPython na manipulaci se senzorov\u00fdmi daty\n",
15 "=======\n",
16 "\n",
17 "P\u0159\u00edklad vyu\u017e\u00edv\u00e1 modulovou stavebnici MLAB a jej\u00ed knihovnu https://github.com/MLAB-project/MLAB-I2c-modules \n",
18 "\n",
19 "Zprovozn\u011bn\u00ed demo k\u00f3du\n",
20 "---------------------\n",
21 "\n",
22 "Nejd\u0159\u00edve zjist\u00edme zda m\u00e1me p\u0159\u00edstup pro z\u00e1pis a \u010dten\u00ed do syst\u00e9mov\u00e9ho za\u0159\u00edzen\u00ed. A jak\u00e9 \u010d\u00edslo m\u00e1 I\u00b2C sb\u011brnice na kterou m\u00e1me p\u0159ipojen\u00e1 \u010didla. \n"
23 ]
24 },
25 {
26 "cell_type": "code",
27 "collapsed": false,
28 "input": [
29 "!i2cdetect -l"
30 ],
31 "language": "python",
32 "metadata": {},
33 "outputs": [
34 {
35 "output_type": "stream",
36 "stream": "stdout",
37 "text": [
38 "i2c-0\ti2c \tintel drm CRTDDC_A \tI2C adapter\r\n",
39 "i2c-1\ti2c \tintel drm LVDSBLC_B \tI2C adapter\r\n",
40 "i2c-2\ti2c \tintel drm LVDSDDC_C \tI2C adapter\r\n",
41 "i2c-3\ti2c \tintel drm HDMIB \tI2C adapter\r\n",
42 "i2c-4\ti2c \tDPDDC-B \tI2C adapter\r\n",
43 "i2c-5\ti2c \ti2c-tiny-usb at bus 002 device 002\tI2C adapter\r\n"
44 ]
45 }
46 ],
47 "prompt_number": 1
48 },
49 {
50 "cell_type": "markdown",
51 "metadata": {},
52 "source": [
53 "Proto\u017ee pro p\u0159ipojen\u00ed \u010didel k po\u010d\u00edta\u010di pou\u017e\u00edv\u00e1me adapt\u00e9r i2c-tiny-usb. Vid\u00edme, \u017ee sb\u011brnice m\u00e1 aktu\u00e1ln\u011b ozna\u010den\u00ed nap\u0159\u00edklad i2c-8. \n",
54 "\n",
55 "V p\u0159\u00edpad\u011b, \u017ee v\u00fd\u0161e uveden\u00fd p\u0159\u00edklad vr\u00e1t\u00ed chybu, nebo pojmenov\u00e1n\u00ed \"unknown\" tak nem\u00e1me p\u0159\u00edstup k syst\u00e9mov\u00fdm rozhran\u00edm. Ten z\u00edsk\u00e1me vytvo\u0159en\u00edm souboru s n\u00e1sleduj\u00edc\u00edm obsahem ve slo\u017ece: /etc/udev/rules.d/i2c-devices.rules"
56 ]
57 },
58 {
59 "cell_type": "raw",
60 "metadata": {},
61 "source": [
62 "KERNEL==\"i2c-[0-9]*\", GROUP=\"i2c\""
63 ]
64 },
65 {
66 "cell_type": "markdown",
67 "metadata": {},
68 "source": [
69 "Toto ozna\u010den\u00ed budeme je\u0161t\u011b d\u00e1le pot\u0159ebovat, proto si jej ulo\u017e\u00edme da prom\u011bnn\u00e9. "
70 ]
71 },
72 {
73 "cell_type": "code",
74 "collapsed": false,
75 "input": [
76 "port = 5"
77 ],
78 "language": "python",
79 "metadata": {},
80 "outputs": [],
81 "prompt_number": 2
82 },
83 {
84 "cell_type": "markdown",
85 "metadata": {},
86 "source": [
87 "Budeme pokra\u010dovat na\u010dten\u00edm pot\u0159ebn\u00fdch modul\u016f pro zach\u00e1zen\u00ed s I\u00b2C sn\u00edma\u010di."
88 ]
89 },
90 {
91 "cell_type": "code",
92 "collapsed": false,
93 "input": [
94 "import time\n",
95 "import datetime\n",
96 "import sys\n",
97 "\n",
98 "from pymlab import config\n",
99 "import matplotlib.pyplot as plt\n",
100 "import numpy as np"
101 ],
102 "language": "python",
103 "metadata": {},
104 "outputs": [],
105 "prompt_number": 3
106 },
107 {
108 "cell_type": "markdown",
109 "metadata": {},
110 "source": [
111 "Nyn\u00ed si nadefinujeme strukturu p\u0159ipojen\u00ed jednotliv\u00fdch \u010didel na I\u00b2C sb\u011brnici."
112 ]
113 },
114 {
115 "cell_type": "code",
116 "collapsed": false,
117 "input": [
118 "cfg = config.Config(\n",
119 " port = port,\n",
120 " bus = [\n",
121 " {\n",
122 " \"type\": \"i2chub\",\n",
123 " \"address\": 0x72,\n",
124 " \n",
125 " \"children\": [\n",
126 " {\"name\": \"mag\", \"type\": \"mag01\" , \"channel\": 6, }, \n",
127 " ],\n",
128 " },\n",
129 " ],\n",
130 ")"
131 ],
132 "language": "python",
133 "metadata": {},
134 "outputs": [],
135 "prompt_number": 4
136 },
137 {
138 "cell_type": "markdown",
139 "metadata": {},
140 "source": [
141 "Tuto strukturu inicializujeme, aby jsme dos\u00e1hli definovan\u00e9 konfigurace \u010didel."
142 ]
143 },
144 {
145 "cell_type": "code",
146 "collapsed": false,
147 "input": [
148 "cfg.initialize()\n",
149 "mag_sensor = cfg.get_device(\"mag\")\n",
150 "time.sleep(0.5)"
151 ],
152 "language": "python",
153 "metadata": {},
154 "outputs": [],
155 "prompt_number": 5
156 },
157 {
158 "cell_type": "markdown",
159 "metadata": {},
160 "source": [
161 "Nyn\u00ed u\u017e m\u016f\u017eeme p\u0159\u00edmo komunikovat se za\u0159\u00edzen\u00edm pojmenovan\u00fdm jako gauge."
162 ]
163 },
164 {
165 "cell_type": "code",
166 "collapsed": false,
167 "input": [
168 "MEASUREMENTS = 100\n",
169 "x = np.zeros(MEASUREMENTS)\n",
170 "y = np.zeros(MEASUREMENTS)\n",
171 "z = np.zeros(MEASUREMENTS)\n",
172 "\n",
173 "\n",
174 "for n in range(MEASUREMENTS):\n",
175 " mag_sensor.route() #V p\u0159\u00edpad\u011b v\u00edce \u010didel je pot\u0159eba ke ka\u017ed\u00e9mu p\u0159ed jeho pou\u017eit\u00edm nechat vyroutovat cesutu na sb\u011brnici.\n",
176 " (x[n], y[n], z[n]) = mag_sensor.axes()\n",
177 " print(n, x[n], y[n], z[n])"
178 ],
179 "language": "python",
180 "metadata": {},
181 "outputs": [],
182 "prompt_number": "*"
183 },
184 {
185 "cell_type": "code",
186 "collapsed": false,
187 "input": [
188 "np.savez(\"data_ground\", t, p)\n",
189 "#np.savez(\"data_top\", t, p)"
190 ],
191 "language": "python",
192 "metadata": {},
193 "outputs": [],
194 "prompt_number": "*"
195 },
196 {
197 "cell_type": "code",
198 "collapsed": false,
199 "input": [
200 "amin(p)"
201 ],
202 "language": "python",
203 "metadata": {},
204 "outputs": [],
205 "prompt_number": "*"
206 },
207 {
208 "cell_type": "code",
209 "collapsed": false,
210 "input": [
211 "amax(p)"
212 ],
213 "language": "python",
214 "metadata": {},
215 "outputs": [],
216 "prompt_number": "*"
217 },
218 {
219 "cell_type": "code",
220 "collapsed": false,
221 "input": [
222 "std(p)"
223 ],
224 "language": "python",
225 "metadata": {},
226 "outputs": [],
227 "prompt_number": "*"
228 },
229 {
230 "cell_type": "code",
231 "collapsed": false,
232 "input": [
233 "plt.plot(p)"
234 ],
235 "language": "python",
236 "metadata": {},
237 "outputs": [],
238 "prompt_number": "*"
239 },
240 {
241 "cell_type": "code",
242 "collapsed": false,
243 "input": [
244 "plt.plot(t)"
245 ],
246 "language": "python",
247 "metadata": {},
248 "outputs": [],
249 "prompt_number": "*"
250 },
251 {
252 "cell_type": "code",
253 "collapsed": false,
254 "input": [],
255 "language": "python",
256 "metadata": {},
257 "outputs": [],
258 "prompt_number": "*"
259 }
260 ],
261 "metadata": {}
262 }
263 ]
264 }