![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Gyro output to TCP in degrees with calibration and correction
Dependencies: EthernetNetIf mbed
main.cpp@1:a88d1309f810, 2012-03-02 (annotated)
- Committer:
- SED9008
- Date:
- Fri Mar 02 22:31:23 2012 +0000
- Revision:
- 1:a88d1309f810
- Parent:
- 0:5de940cf9783
- Child:
- 2:96f81996a332
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:a88d1309f810 | 30 | |
SED9008 | 1:a88d1309f810 | 31 | //############## INCLUDE FILES ##############// |
SED9008 | 0:5de940cf9783 | 32 | #include "mbed.h" |
SED9008 | 0:5de940cf9783 | 33 | #include "EthernetNetIf.h" |
SED9008 | 0:5de940cf9783 | 34 | #include "TCPSocket.h" |
SED9008 | 0:5de940cf9783 | 35 | #include "ADXL345_I2C.h" |
SED9008 | 1:a88d1309f810 | 36 | //#############################################// |
SED9008 | 0:5de940cf9783 | 37 | |
SED9008 | 1:a88d1309f810 | 38 | //############## DEFINES ##############// |
SED9008 | 0:5de940cf9783 | 39 | #define TCP_LISTENING_PORT 1337 |
SED9008 | 1:a88d1309f810 | 40 | #define PI 3.14159265 |
SED9008 | 1:a88d1309f810 | 41 | //#############################################// |
SED9008 | 0:5de940cf9783 | 42 | |
SED9008 | 1:a88d1309f810 | 43 | //############## VARIABLES ##############// |
SED9008 | 1:a88d1309f810 | 44 | double x_cor = 0, |
SED9008 | 1:a88d1309f810 | 45 | y_cor = 0, |
SED9008 | 1:a88d1309f810 | 46 | cont[2] = {0,0}; |
SED9008 | 1:a88d1309f810 | 47 | int print = 0, |
SED9008 | 1:a88d1309f810 | 48 | go = 0, |
SED9008 | 1:a88d1309f810 | 49 | calibrated = 0; |
SED9008 | 1:a88d1309f810 | 50 | //#############################################// |
SED9008 | 1:a88d1309f810 | 51 | |
SED9008 | 1:a88d1309f810 | 52 | //##############PIN CONFIGURATION##############// |
SED9008 | 0:5de940cf9783 | 53 | DigitalOut led3(LED3, "led3"); |
SED9008 | 0:5de940cf9783 | 54 | DigitalOut led4(LED4, "led4"); |
SED9008 | 0:5de940cf9783 | 55 | DigitalIn init(p5); |
SED9008 | 0:5de940cf9783 | 56 | DigitalIn calibrate(p20); |
SED9008 | 0:5de940cf9783 | 57 | //I2C i2c(p9, p10); // sda, scl |
SED9008 | 0:5de940cf9783 | 58 | ADXL345_I2C acc(p9, p10); |
SED9008 | 1:a88d1309f810 | 59 | //#############################################// |
SED9008 | 0:5de940cf9783 | 60 | |
SED9008 | 1:a88d1309f810 | 61 | //############## PROTOTYPES ##############// |
SED9008 | 0:5de940cf9783 | 62 | void tcp_send(const char*); |
SED9008 | 0:5de940cf9783 | 63 | void tcp_sendIfConnected( const char* data ); |
SED9008 | 0:5de940cf9783 | 64 | void onConnectedTCPSocketEvent(TCPSocketEvent e); |
SED9008 | 0:5de940cf9783 | 65 | void onListeningTCPSocketEvent(TCPSocketEvent e); |
SED9008 | 1:a88d1309f810 | 66 | double getAngle(char axis, int precision); |
SED9008 | 1:a88d1309f810 | 67 | //#############################################// |
SED9008 | 0:5de940cf9783 | 68 | |
SED9008 | 1:a88d1309f810 | 69 | //############## ETHERNET CONFIG ##############// |
SED9008 | 0:5de940cf9783 | 70 | // EthernetNetIf eth; |
SED9008 | 0:5de940cf9783 | 71 | EthernetNetIf eth( |
SED9008 | 0:5de940cf9783 | 72 | IpAddr(192,168,0,16), //IP Address |
SED9008 | 0:5de940cf9783 | 73 | IpAddr(255,255,255,0), //Network Mask |
SED9008 | 1:a88d1309f810 | 74 | IpAddr(192,168,0,1), //Gateway |
SED9008 | 1:a88d1309f810 | 75 | IpAddr(192,168,0,1) //DNS |
SED9008 | 0:5de940cf9783 | 76 | ); |
SED9008 | 0:5de940cf9783 | 77 | |
SED9008 | 0:5de940cf9783 | 78 | TCPSocket ListeningSock; |
SED9008 | 0:5de940cf9783 | 79 | TCPSocket* pConnectedSock; // for ConnectedSock |
SED9008 | 0:5de940cf9783 | 80 | Host client; |
SED9008 | 0:5de940cf9783 | 81 | TCPSocketErr err; |
SED9008 | 1:a88d1309f810 | 82 | //#############################################// |
SED9008 | 1:a88d1309f810 | 83 | |
SED9008 | 0:5de940cf9783 | 84 | Ticker routine; |
SED9008 | 0:5de940cf9783 | 85 | |
SED9008 | 1:a88d1309f810 | 86 | //############## INTERRUPT ##############// |
SED9008 | 0:5de940cf9783 | 87 | void interrupt() |
SED9008 | 0:5de940cf9783 | 88 | { |
SED9008 | 0:5de940cf9783 | 89 | // int count = 0; |
SED9008 | 0:5de940cf9783 | 90 | // for (int address=0; address<256; address+=2) { |
SED9008 | 0:5de940cf9783 | 91 | // if (!i2c.write(address, NULL, 0)) { // 0 returned is ok |
SED9008 | 0:5de940cf9783 | 92 | // if(go) { |
SED9008 | 0:5de940cf9783 | 93 | // tcp_send(" - I2C device found at address:"); |
SED9008 | 0:5de940cf9783 | 94 | // char buffer [128]; |
SED9008 | 0:5de940cf9783 | 95 | // sprintf (buffer, "%i\n",address); |
SED9008 | 0:5de940cf9783 | 96 | // tcp_send(buffer); |
SED9008 | 0:5de940cf9783 | 97 | // } |
SED9008 | 0:5de940cf9783 | 98 | // count++; |
SED9008 | 0:5de940cf9783 | 99 | // } |
SED9008 | 0:5de940cf9783 | 100 | // } |
SED9008 | 1:a88d1309f810 | 101 | if(go){ |
SED9008 | 1:a88d1309f810 | 102 | if(calibrated){ |
SED9008 | 1:a88d1309f810 | 103 | if(print){ |
SED9008 | 1:a88d1309f810 | 104 | char buffer [128]; |
SED9008 | 1:a88d1309f810 | 105 | sprintf (buffer, "x:%f - y:%f\n",cont[0],cont[1]); |
SED9008 | 1:a88d1309f810 | 106 | tcp_send(buffer); |
SED9008 | 1:a88d1309f810 | 107 | print = 0; |
SED9008 | 1:a88d1309f810 | 108 | } |
SED9008 | 1:a88d1309f810 | 109 | } |
SED9008 | 1:a88d1309f810 | 110 | } |
SED9008 | 1:a88d1309f810 | 111 | } |
SED9008 | 1:a88d1309f810 | 112 | //#############################################// |
SED9008 | 0:5de940cf9783 | 113 | |
SED9008 | 1:a88d1309f810 | 114 | //############## MAIN ##############// |
SED9008 | 0:5de940cf9783 | 115 | int main() { |
SED9008 | 1:a88d1309f810 | 116 | //##############ETHERNET&TCP INIT##############// |
SED9008 | 0:5de940cf9783 | 117 | EthernetErr ethErr = eth.setup(); |
SED9008 | 0:5de940cf9783 | 118 | if(ethErr) |
SED9008 | 0:5de940cf9783 | 119 | { |
SED9008 | 0:5de940cf9783 | 120 | return -1; |
SED9008 | 1:a88d1309f810 | 121 | } |
SED9008 | 1:a88d1309f810 | 122 | IpAddr ip = eth.getIp(); |
SED9008 | 0:5de940cf9783 | 123 | // Set the callbacks for Listening |
SED9008 | 1:a88d1309f810 | 124 | ListeningSock.setOnEvent(&onListeningTCPSocketEvent); |
SED9008 | 0:5de940cf9783 | 125 | // bind and listen on TCP |
SED9008 | 0:5de940cf9783 | 126 | err=ListeningSock.bind(Host(IpAddr(), TCP_LISTENING_PORT)); |
SED9008 | 0:5de940cf9783 | 127 | if(err) |
SED9008 | 0:5de940cf9783 | 128 | { |
SED9008 | 0:5de940cf9783 | 129 | //Deal with that error... |
SED9008 | 1:a88d1309f810 | 130 | } |
SED9008 | 0:5de940cf9783 | 131 | err=ListeningSock.listen(); // Starts listening |
SED9008 | 0:5de940cf9783 | 132 | if(err) |
SED9008 | 0:5de940cf9783 | 133 | { |
SED9008 | 0:5de940cf9783 | 134 | //listening error |
SED9008 | 0:5de940cf9783 | 135 | } |
SED9008 | 1:a88d1309f810 | 136 | //#############################################// |
SED9008 | 0:5de940cf9783 | 137 | |
SED9008 | 1:a88d1309f810 | 138 | //#############ACCELEROMETER INIT##############// |
SED9008 | 0:5de940cf9783 | 139 | //Go into standby mode to configure the device. |
SED9008 | 0:5de940cf9783 | 140 | acc.setPowerControl(0x00); |
SED9008 | 0:5de940cf9783 | 141 | //Full resolution, +/-16g, 4mg/LSB. |
SED9008 | 1:a88d1309f810 | 142 | acc.setDataFormatControl(0x00); |
SED9008 | 0:5de940cf9783 | 143 | //3.2kHz data rate. |
SED9008 | 0:5de940cf9783 | 144 | acc.setDataRate(ADXL345_3200HZ); |
SED9008 | 0:5de940cf9783 | 145 | //Measurement mode. |
SED9008 | 0:5de940cf9783 | 146 | acc.setPowerControl(0x08); |
SED9008 | 0:5de940cf9783 | 147 | wait(0.1); |
SED9008 | 1:a88d1309f810 | 148 | //#############################################// |
SED9008 | 1:a88d1309f810 | 149 | |
SED9008 | 0:5de940cf9783 | 150 | Timer tmr; |
SED9008 | 0:5de940cf9783 | 151 | tmr.start(); |
SED9008 | 1:a88d1309f810 | 152 | |
SED9008 | 0:5de940cf9783 | 153 | while(true) |
SED9008 | 0:5de940cf9783 | 154 | { |
SED9008 | 0:5de940cf9783 | 155 | Net::poll(); |
SED9008 | 0:5de940cf9783 | 156 | while(init) |
SED9008 | 0:5de940cf9783 | 157 | { |
SED9008 | 0:5de940cf9783 | 158 | if(!init) |
SED9008 | 0:5de940cf9783 | 159 | { |
SED9008 | 0:5de940cf9783 | 160 | tcp_send("You are a go!\n"); |
SED9008 | 0:5de940cf9783 | 161 | led3 = 1; |
SED9008 | 0:5de940cf9783 | 162 | go = 1; |
SED9008 | 0:5de940cf9783 | 163 | } |
SED9008 | 0:5de940cf9783 | 164 | } |
SED9008 | 0:5de940cf9783 | 165 | if(tmr.read() > 0.2) // sec |
SED9008 | 0:5de940cf9783 | 166 | { |
SED9008 | 0:5de940cf9783 | 167 | led4=!led4; //Show that we are alive |
SED9008 | 0:5de940cf9783 | 168 | tmr.reset(); |
SED9008 | 0:5de940cf9783 | 169 | } |
SED9008 | 0:5de940cf9783 | 170 | if(calibrate){ |
SED9008 | 0:5de940cf9783 | 171 | while(calibrate){}; |
SED9008 | 1:a88d1309f810 | 172 | x_cor = getAngle('x',100); |
SED9008 | 1:a88d1309f810 | 173 | y_cor = getAngle('y',100); |
SED9008 | 1:a88d1309f810 | 174 | |
SED9008 | 1:a88d1309f810 | 175 | char buffer [128]; |
SED9008 | 1:a88d1309f810 | 176 | sprintf (buffer, "Calibration values:\n"); |
SED9008 | 1:a88d1309f810 | 177 | tcp_send(buffer); |
SED9008 | 1:a88d1309f810 | 178 | sprintf (buffer, "x:%f - y:%f\n",x_cor, y_cor); |
SED9008 | 1:a88d1309f810 | 179 | tcp_send(buffer); |
SED9008 | 1:a88d1309f810 | 180 | sprintf (buffer, "------------------------\n"); |
SED9008 | 1:a88d1309f810 | 181 | tcp_send(buffer); |
SED9008 | 1:a88d1309f810 | 182 | |
SED9008 | 1:a88d1309f810 | 183 | calibrated = 1; |
SED9008 | 1:a88d1309f810 | 184 | |
SED9008 | 1:a88d1309f810 | 185 | wait(0.1); |
SED9008 | 1:a88d1309f810 | 186 | |
SED9008 | 1:a88d1309f810 | 187 | routine.attach_us(&interrupt,10); |
SED9008 | 1:a88d1309f810 | 188 | } |
SED9008 | 1:a88d1309f810 | 189 | if(calibrated){ |
SED9008 | 1:a88d1309f810 | 190 | |
SED9008 | 1:a88d1309f810 | 191 | cont[0] = getAngle('x',10) - x_cor; |
SED9008 | 1:a88d1309f810 | 192 | cont[1] = getAngle('y',10) - y_cor; |
SED9008 | 1:a88d1309f810 | 193 | |
SED9008 | 1:a88d1309f810 | 194 | print = 1; |
SED9008 | 0:5de940cf9783 | 195 | } |
SED9008 | 0:5de940cf9783 | 196 | } |
SED9008 | 0:5de940cf9783 | 197 | } |
SED9008 | 1:a88d1309f810 | 198 | //#############################################// |
SED9008 | 1:a88d1309f810 | 199 | |
SED9008 | 1:a88d1309f810 | 200 | //############## SUBROUTINES ##############// |
SED9008 | 0:5de940cf9783 | 201 | |
SED9008 | 0:5de940cf9783 | 202 | void tcp_send( const char* data ){ |
SED9008 | 0:5de940cf9783 | 203 | int len = strlen(data); |
SED9008 | 0:5de940cf9783 | 204 | pConnectedSock->send(data, len); |
SED9008 | 0:5de940cf9783 | 205 | } |
SED9008 | 0:5de940cf9783 | 206 | |
SED9008 | 0:5de940cf9783 | 207 | void onConnectedTCPSocketEvent(TCPSocketEvent e) |
SED9008 | 0:5de940cf9783 | 208 | { |
SED9008 | 0:5de940cf9783 | 209 | switch(e) |
SED9008 | 0:5de940cf9783 | 210 | { |
SED9008 | 0:5de940cf9783 | 211 | case TCPSOCKET_CONNECTED: |
SED9008 | 0:5de940cf9783 | 212 | break; |
SED9008 | 0:5de940cf9783 | 213 | case TCPSOCKET_WRITEABLE: |
SED9008 | 0:5de940cf9783 | 214 | //Can now write some data... |
SED9008 | 0:5de940cf9783 | 215 | printf("TCP Socket Writable\r\n"); |
SED9008 | 0:5de940cf9783 | 216 | break; |
SED9008 | 0:5de940cf9783 | 217 | case TCPSOCKET_READABLE: |
SED9008 | 0:5de940cf9783 | 218 | //Can now read dome data... |
SED9008 | 0:5de940cf9783 | 219 | // Read in any available data into the buffer |
SED9008 | 0:5de940cf9783 | 220 | //char buff[128]; |
SED9008 | 0:5de940cf9783 | 221 | //while ( int len = pConnectedSock->recv(buff, 128) ) { |
SED9008 | 0:5de940cf9783 | 222 | // And send straight back out again |
SED9008 | 0:5de940cf9783 | 223 | // pConnectedSock->send(buff, len); |
SED9008 | 0:5de940cf9783 | 224 | // buff[len]=0; // make terminater |
SED9008 | 0:5de940cf9783 | 225 | //} |
SED9008 | 0:5de940cf9783 | 226 | break; |
SED9008 | 0:5de940cf9783 | 227 | case TCPSOCKET_CONTIMEOUT: |
SED9008 | 0:5de940cf9783 | 228 | printf("TCP Socket Timeout\r\n"); |
SED9008 | 0:5de940cf9783 | 229 | break; |
SED9008 | 0:5de940cf9783 | 230 | case TCPSOCKET_CONRST: |
SED9008 | 0:5de940cf9783 | 231 | printf("TCP Socket CONRST\r\n"); |
SED9008 | 0:5de940cf9783 | 232 | break; |
SED9008 | 0:5de940cf9783 | 233 | case TCPSOCKET_CONABRT: |
SED9008 | 0:5de940cf9783 | 234 | printf("TCP Socket CONABRT\r\n"); |
SED9008 | 0:5de940cf9783 | 235 | break; |
SED9008 | 0:5de940cf9783 | 236 | case TCPSOCKET_ERROR: |
SED9008 | 0:5de940cf9783 | 237 | printf("TCP Socket Error\r\n"); |
SED9008 | 0:5de940cf9783 | 238 | break; |
SED9008 | 0:5de940cf9783 | 239 | case TCPSOCKET_DISCONNECTED: |
SED9008 | 0:5de940cf9783 | 240 | //Close socket... |
SED9008 | 0:5de940cf9783 | 241 | printf("TCP Socket Disconnected\r\n"); |
SED9008 | 0:5de940cf9783 | 242 | pConnectedSock->close(); |
SED9008 | 0:5de940cf9783 | 243 | break; |
SED9008 | 0:5de940cf9783 | 244 | default: |
SED9008 | 0:5de940cf9783 | 245 | printf("DEFAULT\r\n"); |
SED9008 | 0:5de940cf9783 | 246 | } |
SED9008 | 0:5de940cf9783 | 247 | } |
SED9008 | 0:5de940cf9783 | 248 | |
SED9008 | 0:5de940cf9783 | 249 | void onListeningTCPSocketEvent(TCPSocketEvent e) |
SED9008 | 0:5de940cf9783 | 250 | { |
SED9008 | 0:5de940cf9783 | 251 | switch(e) |
SED9008 | 0:5de940cf9783 | 252 | { |
SED9008 | 0:5de940cf9783 | 253 | case TCPSOCKET_ACCEPT: |
SED9008 | 0:5de940cf9783 | 254 | printf("Listening: TCP Socket Accepted\r\n"); |
SED9008 | 0:5de940cf9783 | 255 | // Accepts connection from client and gets connected socket. |
SED9008 | 0:5de940cf9783 | 256 | err=ListeningSock.accept(&client, &pConnectedSock); |
SED9008 | 0:5de940cf9783 | 257 | if (err) { |
SED9008 | 0:5de940cf9783 | 258 | printf("onListeningTcpSocketEvent : Could not accept connection.\r\n"); |
SED9008 | 0:5de940cf9783 | 259 | return; //Error in accept, discard connection |
SED9008 | 0:5de940cf9783 | 260 | } |
SED9008 | 0:5de940cf9783 | 261 | // Setup the new socket events |
SED9008 | 0:5de940cf9783 | 262 | pConnectedSock->setOnEvent(&onConnectedTCPSocketEvent); |
SED9008 | 0:5de940cf9783 | 263 | // We can find out from where the connection is coming by looking at the |
SED9008 | 0:5de940cf9783 | 264 | // Host parameter of the accept() method |
SED9008 | 0:5de940cf9783 | 265 | IpAddr clientIp = client.getIp(); |
SED9008 | 0:5de940cf9783 | 266 | printf("Listening: Incoming TCP connection from %d.%d.%d.%d\r\n", |
SED9008 | 0:5de940cf9783 | 267 | clientIp[0], clientIp[1], clientIp[2], clientIp[3]); |
SED9008 | 0:5de940cf9783 | 268 | break; |
SED9008 | 0:5de940cf9783 | 269 | // the following cases will not happen |
SED9008 | 0:5de940cf9783 | 270 | case TCPSOCKET_CONNECTED: |
SED9008 | 0:5de940cf9783 | 271 | printf("Listening: TCP Socket Connected\r\n"); |
SED9008 | 0:5de940cf9783 | 272 | break; |
SED9008 | 0:5de940cf9783 | 273 | case TCPSOCKET_WRITEABLE: |
SED9008 | 0:5de940cf9783 | 274 | printf("Listening: TCP Socket Writable\r\n"); |
SED9008 | 0:5de940cf9783 | 275 | break; |
SED9008 | 0:5de940cf9783 | 276 | case TCPSOCKET_READABLE: |
SED9008 | 0:5de940cf9783 | 277 | printf("Listening: TCP Socket Readable\r\n"); |
SED9008 | 0:5de940cf9783 | 278 | break; |
SED9008 | 0:5de940cf9783 | 279 | case TCPSOCKET_CONTIMEOUT: |
SED9008 | 0:5de940cf9783 | 280 | printf("Listening: TCP Socket Timeout\r\n"); |
SED9008 | 0:5de940cf9783 | 281 | break; |
SED9008 | 0:5de940cf9783 | 282 | case TCPSOCKET_CONRST: |
SED9008 | 0:5de940cf9783 | 283 | printf("Listening: TCP Socket CONRST\r\n"); |
SED9008 | 0:5de940cf9783 | 284 | break; |
SED9008 | 0:5de940cf9783 | 285 | case TCPSOCKET_CONABRT: |
SED9008 | 0:5de940cf9783 | 286 | printf("Listening: TCP Socket CONABRT\r\n"); |
SED9008 | 0:5de940cf9783 | 287 | break; |
SED9008 | 0:5de940cf9783 | 288 | case TCPSOCKET_ERROR: |
SED9008 | 0:5de940cf9783 | 289 | printf("Listening: TCP Socket Error\r\n"); |
SED9008 | 0:5de940cf9783 | 290 | break; |
SED9008 | 0:5de940cf9783 | 291 | case TCPSOCKET_DISCONNECTED: |
SED9008 | 0:5de940cf9783 | 292 | //Close socket... |
SED9008 | 0:5de940cf9783 | 293 | printf("Listening: TCP Socket Disconnected\r\n"); |
SED9008 | 0:5de940cf9783 | 294 | ListeningSock.close(); |
SED9008 | 0:5de940cf9783 | 295 | break; |
SED9008 | 0:5de940cf9783 | 296 | default: |
SED9008 | 0:5de940cf9783 | 297 | printf("DEFAULT\r\n"); |
SED9008 | 0:5de940cf9783 | 298 | }; |
SED9008 | 0:5de940cf9783 | 299 | } |
SED9008 | 1:a88d1309f810 | 300 | |
SED9008 | 1:a88d1309f810 | 301 | double getAngle(char axis, int precision) |
SED9008 | 1:a88d1309f810 | 302 | { |
SED9008 | 1:a88d1309f810 | 303 | int measurement[3] = {0,0,0}; |
SED9008 | 1:a88d1309f810 | 304 | double x_temp = 0, |
SED9008 | 1:a88d1309f810 | 305 | y_temp = 0, |
SED9008 | 1:a88d1309f810 | 306 | z_temp = 0, |
SED9008 | 1:a88d1309f810 | 307 | x_angle = 0, |
SED9008 | 1:a88d1309f810 | 308 | y_angle = 0; |
SED9008 | 1:a88d1309f810 | 309 | |
SED9008 | 1:a88d1309f810 | 310 | int i; |
SED9008 | 1:a88d1309f810 | 311 | |
SED9008 | 1:a88d1309f810 | 312 | for(i=0;i<precision;i++) |
SED9008 | 1:a88d1309f810 | 313 | { |
SED9008 | 1:a88d1309f810 | 314 | acc.getOutput(measurement); |
SED9008 | 1:a88d1309f810 | 315 | x_temp = (int16_t)measurement[0]; |
SED9008 | 1:a88d1309f810 | 316 | y_temp = (int16_t)measurement[1]; |
SED9008 | 1:a88d1309f810 | 317 | z_temp = (int16_t)measurement[2]; |
SED9008 | 1:a88d1309f810 | 318 | x_angle += x_temp/z_temp; |
SED9008 | 1:a88d1309f810 | 319 | y_angle += y_temp/z_temp; |
SED9008 | 1:a88d1309f810 | 320 | } |
SED9008 | 1:a88d1309f810 | 321 | x_angle = x_angle/precision; |
SED9008 | 1:a88d1309f810 | 322 | y_angle = y_angle/precision; |
SED9008 | 1:a88d1309f810 | 323 | // x_angle = atan(x_angle) * 180 / PI; |
SED9008 | 1:a88d1309f810 | 324 | // y_angle = atan(y_angle) * 180 / PI; |
SED9008 | 1:a88d1309f810 | 325 | if(axis == 'x')return atan(x_angle) * 180 / PI; |
SED9008 | 1:a88d1309f810 | 326 | if(axis == 'y')return atan(y_angle) * 180 / PI; |
SED9008 | 1:a88d1309f810 | 327 | } |
SED9008 | 1:a88d1309f810 | 328 | |
SED9008 | 1:a88d1309f810 | 329 |