This is the end gate portion of a pinewood derby timer that uses an mbed. It communicates via HTTP to a web server that is doing the racer control and race management. Still need to put all that code together into a package on the net.

Dependencies:   EthernetNetIf mbed HTTPServer Servo

Files at this revision

API Documentation at this revision

Comitter:
dminear
Date:
Wed Mar 09 05:09:42 2011 +0000
Commit message:
Initial release

Changed in this revision

EthernetNetIf.lib Show annotated file Show diff for this revision Revisions of this file
HTTPServer.lib Show annotated file Show diff for this revision Revisions of this file
Servo.lib Show annotated file Show diff for this revision Revisions of this file
blinkm.cpp Show annotated file Show diff for this revision Revisions of this file
derby_handler.cpp Show annotated file Show diff for this revision Revisions of this file
derby_handler.h 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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
share.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetNetIf.lib	Wed Mar 09 05:09:42 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/EthernetNetIf/#bc7df6da7589
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPServer.lib	Wed Mar 09 05:09:42 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/HTTPServer/#d753966e4d97
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.lib	Wed Mar 09 05:09:42 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/Servo/#36b69a7ced07
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blinkm.cpp	Wed Mar 09 05:09:42 2011 +0000
@@ -0,0 +1,70 @@
+#include "mbed.h"
+#include "Servo.h"
+#include "share.h"
+
+//Serial pc(USBTX, USBRX); // tx, rx
+
+//prototypes
+void FadeToColor(char r,char g, char b);
+void FastColorChg(char r,char g, char b);
+void PlayScript(char s);
+void BlinkInit(void);
+//DEFINES
+#define BLINKADR    0x12
+
+//I2C blink(p9,p10);
+I2C blink(p28,p27);
+
+#ifdef test
+
+int main() {
+    BlinkInit();
+    FadeToColor(0x00,0x00,0x00);
+    wait(2);
+    FadeToColor(0xff,0xff,0xff);
+    wait(2);
+    int i = 0;
+    while(1) {
+       
+        for(i = 0; i < 18; i++) {
+        //pc.printf("script %d\n\r", i );
+        PlayScript(i); //virtual candle
+        wait(60);   // 10 sec script play
+        }
+    }
+}
+#endif
+
+void BlinkInit(void)
+{
+    char data[1] = {'o'};
+    blink.frequency(100000);
+    blink.write(0x00, data, 1);  // will stop it at whatever address  
+}
+void FadeToColor(char r,char g, char b)
+{
+    char data[4] = {'c',r,g,b};
+    blink.write(BLINKADR, data, 4);
+}
+void FastColorChg(char r,char g, char b)
+{
+    char data[4] = {'n',r,g,b};
+    blink.write(BLINKADR, data, 4);
+}
+void PlayScript(char s)
+{
+    FadeToColor(0x00,0x00,0x00); // fade to black
+    wait(0.25); //100 ms to make sure fade is complete
+    if(s < 19) //only 19 scripts onboard
+    {
+        char data[4] = {'p',s,0,0}; //play script from start indefinetly
+        blink.write(BLINKADR, data, 4);
+    }
+}
+
+void FadeToRandomColor(void) {
+    char data[4] = {'C',0xff,0xff,0xff};
+       
+        blink.write(BLINKADR, data, 4); // Send command string
+        wait(0.07);              // Could also poll, 65ms is typical
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/derby_handler.cpp	Wed Mar 09 05:09:42 2011 +0000
@@ -0,0 +1,203 @@
+/*
+Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
+ 
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+ 
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+ 
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include "derby_handler.h"
+#include "Servo.h"
+#include "share.h"
+
+#include <string>
+#include <cstring>
+
+
+//#define __DEBUG
+#include "dbg/dbg.h"
+
+void reset_lane_latch_enables(void);
+
+Timeout gatetimer;
+Timeout soltimer;
+Timeout gameover;
+
+SimpleHandler3::SimpleHandler3(const char* rootPath, 
+                                const char* path, 
+                                TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath,
+                                                                        path,
+                                                                        pTCPSocket)
+{
+
+}
+
+SimpleHandler3::~SimpleHandler3()
+{
+  DBG("\r\nHandler destroyed\r\n");
+}
+
+void SimpleHandler3::doGet()
+{
+  char resp[300];
+ 
+  DBG("\r\nIn SimpleHandler::doGet()\r\n");  
+  string str = "GET path is " + path();
+  sprintf( resp, "%s\nlane1:%f\nlane2:%f\nlane3:%f\nlane4:%f\n", str.c_str(), lane1, lane2, lane3, lane4 );
+  
+  setContentLen( strlen(resp) );
+  
+  respHeaders()["Connection"] = "close";
+  respHeaders()["Content-type"] = "text/plain";
+  writeData(resp, strlen(resp));
+  DBG("\r\nExit SimpleHandler::doGet()\r\n");
+}
+
+void SimpleHandler3::doPost()
+{
+ string str = "POST path is " + path();
+ 
+ // find out if it's a reset or start
+ const char * match = strstr( path().c_str(), "reset");
+ if  (match > 0) {         // must be reset
+    // handle reset
+    timer.reset();
+    myservo = 0.7;
+    energizeSol();
+    gatetimer.detach();
+    soltimer.detach();
+    gameover.detach();
+    raiseGate();
+    deenergizeSol();
+    turnoffleds();
+    ledcheck();
+
+    DBG("\r\nreset called\r\n");
+  }
+ match = strstr( path().c_str(), "start");
+ if  (match > 0) {         // must be start
+    gatetimer.detach();
+    soltimer.detach();
+    gameover.detach();
+    raiseGate();
+    deenergizeSol();
+    if (blinkm) {
+        FadeToColor(255,0,0);
+        wait(1.0);
+        FadeToColor(255,255,0);
+        wait(1.0);
+        FastColorChg(0,255,0);
+    }
+    lane1led = 1;
+    lane2led = 1;
+    lane3led = 1;
+    lane4led = 1;
+    reset_lane_latch_enables();
+    wait(0.1);
+    timer.reset();
+    timer.start();
+    lane1 = 10.0;
+    lane2 = 10.0;
+    lane3 = 10.0;
+    lane4 = 10.0;
+    myservo = 0.2;      // drop the gate
+    startsol = 0;
+    gatetimer.attach( this, &SimpleHandler3::raiseGate, 2.0 );
+    soltimer.attach( this, &SimpleHandler3::energizeSol, 10.0 );
+    gameover.attach( this, &SimpleHandler3::turnoffleds, 10.0 );
+    
+    DBG("\r\nstart called\r\n");
+  }
+  
+  const char * resp = str.c_str(); 
+  setContentLen( strlen(resp) );
+  respHeaders()["Content-type"] = "text/plain";
+  respHeaders()["Connection"] = "close";
+  writeData(resp, strlen(resp));
+  DBG("\r\nExit SimpleHandler::doGet()\r\n");
+}
+
+void SimpleHandler3::doHead()
+{
+
+}
+
+  
+void SimpleHandler3::onReadable() //Data has been read
+{
+
+}
+
+void SimpleHandler3::onWriteable() //Data has been written & buf is free
+{
+  DBG("\r\nSimpleHandler::onWriteable() event\r\n");
+  close(); //Data written, we can close the connection
+}
+
+void SimpleHandler3::onClose() //Connection is closing
+{
+  //Nothing to do
+}
+
+void SimpleHandler3::raiseGate( void )
+{
+    myservo = 0.7;     // reset the gate
+    DBG( "\r\nraiseGate\r\n");
+}
+
+void SimpleHandler3::deenergizeSol( void )
+{
+    startsol = 0;
+    DBG( "\r\ndeenergizeSol\r\n");
+}
+
+void SimpleHandler3::energizeSol( void )
+{
+    startsol = 1;
+}
+
+void SimpleHandler3::turnoffleds( void )
+{
+    lane1led = 0;
+    lane2led = 0;
+    lane3led = 0;
+    lane4led = 0;
+    if (blinkm) {
+        FadeToColor(0,0,0);
+    }
+
+    DBG( "\r\ndeenergizeSol\r\n");
+}
+
+void SimpleHandler3::ledcheck( void ) {
+    lane1led = 1;
+    wait(0.2);
+    lane2led = 1;
+    wait(0.2);
+    lane3led = 1;
+    wait(0.2);
+    lane4led = 1;
+    wait(0.2);
+    lane1led = 0;
+    wait(0.2);
+    lane2led = 0;
+    wait(0.2);
+    lane3led = 0;
+    wait(0.2);
+    lane4led = 0;
+    wait(0.2);
+}    
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/derby_handler.h	Wed Mar 09 05:09:42 2011 +0000
@@ -0,0 +1,52 @@
+/*
+Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
+ 
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+ 
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+ 
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#ifndef SIMPLE_HANDLER3_H
+#define SIMPLE_HANDLER3_H
+
+#include "../HTTPRequestHandler.h"
+#include "mbed.h"
+
+class SimpleHandler3 : public HTTPRequestHandler
+{
+public:
+  SimpleHandler3(const char* rootPath, const char* path, TCPSocket* pTCPSocket);
+  virtual ~SimpleHandler3();
+
+//protected:
+  static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocket* pTCPSocket) { return new SimpleHandler3(rootPath, path, pTCPSocket); } //if we ever could do static virtual functions, this would be one
+
+  virtual void doGet();
+  virtual void doPost();
+  virtual void doHead();
+  
+  virtual void onReadable(); //Data has been read
+  virtual void onWriteable(); //Data has been written & buf is free
+  virtual void onClose(); //Connection is closing
+  void raiseGate();
+  void deenergizeSol();
+  void energizeSol();
+  void turnoffleds();
+  void ledcheck();
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Mar 09 05:09:42 2011 +0000
@@ -0,0 +1,150 @@
+#include "mbed.h"
+#include "EthernetNetIf.h"
+#include "HTTPServer.h"
+#include "derby_handler.h"
+#include "Servo.h"
+#include "share.h"
+
+//I2C i2c(p28,p27); //p9, p10);        // sda, scl
+
+DigitalOut led1(LED1, "led1");
+DigitalOut led2(LED2, "led2");
+DigitalOut led3(LED3, "led3");
+DigitalOut led4(LED4, "led4");
+DigitalOut ledgreen(p30);
+DigitalOut ledyellow(p29);
+
+LocalFileSystem fs("webfs");
+
+EthernetNetIf eth;  
+HTTPServer svr;
+InterruptIn button1(p11);
+InterruptIn button2(p12);
+InterruptIn button3(p13);
+InterruptIn button4(p14);
+Timer timer;
+Servo myservo(p26);
+DigitalOut startsol(p25);
+DigitalOut lane1led(p21);
+DigitalOut lane2led(p22);
+DigitalOut lane3led(p23);
+DigitalOut lane4led(p24);
+
+int blinkm=0;
+float lane1;
+float lane2;
+float lane3;
+float lane4;
+int lane1enable = 0;
+int lane2enable = 0;
+int lane3enable = 0;
+int lane4enable = 0;
+
+void reset_lane_latch_enables() {
+    lane1enable = 1;
+    lane2enable = 1;
+    lane3enable = 1;
+    lane4enable = 1;
+}
+    
+void lane1trig () {
+    if (lane1enable == 1) {
+        lane1enable = 0;
+        lane1 = timer.read();
+        lane1led = 0;
+    }
+}
+    
+void lane2trig () {
+    if (lane2enable == 1) {
+        lane2enable = 0;
+        lane2 = timer.read();
+        lane2led = 0;
+    }
+}
+
+void lane3trig () {
+    if (lane3enable == 1) {
+        lane3enable = 0;
+        lane3 = timer.read();
+        lane3led = 0;
+    }
+}
+
+void lane4trig () {
+    if (lane4enable == 1) {
+        lane4enable = 0;
+        lane4 = timer.read();
+        lane4led = 0;
+    }
+}
+
+
+int main() {
+  //Base::add_rpc_class<DigitalOut>();
+  button1.rise( &lane1trig );
+  button2.rise( &lane2trig );
+  button3.rise( &lane3trig );
+  button4.rise( &lane4trig );
+  ledyellow = 0;
+  ledgreen = 0;
+  myservo.calibrate(0.0006,45);
+  timer.reset();
+  myservo = 0.7;
+  startsol = 0;
+    lane1led = 0;
+    lane2led = 0;
+    lane3led = 0;
+    lane4led = 0;
+
+
+  printf("Setting up...\n");
+  EthernetErr ethErr = eth.setup();
+  if(ethErr)
+  {
+    printf("Error %d in setup.\n", ethErr);
+    return -1;
+  } else {
+    ledgreen = 1;
+  }
+
+  printf("Setup OK\n");
+  
+  FSHandler::mount("/webfs", "/files"); //Mount /webfs path on /files web path
+  FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path
+  
+  svr.addHandler<SimpleHandler>("/simple");
+  svr.addHandler<SimpleHandler3>("/simple3");
+  //svr.addHandler<RPCHandler>("/rpc");
+  svr.addHandler<FSHandler>("/files");
+  svr.addHandler<FSHandler>("/"); //Default handler
+  //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm
+  
+  svr.bind(80);
+  
+  printf("Listening...\n");
+  if (blinkm) {BlinkInit();}
+  
+  Timer tm;
+  tm.start();
+  //Listen indefinitely
+  while(true) {
+    Net::poll();
+    if(tm.read()>0.5) {
+      ledyellow = !ledyellow; //Show that we are alive
+      //FadeToRandomColor();
+      tm.start();
+      //printf ("timer is %f\n\r", timer.read() );
+    }
+    
+    led1 = ! button1;
+    led2 = ! button2;
+    led3 = ! button3;
+    led4 = ! button4;
+    
+  }
+  
+  return 0;
+
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Mar 09 05:09:42 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/share.h	Wed Mar 09 05:09:42 2011 +0000
@@ -0,0 +1,26 @@
+#ifndef SHARE_H
+#define SHARE_H
+
+extern int blinkm;
+extern Timer timer;
+extern float lane1;
+extern float lane2;
+extern float lane3;
+extern float lane4;
+extern Servo myservo;
+extern DigitalOut startsol;
+extern DigitalOut startsol;
+extern DigitalOut lane1led;
+extern DigitalOut lane2led;
+extern DigitalOut lane3led;
+extern DigitalOut lane4led;
+
+extern I2C blink;
+
+//prototypes
+void FadeToColor(char r,char g, char b);
+void FastColorChg(char r,char g, char b);
+void PlayScript(char s);
+void BlinkInit(void);
+void FadeToRandomColor(void);
+#endif
\ No newline at end of file