Gyro output to TCP in degrees with calibration and correction

Dependencies:   EthernetNetIf mbed

Committer:
SED9008
Date:
Thu Mar 01 15:25:57 2012 +0000
Revision:
0:5de940cf9783
Child:
1:a88d1309f810

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SED9008 0:5de940cf9783 1
SED9008 0:5de940cf9783 2 // TCP Echo server
SED9008 0:5de940cf9783 3 // 2010/9/7
SED9008 0:5de940cf9783 4
SED9008 0:5de940cf9783 5
SED9008 0:5de940cf9783 6 /*
SED9008 0:5de940cf9783 7
SED9008 0:5de940cf9783 8 (Execute Sample)
SED9008 0:5de940cf9783 9 PC side:
SED9008 0:5de940cf9783 10 telnet 192.168.0.25 12345
SED9008 0:5de940cf9783 11 Trying 192.168.0.25...
SED9008 0:5de940cf9783 12 Connected to 192.168.0.25.
SED9008 0:5de940cf9783 13 Escape character is '^]'.
SED9008 0:5de940cf9783 14
SED9008 0:5de940cf9783 15 mbed side:
SED9008 0:5de940cf9783 16 Setup OK
SED9008 0:5de940cf9783 17 mbed IP Address is 192.168.0.25
SED9008 0:5de940cf9783 18 Binding..
SED9008 0:5de940cf9783 19 Listening...
SED9008 0:5de940cf9783 20 Listening: TCP Socket Accepted
SED9008 0:5de940cf9783 21 Listening: Incoming TCP connection from 192.168.0.7
SED9008 0:5de940cf9783 22 TCP Socket Readable
SED9008 0:5de940cf9783 23 Received&Wrote:test text
SED9008 0:5de940cf9783 24
SED9008 0:5de940cf9783 25 TCP Socket Writable
SED9008 0:5de940cf9783 26
SED9008 0:5de940cf9783 27
SED9008 0:5de940cf9783 28 */
SED9008 0:5de940cf9783 29
SED9008 0:5de940cf9783 30 #include "mbed.h"
SED9008 0:5de940cf9783 31 #include "EthernetNetIf.h"
SED9008 0:5de940cf9783 32 #include "TCPSocket.h"
SED9008 0:5de940cf9783 33 #include "ADXL345_I2C.h"
SED9008 0:5de940cf9783 34
SED9008 0:5de940cf9783 35 #define TCP_LISTENING_PORT 1337
SED9008 0:5de940cf9783 36 int go = 0;
SED9008 0:5de940cf9783 37
SED9008 0:5de940cf9783 38 DigitalOut led3(LED3, "led3");
SED9008 0:5de940cf9783 39 DigitalOut led4(LED4, "led4");
SED9008 0:5de940cf9783 40 DigitalIn init(p5);
SED9008 0:5de940cf9783 41 DigitalIn calibrate(p20);
SED9008 0:5de940cf9783 42 //I2C i2c(p9, p10); // sda, scl
SED9008 0:5de940cf9783 43 ADXL345_I2C acc(p9, p10);
SED9008 0:5de940cf9783 44
SED9008 0:5de940cf9783 45
SED9008 0:5de940cf9783 46 void tcp_send(const char*);
SED9008 0:5de940cf9783 47 void tcp_sendIfConnected( const char* data );
SED9008 0:5de940cf9783 48 void onConnectedTCPSocketEvent(TCPSocketEvent e);
SED9008 0:5de940cf9783 49 void onListeningTCPSocketEvent(TCPSocketEvent e);
SED9008 0:5de940cf9783 50
SED9008 0:5de940cf9783 51
SED9008 0:5de940cf9783 52 // EthernetNetIf eth;
SED9008 0:5de940cf9783 53 EthernetNetIf eth(
SED9008 0:5de940cf9783 54 IpAddr(192,168,0,16), //IP Address
SED9008 0:5de940cf9783 55 IpAddr(255,255,255,0), //Network Mask
SED9008 0:5de940cf9783 56 IpAddr(192,168,0,2), //Gateway
SED9008 0:5de940cf9783 57 IpAddr(192,168,0,2) //DNS
SED9008 0:5de940cf9783 58 );
SED9008 0:5de940cf9783 59
SED9008 0:5de940cf9783 60 TCPSocket ListeningSock;
SED9008 0:5de940cf9783 61 TCPSocket* pConnectedSock; // for ConnectedSock
SED9008 0:5de940cf9783 62 Host client;
SED9008 0:5de940cf9783 63 TCPSocketErr err;
SED9008 0:5de940cf9783 64 Ticker routine;
SED9008 0:5de940cf9783 65
SED9008 0:5de940cf9783 66 uint16_t x_cor = 0;
SED9008 0:5de940cf9783 67 uint16_t y_cor = 0;
SED9008 0:5de940cf9783 68 uint16_t z_cor = 0;
SED9008 0:5de940cf9783 69
SED9008 0:5de940cf9783 70 void interrupt()
SED9008 0:5de940cf9783 71 {
SED9008 0:5de940cf9783 72 // int count = 0;
SED9008 0:5de940cf9783 73 // for (int address=0; address<256; address+=2) {
SED9008 0:5de940cf9783 74 // if (!i2c.write(address, NULL, 0)) { // 0 returned is ok
SED9008 0:5de940cf9783 75 // if(go) {
SED9008 0:5de940cf9783 76 // tcp_send(" - I2C device found at address:");
SED9008 0:5de940cf9783 77 // char buffer [128];
SED9008 0:5de940cf9783 78 // sprintf (buffer, "%i\n",address);
SED9008 0:5de940cf9783 79 // tcp_send(buffer);
SED9008 0:5de940cf9783 80 // }
SED9008 0:5de940cf9783 81 // count++;
SED9008 0:5de940cf9783 82 // }
SED9008 0:5de940cf9783 83 // }
SED9008 0:5de940cf9783 84
SED9008 0:5de940cf9783 85 int readings[3] = {0, 0, 0};
SED9008 0:5de940cf9783 86 if(go)acc.getOutput(readings);
SED9008 0:5de940cf9783 87 char buffer [128];
SED9008 0:5de940cf9783 88 if(go)sprintf (buffer, "x:%i - y:%i - z:%i\n",(int16_t)readings[0],(int16_t)readings[1],(int16_t)readings[2]);
SED9008 0:5de940cf9783 89 if(go)tcp_send(buffer);
SED9008 0:5de940cf9783 90 }
SED9008 0:5de940cf9783 91
SED9008 0:5de940cf9783 92 int main() {
SED9008 0:5de940cf9783 93
SED9008 0:5de940cf9783 94 EthernetErr ethErr = eth.setup();
SED9008 0:5de940cf9783 95 if(ethErr)
SED9008 0:5de940cf9783 96 {
SED9008 0:5de940cf9783 97 return -1;
SED9008 0:5de940cf9783 98 }
SED9008 0:5de940cf9783 99
SED9008 0:5de940cf9783 100 IpAddr ip = eth.getIp();
SED9008 0:5de940cf9783 101
SED9008 0:5de940cf9783 102 // Set the callbacks for Listening
SED9008 0:5de940cf9783 103 ListeningSock.setOnEvent(&onListeningTCPSocketEvent);
SED9008 0:5de940cf9783 104
SED9008 0:5de940cf9783 105 // bind and listen on TCP
SED9008 0:5de940cf9783 106 err=ListeningSock.bind(Host(IpAddr(), TCP_LISTENING_PORT));
SED9008 0:5de940cf9783 107 if(err)
SED9008 0:5de940cf9783 108 {
SED9008 0:5de940cf9783 109 //Deal with that error...
SED9008 0:5de940cf9783 110 }
SED9008 0:5de940cf9783 111
SED9008 0:5de940cf9783 112 err=ListeningSock.listen(); // Starts listening
SED9008 0:5de940cf9783 113 if(err)
SED9008 0:5de940cf9783 114 {
SED9008 0:5de940cf9783 115 //listening error
SED9008 0:5de940cf9783 116 }
SED9008 0:5de940cf9783 117
SED9008 0:5de940cf9783 118 //Go into standby mode to configure the device.
SED9008 0:5de940cf9783 119 acc.setPowerControl(0x00);
SED9008 0:5de940cf9783 120
SED9008 0:5de940cf9783 121 //Full resolution, +/-16g, 4mg/LSB.
SED9008 0:5de940cf9783 122 acc.setDataFormatControl(0x0B);
SED9008 0:5de940cf9783 123
SED9008 0:5de940cf9783 124 //3.2kHz data rate.
SED9008 0:5de940cf9783 125 acc.setDataRate(ADXL345_3200HZ);
SED9008 0:5de940cf9783 126
SED9008 0:5de940cf9783 127 //Measurement mode.
SED9008 0:5de940cf9783 128 acc.setPowerControl(0x08);
SED9008 0:5de940cf9783 129 wait(0.1);
SED9008 0:5de940cf9783 130 Timer tmr;
SED9008 0:5de940cf9783 131 tmr.start();
SED9008 0:5de940cf9783 132 while(true)
SED9008 0:5de940cf9783 133 {
SED9008 0:5de940cf9783 134 Net::poll();
SED9008 0:5de940cf9783 135 while(init)
SED9008 0:5de940cf9783 136 {
SED9008 0:5de940cf9783 137 if(!init)
SED9008 0:5de940cf9783 138 {
SED9008 0:5de940cf9783 139 tcp_send("You are a go!\n");
SED9008 0:5de940cf9783 140 led3 = 1;
SED9008 0:5de940cf9783 141 go = 1;
SED9008 0:5de940cf9783 142 }
SED9008 0:5de940cf9783 143 }
SED9008 0:5de940cf9783 144 if(tmr.read() > 0.2) // sec
SED9008 0:5de940cf9783 145 {
SED9008 0:5de940cf9783 146 led4=!led4; //Show that we are alive
SED9008 0:5de940cf9783 147 tmr.reset();
SED9008 0:5de940cf9783 148 }
SED9008 0:5de940cf9783 149 if(calibrate){
SED9008 0:5de940cf9783 150 while(calibrate){};
SED9008 0:5de940cf9783 151 int readings[3] = {0, 0, 0};
SED9008 0:5de940cf9783 152 if(go)acc.getOutput(readings);
SED9008 0:5de940cf9783 153 x_cor = readings[0];
SED9008 0:5de940cf9783 154 y_cor = readings[1];
SED9008 0:5de940cf9783 155 z_cor = readings[2];
SED9008 0:5de940cf9783 156 routine.attach_us(&interrupt, 10);
SED9008 0:5de940cf9783 157 }
SED9008 0:5de940cf9783 158 }
SED9008 0:5de940cf9783 159 }
SED9008 0:5de940cf9783 160
SED9008 0:5de940cf9783 161 void tcp_send( const char* data ){
SED9008 0:5de940cf9783 162 int len = strlen(data);
SED9008 0:5de940cf9783 163 pConnectedSock->send(data, len);
SED9008 0:5de940cf9783 164 }
SED9008 0:5de940cf9783 165
SED9008 0:5de940cf9783 166 void onConnectedTCPSocketEvent(TCPSocketEvent e)
SED9008 0:5de940cf9783 167 {
SED9008 0:5de940cf9783 168 switch(e)
SED9008 0:5de940cf9783 169 {
SED9008 0:5de940cf9783 170 case TCPSOCKET_CONNECTED:
SED9008 0:5de940cf9783 171 break;
SED9008 0:5de940cf9783 172 case TCPSOCKET_WRITEABLE:
SED9008 0:5de940cf9783 173 //Can now write some data...
SED9008 0:5de940cf9783 174 printf("TCP Socket Writable\r\n");
SED9008 0:5de940cf9783 175 break;
SED9008 0:5de940cf9783 176 case TCPSOCKET_READABLE:
SED9008 0:5de940cf9783 177 //Can now read dome data...
SED9008 0:5de940cf9783 178 // Read in any available data into the buffer
SED9008 0:5de940cf9783 179 //char buff[128];
SED9008 0:5de940cf9783 180 //while ( int len = pConnectedSock->recv(buff, 128) ) {
SED9008 0:5de940cf9783 181 // And send straight back out again
SED9008 0:5de940cf9783 182 // pConnectedSock->send(buff, len);
SED9008 0:5de940cf9783 183 // buff[len]=0; // make terminater
SED9008 0:5de940cf9783 184 //}
SED9008 0:5de940cf9783 185 break;
SED9008 0:5de940cf9783 186 case TCPSOCKET_CONTIMEOUT:
SED9008 0:5de940cf9783 187 printf("TCP Socket Timeout\r\n");
SED9008 0:5de940cf9783 188 break;
SED9008 0:5de940cf9783 189 case TCPSOCKET_CONRST:
SED9008 0:5de940cf9783 190 printf("TCP Socket CONRST\r\n");
SED9008 0:5de940cf9783 191 break;
SED9008 0:5de940cf9783 192 case TCPSOCKET_CONABRT:
SED9008 0:5de940cf9783 193 printf("TCP Socket CONABRT\r\n");
SED9008 0:5de940cf9783 194 break;
SED9008 0:5de940cf9783 195 case TCPSOCKET_ERROR:
SED9008 0:5de940cf9783 196 printf("TCP Socket Error\r\n");
SED9008 0:5de940cf9783 197 break;
SED9008 0:5de940cf9783 198 case TCPSOCKET_DISCONNECTED:
SED9008 0:5de940cf9783 199 //Close socket...
SED9008 0:5de940cf9783 200 printf("TCP Socket Disconnected\r\n");
SED9008 0:5de940cf9783 201 pConnectedSock->close();
SED9008 0:5de940cf9783 202 break;
SED9008 0:5de940cf9783 203 default:
SED9008 0:5de940cf9783 204 printf("DEFAULT\r\n");
SED9008 0:5de940cf9783 205 }
SED9008 0:5de940cf9783 206 }
SED9008 0:5de940cf9783 207
SED9008 0:5de940cf9783 208 void onListeningTCPSocketEvent(TCPSocketEvent e)
SED9008 0:5de940cf9783 209 {
SED9008 0:5de940cf9783 210 switch(e)
SED9008 0:5de940cf9783 211 {
SED9008 0:5de940cf9783 212 case TCPSOCKET_ACCEPT:
SED9008 0:5de940cf9783 213 printf("Listening: TCP Socket Accepted\r\n");
SED9008 0:5de940cf9783 214 // Accepts connection from client and gets connected socket.
SED9008 0:5de940cf9783 215 err=ListeningSock.accept(&client, &pConnectedSock);
SED9008 0:5de940cf9783 216 if (err) {
SED9008 0:5de940cf9783 217 printf("onListeningTcpSocketEvent : Could not accept connection.\r\n");
SED9008 0:5de940cf9783 218 return; //Error in accept, discard connection
SED9008 0:5de940cf9783 219 }
SED9008 0:5de940cf9783 220 // Setup the new socket events
SED9008 0:5de940cf9783 221 pConnectedSock->setOnEvent(&onConnectedTCPSocketEvent);
SED9008 0:5de940cf9783 222 // We can find out from where the connection is coming by looking at the
SED9008 0:5de940cf9783 223 // Host parameter of the accept() method
SED9008 0:5de940cf9783 224 IpAddr clientIp = client.getIp();
SED9008 0:5de940cf9783 225 printf("Listening: Incoming TCP connection from %d.%d.%d.%d\r\n",
SED9008 0:5de940cf9783 226 clientIp[0], clientIp[1], clientIp[2], clientIp[3]);
SED9008 0:5de940cf9783 227 break;
SED9008 0:5de940cf9783 228 // the following cases will not happen
SED9008 0:5de940cf9783 229 case TCPSOCKET_CONNECTED:
SED9008 0:5de940cf9783 230 printf("Listening: TCP Socket Connected\r\n");
SED9008 0:5de940cf9783 231 break;
SED9008 0:5de940cf9783 232 case TCPSOCKET_WRITEABLE:
SED9008 0:5de940cf9783 233 printf("Listening: TCP Socket Writable\r\n");
SED9008 0:5de940cf9783 234 break;
SED9008 0:5de940cf9783 235 case TCPSOCKET_READABLE:
SED9008 0:5de940cf9783 236 printf("Listening: TCP Socket Readable\r\n");
SED9008 0:5de940cf9783 237 break;
SED9008 0:5de940cf9783 238 case TCPSOCKET_CONTIMEOUT:
SED9008 0:5de940cf9783 239 printf("Listening: TCP Socket Timeout\r\n");
SED9008 0:5de940cf9783 240 break;
SED9008 0:5de940cf9783 241 case TCPSOCKET_CONRST:
SED9008 0:5de940cf9783 242 printf("Listening: TCP Socket CONRST\r\n");
SED9008 0:5de940cf9783 243 break;
SED9008 0:5de940cf9783 244 case TCPSOCKET_CONABRT:
SED9008 0:5de940cf9783 245 printf("Listening: TCP Socket CONABRT\r\n");
SED9008 0:5de940cf9783 246 break;
SED9008 0:5de940cf9783 247 case TCPSOCKET_ERROR:
SED9008 0:5de940cf9783 248 printf("Listening: TCP Socket Error\r\n");
SED9008 0:5de940cf9783 249 break;
SED9008 0:5de940cf9783 250 case TCPSOCKET_DISCONNECTED:
SED9008 0:5de940cf9783 251 //Close socket...
SED9008 0:5de940cf9783 252 printf("Listening: TCP Socket Disconnected\r\n");
SED9008 0:5de940cf9783 253 ListeningSock.close();
SED9008 0:5de940cf9783 254 break;
SED9008 0:5de940cf9783 255 default:
SED9008 0:5de940cf9783 256 printf("DEFAULT\r\n");
SED9008 0:5de940cf9783 257 };
SED9008 0:5de940cf9783 258 }