Hugh S / Mbed 2 deprecated imu-daq-eth

Dependencies:   mbed StrippedDownNetServices

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //
00002 // main.cpp
00003 //
00004 // Container class for mbed-based ADIS16355 IMU data acquisition system
00005 //
00006 // copyright 2010 Hugh Shane
00007 //
00008 #include "mbed.h"
00009 #include "imu-spi.h"
00010 
00011 // http://mbed.org/cookbook/Networking-Stack-Releases
00012 #include "EthernetNetIf.h"
00013 #include "UDPSocket.h"
00014  
00015 #define NLOOPS 100
00016 #define PORT_DONT_CARE 10000
00017 
00018 AnalogOut signal(p18);
00019 
00020 // mask off the flag bits 
00021 // convert two's complement to offset binary
00022 uint16_t fixImuData(int16_t imuData) {
00023     uint16_t imu16 = (imuData & 0x3fff) ^ 0x2000;
00024     return imu16;
00025 }
00026     
00027 int main() {
00028 
00029     EthernetNetIf eth(
00030         IpAddr(192,168,1,2), //IP Address
00031         IpAddr(255,255,255,0), //Network Mask
00032         IpAddr(192,168,1,1), //Gateway
00033         IpAddr(192,168,1,1)  //DNS
00034     );
00035     
00036     int ethErr =  eth.setup();
00037     
00038     if ( ethErr == ETH_OK ) {
00039         IpAddr localIp = eth.getIp();
00040         printf("mbed IP Address is %d.%d.%d.%d\r\n", 
00041             localIp[0], localIp[1], localIp[2], localIp[3]);
00042     } else printf ("ETHERNETSETUP FAILED\n");    
00043     
00044     Host destHost(IpAddr(192,168,1,8), 55555);
00045     UDPSocket udpSocket;
00046     UDPSocketErr udpErr = udpSocket.bind(Host(IpAddr(), PORT_DONT_CARE));
00047     
00048     if (udpErr != UDPSOCKET_OK) {
00049         printf("error %d\n", udpErr);
00050     };
00051      
00052     DigitalOut diag_led(LED1);
00053     ImuSpi imu;
00054     int16_t* imubuffer;
00055     int bufLength = 16;
00056     int bufDepth = 10; // buffer this many acq. cycles before UDP send
00057     char outBuf[bufLength * bufDepth];
00058     int nSent;
00059 
00060     diag_led = 0;
00061     
00062     Net::poll(); // ensure that every component of the network stack keeps running
00063     wait(1); // give things time to settle before commencing
00064     
00065     for (int i = 0; i < NLOOPS; i++) {    
00066         //while(1) {
00067         
00068             for (int n = 0; n < bufDepth; n++) {
00069                 while (!imu.IsBufferReady()) {} // wait for the IMU buffer-ready signal
00070                 imubuffer = imu.GetBufferReadPtr(); // get a pointer to the new IMU output data
00071                 memcpy(&outBuf[bufLength*n] , imubuffer, bufLength);
00072                 signal.write_u16(fixImuData(imubuffer[6])); // write to the D/A converter
00073             }
00074             
00075             Net::poll(); // ensure that every component of the network stack keeps running
00076             nSent = udpSocket.sendto((char*)outBuf,(bufLength*bufDepth),&destHost);
00077             
00078             if ( nSent <= 0 ) {
00079                 printf("error %d\n", (UDPSocketErr)nSent);
00080                 fflush(stdout);
00081                 diag_led = 0;
00082                 break;
00083             } else {
00084                 diag_led = 1;
00085                 //printf(".");
00086                 //fflush(stdout);
00087             }
00088     
00089     }
00090 
00091 
00092 }
00093