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@9:7dcfa24d5e7a, 2013-05-30 (annotated)
- Committer:
- njewin
- Date:
- Thu May 30 13:32:54 2013 +0000
- Revision:
- 9:7dcfa24d5e7a
- Parent:
- 8:0ce247da6370
- Child:
- 10:d96e068f3595
Saved before compiling via macOS
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 | 4:8dcf0bdc25c8 | 9 | MODSERIAL pc(USBTX, USBRX); // sets up serial connection to pc terminal |
njewin | 0:52295643d083 | 10 | |
njewin | 1:9fe40d9ac0f5 | 11 | //------------ Hardware setup ----------------------------------------// |
njewin | 9:7dcfa24d5e7a | 12 | DigitalOut pc_led(LED1); // LED1 = PC SERIAL |
njewin | 9:7dcfa24d5e7a | 13 | DigitalOut uart_led(LED2); // LED2 = UM6 SERIAL |
njewin | 9:7dcfa24d5e7a | 14 | DigitalOut log_led(LED3); // debug LED |
njewin | 9:7dcfa24d5e7a | 15 | DigitalOut debug_led(LED4); // debug LED |
njewin | 9:7dcfa24d5e7a | 16 | DigitalIn enable(p10); // enable logging pin |
njewin | 9:7dcfa24d5e7a | 17 | DigitalOut sync(p11); // sychronization (with CAN logger) pin |
njewin | 3:b3358ec2f57c | 18 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board |
njewin | 3:b3358ec2f57c | 19 | |
njewin | 5:ac633cdbb75c | 20 | // interupt function for processing uart messages --------------------// |
njewin | 5:ac633cdbb75c | 21 | void rxCallback(MODSERIAL_IRQ_INFO *q) { |
njewin | 5:ac633cdbb75c | 22 | if (um6_uart.rxBufferGetCount() >= MAX_PACKET_DATA) { |
njewin | 5:ac633cdbb75c | 23 | uart_led = !uart_led; // Lights LED when uart RxBuff has > 40 bytes |
njewin | 5:ac633cdbb75c | 24 | Process_um6_packet(); |
njewin | 5:ac633cdbb75c | 25 | } |
njewin | 5:ac633cdbb75c | 26 | } |
njewin | 5:ac633cdbb75c | 27 | |
njewin | 1:9fe40d9ac0f5 | 28 | //------------ LogData interrupt function ----------------------------// |
njewin | 9:7dcfa24d5e7a | 29 | int flag=0; |
njewin | 1:9fe40d9ac0f5 | 30 | void LogData() { |
njewin | 9:7dcfa24d5e7a | 31 | flag=1; //interrupt sets flag that causes variables to be logged |
njewin | 1:9fe40d9ac0f5 | 32 | } |
njewin | 1:9fe40d9ac0f5 | 33 | |
njewin | 1:9fe40d9ac0f5 | 34 | //============= Main Program =========================================// |
njewin | 0:52295643d083 | 35 | int main() { |
njewin | 9:7dcfa24d5e7a | 36 | pc.baud(115200); // baud rate to pc interface |
njewin | 5:ac633cdbb75c | 37 | um6_uart.baud(115200); // baud rate to um6 interface |
njewin | 9:7dcfa24d5e7a | 38 | Ticker tick; |
njewin | 9:7dcfa24d5e7a | 39 | Timer t; |
njewin | 9:7dcfa24d5e7a | 40 | |
njewin | 9:7dcfa24d5e7a | 41 | //---- call interrupt functions --------------------------// |
njewin | 5:ac633cdbb75c | 42 | um6_uart.attach(&rxCallback, MODSERIAL::RxIrq); // attach interupt function to uart |
njewin | 7:af9f373ac87b | 43 | tick.attach(&LogData, 0.5); // attaches LogData function to 'tick' ticker interrupt every 0.5s |
njewin | 9:7dcfa24d5e7a | 44 | t.start(); // start logging time |
njewin | 9:7dcfa24d5e7a | 45 | |
njewin | 9:7dcfa24d5e7a | 46 | //---------- setup sd card directory------------------------------// |
njewin | 9:7dcfa24d5e7a | 47 | int FileNo = 0; |
njewin | 9:7dcfa24d5e7a | 48 | mkdir("/sd/log_data", 0777); |
njewin | 1:9fe40d9ac0f5 | 49 | |
njewin | 9:7dcfa24d5e7a | 50 | //---- main while loop -------------------------------------------// |
njewin | 9:7dcfa24d5e7a | 51 | while(1) { |
njewin | 9:7dcfa24d5e7a | 52 | //---- Setup file on SD card ----------------------------// |
njewin | 9:7dcfa24d5e7a | 53 | printf("Opening sd card\n"); |
njewin | 9:7dcfa24d5e7a | 54 | char buffer[50]; |
njewin | 9:7dcfa24d5e7a | 55 | sprintf(buffer, "/sd/log_data/%i.csv", FileNo); |
njewin | 9:7dcfa24d5e7a | 56 | //**************************************************** |
njewin | 9:7dcfa24d5e7a | 57 | // This is necessary for card to work when reconnected |
njewin | 9:7dcfa24d5e7a | 58 | // Initialise disk |
njewin | 9:7dcfa24d5e7a | 59 | sd.disk_initialize(); |
njewin | 9:7dcfa24d5e7a | 60 | //**************************************************** |
njewin | 9:7dcfa24d5e7a | 61 | // Open a file for write |
njewin | 9:7dcfa24d5e7a | 62 | FILE *fp = fopen(buffer, "w"); |
njewin | 9:7dcfa24d5e7a | 63 | if(fp == NULL) { |
njewin | 9:7dcfa24d5e7a | 64 | error("Could not open file for write\n"); |
njewin | 3:b3358ec2f57c | 65 | } |
njewin | 9:7dcfa24d5e7a | 66 | //--- print TEST signals to file, header |
njewin | 9:7dcfa24d5e7a | 67 | //fprintf(fp,"time(s),Yaw(deg),Accel(m/s2),GPS Speed(m/s) \r"); |
njewin | 9:7dcfa24d5e7a | 68 | //--- print ALL signals to file, header |
njewin | 9:7dcfa24d5e7a | 69 | 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 | 9:7dcfa24d5e7a | 70 | |
njewin | 9:7dcfa24d5e7a | 71 | while (!pc.readable()) { |
njewin | 9:7dcfa24d5e7a | 72 | if(flag==1) { |
njewin | 9:7dcfa24d5e7a | 73 | log_led=1; // turns on LED3 to indicate logging |
njewin | 9:7dcfa24d5e7a | 74 | float time=t.read(); |
njewin | 9:7dcfa24d5e7a | 75 | float Yaw=data.Yaw; |
njewin | 9:7dcfa24d5e7a | 76 | float Roll=data.Roll; |
njewin | 9:7dcfa24d5e7a | 77 | float Pitch=data.Pitch; |
njewin | 9:7dcfa24d5e7a | 78 | float GyroX=data.Gyro_Proc_X; |
njewin | 9:7dcfa24d5e7a | 79 | float GyroY=data.Gyro_Proc_Y; |
njewin | 9:7dcfa24d5e7a | 80 | float GyroZ=data.Gyro_Proc_Z; |
njewin | 9:7dcfa24d5e7a | 81 | float AccelX=data.Accel_Proc_X; |
njewin | 9:7dcfa24d5e7a | 82 | float AccelY=data.Accel_Proc_Y; |
njewin | 9:7dcfa24d5e7a | 83 | float AccelZ=data.Accel_Proc_Z; |
njewin | 9:7dcfa24d5e7a | 84 | float GPSlong=data.GPS_long; // currently not reading GPS longitude correctly |
njewin | 9:7dcfa24d5e7a | 85 | float GPSlat=data.GPS_lat; // currently not reading GPS latitude correctly |
njewin | 9:7dcfa24d5e7a | 86 | float GPScourse=data.GPS_course; |
njewin | 9:7dcfa24d5e7a | 87 | float GPSspeed=data.GPS_speed; |
njewin | 9:7dcfa24d5e7a | 88 | int32_t GPSlatR=data.GPS_lat_raw; |
njewin | 9:7dcfa24d5e7a | 89 | |
njewin | 9:7dcfa24d5e7a | 90 | //----- print TEST signals----------------------------// |
njewin | 9:7dcfa24d5e7a | 91 | // fprintf(fp,"%.3f,%.2f,%.4f,%.2f \r",time,Yaw,AccelX,GPSspeed); |
njewin | 9:7dcfa24d5e7a | 92 | pc.printf("time %.3f,Yaw %f,Accel %f, Speed %f, Lat %f, Long %f, LatR %d \n",time,Yaw,AccelX,GPSspeed,GPSlat,GPSlong,GPSlatR); |
njewin | 9:7dcfa24d5e7a | 93 | //pc.printf("time %.3f,Yaw %f,Accel %f, Speed %f \n",time,Yaw,AccelX,GPSspeed); |
njewin | 9:7dcfa24d5e7a | 94 | // pc.printf("time %f,LongIn 0x%08X, LongOut %f\n",time,GPSlong,*(int *)&GPSlong); |
njewin | 9:7dcfa24d5e7a | 95 | |
njewin | 9:7dcfa24d5e7a | 96 | //----- print ALL signals to file --------------------// |
njewin | 9:7dcfa24d5e7a | 97 | 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,%d,%f\r",time,Yaw,Roll,Pitch,GyroX,GyroY,GyroZ,AccelX,AccelY,AccelZ,GPScourse,GPSspeed,GPSlat,GPSlong); |
njewin | 9:7dcfa24d5e7a | 98 | flag=0; // reset LogData interrupt flag |
njewin | 9:7dcfa24d5e7a | 99 | pc_led = !pc_led; // Lights LED1 when transmitting to PC screen |
njewin | 9:7dcfa24d5e7a | 100 | } // end if(flag=1) loop |
njewin | 9:7dcfa24d5e7a | 101 | |
njewin | 9:7dcfa24d5e7a | 102 | if(enable==0) { |
njewin | 9:7dcfa24d5e7a | 103 | break; // breaks while loop if enable switched off |
njewin | 9:7dcfa24d5e7a | 104 | } |
njewin | 9:7dcfa24d5e7a | 105 | } // end while(!pc.readable()) loop |
njewin | 9:7dcfa24d5e7a | 106 | pc.printf(" Done. \n"); // prints 'done when logging is finished/enable switched off |
njewin | 9:7dcfa24d5e7a | 107 | log_led=0; // turns off LED logging is finished/enable switched off |
njewin | 9:7dcfa24d5e7a | 108 | wait(0.5); // debug wait for pc.printf |
njewin | 9:7dcfa24d5e7a | 109 | fclose(fp); // closes log file |
njewin | 9:7dcfa24d5e7a | 110 | pc.getc(); // Clear character from buffer |
njewin | 9:7dcfa24d5e7a | 111 | |
njewin | 9:7dcfa24d5e7a | 112 | while (!pc.readable()) { // Wait for a key press to restart logging |
njewin | 9:7dcfa24d5e7a | 113 | }; |
njewin | 9:7dcfa24d5e7a | 114 | pc.getc(); // Clear character from buffer |
njewin | 9:7dcfa24d5e7a | 115 | FileNo++; // Increment file number |
njewin | 3:b3358ec2f57c | 116 | } // end while(1) loop |
njewin | 9:7dcfa24d5e7a | 117 | |
njewin | 5:ac633cdbb75c | 118 | } // end main() program |
njewin | 5:ac633cdbb75c | 119 | |
njewin | 5:ac633cdbb75c | 120 | |
njewin | 9:7dcfa24d5e7a | 121 | /* DEUBBING NOTES |
njewin | 9:7dcfa24d5e7a | 122 | opening a file BEFORE calling interrupts OK |
njewin | 5:ac633cdbb75c | 123 | opening a file and print to it BEFORE calling interrupts NOT OK (stops rest of program) |
njewin | 5:ac633cdbb75c | 124 | open a (local) file and print to it AFTER calling interrupts NOT OK (stops rest of program) |
njewin | 5:ac633cdbb75c | 125 | open a (sd) file and print to it AFTER calling interrupts OK |
njewin | 5:ac633cdbb75c | 126 | */ |