Gyro output to TCP in degrees with calibration and correction
Dependencies: EthernetNetIf mbed
Diff: main.cpp
- Revision:
- 0:5de940cf9783
- Child:
- 1:a88d1309f810
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Mar 01 15:25:57 2012 +0000 @@ -0,0 +1,258 @@ + +// TCP Echo server +// 2010/9/7 + + +/* + +(Execute Sample) +PC side: +telnet 192.168.0.25 12345 +Trying 192.168.0.25... +Connected to 192.168.0.25. +Escape character is '^]'. + +mbed side: +Setup OK +mbed IP Address is 192.168.0.25 +Binding.. +Listening... +Listening: TCP Socket Accepted +Listening: Incoming TCP connection from 192.168.0.7 +TCP Socket Readable +Received&Wrote:test text + +TCP Socket Writable + + +*/ + +#include "mbed.h" +#include "EthernetNetIf.h" +#include "TCPSocket.h" +#include "ADXL345_I2C.h" + +#define TCP_LISTENING_PORT 1337 +int go = 0; + +DigitalOut led3(LED3, "led3"); +DigitalOut led4(LED4, "led4"); +DigitalIn init(p5); +DigitalIn calibrate(p20); +//I2C i2c(p9, p10); // sda, scl +ADXL345_I2C acc(p9, p10); + + +void tcp_send(const char*); +void tcp_sendIfConnected( const char* data ); +void onConnectedTCPSocketEvent(TCPSocketEvent e); +void onListeningTCPSocketEvent(TCPSocketEvent e); + + +// EthernetNetIf eth; +EthernetNetIf eth( + IpAddr(192,168,0,16), //IP Address + IpAddr(255,255,255,0), //Network Mask + IpAddr(192,168,0,2), //Gateway + IpAddr(192,168,0,2) //DNS +); + +TCPSocket ListeningSock; +TCPSocket* pConnectedSock; // for ConnectedSock +Host client; +TCPSocketErr err; +Ticker routine; + +uint16_t x_cor = 0; +uint16_t y_cor = 0; +uint16_t z_cor = 0; + +void interrupt() +{ +// int count = 0; +// for (int address=0; address<256; address+=2) { +// if (!i2c.write(address, NULL, 0)) { // 0 returned is ok +// if(go) { +// tcp_send(" - I2C device found at address:"); +// char buffer [128]; +// sprintf (buffer, "%i\n",address); +// tcp_send(buffer); +// } +// count++; +// } +// } + + int readings[3] = {0, 0, 0}; + if(go)acc.getOutput(readings); + char buffer [128]; + if(go)sprintf (buffer, "x:%i - y:%i - z:%i\n",(int16_t)readings[0],(int16_t)readings[1],(int16_t)readings[2]); + if(go)tcp_send(buffer); +} + +int main() { + + EthernetErr ethErr = eth.setup(); + if(ethErr) + { + return -1; + } + + IpAddr ip = eth.getIp(); + + // Set the callbacks for Listening + ListeningSock.setOnEvent(&onListeningTCPSocketEvent); + + // bind and listen on TCP + err=ListeningSock.bind(Host(IpAddr(), TCP_LISTENING_PORT)); + if(err) + { + //Deal with that error... + } + + err=ListeningSock.listen(); // Starts listening + if(err) + { + //listening error + } + + //Go into standby mode to configure the device. + acc.setPowerControl(0x00); + + //Full resolution, +/-16g, 4mg/LSB. + acc.setDataFormatControl(0x0B); + + //3.2kHz data rate. + acc.setDataRate(ADXL345_3200HZ); + + //Measurement mode. + acc.setPowerControl(0x08); + wait(0.1); + Timer tmr; + tmr.start(); + while(true) + { + Net::poll(); + while(init) + { + if(!init) + { + tcp_send("You are a go!\n"); + led3 = 1; + go = 1; + } + } + if(tmr.read() > 0.2) // sec + { + led4=!led4; //Show that we are alive + tmr.reset(); + } + if(calibrate){ + while(calibrate){}; + int readings[3] = {0, 0, 0}; + if(go)acc.getOutput(readings); + x_cor = readings[0]; + y_cor = readings[1]; + z_cor = readings[2]; + routine.attach_us(&interrupt, 10); + } + } +} + +void tcp_send( const char* data ){ + int len = strlen(data); + pConnectedSock->send(data, len); +} + +void onConnectedTCPSocketEvent(TCPSocketEvent e) +{ + switch(e) + { + case TCPSOCKET_CONNECTED: + break; + case TCPSOCKET_WRITEABLE: + //Can now write some data... + printf("TCP Socket Writable\r\n"); + break; + case TCPSOCKET_READABLE: + //Can now read dome data... + // Read in any available data into the buffer + //char buff[128]; + //while ( int len = pConnectedSock->recv(buff, 128) ) { + // And send straight back out again + // pConnectedSock->send(buff, len); + // buff[len]=0; // make terminater + //} + break; + case TCPSOCKET_CONTIMEOUT: + printf("TCP Socket Timeout\r\n"); + break; + case TCPSOCKET_CONRST: + printf("TCP Socket CONRST\r\n"); + break; + case TCPSOCKET_CONABRT: + printf("TCP Socket CONABRT\r\n"); + break; + case TCPSOCKET_ERROR: + printf("TCP Socket Error\r\n"); + break; + case TCPSOCKET_DISCONNECTED: + //Close socket... + printf("TCP Socket Disconnected\r\n"); + pConnectedSock->close(); + break; + default: + printf("DEFAULT\r\n"); + } +} + +void onListeningTCPSocketEvent(TCPSocketEvent e) +{ + switch(e) + { + case TCPSOCKET_ACCEPT: + printf("Listening: TCP Socket Accepted\r\n"); + // Accepts connection from client and gets connected socket. + err=ListeningSock.accept(&client, &pConnectedSock); + if (err) { + printf("onListeningTcpSocketEvent : Could not accept connection.\r\n"); + return; //Error in accept, discard connection + } + // Setup the new socket events + pConnectedSock->setOnEvent(&onConnectedTCPSocketEvent); + // We can find out from where the connection is coming by looking at the + // Host parameter of the accept() method + IpAddr clientIp = client.getIp(); + printf("Listening: Incoming TCP connection from %d.%d.%d.%d\r\n", + clientIp[0], clientIp[1], clientIp[2], clientIp[3]); + break; + // the following cases will not happen + case TCPSOCKET_CONNECTED: + printf("Listening: TCP Socket Connected\r\n"); + break; + case TCPSOCKET_WRITEABLE: + printf("Listening: TCP Socket Writable\r\n"); + break; + case TCPSOCKET_READABLE: + printf("Listening: TCP Socket Readable\r\n"); + break; + case TCPSOCKET_CONTIMEOUT: + printf("Listening: TCP Socket Timeout\r\n"); + break; + case TCPSOCKET_CONRST: + printf("Listening: TCP Socket CONRST\r\n"); + break; + case TCPSOCKET_CONABRT: + printf("Listening: TCP Socket CONABRT\r\n"); + break; + case TCPSOCKET_ERROR: + printf("Listening: TCP Socket Error\r\n"); + break; + case TCPSOCKET_DISCONNECTED: + //Close socket... + printf("Listening: TCP Socket Disconnected\r\n"); + ListeningSock.close(); + break; + default: + printf("DEFAULT\r\n"); + }; +}