This program acquires data from an ADIS16355 IMU via the SPI bus and sends the data to a host computer via UDP over Ethernet.
Dependencies: mbed StrippedDownNetServices
main.cpp@0:79f663186c05, 2012-05-31 (annotated)
- Committer:
- yahugh
- Date:
- Thu May 31 18:37:12 2012 +0000
- Revision:
- 0:79f663186c05
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yahugh | 0:79f663186c05 | 1 | // |
yahugh | 0:79f663186c05 | 2 | // main.cpp |
yahugh | 0:79f663186c05 | 3 | // |
yahugh | 0:79f663186c05 | 4 | // Container class for mbed-based ADIS16355 IMU data acquisition system |
yahugh | 0:79f663186c05 | 5 | // |
yahugh | 0:79f663186c05 | 6 | // copyright 2010 Hugh Shane |
yahugh | 0:79f663186c05 | 7 | // |
yahugh | 0:79f663186c05 | 8 | #include "mbed.h" |
yahugh | 0:79f663186c05 | 9 | #include "imu-spi.h" |
yahugh | 0:79f663186c05 | 10 | |
yahugh | 0:79f663186c05 | 11 | // http://mbed.org/cookbook/Networking-Stack-Releases |
yahugh | 0:79f663186c05 | 12 | #include "EthernetNetIf.h" |
yahugh | 0:79f663186c05 | 13 | #include "UDPSocket.h" |
yahugh | 0:79f663186c05 | 14 | |
yahugh | 0:79f663186c05 | 15 | #define NLOOPS 100 |
yahugh | 0:79f663186c05 | 16 | #define PORT_DONT_CARE 10000 |
yahugh | 0:79f663186c05 | 17 | |
yahugh | 0:79f663186c05 | 18 | AnalogOut signal(p18); |
yahugh | 0:79f663186c05 | 19 | |
yahugh | 0:79f663186c05 | 20 | // mask off the flag bits |
yahugh | 0:79f663186c05 | 21 | // convert two's complement to offset binary |
yahugh | 0:79f663186c05 | 22 | uint16_t fixImuData(int16_t imuData) { |
yahugh | 0:79f663186c05 | 23 | uint16_t imu16 = (imuData & 0x3fff) ^ 0x2000; |
yahugh | 0:79f663186c05 | 24 | return imu16; |
yahugh | 0:79f663186c05 | 25 | } |
yahugh | 0:79f663186c05 | 26 | |
yahugh | 0:79f663186c05 | 27 | int main() { |
yahugh | 0:79f663186c05 | 28 | |
yahugh | 0:79f663186c05 | 29 | EthernetNetIf eth( |
yahugh | 0:79f663186c05 | 30 | IpAddr(192,168,1,2), //IP Address |
yahugh | 0:79f663186c05 | 31 | IpAddr(255,255,255,0), //Network Mask |
yahugh | 0:79f663186c05 | 32 | IpAddr(192,168,1,1), //Gateway |
yahugh | 0:79f663186c05 | 33 | IpAddr(192,168,1,1) //DNS |
yahugh | 0:79f663186c05 | 34 | ); |
yahugh | 0:79f663186c05 | 35 | |
yahugh | 0:79f663186c05 | 36 | int ethErr = eth.setup(); |
yahugh | 0:79f663186c05 | 37 | |
yahugh | 0:79f663186c05 | 38 | if ( ethErr == ETH_OK ) { |
yahugh | 0:79f663186c05 | 39 | IpAddr localIp = eth.getIp(); |
yahugh | 0:79f663186c05 | 40 | printf("mbed IP Address is %d.%d.%d.%d\r\n", |
yahugh | 0:79f663186c05 | 41 | localIp[0], localIp[1], localIp[2], localIp[3]); |
yahugh | 0:79f663186c05 | 42 | } else printf ("ETHERNETSETUP FAILED\n"); |
yahugh | 0:79f663186c05 | 43 | |
yahugh | 0:79f663186c05 | 44 | Host destHost(IpAddr(192,168,1,8), 55555); |
yahugh | 0:79f663186c05 | 45 | UDPSocket udpSocket; |
yahugh | 0:79f663186c05 | 46 | UDPSocketErr udpErr = udpSocket.bind(Host(IpAddr(), PORT_DONT_CARE)); |
yahugh | 0:79f663186c05 | 47 | |
yahugh | 0:79f663186c05 | 48 | if (udpErr != UDPSOCKET_OK) { |
yahugh | 0:79f663186c05 | 49 | printf("error %d\n", udpErr); |
yahugh | 0:79f663186c05 | 50 | }; |
yahugh | 0:79f663186c05 | 51 | |
yahugh | 0:79f663186c05 | 52 | DigitalOut diag_led(LED1); |
yahugh | 0:79f663186c05 | 53 | ImuSpi imu; |
yahugh | 0:79f663186c05 | 54 | int16_t* imubuffer; |
yahugh | 0:79f663186c05 | 55 | int bufLength = 16; |
yahugh | 0:79f663186c05 | 56 | int bufDepth = 10; // buffer this many acq. cycles before UDP send |
yahugh | 0:79f663186c05 | 57 | char outBuf[bufLength * bufDepth]; |
yahugh | 0:79f663186c05 | 58 | int nSent; |
yahugh | 0:79f663186c05 | 59 | |
yahugh | 0:79f663186c05 | 60 | diag_led = 0; |
yahugh | 0:79f663186c05 | 61 | |
yahugh | 0:79f663186c05 | 62 | Net::poll(); // ensure that every component of the network stack keeps running |
yahugh | 0:79f663186c05 | 63 | wait(1); // give things time to settle before commencing |
yahugh | 0:79f663186c05 | 64 | |
yahugh | 0:79f663186c05 | 65 | for (int i = 0; i < NLOOPS; i++) { |
yahugh | 0:79f663186c05 | 66 | //while(1) { |
yahugh | 0:79f663186c05 | 67 | |
yahugh | 0:79f663186c05 | 68 | for (int n = 0; n < bufDepth; n++) { |
yahugh | 0:79f663186c05 | 69 | while (!imu.IsBufferReady()) {} // wait for the IMU buffer-ready signal |
yahugh | 0:79f663186c05 | 70 | imubuffer = imu.GetBufferReadPtr(); // get a pointer to the new IMU output data |
yahugh | 0:79f663186c05 | 71 | memcpy(&outBuf[bufLength*n] , imubuffer, bufLength); |
yahugh | 0:79f663186c05 | 72 | signal.write_u16(fixImuData(imubuffer[6])); // write to the D/A converter |
yahugh | 0:79f663186c05 | 73 | } |
yahugh | 0:79f663186c05 | 74 | |
yahugh | 0:79f663186c05 | 75 | Net::poll(); // ensure that every component of the network stack keeps running |
yahugh | 0:79f663186c05 | 76 | nSent = udpSocket.sendto((char*)outBuf,(bufLength*bufDepth),&destHost); |
yahugh | 0:79f663186c05 | 77 | |
yahugh | 0:79f663186c05 | 78 | if ( nSent <= 0 ) { |
yahugh | 0:79f663186c05 | 79 | printf("error %d\n", (UDPSocketErr)nSent); |
yahugh | 0:79f663186c05 | 80 | fflush(stdout); |
yahugh | 0:79f663186c05 | 81 | diag_led = 0; |
yahugh | 0:79f663186c05 | 82 | break; |
yahugh | 0:79f663186c05 | 83 | } else { |
yahugh | 0:79f663186c05 | 84 | diag_led = 1; |
yahugh | 0:79f663186c05 | 85 | //printf("."); |
yahugh | 0:79f663186c05 | 86 | //fflush(stdout); |
yahugh | 0:79f663186c05 | 87 | } |
yahugh | 0:79f663186c05 | 88 | |
yahugh | 0:79f663186c05 | 89 | } |
yahugh | 0:79f663186c05 | 90 | |
yahugh | 0:79f663186c05 | 91 | |
yahugh | 0:79f663186c05 | 92 | } |
yahugh | 0:79f663186c05 | 93 |