there is a problem. i cant send data continuously.
Dependencies: EthernetNetIf mbed HMC6352 ITG3200 ADXL345 IMUfilter
main.cpp@0:711905e937b9, 2011-07-24 (annotated)
- Committer:
- fyazgan
- Date:
- Sun Jul 24 19:49:51 2011 +0000
- Revision:
- 0:711905e937b9
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fyazgan | 0:711905e937b9 | 1 | #include "mbed.h" |
fyazgan | 0:711905e937b9 | 2 | #include "EthernetNetIf.h" |
fyazgan | 0:711905e937b9 | 3 | #include "TCPSocket.h" |
fyazgan | 0:711905e937b9 | 4 | #include "IMUTICKER.h" |
fyazgan | 0:711905e937b9 | 5 | #include "SCP1000.h" |
fyazgan | 0:711905e937b9 | 6 | #include "HMC6352.h" |
fyazgan | 0:711905e937b9 | 7 | #include <iostream> |
fyazgan | 0:711905e937b9 | 8 | #include <math.h> |
fyazgan | 0:711905e937b9 | 9 | |
fyazgan | 0:711905e937b9 | 10 | |
fyazgan | 0:711905e937b9 | 11 | |
fyazgan | 0:711905e937b9 | 12 | |
fyazgan | 0:711905e937b9 | 13 | |
fyazgan | 0:711905e937b9 | 14 | |
fyazgan | 0:711905e937b9 | 15 | |
fyazgan | 0:711905e937b9 | 16 | |
fyazgan | 0:711905e937b9 | 17 | SCP1000 scp1(p11,p12,p13,p14); |
fyazgan | 0:711905e937b9 | 18 | SCP1000 scp2(p11,p12,p13,p15); |
fyazgan | 0:711905e937b9 | 19 | HMC6352 compass(p28, p27); |
fyazgan | 0:711905e937b9 | 20 | |
fyazgan | 0:711905e937b9 | 21 | |
fyazgan | 0:711905e937b9 | 22 | //++++++++++++++++++++++++++Ethernet++++++++++++++++++++++++++++++ |
fyazgan | 0:711905e937b9 | 23 | int t0=0,t1=0,t2=0,t3=0; |
fyazgan | 0:711905e937b9 | 24 | Timer tmr; |
fyazgan | 0:711905e937b9 | 25 | |
fyazgan | 0:711905e937b9 | 26 | void ByteToChar(int bytes[29], char chars[29], unsigned int count) { |
fyazgan | 0:711905e937b9 | 27 | for (unsigned int i = 0; i < count; i++) |
fyazgan | 0:711905e937b9 | 28 | chars[i] = (char)bytes[i]; |
fyazgan | 0:711905e937b9 | 29 | } |
fyazgan | 0:711905e937b9 | 30 | |
fyazgan | 0:711905e937b9 | 31 | |
fyazgan | 0:711905e937b9 | 32 | |
fyazgan | 0:711905e937b9 | 33 | EthernetNetIf eth( |
fyazgan | 0:711905e937b9 | 34 | IpAddr(10,0,0,100), //IP Address |
fyazgan | 0:711905e937b9 | 35 | IpAddr(255,255,255,0), //Network Mask |
fyazgan | 0:711905e937b9 | 36 | IpAddr(10,0,0,1), //Gateway |
fyazgan | 0:711905e937b9 | 37 | IpAddr(10,0,0,1) //DNS |
fyazgan | 0:711905e937b9 | 38 | ); |
fyazgan | 0:711905e937b9 | 39 | |
fyazgan | 0:711905e937b9 | 40 | |
fyazgan | 0:711905e937b9 | 41 | // EthernetNetIf eth; |
fyazgan | 0:711905e937b9 | 42 | |
fyazgan | 0:711905e937b9 | 43 | DigitalOut connectionLED(LED2); |
fyazgan | 0:711905e937b9 | 44 | DigitalOut statusLED(LED1); |
fyazgan | 0:711905e937b9 | 45 | |
fyazgan | 0:711905e937b9 | 46 | TCPSocket socket; |
fyazgan | 0:711905e937b9 | 47 | |
fyazgan | 0:711905e937b9 | 48 | void onTCPSocketEvent(TCPSocketEvent e) { |
fyazgan | 0:711905e937b9 | 49 | switch (e) { |
fyazgan | 0:711905e937b9 | 50 | case TCPSOCKET_CONNECTED: { |
fyazgan | 0:711905e937b9 | 51 | // Tells me that I'm connected to the server |
fyazgan | 0:711905e937b9 | 52 | pc.printf("Connected\r\n"); |
fyazgan | 0:711905e937b9 | 53 | connectionLED = 1; |
fyazgan | 0:711905e937b9 | 54 | wait(0.5); |
fyazgan | 0:711905e937b9 | 55 | |
fyazgan | 0:711905e937b9 | 56 | int b[29] = { 0, 0, 0, 0, 2,0,0,0,0,0,0,0,85,49 }; |
fyazgan | 0:711905e937b9 | 57 | char m[29]; |
fyazgan | 0:711905e937b9 | 58 | ByteToChar(b,m,29); |
fyazgan | 0:711905e937b9 | 59 | int sendError = socket.send(m,14); |
fyazgan | 0:711905e937b9 | 60 | } |
fyazgan | 0:711905e937b9 | 61 | break; |
fyazgan | 0:711905e937b9 | 62 | |
fyazgan | 0:711905e937b9 | 63 | case TCPSOCKET_READABLE: { |
fyazgan | 0:711905e937b9 | 64 | |
fyazgan | 0:711905e937b9 | 65 | |
fyazgan | 0:711905e937b9 | 66 | // Get the data and send them back to pc |
fyazgan | 0:711905e937b9 | 67 | char rxBuffer[29]; |
fyazgan | 0:711905e937b9 | 68 | int len = socket.recv(rxBuffer, sizeof(rxBuffer)-1); |
fyazgan | 0:711905e937b9 | 69 | if ( len != 0 ) { |
fyazgan | 0:711905e937b9 | 70 | rxBuffer[len] = '\0'; |
fyazgan | 0:711905e937b9 | 71 | |
fyazgan | 0:711905e937b9 | 72 | pc.printf("Received:\r\n%s\r\n"); |
fyazgan | 0:711905e937b9 | 73 | |
fyazgan | 0:711905e937b9 | 74 | int sendError = socket.send(rxBuffer,(strlen(rxBuffer) + 1)); |
fyazgan | 0:711905e937b9 | 75 | } |
fyazgan | 0:711905e937b9 | 76 | } |
fyazgan | 0:711905e937b9 | 77 | break; |
fyazgan | 0:711905e937b9 | 78 | |
fyazgan | 0:711905e937b9 | 79 | case TCPSOCKET_WRITEABLE: { |
fyazgan | 0:711905e937b9 | 80 | |
fyazgan | 0:711905e937b9 | 81 | |
fyazgan | 0:711905e937b9 | 82 | char m[29]; |
fyazgan | 0:711905e937b9 | 83 | int b[29] = { 2, 0, 0, 0, 2,0,0,0,15,0,0,0,85,49,64,t0,t1,t2,t3,255,255,255,255,255,255,255,255,255,255 }; |
fyazgan | 0:711905e937b9 | 84 | |
fyazgan | 0:711905e937b9 | 85 | ByteToChar(b,m,27); |
fyazgan | 0:711905e937b9 | 86 | |
fyazgan | 0:711905e937b9 | 87 | int sendError = socket.send(m,sizeof(m)); |
fyazgan | 0:711905e937b9 | 88 | |
fyazgan | 0:711905e937b9 | 89 | |
fyazgan | 0:711905e937b9 | 90 | } |
fyazgan | 0:711905e937b9 | 91 | break; |
fyazgan | 0:711905e937b9 | 92 | |
fyazgan | 0:711905e937b9 | 93 | case TCPSOCKET_CONTIMEOUT: |
fyazgan | 0:711905e937b9 | 94 | case TCPSOCKET_CONRST: |
fyazgan | 0:711905e937b9 | 95 | case TCPSOCKET_CONABRT: |
fyazgan | 0:711905e937b9 | 96 | case TCPSOCKET_ERROR: { |
fyazgan | 0:711905e937b9 | 97 | pc.printf("Connection Error: %i\r\n",e); |
fyazgan | 0:711905e937b9 | 98 | socket.close(); |
fyazgan | 0:711905e937b9 | 99 | } |
fyazgan | 0:711905e937b9 | 100 | break; |
fyazgan | 0:711905e937b9 | 101 | |
fyazgan | 0:711905e937b9 | 102 | case TCPSOCKET_DISCONNECTED: { |
fyazgan | 0:711905e937b9 | 103 | pc.printf("Disconnected\r\n"); |
fyazgan | 0:711905e937b9 | 104 | socket.close(); |
fyazgan | 0:711905e937b9 | 105 | connectionLED = 0; |
fyazgan | 0:711905e937b9 | 106 | } |
fyazgan | 0:711905e937b9 | 107 | break; |
fyazgan | 0:711905e937b9 | 108 | } |
fyazgan | 0:711905e937b9 | 109 | } |
fyazgan | 0:711905e937b9 | 110 | |
fyazgan | 0:711905e937b9 | 111 | string binary( unsigned long n ) { |
fyazgan | 0:711905e937b9 | 112 | char result[ (sizeof( unsigned long ) * 8) + 1 ]; |
fyazgan | 0:711905e937b9 | 113 | unsigned index = sizeof( unsigned long ) * 8; |
fyazgan | 0:711905e937b9 | 114 | result[ index ] = '\0'; |
fyazgan | 0:711905e937b9 | 115 | |
fyazgan | 0:711905e937b9 | 116 | do result[ --index ] = '0' + (n & 1); |
fyazgan | 0:711905e937b9 | 117 | while (n >>= 1); |
fyazgan | 0:711905e937b9 | 118 | |
fyazgan | 0:711905e937b9 | 119 | return string( result + index ); |
fyazgan | 0:711905e937b9 | 120 | } |
fyazgan | 0:711905e937b9 | 121 | |
fyazgan | 0:711905e937b9 | 122 | |
fyazgan | 0:711905e937b9 | 123 | int main() { |
fyazgan | 0:711905e937b9 | 124 | pc.baud(9600); |
fyazgan | 0:711905e937b9 | 125 | |
fyazgan | 0:711905e937b9 | 126 | start(); |
fyazgan | 0:711905e937b9 | 127 | |
fyazgan | 0:711905e937b9 | 128 | compass.setOpMode(HMC6352_CONTINUOUS, 1, 20); |
fyazgan | 0:711905e937b9 | 129 | |
fyazgan | 0:711905e937b9 | 130 | //+++++++++++++++++++++++++++++ETHERNET+++++++++++++++++++++++++ |
fyazgan | 0:711905e937b9 | 131 | |
fyazgan | 0:711905e937b9 | 132 | // Ethernet setup, callback setup and server declaration |
fyazgan | 0:711905e937b9 | 133 | EthernetErr ethErr = eth.setup(); |
fyazgan | 0:711905e937b9 | 134 | if (ethErr) |
fyazgan | 0:711905e937b9 | 135 | error("Error %d in setup.\r\n", ethErr); |
fyazgan | 0:711905e937b9 | 136 | |
fyazgan | 0:711905e937b9 | 137 | socket.setOnEvent(&onTCPSocketEvent); |
fyazgan | 0:711905e937b9 | 138 | Host server(IpAddr(10, 0, 0, 7), 1000); |
fyazgan | 0:711905e937b9 | 139 | TCPSocketErr bindStatus = socket.connect(server); |
fyazgan | 0:711905e937b9 | 140 | if (bindStatus != TCPSOCKET_OK) |
fyazgan | 0:711905e937b9 | 141 | pc.printf("Bind error\r\n"); |
fyazgan | 0:711905e937b9 | 142 | |
fyazgan | 0:711905e937b9 | 143 | tmr.start(); |
fyazgan | 0:711905e937b9 | 144 | |
fyazgan | 0:711905e937b9 | 145 | |
fyazgan | 0:711905e937b9 | 146 | while (true) |
fyazgan | 0:711905e937b9 | 147 | { |
fyazgan | 0:711905e937b9 | 148 | |
fyazgan | 0:711905e937b9 | 149 | Net::poll(); |
fyazgan | 0:711905e937b9 | 150 | |
fyazgan | 0:711905e937b9 | 151 | |
fyazgan | 0:711905e937b9 | 152 | if (tmr.read() > 2) |
fyazgan | 0:711905e937b9 | 153 | { |
fyazgan | 0:711905e937b9 | 154 | tmr.reset(); |
fyazgan | 0:711905e937b9 | 155 | |
fyazgan | 0:711905e937b9 | 156 | statusLED = !statusLED; |
fyazgan | 0:711905e937b9 | 157 | |
fyazgan | 0:711905e937b9 | 158 | pc.printf("%f,%f,%f\n\r",toDegrees(imuFilter.getRoll()),toDegrees(imuFilter.getPitch()),toDegrees(imuFilter.getYaw())); |
fyazgan | 0:711905e937b9 | 159 | pc.printf("The pressure is %d Pa,temperature is %f C\n\r", scp1.readPressure(), scp1.readTemperature()/100); |
fyazgan | 0:711905e937b9 | 160 | pc.printf("The pressure is %d Pa,temperature is %f C\n\r", scp2.readPressure(), scp2.readTemperature()/100); |
fyazgan | 0:711905e937b9 | 161 | pc.printf("Heading is: %f", compass.sample() / 10.0); |
fyazgan | 0:711905e937b9 | 162 | |
fyazgan | 0:711905e937b9 | 163 | float rol=toDegrees(imuFilter.getRoll()); |
fyazgan | 0:711905e937b9 | 164 | int i=0; |
fyazgan | 0:711905e937b9 | 165 | int r[16]; |
fyazgan | 0:711905e937b9 | 166 | double bol=3; |
fyazgan | 0:711905e937b9 | 167 | while (rol>2) { |
fyazgan | 0:711905e937b9 | 168 | bol=int(floor(rol/2)); |
fyazgan | 0:711905e937b9 | 169 | r[i]=floor(rol-(bol*2)); |
fyazgan | 0:711905e937b9 | 170 | rol=((rol-r[i])/2); |
fyazgan | 0:711905e937b9 | 171 | i=i+1; |
fyazgan | 0:711905e937b9 | 172 | } |
fyazgan | 0:711905e937b9 | 173 | bol=int(floor(rol/2)); |
fyazgan | 0:711905e937b9 | 174 | r[i]=floor(rol-(bol*2)); |
fyazgan | 0:711905e937b9 | 175 | r[i+1]=1; |
fyazgan | 0:711905e937b9 | 176 | for (int ii=i+2; ii<16; ii++) { |
fyazgan | 0:711905e937b9 | 177 | r[ii]=0; |
fyazgan | 0:711905e937b9 | 178 | } |
fyazgan | 0:711905e937b9 | 179 | int kov[16]; |
fyazgan | 0:711905e937b9 | 180 | for (int ii=0; ii<8; ii++) { |
fyazgan | 0:711905e937b9 | 181 | kov[ii]=r[ii]; |
fyazgan | 0:711905e937b9 | 182 | r[ii]=r[15-ii]; |
fyazgan | 0:711905e937b9 | 183 | r[15-ii]=kov[ii]; |
fyazgan | 0:711905e937b9 | 184 | } |
fyazgan | 0:711905e937b9 | 185 | |
fyazgan | 0:711905e937b9 | 186 | rol=toDegrees(imuFilter.getPitch()); |
fyazgan | 0:711905e937b9 | 187 | i=0; |
fyazgan | 0:711905e937b9 | 188 | int p[16]; |
fyazgan | 0:711905e937b9 | 189 | bol=3; |
fyazgan | 0:711905e937b9 | 190 | while (rol>2) { |
fyazgan | 0:711905e937b9 | 191 | bol=(floor(rol/2)); |
fyazgan | 0:711905e937b9 | 192 | p[i]=floor(rol-(bol*2)); |
fyazgan | 0:711905e937b9 | 193 | rol=((rol-p[i])/2); |
fyazgan | 0:711905e937b9 | 194 | i=i+1; |
fyazgan | 0:711905e937b9 | 195 | } |
fyazgan | 0:711905e937b9 | 196 | bol=int(floor(rol/2)); |
fyazgan | 0:711905e937b9 | 197 | p[i]=floor(rol-(bol*2)); |
fyazgan | 0:711905e937b9 | 198 | p[i+1]=1; |
fyazgan | 0:711905e937b9 | 199 | for (int ii=i+2; ii<16; ii++) { |
fyazgan | 0:711905e937b9 | 200 | p[ii]=0; |
fyazgan | 0:711905e937b9 | 201 | } |
fyazgan | 0:711905e937b9 | 202 | for (int ii=0; ii<8; ii++) { |
fyazgan | 0:711905e937b9 | 203 | kov[ii]=p[ii]; |
fyazgan | 0:711905e937b9 | 204 | p[ii]=p[15-ii]; |
fyazgan | 0:711905e937b9 | 205 | p[15-ii]=kov[ii]; |
fyazgan | 0:711905e937b9 | 206 | } |
fyazgan | 0:711905e937b9 | 207 | |
fyazgan | 0:711905e937b9 | 208 | rol=toDegrees(imuFilter.getYaw()); |
fyazgan | 0:711905e937b9 | 209 | i=0; |
fyazgan | 0:711905e937b9 | 210 | int y[16]; |
fyazgan | 0:711905e937b9 | 211 | bol=3; |
fyazgan | 0:711905e937b9 | 212 | while (rol>2) { |
fyazgan | 0:711905e937b9 | 213 | bol=(floor(rol/2)); |
fyazgan | 0:711905e937b9 | 214 | y[i]=floor(rol-(bol*2)); |
fyazgan | 0:711905e937b9 | 215 | rol=((rol-y[i])/2); |
fyazgan | 0:711905e937b9 | 216 | i=i+1; |
fyazgan | 0:711905e937b9 | 217 | } |
fyazgan | 0:711905e937b9 | 218 | bol=(floor(rol/2)); |
fyazgan | 0:711905e937b9 | 219 | y[i]=floor(rol-(bol*2)); |
fyazgan | 0:711905e937b9 | 220 | y[i+1]=1; |
fyazgan | 0:711905e937b9 | 221 | for (int ii=i+2; ii<16; ii++) { |
fyazgan | 0:711905e937b9 | 222 | y[ii]=0; |
fyazgan | 0:711905e937b9 | 223 | } |
fyazgan | 0:711905e937b9 | 224 | for (int ii=0; ii<8; ii++) { |
fyazgan | 0:711905e937b9 | 225 | kov[ii]=y[ii]; |
fyazgan | 0:711905e937b9 | 226 | y[ii]=y[15-ii]; |
fyazgan | 0:711905e937b9 | 227 | y[15-ii]=kov[ii]; |
fyazgan | 0:711905e937b9 | 228 | } |
fyazgan | 0:711905e937b9 | 229 | |
fyazgan | 0:711905e937b9 | 230 | char pp[32]; |
fyazgan | 0:711905e937b9 | 231 | for (int ii=0; ii<9; ii++) { |
fyazgan | 0:711905e937b9 | 232 | pp[ii]=p[ii+8]; |
fyazgan | 0:711905e937b9 | 233 | } |
fyazgan | 0:711905e937b9 | 234 | pp[8]=p[7]; |
fyazgan | 0:711905e937b9 | 235 | |
fyazgan | 0:711905e937b9 | 236 | for (int ii=0; ii<9; ii++) { |
fyazgan | 0:711905e937b9 | 237 | pp[ii+9]=r[ii+8]; |
fyazgan | 0:711905e937b9 | 238 | } |
fyazgan | 0:711905e937b9 | 239 | pp[17]=r[7]; |
fyazgan | 0:711905e937b9 | 240 | |
fyazgan | 0:711905e937b9 | 241 | for (int ii=0; ii<9; ii++) { |
fyazgan | 0:711905e937b9 | 242 | pp[ii+18]=y[ii+8]; |
fyazgan | 0:711905e937b9 | 243 | } |
fyazgan | 0:711905e937b9 | 244 | pp[26]=y[7]; |
fyazgan | 0:711905e937b9 | 245 | |
fyazgan | 0:711905e937b9 | 246 | for (int ii=0; ii<6; ii++) { |
fyazgan | 0:711905e937b9 | 247 | pp[ii+27]=0; |
fyazgan | 0:711905e937b9 | 248 | } |
fyazgan | 0:711905e937b9 | 249 | |
fyazgan | 0:711905e937b9 | 250 | char by[3][7]; |
fyazgan | 0:711905e937b9 | 251 | |
fyazgan | 0:711905e937b9 | 252 | for (int ii=0; ii<4; ii++) { |
fyazgan | 0:711905e937b9 | 253 | for (int jj=0; jj<8; jj++) { |
fyazgan | 0:711905e937b9 | 254 | by[ii][jj]=pp[(ii*8)+jj]; |
fyazgan | 0:711905e937b9 | 255 | } |
fyazgan | 0:711905e937b9 | 256 | } |
fyazgan | 0:711905e937b9 | 257 | |
fyazgan | 0:711905e937b9 | 258 | char by0[8],by1[8],by2[8],by3[8]; |
fyazgan | 0:711905e937b9 | 259 | for (int ii=0; ii<8; ii++) { |
fyazgan | 0:711905e937b9 | 260 | by0[ii]=by[0][ii]; |
fyazgan | 0:711905e937b9 | 261 | } |
fyazgan | 0:711905e937b9 | 262 | for (int ii=0; ii<8; ii++) { |
fyazgan | 0:711905e937b9 | 263 | by1[ii]=by[1][ii]; |
fyazgan | 0:711905e937b9 | 264 | } |
fyazgan | 0:711905e937b9 | 265 | for (int ii=0; ii<8; ii++) { |
fyazgan | 0:711905e937b9 | 266 | by2[ii]=by[2][ii]; |
fyazgan | 0:711905e937b9 | 267 | } |
fyazgan | 0:711905e937b9 | 268 | for (int ii=0; ii<8; ii++) { |
fyazgan | 0:711905e937b9 | 269 | by3[ii]=by[3][ii]; |
fyazgan | 0:711905e937b9 | 270 | } |
fyazgan | 0:711905e937b9 | 271 | |
fyazgan | 0:711905e937b9 | 272 | |
fyazgan | 0:711905e937b9 | 273 | |
fyazgan | 0:711905e937b9 | 274 | for (int ii=0; ii<8; ii++) { |
fyazgan | 0:711905e937b9 | 275 | i=by0[ii]; |
fyazgan | 0:711905e937b9 | 276 | t0=t0+(i*pow(2.0,ii)); |
fyazgan | 0:711905e937b9 | 277 | } |
fyazgan | 0:711905e937b9 | 278 | for (int ii=0; ii<8; ii++) { |
fyazgan | 0:711905e937b9 | 279 | i=by1[ii]; |
fyazgan | 0:711905e937b9 | 280 | t1=t1+(i*pow(2.0,ii)); |
fyazgan | 0:711905e937b9 | 281 | } |
fyazgan | 0:711905e937b9 | 282 | for (int ii=0; ii<8; ii++) { |
fyazgan | 0:711905e937b9 | 283 | i=by2[ii]; |
fyazgan | 0:711905e937b9 | 284 | t2=t2+(i*pow(2.0,ii)); |
fyazgan | 0:711905e937b9 | 285 | } |
fyazgan | 0:711905e937b9 | 286 | for (int ii=0; ii<8; ii++) { |
fyazgan | 0:711905e937b9 | 287 | i=by3[ii]; |
fyazgan | 0:711905e937b9 | 288 | |
fyazgan | 0:711905e937b9 | 289 | t3=t3+(i*pow(2.0,ii)); |
fyazgan | 0:711905e937b9 | 290 | } |
fyazgan | 0:711905e937b9 | 291 | //----------------------------------------------------- |
fyazgan | 0:711905e937b9 | 292 | } |
fyazgan | 0:711905e937b9 | 293 | |
fyazgan | 0:711905e937b9 | 294 | //+++++++++++++++++++++++++++++IMU+++++++++++++++++++++++++++++++++++ |
fyazgan | 0:711905e937b9 | 295 | |
fyazgan | 0:711905e937b9 | 296 | } |
fyazgan | 0:711905e937b9 | 297 | } |