{"id":832,"date":"2014-11-25T19:03:25","date_gmt":"2014-11-25T19:03:25","guid":{"rendered":"http:\/\/ofalcao.pt\/blog\/?p=832"},"modified":"2016-06-14T15:30:12","modified_gmt":"2016-06-14T14:30:12","slug":"lego-wedo-with-ev3-kernel-support","status":"publish","type":"post","link":"https:\/\/ofalcao.pt\/blog\/2014\/lego-wedo-with-ev3-kernel-support","title":{"rendered":"LEGO WeDO with EV3 &#8211; kernel support"},"content":{"rendered":"<p>Last night the ev3dev people released <a href=\"http:\/\/www.ev3dev.org\/docs\/drivers\/wedo-hub\/\">a Linux driver for WeDo<\/a>. For now it is just for ev3dev so it works only in Mindstorms EV3 but hopefully it will get to other distributions and it will allow anyone with a Raspberry Pi or an Ubuntu laptop (like me) to use WeDo without python WeDo or WeDoMore libraries &#8211; just a shell is enough!<\/p>\n<p>We need a ev3dev with kernel 3.16.1-7 or above:<\/p>\n<pre>root@ev3dev:~# uname -a\r\nLinux ev3dev 3.16.1-7-ev3dev #2 PREEMPT Tue Nov 25 11:24:38 CST 2014 armv5tejl GNU\/Linux<\/pre>\n<p>If your kernel is older, just<\/p>\n<pre>apt-get update\r\napt-get dist-upgrade\r\nreboot<\/pre>\n<p>After reboot, creat a udev rule for WeDo &#8211; it will prevent the kernel to bind it to the usbhid driver, binding it instead to legowedo driver:<\/p>\n<pre>nano \/etc\/udev\/rules.d\/80-wedo.rules\r\n\r\nSUBSYSTEM==\"usb\", ATTRS{idVendor}==\"0694\", ATTRS{idProduct}==\"0003\" ACTION==\"add\", \\\r\n\u00a0\u00a0\u00a0 RUN+=\"\/bin\/sh -c 'echo $kernel &gt; \/sys\/bus\/usb\/drivers\/usbhid\/unbind;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo $kernel &gt; \/sys\/bus\/usb\/drivers\/legowedo\/bind'\"<\/pre>\n<p>Now just reboot or refresh the udev rules by<\/p>\n<pre>udevadm control --reload-rules<\/pre>\n<p>If you plug the LEGO WeDo USB Hub and check dmesg it wil show<\/p>\n<pre>[ 1222.200538] usb 1-1.2: new low-speed USB device number 5 using ohci\r\n[ 1222.370828] hid-generic 0003:0694:0003.0002: hidraw0: USB HID v1.10 Device [LEGO USB Hub V1.00] on usb-ohci.0-1.2\/input0\r\n[ 1222.691293] wedo hub0: Bound 'hub0' to 'usb1-1.2:1.0'<\/pre>\n<p>We now have a new device &#8216;hub0&#8217;:<\/p>\n<pre>root@ev3dev:~# ls \/sys\/bus\/wedo\/devices\/ -la\r\nlrwxrwxrwx 1 root root 0 Nov 25 06:21 hub0 -&gt; ..\/..\/..\/devices\/platform\/ohci.0\/usb1\/1-1\/1-1.2\/1-1.2:1.0\/hub0<\/pre>\n<p>with the following properties:<\/p>\n<pre>root@ev3dev:~# ls \/sys\/bus\/wedo\/devices\/hub0\r\nclear_error\u00a0 error\u00a0 high_power\u00a0 port0\u00a0 port1\u00a0 power\u00a0 reset\u00a0 shut_down\u00a0 subsystem\u00a0 uevent\u00a0 voltage<\/pre>\n<p>So we may check if the USB port gives enough current for a motor:<\/p>\n<pre>root@ev3dev:~# cat \/sys\/bus\/wedo\/devices\/hub0\/high_power \r\n1<\/pre>\n<p>or check for the USB power quality (in mV):<\/p>\n<pre>root@ev3dev:~# cat \/sys\/bus\/wedo\/devices\/hub0\/voltage \r\n5145<\/pre>\n<p>Now if we plug a LEGO Power Functions motor to one of the ports it will immediately be recognized by the kernel:<\/p>\n<pre>[ 1736.348469] dc-motor motor0: Bound to device 'port0'<\/pre>\n<p>and the motor is seen by the kernel as dc-motor like if we were using an NXT\/EV3 to Power Functions or RCX adapter:<\/p>\n<pre>root@ev3dev:~# ls \/sys\/class\/dc-motor -la\r\nlrwxrwxrwx\u00a0 1 root root 0 Nov 25 06:30 motor0 -&gt; ..\/..\/devices\/platform\/ohci.0\/usb1\/1-1\/1-1.2\/1-1.2:1.0\/hub0\/port0\/dc-motor\/motor0<\/pre>\n<pre>root@ev3dev:~# ls \/sys\/class\/dc-motor\/motor0\r\ncommand\u00a0\u00a0 device\u00a0\u00a0\u00a0\u00a0\u00a0 duty_cycle_sp\u00a0 polarity\u00a0\u00a0 power\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ramp_up_ms\u00a0 uevent\r\ncommands\u00a0 duty_cycle\u00a0 name\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 port_name\u00a0 ramp_down_ms\u00a0 subsystem<\/pre>\n<p>So we can make it spin with 35% duty cycle:<\/p>\n<pre>echo 35 &gt; \/sys\/class\/dc-motor\/motor0\/duty_cycle_sp<\/pre>\n<p>And it&#8217;s the same for WeDo sensors &#8211; as I just have a tilt sensor, lets plug it:<\/p>\n<pre>root@ev3dev:~# ls \/sys\/class\/msensor\/ -la\r\nlrwxrwxrwx\u00a0 1 root root 0 Nov 25 06:40 sensor0 -&gt; ..\/..\/devices\/platform\/ohci.0\/usb1\/1-1\/1-1.2\/1-1.2:1.0\/hub0\/port1\/msensor\/sensor0<\/pre>\n<pre>root@ev3dev:~# ls \/sys\/class\/msensor\/sensor0\r\naddress\u00a0\u00a0 bin_data_format\u00a0 commands\u00a0 dp\u00a0\u00a0\u00a0 modes\u00a0 num_values\u00a0 power\u00a0\u00a0\u00a0\u00a0\u00a0 uevent\u00a0 value0\u00a0 value2\u00a0 value4\u00a0 value6\r\nbin_data\u00a0 command\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 device\u00a0\u00a0\u00a0 mode\u00a0 name\u00a0\u00a0 port_name\u00a0\u00a0 subsystem\u00a0 units\u00a0\u00a0 value1\u00a0 value3\u00a0 value5\u00a0 value7<\/pre>\n<p>The <a href=\"http:\/\/www.ev3dev.org\/docs\/sensors\/lego-wedo-tilt-sensor\/\">tilt sensor driver<\/a> has 3 operation modes:<\/p>\n<pre>root@ev3dev:~# cat \/sys\/class\/msensor\/sensor0\/modes\r\nTILT TILT-AXIS RAW<\/pre>\n<p>by default it operates in TILT mode:<\/p>\n<pre>root@ev3dev:~# cat \/sys\/class\/msensor\/sensor0\/mode\r\nTILT<\/pre>\n<p>This is the expected operation mode with LEGO WeDo software: it returns 0 when flat and 1 to 4 when tilted.<\/p>\n<p>TILT-AXIS mode returns 3 values: -1 or +1 when tilted, one value for each axle, the third value being 1 whenever it&#8217;s not flat.<\/p>\n<p>And RAW mode returns the original analogic value read at the C2 pin. Almost useless for a tilt sensor but VERY usefull if we want to make our own sensors (more of it in my next post):<\/p>\n<pre>root@ev3dev:~# echo RAW &gt; \/sys\/class\/msensor\/sensor0\/mode\r\nroot@ev3dev:~# cat \/sys\/class\/msensor\/sensor0\/value0\r\n179<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last night the ev3dev people released a Linux driver for WeDo. For now it is just for ev3dev so it works only in Mindstorms EV3 but hopefully it will get to other distributions and it will allow anyone with a Raspberry Pi or an Ubuntu laptop (like me) to use WeDo without python WeDo or &hellip; <a href=\"https:\/\/ofalcao.pt\/blog\/2014\/lego-wedo-with-ev3-kernel-support\" class=\"more-link\">Continuar a ler<span class=\"screen-reader-text\"> &#8220;LEGO WeDO with EV3 &#8211; kernel support&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[251],"series":[],"class_list":["post-832","post","type-post","status-publish","format-standard","hentry","category-sem-categoria","tag-wedo-pt"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2Mhyv-dq","_links":{"self":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/832","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/comments?post=832"}],"version-history":[{"count":0,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/posts\/832\/revisions"}],"wp:attachment":[{"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/media?parent=832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/categories?post=832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/tags?post=832"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/ofalcao.pt\/blog\/wp-json\/wp\/v2\/series?post=832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}