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

Files at this revision

API Documentation at this revision

Comitter:
botdream
Date:
Thu Oct 27 23:28:15 2011 +0000
Commit message:

Changed in this revision

Libs/EthernetNetIf.lib Show annotated file Show diff for this revision Revisions of this file
Libs/HTTPServer.lib Show annotated file Show diff for this revision Revisions of this file
Libs/RPCInterface.lib Show annotated file Show diff for this revision Revisions of this file
Libs/Servo.lib Show annotated file Show diff for this revision Revisions of this file
Libs/mbed.bld Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
servo.htm.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r fd56ae0fc8ea Libs/EthernetNetIf.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Libs/EthernetNetIf.lib	Thu Oct 27 23:28:15 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/EthernetNetIf/#bc7df6da7589
diff -r 000000000000 -r fd56ae0fc8ea Libs/HTTPServer.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Libs/HTTPServer.lib	Thu Oct 27 23:28:15 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/HTTPServer/#d753966e4d97
diff -r 000000000000 -r fd56ae0fc8ea Libs/RPCInterface.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Libs/RPCInterface.lib	Thu Oct 27 23:28:15 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/MichaelW/code/RPCInterface/#a9e2c45097c8
diff -r 000000000000 -r fd56ae0fc8ea Libs/Servo.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Libs/Servo.lib	Thu Oct 27 23:28:15 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/Servo/#36b69a7ced07
diff -r 000000000000 -r fd56ae0fc8ea Libs/mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Libs/mbed.bld	Thu Oct 27 23:28:15 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912
diff -r 000000000000 -r fd56ae0fc8ea main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Oct 27 23:28:15 2011 +0000
@@ -0,0 +1,258 @@
+/* //---------------------------------------------------------------------------------------------
+// RPC ServoCmd
+http://192.168.1.100/rpc/servocmd1/run 0
+http://192.168.1.100/rpc/servocmd1/run 50
+http://192.168.1.100/rpc/servocmd1/run 100
+
+http://192.168.1.100/rpc/servocmd2/run 0
+http://192.168.1.100/rpc/servocmd2/run 50
+http://192.168.1.100/rpc/servocmd2/run 100
+
+// HTML5 demo (use chrome or html5 compatible browser)
+-> copy servo.htm.cpp file to MBED internal flash memory (where the binary code is uploaded)
+-> open file with an TextEditor and remove comments indicator chars "/*" and "*\/"
+-> rename servo.htm.cpp to servo.htm
+-> open browser http://192.168.1.100/servo.htm
+
+// Hardware
+-> Connect Servo1 Signal pin to P21 and Servo2 Signal pin to P22
+
+//---------------------------------------------------------------------------------------------
+// resources
+//---------------------------------------------------------------------------------------------
+http://mbed.org/handbook/C-Data-Types
+http://mbed.org/cookbook/RPC-Interface-Library
+http://mbed.org/cookbook/HTTP-Server
+http://mbed.org/cookbook/Ethernet
+http://mbed.org/handbook/Ticker
+//--------------------------------------------------------------------------------------------- */
+#include "mbed.h"
+#include "EthernetNetIf.h"
+#include "HTTPServer.h"
+#include "RPCFunction.h"
+#include "Servo.h"
+//---------------------------------------------------------------------------------------------
+DigitalOut myled(LED1);
+Serial pc(USBTX, USBRX); // tx, rx
+//---------------------------------------------------------------------------------------------
+//#define internaldebug // send debug messages to USB Serial port (9600,1,N)
+//#define dhcpenable    // auto-setup IP Address from DHCP router
+//---------------------------------------------------------------------------------------------
+// Timer Interrupt - NetPool
+//---------------------------------------------------------------------------------------------
+Ticker netpool;
+//---------------------------------------------------------------------------------------------
+// Ethernet Object Setup
+//---------------------------------------------------------------------------------------------
+#ifdef dhcpenable
+  EthernetNetIf eth;  
+#else
+  EthernetNetIf eth(
+    IpAddr(192,168,1,100), //IP Address
+    IpAddr(255,255,255,0), //Network Mask
+    IpAddr(192,168,1,254), //Gateway
+    IpAddr(192,168,1,254)  //DNS
+  );
+#endif
+//---------------------------------------------------------------------------------------------
+// HTTP Server
+//---------------------------------------------------------------------------------------------
+HTTPServer httpserver;
+LocalFileSystem fs("webfs");
+
+Servo servo1(p21);
+Servo servo2(p22);
+
+int8_t servo_flag;
+float servo_cursor1;
+float servo_cursor2;
+
+int8_t pool_flag;
+//---------------------------------------------------------------------------------------------
+
+//---------------------------------------------------------------------------------------------
+// ISR -> Pool Ethernet - will be triggered by netpool ticker
+//---------------------------------------------------------------------------------------------
+void netpoolupdate()
+{
+  if(pool_flag !=0)
+    return;
+
+  pool_flag = 1; // start processing ...
+  Net::poll();
+  pool_flag = 0; // end processing ...
+}
+//---------------------------------------------------------------------------------------------
+
+//---------------------------------------------------------------------------------------------
+// Generic ServoCmd function
+//---------------------------------------------------------------------------------------------
+void servocmd(char *input, char *output, int8_t servonbr)
+{
+  if(servo_flag != 0) // busy ...
+  {
+    #ifdef internaldebug
+      printf("ServoCmd%d is busy. Return from function!\r\n",servonbr);
+    #endif
+   
+    return;  
+  }
+  servo_flag = 1; // REST request
+  
+  int iarg1 = 0;
+  sscanf(input, "%i", &iarg1);
+    
+  #ifdef internaldebug
+    printf("Calling RCP Function ServoCmd%d.\r\n",servonbr);
+    //printf("INPUT: %s.\r\n", input);
+    //printf("OUTPUT: %s.\r\n", output);
+    printf("ARG1: %d\r\n", iarg1);
+  #endif 
+  
+  if(servonbr == 1)
+  {
+    servo_cursor1 = (float)iarg1/(float)100.0;
+    #ifdef internaldebug
+      sprintf(output, "<html><body>RCP ServoCmd1 Completed!<br>PARAM=%d<br>Cursor=%f</body></html>", iarg1, servo_cursor1);
+    #else
+      sprintf(output, "<html><body>OK</body></html>");
+    #endif
+      
+  }
+  else if(servonbr == 2)
+  {
+    servo_cursor2 = (float)iarg1/(float)100.0;
+    #ifdef internaldebug
+      sprintf(output, "<html><body>RCP ServoCmd2 Completed!<br>PARAM=%d<br>Cursor=%f</body></html>", iarg1, servo_cursor2);
+    #else
+      sprintf(output, "<html><body>OK</body></html>");
+    #endif      
+  }
+  else
+  {
+    #ifdef internaldebug    
+      sprintf(output, "<html><body>RCP ServoCmd%d Error<br>PARAM=%d</body></html>", servonbr, iarg1);  
+    #else
+      sprintf(output, "<html><body>ERROR</body></html>");
+    #endif      
+  }
+  
+  servo_flag = 2; // can now update servos
+}
+//---------------------------------------------------------------------------------------------
+// RPC TList Commands (Task/Job List)
+//---------------------------------------------------------------------------------------------
+/*
+void rpc_dummy(char *input, char *output)
+{
+// dummy!!!
+}
+//--------------------------------------------------------------------------------------------- */
+
+void rpc_servocmd1(char *input, char *output)
+{
+  servocmd(input, output, 1);
+}
+//---------------------------------------------------------------------------------------------
+
+void rpc_servocmd2(char *input, char *output)
+{
+  servocmd(input, output, 2);
+}
+//---------------------------------------------------------------------------------------------
+
+//---------------------------------------------------------------------------------------------
+// MAIN
+//---------------------------------------------------------------------------------------------
+int main() 
+{
+  // Set Serial Port Transfer Rate
+  pc.baud(115200);  
+  
+  //--------------------------------------------------------  
+  servo_flag = 0; // 0-NOP; 1-REST request; 2-update servos;
+  servo_cursor1 = 0.5;
+  servo_cursor2 = 0.5; 
+  servo1 = 0.5;
+  servo2 = 0.5;
+  
+  pool_flag = 0;
+  //--------------------------------------------------------
+  // Setting Ethernet
+  //--------------------------------------------------------    
+  #ifdef internaldebug
+    printf("\r\nSetting up Ethernet interface!\r\n");
+  #endif
+  // Create return object for error check
+  EthernetErr ethErr = eth.setup(); 
+  if(ethErr)
+  {
+    #ifdef internaldebug
+      printf("\r\nError %d in Ethernet setup.\r\n", ethErr);
+    #endif
+    return -1;
+  }
+  #ifdef internaldebug
+    printf("\r\nEthernet setup completed with success!\r\n");
+  #endif  
+  //--------------------------------------------------------    
+
+  //--------------------------------------------------------
+  // adding RPC functions
+  //--------------------------------------------------------
+  //RPCFunction RPCdummy(&rpc_dummy, "dummy");      
+  RPCFunction RPCservocmd1(&rpc_servocmd1, "servocmd1");    
+  RPCFunction RPCservocmd2(&rpc_servocmd2, "servocmd2");    
+  //--------------------------------------------------------
+
+  //--------------------------------------------------------
+  // adding Handlers 
+  //--------------------------------------------------------  
+  FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path
+ 
+  httpserver.addHandler<RPCHandler>("/rpc");
+  httpserver.addHandler<FSHandler>("/"); //Default handler  
+  //--------------------------------------------------------
+  
+  //--------------------------------------------------------
+  // bind http server to port 80 (Listen)
+  //--------------------------------------------------------       
+  httpserver.bind(80);  
+  #ifdef internaldebug
+    printf("Listening on port 80!\r\n");
+  #endif
+  //--------------------------------------------------------         
+    
+  //--------------------------------------------------------
+  // ISR -> attach timer interrupt to update Net::Pool();
+  //--------------------------------------------------------
+  //netpool.attach(&netpoolupdate, 0.1);
+  //--------------------------------------------------------
+
+  //--------------------------------------------------------    
+  // main loop
+  //--------------------------------------------------------    
+  while(1) 
+  { 
+    /*   
+    myled = 1;
+    wait(0.5);
+    myled = 0;
+    wait(0.5);     
+    */
+    
+    if(servo_flag == 2) // update servos
+    {   
+      #ifdef internaldebug
+        printf("Updating Servos!\r\n");
+      #endif
+    
+      servo1.write(servo_cursor1);
+      servo2.write(servo_cursor2);
+      servo_flag = 0; // NOP      
+    }
+    wait(0.1);
+    netpoolupdate();
+  }
+}
+//---------------------------------------------------------------------------------------------
\ No newline at end of file
diff -r 000000000000 -r fd56ae0fc8ea servo.htm.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/servo.htm.cpp	Thu Oct 27 23:28:15 2011 +0000
@@ -0,0 +1,66 @@
+/*
+<html>
+<head>
+<script type="text/javascript">
+
+var global_flag = 0;
+
+function sendrequest(request_uri)
+{
+  var xmlhttp;
+  if(window.XMLHttpRequest)
+  {// code for IE7+, Firefox, Chrome, Opera, Safari
+    xmlhttp=new XMLHttpRequest();
+  }
+  else
+  {// code for IE6, IE5
+    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
+  }
+  xmlhttp.onreadystatechange=function()
+  {
+    if (xmlhttp.readyState==4 && xmlhttp.status==200)
+    {
+      document.getElementById("debugtxt").innerHTML=xmlhttp.responseText;
+      global_flag = 0;
+    }
+  }
+  console.log(request_uri);
+  xmlhttp.open("GET",request_uri,true);
+  xmlhttp.send();
+}
+
+
+function setValue(event) {
+
+  if(global_flag != 0)
+    return; // work in progress
+
+  global_flag = 1; // initialize request
+  document.getElementById("debugtxt").innerHTML="...";
+
+  if(event.target.id == 'servo1')
+    sendrequest('./rpc/servocmd1/run%20'+event.target.value);
+  else if(event.target.id == 'servo2')
+    sendrequest('./rpc/servocmd2/run%20'+event.target.value);
+}
+
+</script>
+</head>
+<body>
+
+<h2>Testing WebCam PAN & TILT Servos via HTTP REST (RPC) interface.</h2>
+
+<input id="servo1" type="range" min="0" max="100" step="5" oninput="setValue(event)" />
+<input id="servo2" type="range" min="0" max="100" step="5" oninput="setValue(event)" />
+
+<!--
+<button type="button" onclick="sendrequest('./rpc/servocmd2/run%200')">Manual Test 0</button>
+<button type="button" onclick="sendrequest('./rpc/servocmd2/run%2050')">Manual Test 50</button>
+<button type="button" onclick="sendrequest('./rpc/servocmd2/run%20100')">Manual Test 100</button>
+-->
+
+<div id="debugtxt"></div>
+
+</body>
+</html>
+*/
\ No newline at end of file