{"id":610,"date":"2012-04-15T11:24:26","date_gmt":"2012-04-15T09:24:26","guid":{"rendered":"http:\/\/www.capri-soft.de\/blog\/?p=610"},"modified":"2012-05-03T10:46:32","modified_gmt":"2012-05-03T08:46:32","slug":"610","status":"publish","type":"post","link":"https:\/\/www.capri-soft.de\/blog\/?p=610","title":{"rendered":"Arduino Roboter mit USB Host Shield (1)"},"content":{"rendered":"<h1>Aufgabenstellung<\/h1>\n<p>Es wird eine L\u00f6sung ben\u00f6tigt, um einen Arduino Microcontroller als USB Host zu betreiben, welcher selber die Stromversorgung der angeschlossenen USB-Ger\u00e4te \u00fcbernimmt. F\u00fcr die einfache Erweiterung des Bausatzes existieren sogenannte &#8222;Shields&#8220;, welche eine preiswerte L\u00f6sung ohne die Notwendigkeit eines eigenen Platinenentwurfs zur Verf\u00fcgung stellen.<\/p>\n<h1>Ansatz<\/h1>\n<p>Durch die Verwendung des USB Host Shields von DFRobot.com l\u00e4sst sich \u00fcber eine Aufsteckvariante \u00fcber die SPI Schnittstelle und existierender, fertiger Bibliotheken eine solche Konfiguration schnell umsetzen.<\/p>\n<p>Im Nachfolgebeispiel wird der bereits existierende Roboter (siehe Kategorie Mikrocontroller) mit einem Arduino Duemilanove (Arduino UNO kombatibel) und dem USB Host Shield ausgestattet. Dieser wird \u00fcber eine USB Tastatur gesteuert.<\/p>\n<h1>L\u00f6sung<\/h1>\n<h2>Arduino Duemilanove mit USB Host Shield (Pan&#038;Tilt Servos, Tamiya Twin Motor Gear Box&#8230;)<\/h2>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"http:\/\/www.youtube.com\/embed\/XanM2o_Q4og\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<h2>Code<\/h2>\n<p>Der Code benutzt die downloadbare Bibliothek des USB Host Shields, welche sich nach dem Kopieren in die Arduino IDE verwenden l\u00e4sst. Dar\u00fcber hinaus wird der Pan and Tilt Servo \u00fcber die Servo-Bibliothek gesteuert. <\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n#include &lt;avr\/pgmspace.h&gt;\r\n\r\n#include &lt;avrpins.h&gt;\r\n#include &lt;max3421e.h&gt;\r\n#include &lt;usbhost.h&gt;\r\n#include &lt;usb_ch9.h&gt;\r\n#include &lt;Usb.h&gt;\r\n#include &lt;usbhub.h&gt;\r\n#include &lt;avr\/pgmspace.h&gt;\r\n#include &lt;address.h&gt;\r\n#include &lt;hidboot.h&gt;\r\n\r\n#include &lt;printhex.h&gt;\r\n#include &lt;message.h&gt;\r\n#include &lt;hexdump.h&gt;\r\n#include &lt;parsetools.h&gt;\r\n\r\n#include &lt;Servo.h&gt; \r\n\r\nint E1 = 6; \/\/M1 Speed Control\r\nint E2 = 5; \/\/M2 Speed Control\r\nint M1 = 8; \/\/M1 Direction Control\r\nint M2 = 7; \/\/M2 Direction Control\r\n\r\n \r\nServo myservo;  \/\/ create servo object to control a servo \r\nServo myservo2;  \/\/ create servo object to control a servo \r\nint horizontal=64;\r\nint vertical=64;\r\n\r\nboolean servoLeft = false;\r\nboolean servoRight = false;\r\nboolean servoUp = false;\r\nboolean servoDown = false;\r\n\r\n\r\nclass KbdRptParser : public KeyboardReportParser\r\n{\r\n        void PrintKey(uint8_t mod, uint8_t key);\r\n        \r\nprotected:\r\n\tvirtual void OnKeyDown\t(uint8_t mod, uint8_t key);\r\n\tvirtual void OnKeyUp\t(uint8_t mod, uint8_t key);\r\n\tvirtual void OnKeyPressed(uint8_t key);\r\n};\r\n\r\nvoid KbdRptParser::PrintKey(uint8_t m, uint8_t key)\t\r\n{\r\n    MODIFIERKEYS mod;\r\n    *((uint8_t*)&amp;mod) = m;\r\n    Serial.print((mod.bmLeftCtrl   == 1) ? &quot;C&quot; : &quot; &quot;);\r\n    Serial.print((mod.bmLeftShift  == 1) ? &quot;S&quot; : &quot; &quot;);\r\n    Serial.print((mod.bmLeftAlt    == 1) ? &quot;A&quot; : &quot; &quot;);\r\n    Serial.print((mod.bmLeftGUI    == 1) ? &quot;G&quot; : &quot; &quot;);\r\n    \r\n    Serial.print(&quot; &gt;&quot;);\r\n    PrintHex&lt;uint8_t&gt;(key);\r\n    Serial.print(&quot;&lt; &quot;);\r\n\r\n    Serial.print((mod.bmRightCtrl   == 1) ? &quot;C&quot; : &quot; &quot;);\r\n    Serial.print((mod.bmRightShift  == 1) ? &quot;S&quot; : &quot; &quot;);\r\n    Serial.print((mod.bmRightAlt    == 1) ? &quot;A&quot; : &quot; &quot;);\r\n    Serial.println((mod.bmRightGUI    == 1) ? &quot;G&quot; : &quot; &quot;);\r\n};\r\n\r\nvoid KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)\t\r\n{\r\n    Serial.print(&quot;DN &quot;);\r\n    PrintKey(mod, key);\r\n    uint8_t c = OemToAscii(mod, key);\r\n    \r\n    \r\n    int leftspeed = 255; \/\/255 is maximum speed\r\n    int rightspeed = 255;      \r\n    \r\n    switch(key)\r\n    {\r\n       case 0x50:\r\n       {\r\n          horizontal-=10;\r\n       }\r\n       break;\r\n       case 0x4F:\r\n       {\r\n          horizontal+=10;\r\n       }\r\n       break;       \r\n       case 0x52:\r\n       {\r\n          vertical-=10;\r\n       }\r\n       break;       \r\n       case 0x51:\r\n       {\r\n          vertical+=10;\r\n       }\r\n       break;             \r\n       case 0x5C:\r\n       {\r\n           left (leftspeed,rightspeed);\r\n       }\r\n       break;\r\n       case 0x5E:\r\n       {\r\n           right (leftspeed,rightspeed);\r\n       }\r\n       break;\r\n       case 0x60:\r\n       {\r\n           forward (leftspeed,rightspeed);\r\n       }\r\n       break;\r\n       case 0x5A:\r\n       {\r\n           reverse (leftspeed,rightspeed);\r\n       }\r\n       break;   \r\n       case 0x5D:\r\n       {\r\n           stop();\r\n       }\r\n       break;          \r\n    }  \r\n    \r\n    if (horizontal &lt;= 1 || horizontal &gt;=128) horizontal=64;    \r\n    if (vertical &lt;= 1 || vertical &gt;=128) vertical=64;    \r\n    \r\n    if (c)\r\n        OnKeyPressed(c);\r\n}\r\n\r\nvoid KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)\t\r\n{\r\n    Serial.print(&quot;UP &quot;);\r\n    \r\n    \r\n  \r\n    \r\n    PrintKey(mod, key);\r\n}\r\n\r\nvoid KbdRptParser::OnKeyPressed(uint8_t key)\t\r\n{\r\n    Serial.print(&quot;ASCII: &quot;);\r\n    Serial.println((char)key);\r\n};\r\n\r\nUSB     Usb;\r\n\/\/USBHub     Hub(&amp;Usb);\r\nHIDBoot&lt;HID_PROTOCOL_KEYBOARD&gt;    Keyboard(&amp;Usb);\r\n\r\nuint32_t next_time;\r\n\r\nKbdRptParser Prs;\r\n\r\nvoid setup()\r\n{\r\n    \/\/ attaches the servo on pin 2 to the servo object \r\n    myservo.attach(2);  \r\n\r\n    \/\/ attaches the servo on pin 3 to the servo object \r\n    myservo2.attach(3);  \r\n  \r\n    Serial.begin( 9600 );\r\n    Serial.println(&quot;Start&quot;);\r\n\r\n    if (Usb.Init() == -1)\r\n        Serial.println(&quot;OSC did not start.&quot;);\r\n      \r\n    delay( 200 );\r\n  \r\n    next_time = millis() + 5000;\r\n  \r\n    Keyboard.SetReportParser(0, (HIDReportParser*)&amp;Prs);\r\n}\r\n\r\n\r\n\r\nvoid loop()\r\n{\r\n  \/*\r\n    if(servoLeft) horizontal-=10;\r\n    if(servoRight) horizontal+=10;\r\n    if(servoUp) vertical-=10;\r\n    if(servoDown) vertical+=10;    \r\n    *\/\r\n    \r\n\/\/ sets the servo position according to the scaled value \r\n    myservo.write(horizontal);                \r\n\r\n\/\/ sets the servo position according to the scaled value  \r\n    myservo2.write(vertical);                  \r\n    \r\n    Serial.println(&quot;Horizontal: &quot;+horizontal );\r\n    Serial.println(&quot;Vertical: &quot;+vertical );\r\n      \r\n  \r\n    Usb.Task();\r\n}\r\n\r\nvoid stop(void) \/\/Stop\r\n{\r\n  digitalWrite(E1,LOW);\r\n  digitalWrite(E2,LOW);\r\n}\r\nvoid forward(char a,char b)\r\n{\r\n  analogWrite (E1,a);\r\n  digitalWrite(M1,LOW);\r\n  analogWrite (E2,b);\r\n  digitalWrite(M2,LOW);\r\n}\r\nvoid reverse (char a,char b)\r\n{\r\n  analogWrite (E1,a);\r\n  digitalWrite(M1,HIGH);\r\n  analogWrite (E2,b);\r\n  digitalWrite(M2,HIGH);\r\n}\r\n\r\nvoid left (char a,char b)\r\n{\r\n  analogWrite (E1,a);\r\n  digitalWrite(M1,HIGH);\r\n  analogWrite (E2,b);\r\n  digitalWrite(M2,LOW);\r\n}\r\n\r\nvoid right (char a,char b)\r\n{\r\n  analogWrite (E1,a);\r\n  digitalWrite(M1,LOW);\r\n  analogWrite (E2,b);\r\n  digitalWrite(M2,HIGH);\r\n}\r\n\r\n<\/pre>\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https%3A%2F%2Fwww.capri-soft.de%2Fblog%2F%3Fp%3D610&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px;margin-top:5px;\"><\/iframe>","protected":false},"excerpt":{"rendered":"<p>Aufgabenstellung Es wird eine L\u00f6sung ben\u00f6tigt, um einen Arduino Microcontroller als USB Host zu betreiben, welcher selber die Stromversorgung der angeschlossenen USB-Ger\u00e4te \u00fcbernimmt. F\u00fcr die einfache Erweiterung des Bausatzes existieren sogenannte &#8222;Shields&#8220;, welche eine preiswerte L\u00f6sung ohne die Notwendigkeit eines eigenen Platinenentwurfs zur Verf\u00fcgung stellen. Ansatz Durch die Verwendung des USB Host Shields von DFRobot.com &hellip; <a href=\"https:\/\/www.capri-soft.de\/blog\/?p=610\" class=\"more-link\"><span class=\"screen-reader-text\">Arduino Roboter mit USB Host Shield (1)<\/span> weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[],"class_list":["post-610","post","type-post","status-publish","format-standard","hentry","category-mikrocontroller"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/s4yGeN-610","jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/610","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=610"}],"version-history":[{"count":7,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/610\/revisions"}],"predecessor-version":[{"id":624,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/610\/revisions\/624"}],"wp:attachment":[{"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.capri-soft.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}