@@ -31,21 +31,21 @@ If ev3dev recognizes our Bluetooth 4.0 dongle, we'll have two hci devices - the
3131internal bluetooth and the new USB one:
3232
3333 robot@ev3dev:~# hciconfig -a
34- hci1: Type: BR/EDR Bus: UART
35- BD Address: 00:17:EC:48:44:6B ACL MTU: 1021:4 SCO MTU: 180:4
36- UP RUNNING
37- RX bytes:863 acl:0 sco:0 events:32 errors:0
38- TX bytes:1396 acl:0 sco:0 commands:32 errors:0
39- Features: 0xff 0xff 0x2d 0xfe 0x9b 0xff 0x79 0x83
34+ hci1: Type: BR/EDR Bus: UART
35+ BD Address: 24:71:89:28:F0:D3 ACL MTU: 1021:4 SCO MTU: 180:4
36+ UP RUNNING
37+ RX bytes:1139 acl:0 sco:0 events:54 errors:0
38+ TX bytes:2011 acl:0 sco:0 commands:54 errors:0
39+ Features: 0xff 0xfe 0x2d 0xfe 0x9b 0xff 0x79 0x87
4040 Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
41- Link policy: RSWITCH HOLD SNIFF PARK
41+ Link policy: RSWITCH HOLD SNIFF
4242 Link mode: SLAVE ACCEPT
4343 Name: 'ev3dev'
44- Class: 0x000000
44+ Class: 0x000100
4545 Service Classes: Unspecified
46- Device Class: Miscellaneous,
47- HCI Version: 2.1 (0x4 ) Revision: 0x0
48- LMP Version: 2.1 (0x4 ) Subversion: 0x191f
46+ Device Class: Computer, Uncategorized
47+ HCI Version: 4.0 (0x6 ) Revision: 0x0
48+ LMP Version: 4.0 (0x6 ) Subversion: 0x1b55
4949 Manufacturer: Texas Instruments Inc. (13)
5050
5151 hci0: Type: BR/EDR Bus: USB
@@ -65,10 +65,19 @@ internal bluetooth and the new USB one:
6565 LMP Version: 4.0 (0x6) Subversion: 0x220e
6666 Manufacturer: Broadcom Corporation (15)
6767
68- In the above situation, ` hci0 ` is our Bluetooth 4.0 BLE device (note "BUS: USB" and
69- "HCI version: 4.0"). If you don't see status "UP RUNNING" you need to activate
70- Bluetooth first (one easy way is using the Brickman User Interface: choose "Wireless
71- and Networks" at the main screen then "Bluetooth")
68+ In the above situation, ` hci0 ` is our Bluetooth 4.0 BLE device (note "BUS: USB").
69+ Initial releases of EV3 bricks will show older HCI/LMP versions for the internal
70+ USB device (` hci1 ` here) but it always be shown with ` BUS: UART ` . Although the
71+ newer releases show a HCI/LMP 4.0 version this new chipset doesn't include the
72+ Bluetooth Low Energy subset of the BT 4.0 standard.
73+
74+ If you don't see status "UP RUNNING" you will need to activate Bluetooth
75+ first - one easy way is using the Brickman User Interface: choose "Wireless and
76+ Networks" at the main screen then "Bluetooth" but you can also run
77+
78+ connmanctl enable bluetooth
79+
80+ from a command line as well.
7281
7382We also need a recent bluez version for BLE support. Most recent builds of ev3dev
7483will have it already (checked with "ev3-ev3dev-jessie-2015-12-30.img.xz").
@@ -101,82 +110,43 @@ then stop:
101110(You need to run this script with sudo, unless you already have root previleges)
102111
103112We see that it uses the gatttool command to send a sequence of 4 bytes to one specific
104- handler (0x003d). The WeDo 2.0 has several handlers but until LEGO Education releases
105- the promised SDK this is the only handler we "know" how to use:
106-
107- This is meaning of those 4 bytes:
113+ handler (0x003d). This is the meaning of those 4 bytes:
108114* the first byte defines the port (01 or 02)
109115* the second byte defines the command (01 = motor speed)
110116* the third byte defines the length of the following argument(s) (01)
111- * the fouth byte is the argument, in this case the speed percentage
117+ * the fourth byte is the argument, in this case the speed percentage
112118
113119To spin in one direction we send a positive value from 1 to 100 (or 01 to 64 in
114120hexadecimal).
115121To spin in the opposite direction we send a "negative" value from
116122255 to 156 (or FF to 9C in hexadecimal).
117123To stop the motor we set the speed as zero (00).
118- Please note that for small speed values (less than 20%) the motor will not respond.
124+ Please note that for small speed values (less than 20%) the motor will not respond,
125+ this is normal as we're not giving enough power to overcome it's inertia.
119126
120127
121128## Python example
122129
123130To use pyhton with the WeDo 2.0 we need a BLE library. Unfortunately BLE
124131support in python is still quite imature but there is at least one library that
125- works in ev3dev - [ gattlib] ( https://bitbucket.org/OscarAcena/pygattlib )
126-
127- sudo apt-get install pkg-config libboost-python-dev libboost-thread-dev \
128- libbluetooth-dev libglib2.0-dev python-dev
129-
130- sudo pip install gattlib
131-
132- This library is also used as an extension for a more known library, [ pybluez] ( https://pypi.python.org/pypi/PyBluez )
133- so if you want a library for both bluetooth "Classic" and BLE this would be better:
134-
135- pip install pybluez
136- pip install pybluez[ble]
132+ works in ev3dev - [ gattlib] ( https://bitbucket.org/OscarAcena/pygattlib ) .
137133
138- Unfortunately I couldn't make it work in my ev3dev system.
134+ Since the beginning of 2017 (snapshot-ev3dev-jessie-ev3-generic-2017-01-16) this
135+ library is already included in the ev3dev image. You can check the version of the
136+ installed package with:
139137
140- Please note that it takes ** a lot** of memory and around 2 hours to install gattlib.
141- After some failures ("virtual memory exhausted: Cannot allocate memory") I finally
142- succeeded extending my ev3dev swapfile to almost 1 GB (please first check if you have
143- enough free space in your SD card):
138+ dpkg -s python3-gattlib | grep "Version:"
139+ Version: 0.20150805-1ev3dev1
144140
145- robot@ev3dev:~$ sudo dd if=/dev/zero of=/swapfile1 bs=1024 count=917504
146- 917504+0 records in
147- 917504+0 records out
148- 939524096 bytes (940 MB) copied, 442.332 s, 2.1 MB/s
141+ If version is older (or nonexistent) you should update/install it:
149142
150- it will take 5 to 10 minutes to allocate space for such a big file
143+ sudo apt update
144+ sudo apt install python3-gattlib
151145
152- robot@ev3dev:~$ sudo mkswap /swapfile1
153- Setting up swapspace version 1, size = 917500 KiB
154- no label, UUID=55fcb430-451b-4699-955c-5754bf65999b
146+ This short python script makes the motor spin 2 seconds in each direction then stop:
155147
156- robot@ev3dev:~$ sudo swapon /swapfile1
157- swapon: /swapfile1: insecure permissions 0644, 0600 suggested.
158-
159- this is a temporary measure so we'll skip security warning
160-
161- robot@ev3dev:~$ sudo swapon -s
162- Filename Type Size Used Priority
163- /dev/zram0 partition 98300 8188 16383
164- /swapfile1 file 917500 0 -1
165-
166- We don't want to use the swapfile in memory (it will overflow) so we disable it:
167-
168- sudo systemctl stop zram_swap.service
169-
170- After installation completes we reset the swapfile configuration:
171-
172- sudo systemctl start zram_swap.service
173- sudo swapoff /swapfile1
174- sudo rm /swapfile1
175-
176- This short python script makes the motor spin 2 second in each direction then stop:
177-
178- {% highlight python %}
179- #!/usr/bin/python
148+ ``` python
149+ # !/usr/bin/env python3
180150from gattlib import GATTRequester
181151from time import sleep
182152
@@ -186,7 +156,7 @@ sleep(2)
186156req.write_by_handle(0x 3d , " \x01\x01\x01\x9C " )
187157sleep(2 )
188158req.write_by_handle(0x 3d , " \x01\x01\x01\x00 " )
189- {% endhighlight %}
159+ ```
190160
191161## A more practical example
192162
@@ -197,8 +167,8 @@ to assure this never happens.
197167We will use an EV3 touch sensor to control the direction of the WeDo 2.0 motor and
198168periodically refresh the connection.
199169
200- {% highlight python %}
201- #!/usr/bin/python
170+ ``` python
171+ # !/usr/bin/env python3
202172
203173from ev3dev.auto import *
204174from gattlib import GATTRequester
@@ -244,15 +214,24 @@ while True:
244214 req.connect(True )
245215 print (" OK" )
246216 sleep(DELAY )
247- {% endhighlight %}
217+ ```
248218
249219This video shows the script in action:
250220{% include /util/youtube-embed.html youtube_video_id="0d3MdZuDOTc" %}
251221
252222## Final notes
253223
254- We still need to know all motor commands and option. But until LEGO releases
255- its SDK there is at list this way to extend the number of motors available to the EV3.
256- And if rumours are true, the next generation of LEGO Power Functions and Mindstorms
224+ This is just an introduction to the WeDo 2.0 BLE protocol. LEGO has released
225+ a [ "Communication Software Development Kit"] ( https://education.lego.com/en-us/support/wedo-2/developer-kits ) with some information about
226+ the several BLE services (not just the motor but also sensors, RGB Light,
227+ piezzo buzzer, battery...).
228+
229+ If rumours are true, the next generation of LEGO Power Functions and Mindstorms
257230will both share some components with the WeDo 2.0 (the Hub is already announcing
258231itself as "LEGO Power Functions 2" device) so this might be just the start.
232+
233+ Also in January 2017 LEGO announced the [ LEGO BOOST] ( https://www.lego.com/en-us/boost ) line, a kind of
234+ WeDo 2.0 update for general users (the WeDo 2.0 is intended for Educational) that will
235+ bring us a BOOST Move Hub (similar to the WeDo 2.0 Smart Hub but with 2 motors
236+ and a tilt sensor already included), a new motor and 2 new sensors. Most
237+ probably the contents of this tutorial could also be used with Boost.
0 commit comments