Code to demonstrate how to map 2 RPC functions to command 2 servo motors via REST HTTP Get request. Pretty useful to control a webcam PAN and TILT position when using Servos in this support: http://www.coolcomponents.co.uk/catalog/product_info.php?products_id=470&osCsid=j90j5kqfegquksdbumahtmuqg5 There is also a servo.html file to test some HTML5 slider RANGE object to set the Servo position and make the Http Get request via AJAX (asynchronous JavaScript call) Note: Demo program to be used on the GeekSessionLab Talk (November 2011). http://devrendezvous.com/?lang=en
Dependencies: EthernetNetIf mbed HTTPServer Servo
servo.htm.cpp@0:fd56ae0fc8ea, 2011-10-27 (annotated)
- Committer:
- botdream
- Date:
- Thu Oct 27 23:28:15 2011 +0000
- Revision:
- 0:fd56ae0fc8ea
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
botdream | 0:fd56ae0fc8ea | 1 | /* |
botdream | 0:fd56ae0fc8ea | 2 | <html> |
botdream | 0:fd56ae0fc8ea | 3 | <head> |
botdream | 0:fd56ae0fc8ea | 4 | <script type="text/javascript"> |
botdream | 0:fd56ae0fc8ea | 5 | |
botdream | 0:fd56ae0fc8ea | 6 | var global_flag = 0; |
botdream | 0:fd56ae0fc8ea | 7 | |
botdream | 0:fd56ae0fc8ea | 8 | function sendrequest(request_uri) |
botdream | 0:fd56ae0fc8ea | 9 | { |
botdream | 0:fd56ae0fc8ea | 10 | var xmlhttp; |
botdream | 0:fd56ae0fc8ea | 11 | if(window.XMLHttpRequest) |
botdream | 0:fd56ae0fc8ea | 12 | {// code for IE7+, Firefox, Chrome, Opera, Safari |
botdream | 0:fd56ae0fc8ea | 13 | xmlhttp=new XMLHttpRequest(); |
botdream | 0:fd56ae0fc8ea | 14 | } |
botdream | 0:fd56ae0fc8ea | 15 | else |
botdream | 0:fd56ae0fc8ea | 16 | {// code for IE6, IE5 |
botdream | 0:fd56ae0fc8ea | 17 | xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); |
botdream | 0:fd56ae0fc8ea | 18 | } |
botdream | 0:fd56ae0fc8ea | 19 | xmlhttp.onreadystatechange=function() |
botdream | 0:fd56ae0fc8ea | 20 | { |
botdream | 0:fd56ae0fc8ea | 21 | if (xmlhttp.readyState==4 && xmlhttp.status==200) |
botdream | 0:fd56ae0fc8ea | 22 | { |
botdream | 0:fd56ae0fc8ea | 23 | document.getElementById("debugtxt").innerHTML=xmlhttp.responseText; |
botdream | 0:fd56ae0fc8ea | 24 | global_flag = 0; |
botdream | 0:fd56ae0fc8ea | 25 | } |
botdream | 0:fd56ae0fc8ea | 26 | } |
botdream | 0:fd56ae0fc8ea | 27 | console.log(request_uri); |
botdream | 0:fd56ae0fc8ea | 28 | xmlhttp.open("GET",request_uri,true); |
botdream | 0:fd56ae0fc8ea | 29 | xmlhttp.send(); |
botdream | 0:fd56ae0fc8ea | 30 | } |
botdream | 0:fd56ae0fc8ea | 31 | |
botdream | 0:fd56ae0fc8ea | 32 | |
botdream | 0:fd56ae0fc8ea | 33 | function setValue(event) { |
botdream | 0:fd56ae0fc8ea | 34 | |
botdream | 0:fd56ae0fc8ea | 35 | if(global_flag != 0) |
botdream | 0:fd56ae0fc8ea | 36 | return; // work in progress |
botdream | 0:fd56ae0fc8ea | 37 | |
botdream | 0:fd56ae0fc8ea | 38 | global_flag = 1; // initialize request |
botdream | 0:fd56ae0fc8ea | 39 | document.getElementById("debugtxt").innerHTML="..."; |
botdream | 0:fd56ae0fc8ea | 40 | |
botdream | 0:fd56ae0fc8ea | 41 | if(event.target.id == 'servo1') |
botdream | 0:fd56ae0fc8ea | 42 | sendrequest('./rpc/servocmd1/run%20'+event.target.value); |
botdream | 0:fd56ae0fc8ea | 43 | else if(event.target.id == 'servo2') |
botdream | 0:fd56ae0fc8ea | 44 | sendrequest('./rpc/servocmd2/run%20'+event.target.value); |
botdream | 0:fd56ae0fc8ea | 45 | } |
botdream | 0:fd56ae0fc8ea | 46 | |
botdream | 0:fd56ae0fc8ea | 47 | </script> |
botdream | 0:fd56ae0fc8ea | 48 | </head> |
botdream | 0:fd56ae0fc8ea | 49 | <body> |
botdream | 0:fd56ae0fc8ea | 50 | |
botdream | 0:fd56ae0fc8ea | 51 | <h2>Testing WebCam PAN & TILT Servos via HTTP REST (RPC) interface.</h2> |
botdream | 0:fd56ae0fc8ea | 52 | |
botdream | 0:fd56ae0fc8ea | 53 | <input id="servo1" type="range" min="0" max="100" step="5" oninput="setValue(event)" /> |
botdream | 0:fd56ae0fc8ea | 54 | <input id="servo2" type="range" min="0" max="100" step="5" oninput="setValue(event)" /> |
botdream | 0:fd56ae0fc8ea | 55 | |
botdream | 0:fd56ae0fc8ea | 56 | <!-- |
botdream | 0:fd56ae0fc8ea | 57 | <button type="button" onclick="sendrequest('./rpc/servocmd2/run%200')">Manual Test 0</button> |
botdream | 0:fd56ae0fc8ea | 58 | <button type="button" onclick="sendrequest('./rpc/servocmd2/run%2050')">Manual Test 50</button> |
botdream | 0:fd56ae0fc8ea | 59 | <button type="button" onclick="sendrequest('./rpc/servocmd2/run%20100')">Manual Test 100</button> |
botdream | 0:fd56ae0fc8ea | 60 | --> |
botdream | 0:fd56ae0fc8ea | 61 | |
botdream | 0:fd56ae0fc8ea | 62 | <div id="debugtxt"></div> |
botdream | 0:fd56ae0fc8ea | 63 | |
botdream | 0:fd56ae0fc8ea | 64 | </body> |
botdream | 0:fd56ae0fc8ea | 65 | </html> |
botdream | 0:fd56ae0fc8ea | 66 | */ |