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@13:0782664b5fcb, 2013-07-08 (annotated)
- Committer:
- njewin
- Date:
- Mon Jul 08 16:47:06 2013 +0000
- Revision:
- 13:0782664b5fcb
- Parent:
- 12:894e648638e4
published version
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 | 10:d96e068f3595 | 6 | #include "GPS.h" |
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 | 12:894e648638e4 | 17 | DigitalOut sync(p17); // 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 | 10:d96e068f3595 | 19 | GPS gps(NC,p27); |
njewin | 12:894e648638e4 | 20 | Timer t_debug; |
njewin | 12:894e648638e4 | 21 | int counter; |
njewin | 3:b3358ec2f57c | 22 | |
njewin | 5:ac633cdbb75c | 23 | // interupt function for processing uart messages --------------------// |
njewin | 12:894e648638e4 | 24 | int flag_uart=0; |
njewin | 12:894e648638e4 | 25 | float time_debug; |
njewin | 5:ac633cdbb75c | 26 | void rxCallback(MODSERIAL_IRQ_INFO *q) { |
njewin | 12:894e648638e4 | 27 | if (um6_uart.rxBufferGetCount() >= MAX_PACKET_DATA) { |
njewin | 12:894e648638e4 | 28 | uart_led = 1; // Lights LED when uart RxBuff has > 40 bytes |
njewin | 12:894e648638e4 | 29 | t_debug.start(); |
njewin | 5:ac633cdbb75c | 30 | Process_um6_packet(); |
njewin | 12:894e648638e4 | 31 | t_debug.stop(); |
njewin | 12:894e648638e4 | 32 | time_debug=t_debug.read(); |
njewin | 12:894e648638e4 | 33 | t_debug.reset(); |
njewin | 12:894e648638e4 | 34 | uart_led = 0; |
njewin | 12:894e648638e4 | 35 | counter++; |
njewin | 12:894e648638e4 | 36 | } |
njewin | 5:ac633cdbb75c | 37 | } |
njewin | 5:ac633cdbb75c | 38 | |
njewin | 1:9fe40d9ac0f5 | 39 | //------------ LogData interrupt function ----------------------------// |
njewin | 12:894e648638e4 | 40 | int flag_log=0; |
njewin | 1:9fe40d9ac0f5 | 41 | void LogData() { |
njewin | 12:894e648638e4 | 42 | flag_log=1; //interrupt sets flag that causes variables to be logged |
njewin | 11:2b2537dcf504 | 43 | } |
njewin | 11:2b2537dcf504 | 44 | //------------ Pc print interrupt function ----------------------------// |
njewin | 11:2b2537dcf504 | 45 | // to print to PC a a different rate to logging |
njewin | 12:894e648638e4 | 46 | int flag_pc=0; |
njewin | 11:2b2537dcf504 | 47 | void pcData() { |
njewin | 12:894e648638e4 | 48 | flag_pc=1; //interrupt sets flag that causes variables to be logged |
njewin | 1:9fe40d9ac0f5 | 49 | } |
njewin | 1:9fe40d9ac0f5 | 50 | |
njewin | 1:9fe40d9ac0f5 | 51 | //============= Main Program =========================================// |
njewin | 0:52295643d083 | 52 | int main() { |
njewin | 10:d96e068f3595 | 53 | Ticker tick; |
njewin | 11:2b2537dcf504 | 54 | Ticker tick1; |
njewin | 12:894e648638e4 | 55 | Timer t; |
njewin | 12:894e648638e4 | 56 | sync = 0; |
njewin | 10:d96e068f3595 | 57 | GPS_Time q1; |
njewin | 10:d96e068f3595 | 58 | GPS_VTG v1; |
njewin | 10:d96e068f3595 | 59 | |
njewin | 9:7dcfa24d5e7a | 60 | pc.baud(115200); // baud rate to pc interface |
njewin | 5:ac633cdbb75c | 61 | um6_uart.baud(115200); // baud rate to um6 interface |
njewin | 12:894e648638e4 | 62 | gps.baud(115200); |
njewin | 10:d96e068f3595 | 63 | gps.format(8, GPS::None, 1); |
njewin | 9:7dcfa24d5e7a | 64 | |
njewin | 9:7dcfa24d5e7a | 65 | //---- call interrupt functions --------------------------// |
njewin | 5:ac633cdbb75c | 66 | um6_uart.attach(&rxCallback, MODSERIAL::RxIrq); // attach interupt function to uart |
njewin | 12:894e648638e4 | 67 | tick.attach(&LogData, 1); // attaches LogData function to 'tick' ticker interrupt every 0.5s |
njewin | 12:894e648638e4 | 68 | tick1.attach(&pcData, 0.5); // attaches LogData function to 'tick' ticker interrupt every 0.5s |
njewin | 9:7dcfa24d5e7a | 69 | |
njewin | 9:7dcfa24d5e7a | 70 | //---------- setup sd card directory------------------------------// |
njewin | 9:7dcfa24d5e7a | 71 | int FileNo = 0; |
njewin | 9:7dcfa24d5e7a | 72 | mkdir("/sd/log_data", 0777); |
njewin | 1:9fe40d9ac0f5 | 73 | |
njewin | 9:7dcfa24d5e7a | 74 | //---- main while loop -------------------------------------------// |
njewin | 9:7dcfa24d5e7a | 75 | while(1) { |
njewin | 9:7dcfa24d5e7a | 76 | //---- Setup file on SD card ----------------------------// |
njewin | 9:7dcfa24d5e7a | 77 | printf("Opening sd card\n"); |
njewin | 9:7dcfa24d5e7a | 78 | char buffer[50]; |
njewin | 9:7dcfa24d5e7a | 79 | sprintf(buffer, "/sd/log_data/%i.csv", FileNo); |
njewin | 9:7dcfa24d5e7a | 80 | //**************************************************** |
njewin | 9:7dcfa24d5e7a | 81 | // This is necessary for card to work when reconnected |
njewin | 9:7dcfa24d5e7a | 82 | // Initialise disk |
njewin | 9:7dcfa24d5e7a | 83 | sd.disk_initialize(); |
njewin | 9:7dcfa24d5e7a | 84 | //**************************************************** |
njewin | 9:7dcfa24d5e7a | 85 | // Open a file for write |
njewin | 9:7dcfa24d5e7a | 86 | FILE *fp = fopen(buffer, "w"); |
njewin | 9:7dcfa24d5e7a | 87 | if(fp == NULL) { |
njewin | 9:7dcfa24d5e7a | 88 | error("Could not open file for write\n"); |
njewin | 3:b3358ec2f57c | 89 | } |
njewin | 12:894e648638e4 | 90 | //--- print ALL signals headers to sd card |
njewin | 11:2b2537dcf504 | 91 | 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(km/h),Latitude(deg),Longitude(deg) \r"); // sends header to file |
njewin | 9:7dcfa24d5e7a | 92 | |
njewin | 12:894e648638e4 | 93 | // start time |
njewin | 12:894e648638e4 | 94 | gps.timeNow(&q1); |
njewin | 12:894e648638e4 | 95 | pc.printf("%c %02d:%02d:%02d %02d/%02d/%04d\r\n", |
njewin | 12:894e648638e4 | 96 | q1.status, q1.hour, q1.minute, q1.second, q1.day, q1.month, q1.year); |
njewin | 12:894e648638e4 | 97 | // printf("%c %02d:%02d:%02d %02d/%02d/%04d\r\n", |
njewin | 12:894e648638e4 | 98 | // q1.status, q1.hour, q1.minute, q1.second, q1.day, q1.month, q1.year); |
njewin | 12:894e648638e4 | 99 | t.start(); // start logging timer |
njewin | 12:894e648638e4 | 100 | sync = 1; // sync step sent to VN8900 |
njewin | 12:894e648638e4 | 101 | |
njewin | 12:894e648638e4 | 102 | while (!pc.readable() && enable) { |
njewin | 12:894e648638e4 | 103 | if(flag_log==1) { |
njewin | 12:894e648638e4 | 104 | log_led= !log_led; // turns on LED3 to indicate logging |
njewin | 9:7dcfa24d5e7a | 105 | float time=t.read(); |
njewin | 9:7dcfa24d5e7a | 106 | float Yaw=data.Yaw; |
njewin | 9:7dcfa24d5e7a | 107 | float Roll=data.Roll; |
njewin | 9:7dcfa24d5e7a | 108 | float Pitch=data.Pitch; |
njewin | 9:7dcfa24d5e7a | 109 | float GyroX=data.Gyro_Proc_X; |
njewin | 9:7dcfa24d5e7a | 110 | float GyroY=data.Gyro_Proc_Y; |
njewin | 9:7dcfa24d5e7a | 111 | float GyroZ=data.Gyro_Proc_Z; |
njewin | 9:7dcfa24d5e7a | 112 | float AccelX=data.Accel_Proc_X; |
njewin | 9:7dcfa24d5e7a | 113 | float AccelY=data.Accel_Proc_Y; |
njewin | 9:7dcfa24d5e7a | 114 | float AccelZ=data.Accel_Proc_Z; |
njewin | 12:894e648638e4 | 115 | /* double GPSlong=gps.longitude(); // currently not reading GPS longitude correctly |
njewin | 10:d96e068f3595 | 116 | double GPSlat=gps.latitude(); // currently not reading GPS latitude correctly |
njewin | 10:d96e068f3595 | 117 | double GPSalt=gps.altitude(); |
njewin | 11:2b2537dcf504 | 118 | gps.vtg(&v1); |
njewin | 11:2b2537dcf504 | 119 | float GPScourse=v1.track_true(); |
njewin | 11:2b2537dcf504 | 120 | float GPSspeed=v1.velocity_kph(); |
njewin | 12:894e648638e4 | 121 | */ |
njewin | 12:894e648638e4 | 122 | |
njewin | 9:7dcfa24d5e7a | 123 | //----- print ALL signals to file --------------------// |
njewin | 12:894e648638e4 | 124 | // 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,%f\r", |
njewin | 12:894e648638e4 | 125 | // time, Yaw,Roll,Pitch, GyroX,GyroY,GyroZ, AccelX,AccelY,AccelZ, GPScourse,GPSspeed, GPSlat,GPSlong,GPSalt); |
njewin | 12:894e648638e4 | 126 | flag_log=0; // reset LogData interrupt flag |
njewin | 9:7dcfa24d5e7a | 127 | } // end if(flag=1) loop |
njewin | 12:894e648638e4 | 128 | |
njewin | 12:894e648638e4 | 129 | if(flag_pc==1) { |
njewin | 12:894e648638e4 | 130 | float time=t.read(); |
njewin | 12:894e648638e4 | 131 | float Yaw=data.Yaw; |
njewin | 12:894e648638e4 | 132 | float AccelX=data.Accel_Proc_X; |
njewin | 12:894e648638e4 | 133 | float AccelY=data.Accel_Proc_Y; |
njewin | 12:894e648638e4 | 134 | float AccelZ=data.Accel_Proc_Z; |
njewin | 12:894e648638e4 | 135 | double GPSlong=gps.longitude(); // currently not reading GPS longitude correctly |
njewin | 12:894e648638e4 | 136 | double GPSlat=gps.latitude(); // currently not reading GPS latitude correctly |
njewin | 12:894e648638e4 | 137 | gps.vtg(&v1); |
njewin | 12:894e648638e4 | 138 | float GPScourse=v1.track_true(); |
njewin | 12:894e648638e4 | 139 | float GPSspeed=v1.velocity_kph(); |
njewin | 12:894e648638e4 | 140 | |
njewin | 12:894e648638e4 | 141 | pc.printf("time %.3f,Yaw %3.0f,Accel %.2f, Lat %f, Long %f, kph %.lf \n", |
njewin | 12:894e648638e4 | 142 | time,Yaw,AccelX,GPSlat,GPSlong,GPSspeed); |
njewin | 12:894e648638e4 | 143 | pc.printf("debug time %f, counter %d \n", time_debug, counter); |
njewin | 12:894e648638e4 | 144 | counter = 0; |
njewin | 12:894e648638e4 | 145 | // pc.printf("time %.3f,Yaw %3.0f,Accel %.2f \n", |
njewin | 12:894e648638e4 | 146 | // time,Yaw,AccelX); |
njewin | 12:894e648638e4 | 147 | flag_pc=0; |
njewin | 12:894e648638e4 | 148 | pc_led = !pc_led; // Lights LED1 when transmitting to PC screen |
njewin | 12:894e648638e4 | 149 | } |
njewin | 12:894e648638e4 | 150 | |
njewin | 9:7dcfa24d5e7a | 151 | } // end while(!pc.readable()) loop |
njewin | 12:894e648638e4 | 152 | sync = 0; // sync step end sent to VN900 |
njewin | 12:894e648638e4 | 153 | fprintf(fp,"sync time %f \n",t.read()); |
njewin | 9:7dcfa24d5e7a | 154 | pc.printf(" Done. \n"); // prints 'done when logging is finished/enable switched off |
njewin | 9:7dcfa24d5e7a | 155 | log_led=0; // turns off LED logging is finished/enable switched off |
njewin | 9:7dcfa24d5e7a | 156 | wait(0.5); // debug wait for pc.printf |
njewin | 9:7dcfa24d5e7a | 157 | fclose(fp); // closes log file |
njewin | 12:894e648638e4 | 158 | pc.getc(); // Clear character from buffer |
njewin | 9:7dcfa24d5e7a | 159 | |
njewin | 12:894e648638e4 | 160 | while (!pc.readable() && !enable) { // Wait for a key press to restart logging |
njewin | 9:7dcfa24d5e7a | 161 | }; |
njewin | 9:7dcfa24d5e7a | 162 | pc.getc(); // Clear character from buffer |
njewin | 9:7dcfa24d5e7a | 163 | FileNo++; // Increment file number |
njewin | 3:b3358ec2f57c | 164 | } // end while(1) loop |
njewin | 9:7dcfa24d5e7a | 165 | |
njewin | 5:ac633cdbb75c | 166 | } // end main() program |
njewin | 5:ac633cdbb75c | 167 | |
njewin | 5:ac633cdbb75c | 168 | |
njewin | 9:7dcfa24d5e7a | 169 | /* DEUBBING NOTES |
njewin | 12:894e648638e4 | 170 | ----------------------------------------------------------------------------------------------- |
njewin | 9:7dcfa24d5e7a | 171 | opening a file BEFORE calling interrupts OK |
njewin | 5:ac633cdbb75c | 172 | opening a file and print to it BEFORE calling interrupts NOT OK (stops rest of program) |
njewin | 5:ac633cdbb75c | 173 | open a (local) file and print to it AFTER calling interrupts NOT OK (stops rest of program) |
njewin | 5:ac633cdbb75c | 174 | open a (sd) file and print to it AFTER calling interrupts OK |
njewin | 12:894e648638e4 | 175 | ----------------------------------------------------------------------------------------------- |
njewin | 12:894e648638e4 | 176 | measuring processor time for code |
njewin | 12:894e648638e4 | 177 | defining all of data variables including gps speed and course = 8us |
njewin | 12:894e648638e4 | 178 | printing all data to sd card = 250us - upto 25ms periodically |
njewin | 12:894e648638e4 | 179 | printing select data to pc = 400-500us |
njewin | 12:894e648638e4 | 180 | |
njewin | 12:894e648638e4 | 181 | ----------------------------------------------------------------------------------------------- |
njewin | 12:894e648638e4 | 182 | |
njewin | 5:ac633cdbb75c | 183 | */ |