A project similar to http://mbed.org/users/lhiggs/code/UM6_IMU_AHRS_2012/, where I'm trying to log data from a UM6 (CH Robotics orientation sensor) and a GPS transceiver to an sd card. I've adapted LHiggs code to include ModGPS. For sum reason a soon as I pick up a gps signal the UM6 data freezes i.e. the time and gps signals continue to print out but the UM6 signals fixes on a single value.
Dependencies: MODGPS MODSERIAL SDFileSystem mbed
main.cpp@8:0ce247da6370, 2013-05-27 (annotated)
- Committer:
- njewin
- Date:
- Mon May 27 17:22:36 2013 +0000
- Revision:
- 8:0ce247da6370
- Parent:
- 7:af9f373ac87b
- Child:
- 9:7dcfa24d5e7a
output weird values for Longitude and 0 for Latitude. Not converting from 32bit IEEE floating point properly?
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
njewin | 0:52295643d083 | 1 | #include "mbed.h" |
njewin | 4:8dcf0bdc25c8 | 2 | #include "SDFileSystem.h" // SD file system header from handbook/cookbook offical mbed library |
njewin | 4:8dcf0bdc25c8 | 3 | #include "MODSERIAL.h" |
njewin | 5:ac633cdbb75c | 4 | #include "UM6_usart.h" // UM6 USART HEADER |
njewin | 5:ac633cdbb75c | 5 | #include "UM6_config.h" // UM6 CONFIG HEADER |
njewin | 4:8dcf0bdc25c8 | 6 | |
njewin | 0:52295643d083 | 7 | |
njewin | 1:9fe40d9ac0f5 | 8 | //------------ system and interface setup ----------------------------// |
njewin | 5:ac633cdbb75c | 9 | //////////////////////////////////////LocalFileSystem local("local"); // sets up local file on mbed |
njewin | 4:8dcf0bdc25c8 | 10 | MODSERIAL pc(USBTX, USBRX); // sets up serial connection to pc terminal |
njewin | 0:52295643d083 | 11 | |
njewin | 1:9fe40d9ac0f5 | 12 | //------------ Hardware setup ----------------------------------------// |
njewin | 5:ac633cdbb75c | 13 | DigitalOut pc_led(LED1); // LED1 = PC SERIAL |
njewin | 5:ac633cdbb75c | 14 | DigitalOut uart_led(LED2); // LED2 = UM6 SERIAL |
njewin | 5:ac633cdbb75c | 15 | DigitalOut log_led(LED3); // debug LED |
njewin | 0:52295643d083 | 16 | DigitalIn enable(p10); // enable signal for logging data to file |
njewin | 3:b3358ec2f57c | 17 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board |
njewin | 3:b3358ec2f57c | 18 | |
njewin | 0:52295643d083 | 19 | |
njewin | 8:0ce247da6370 | 20 | //------------ variables setup ---------------------------------------// |
njewin | 1:9fe40d9ac0f5 | 21 | Ticker tick; |
njewin | 3:b3358ec2f57c | 22 | Timer t; |
njewin | 1:9fe40d9ac0f5 | 23 | int flag=0; |
njewin | 1:9fe40d9ac0f5 | 24 | |
njewin | 5:ac633cdbb75c | 25 | // interupt function for processing uart messages --------------------// |
njewin | 5:ac633cdbb75c | 26 | void rxCallback(MODSERIAL_IRQ_INFO *q) { |
njewin | 5:ac633cdbb75c | 27 | if (um6_uart.rxBufferGetCount() >= MAX_PACKET_DATA) { |
njewin | 5:ac633cdbb75c | 28 | uart_led = !uart_led; // Lights LED when uart RxBuff has > 40 bytes |
njewin | 5:ac633cdbb75c | 29 | Process_um6_packet(); |
njewin | 5:ac633cdbb75c | 30 | } |
njewin | 5:ac633cdbb75c | 31 | } |
njewin | 5:ac633cdbb75c | 32 | |
njewin | 1:9fe40d9ac0f5 | 33 | //------------ LogData interrupt function ----------------------------// |
njewin | 1:9fe40d9ac0f5 | 34 | void LogData() { |
njewin | 1:9fe40d9ac0f5 | 35 | flag=1; |
njewin | 1:9fe40d9ac0f5 | 36 | } |
njewin | 1:9fe40d9ac0f5 | 37 | |
njewin | 1:9fe40d9ac0f5 | 38 | //============= Main Program =========================================// |
njewin | 0:52295643d083 | 39 | int main() { |
njewin | 1:9fe40d9ac0f5 | 40 | pc.baud(115200); // baud rate to pc interface |
njewin | 5:ac633cdbb75c | 41 | um6_uart.baud(115200); // baud rate to um6 interface |
njewin | 5:ac633cdbb75c | 42 | |
njewin | 5:ac633cdbb75c | 43 | t.start(); // start log time |
njewin | 5:ac633cdbb75c | 44 | |
njewin | 5:ac633cdbb75c | 45 | //---- call interrupt functions -------------------------// |
njewin | 5:ac633cdbb75c | 46 | um6_uart.attach(&rxCallback, MODSERIAL::RxIrq); // attach interupt function to uart |
njewin | 7:af9f373ac87b | 47 | tick.attach(&LogData, 0.5); // attaches LogData function to 'tick' ticker interrupt every 0.5s |
njewin | 3:b3358ec2f57c | 48 | |
njewin | 3:b3358ec2f57c | 49 | //---------- setup sd card -----------------------------// |
njewin | 7:af9f373ac87b | 50 | mkdir("/sd/mydir", 0777); |
njewin | 8:0ce247da6370 | 51 | FILE *fp = fopen("/sd/mydir/log1.csv", "w"); |
njewin | 7:af9f373ac87b | 52 | if(fp == NULL) { |
njewin | 7:af9f373ac87b | 53 | error("Could not open file for write\n"); |
njewin | 7:af9f373ac87b | 54 | } |
njewin | 8:0ce247da6370 | 55 | //////////////////////////// FILE *fp = fopen("/local/log1.csv", "w"); |
njewin | 8:0ce247da6370 | 56 | // print TEST signals to file, header |
njewin | 8:0ce247da6370 | 57 | // fprintf(fp,"time(s),Yaw(deg),Accel(m/s2),GPS Speed(m/s) \r"); |
njewin | 8:0ce247da6370 | 58 | // print ALL signals to file, header |
njewin | 8:0ce247da6370 | 59 | fprintf(fp, "time(s),Yaw(deg),Roll(deg),Pitch(deg),GyroX(deg/s),GyroY(deg/s),GyroZ(deg/s),AccelX(g),AccelY(g),AccelZ(g),GPScourse(deg),GPSspeed(m/s),Latitude(deg),Longitude(deg) \r"); // sends header to file |
njewin | 8:0ce247da6370 | 60 | |
njewin | 1:9fe40d9ac0f5 | 61 | |
njewin | 5:ac633cdbb75c | 62 | //---- main while loop ----------------------------------// |
njewin | 5:ac633cdbb75c | 63 | //--(interrupt sets flag that causes variables to be logged) |
njewin | 1:9fe40d9ac0f5 | 64 | while(1) { |
njewin | 8:0ce247da6370 | 65 | if(flag==1) { |
njewin | 5:ac633cdbb75c | 66 | log_led=1; // turns on LED3 to indicate logging |
njewin | 8:0ce247da6370 | 67 | float time=t.read(); |
njewin | 5:ac633cdbb75c | 68 | float Yaw=data.Yaw; |
njewin | 8:0ce247da6370 | 69 | float Roll=data.Roll; |
njewin | 8:0ce247da6370 | 70 | float Pitch=data.Pitch; |
njewin | 8:0ce247da6370 | 71 | float GyroX=data.Gyro_Proc_X; |
njewin | 8:0ce247da6370 | 72 | float GyroY=data.Gyro_Proc_Y; |
njewin | 8:0ce247da6370 | 73 | float GyroZ=data.Gyro_Proc_Z; |
njewin | 8:0ce247da6370 | 74 | float AccelX=data.Accel_Proc_X; |
njewin | 8:0ce247da6370 | 75 | float AccelY=data.Accel_Proc_Y; |
njewin | 8:0ce247da6370 | 76 | float AccelZ=data.Accel_Proc_Z; |
njewin | 8:0ce247da6370 | 77 | double GPSlong=data.GPS_long; // currently I can get GPS longitude to out data |
njewin | 8:0ce247da6370 | 78 | double GPSlat=data.GPS_lat; // currently I can get GPS latitude to out data |
njewin | 8:0ce247da6370 | 79 | float GPScourse=data.GPS_course; |
njewin | 8:0ce247da6370 | 80 | float GPSspeed=data.GPS_speed; |
njewin | 8:0ce247da6370 | 81 | |
njewin | 8:0ce247da6370 | 82 | //----- print TEST signals to file -------------------// |
njewin | 8:0ce247da6370 | 83 | // fprintf(fp,"%.3f,%.2f,%.4f,%.2f \r",time,Yaw,AccelX,GPSspeed); |
njewin | 8:0ce247da6370 | 84 | pc.printf("time %.3f,Yaw %f, Speed %f, Lat %f, Long %f \n",time,Yaw,GPSspeed,GPSlat,GPSlong); |
njewin | 8:0ce247da6370 | 85 | // pc.printf("0x%08X, %f\n", *(int *)&GPSlong, GPSlong); |
njewin | 8:0ce247da6370 | 86 | |
njewin | 8:0ce247da6370 | 87 | //----- print ALL signals to file --------------------// |
njewin | 8:0ce247da6370 | 88 | fprintf(fp, "%3.3f, %3.1f,%3.1f,%3.1f, %3.2f,%3.2f,%3.2f, %3.4f,%3.4f,%3.4f, %3.1f,%3.2f,%f,%f\r",time,Yaw,Roll,Pitch,GyroX,GyroY,GyroZ,AccelX,AccelY,AccelZ,GPScourse,GPSspeed,GPSlat,GPSlong); |
njewin | 8:0ce247da6370 | 89 | flag=0; // recents LogData interrupt flag |
njewin | 8:0ce247da6370 | 90 | pc_led = !pc_led; // Lights LED1 when transmitting to PC screen |
njewin | 1:9fe40d9ac0f5 | 91 | } // end if(flag=1) loop |
njewin | 3:b3358ec2f57c | 92 | |
njewin | 5:ac633cdbb75c | 93 | if(enable==0) { |
njewin | 8:0ce247da6370 | 94 | break; // breaks while loop if enable switched off |
njewin | 3:b3358ec2f57c | 95 | } |
njewin | 3:b3358ec2f57c | 96 | } // end while(1) loop |
njewin | 8:0ce247da6370 | 97 | pc.printf(" done. \n"); // prints 'done when logging is finished/enable switched off |
njewin | 5:ac633cdbb75c | 98 | log_led=0; // turns off LED logging is finished/enable switched off |
njewin | 5:ac633cdbb75c | 99 | wait(0.5); // debug wait for pc.printf |
njewin | 7:af9f373ac87b | 100 | fclose(fp); // closes log file |
njewin | 5:ac633cdbb75c | 101 | } // end main() program |
njewin | 5:ac633cdbb75c | 102 | |
njewin | 5:ac633cdbb75c | 103 | |
njewin | 7:af9f373ac87b | 104 | /* opening a file BEFORE calling interrupts OK |
njewin | 5:ac633cdbb75c | 105 | opening a file and print to it BEFORE calling interrupts NOT OK (stops rest of program) |
njewin | 5:ac633cdbb75c | 106 | open a (local) file and print to it AFTER calling interrupts NOT OK (stops rest of program) |
njewin | 5:ac633cdbb75c | 107 | open a (sd) file and print to it AFTER calling interrupts OK |
njewin | 5:ac633cdbb75c | 108 | */ |