Shih-Ho Hsieh / Mbed 2 deprecated Motor_XYZ_UI_SPI_8mag_encoder_L476RG

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ui.cpp Source File

ui.cpp

00001 #include "xyz_sensor_platform.h"
00002 #include "envelopetracker.h"
00003 
00004 #define SPI_FREQUENCY 1e6
00005 #define BIG_CHAR_MASK 0x1F
00006 #define BAUD 115200
00007 #define Fs 1e2 // sampling rate -- max: 1kHz
00008 
00009 typedef unsigned char byte;
00010 uint8_t* dataToSend;
00011 int sendArrayIndex = 0;
00012 int sendBufferMax = 10000;
00013 Serial pc(SERIAL_TX, SERIAL_RX, BAUD );
00014 DigitalOut led(LED2),led3(LED3);
00015 DigitalOut mag_test(PC_8);
00016 InterruptIn button(USER_BUTTON);
00017 Timeout nextRecord;
00018 XYZSensorPlatform platform;
00019 Envelope *command;
00020 EnvelopeTracker tracker;
00021 byte commandToSend[10]= {'H','O','1','2','3','4','5','6','E',0};
00022 bool isRecording = false;
00023 Envelope* result;
00024 int n = 0;
00025 int getMag = 0;
00026 char magSel = 0;
00027 int recordTime;
00028 bool isEcho = false;
00029 bool isTimeToRecord = false;
00030 bool isMagReset = false;
00031 uint8_t magData[80] = {0};  //80 bytes
00032 
00033 
00034 
00035 void echo(char typ, float x, float y, float z);
00036 uint8_t* echo(char typ, int16_t *p_data, bool isWriteNow = true);
00037 void Rx_interrupt();
00038 void recordTimeup()
00039 {
00040     isTimeToRecord = true;
00041 }
00042 void released()
00043 {
00044     led = !led;
00045     pc.attach(&Rx_interrupt, Serial::RxIrq);
00046 }
00047 
00048 int main()
00049 {
00050     led=1;
00051     command = new Envelope;
00052     command->enableHeader(std::string("H"));// 48 H
00053     command->enableFooter(std::string("E"),8);// 45 E
00054     command->enableCheckXOR(9);
00055     tracker.setEnvelope(*command);
00056     tracker.setBufferLength(100);
00057     pc.format(8,SerialBase::None,1);
00058     platform.setSensorSpiFrequency(SPI_FREQUENCY);
00059 // Setup a serial interrupt function to receive data
00060     pc.attach(&Rx_interrupt, Serial::RxIrq);
00061     printf("begin....");
00062     while(1) {
00063         if(isRecording && n < recordTime && isTimeToRecord) {
00064             nextRecord.attach(&recordTimeup, 1.0f/(float)Fs);
00065             isTimeToRecord = false;
00066             char curMag = 1;
00067             int16_t mag[NUMBER_OF_MAGNETOMETERS][3] = {0};
00068             mag_test=!mag_test;
00069             for(int i = 0; i < NUMBER_OF_MAGNETOMETERS; i++)
00070             {
00071                 if((curMag & magSel) != 0) platform.get_mag_raw(i, &(mag[i][0]));
00072                 curMag <<= 1;
00073             }
00074             curMag = 1;
00075             int line = 0;
00076             for(int i = 0; i < NUMBER_OF_MAGNETOMETERS; i++)
00077             {
00078                 if((curMag & magSel) != 0)
00079                 {
00080                     memcpy(&magData[line++*10],echo(('M'&BIG_CHAR_MASK)|(i<<5),&mag[i][0],false),10);
00081                 }
00082                 curMag <<= 1;
00083             }
00084             if(pc.writeable()) {
00085                 for(int i = 0; i < line*10; i++) pc.putc(magData[i]);
00086             }
00087             n++;
00088             if(isRecording && n == recordTime) {
00089                 nextRecord.detach();
00090                 n = 0;
00091                 isRecording = false;
00092                 int16_t stopCommand[3] = {1<<8,0,0};
00093                 echo('S',stopCommand);
00094             }
00095         }// end recording if
00096         if(getMag>0 && isTimeToRecord) {
00097             isTimeToRecord = false;
00098             char curMag = 1;
00099             int16_t mag[NUMBER_OF_MAGNETOMETERS][3] = {0};
00100             for(int i = 0; i < NUMBER_OF_MAGNETOMETERS; i++)
00101             {
00102                 if((curMag & magSel) != 0) platform.get_mag_raw(i, &(mag[i][0]));
00103                 curMag <<= 1;
00104             }
00105             curMag = 1;
00106             int line = 0;
00107             for(int i = 0; i < NUMBER_OF_MAGNETOMETERS; i++)
00108             {
00109                 if((curMag & magSel) != 0)
00110                 {
00111                     memcpy(&magData[line++*10],echo(('M'&BIG_CHAR_MASK)|(i<<5),&mag[i][0],false),10);
00112                 }
00113                 curMag <<= 1;
00114             }
00115             if(pc.writeable()) {
00116                 for(int i = 0; i < line*10; i++) pc.putc(magData[i]);
00117             }
00118             getMag--;
00119         }
00120         if(isMagReset)
00121         {
00122             isMagReset = false;
00123             char curMag = 1;
00124             for(int i = 0; i < NUMBER_OF_MAGNETOMETERS; i++)
00125             {
00126                 if((curMag & magSel) != 0) platform.resetMagnetometer(i);
00127                 curMag <<= 1;
00128             }
00129         }
00130     } // end while
00131 
00132 }
00133 
00134 void echo(char typ,float x, float y, float z)
00135 {
00136     int16_t p_data[3]= {(int16_t)(x*10), int16_t(y*10), int16_t(z*10)};
00137     echo(typ,p_data);
00138 }
00139 
00140 uint8_t* echo(char typ, int16_t *p_data, bool isWriteNow)
00141 {
00142     char tmp[] = {typ, p_data[0]>>8, p_data[0], p_data[1]>>8, p_data[1], p_data[2]>>8, p_data[2]};
00143     command->setEnvelopeData(tmp,7);
00144     dataToSend = (uint8_t*)(command->getEnvelopeArray());
00145     if(isWriteNow)
00146     {
00147        int i = 0;
00148        while(!pc.writeable() && i < 10000) i++;
00149        for(int i = 0; i < 10; i++) pc.putc(dataToSend[i]);
00150     }
00151     return dataToSend;
00152 }
00153 
00154 void Rx_interrupt()
00155 {
00156     char c;
00157     while(pc.readable()) {
00158         c = pc.getc();
00159         tracker.parse(&c,1);
00160 
00161         result = tracker.getEnvelope();
00162         if(result!=NULL) {
00163             char *dataArray = result->getPayload();
00164             switch(dataArray[0]) {
00165                 // following alphabets is used
00166                 // I O C X Y Z M R S P N B
00167                 case 'M': // magnet
00168                     getMag++;
00169                     magSel = dataArray[1];
00170                     isTimeToRecord = true;
00171                     pc.putc('M');
00172                     break;
00173                 case 'R': // record
00174                     recordTime = dataArray[1];
00175                     recordTime *= Fs;
00176                     if( recordTime == 0 ) recordTime = dataArray[3]*Fs/100;
00177                     magSel = dataArray[2];
00178                     isTimeToRecord = true;
00179                     isRecording = true;
00180                     break;
00181                 case 'S': // stop
00182                     isRecording = false;
00183                     nextRecord.detach();
00184                     isTimeToRecord = false;
00185                     break;
00186                 case 'B':
00187                     magSel = dataArray[1];
00188                     isMagReset = true;
00189                     break;
00190                 default:
00191                     break;
00192             } // end switch
00193             result = NULL;
00194             dataArray = NULL;
00195         } // end result if
00196     } // end parsing if
00197 }