On boot, logs 10 seconds of current draw information from an INA169 breakout board to an SD card.
Dependencies: SDFileSystem mbed
main.cpp
00001 /** 00002 * Current Log-O-Matic 00003 * Author: Shawn Hymel @ SparkFun Electronics 00004 * Date: July 1, 2015 00005 * 00006 * Measures current using an INA169 breakout board. Stores current and timestamp 00007 * to an SD card. 00008 * 00009 * INA169 Vout -> Pin 15 00010 * SD Card DI -> Pin 5 00011 * SD Card DO -> Pin 6 00012 * SD Card SCK -> Pin 7 00013 * SD Card CS -> Pin 8 00014 * 00015 * This code is beerware; if you see me (or any other SparkFun 00016 * employee) at the local, and you've found our code helpful, please 00017 * buy us a round! 00018 * 00019 * Distributed as-is; no warranty is given. 00020 */ 00021 00022 #include "mbed.h" 00023 #include "SDFileSystem.h" 00024 00025 #define DEBUG 1 // 1 to print to console. 0 to turn off. 00026 #define MAX_FILES 100 // Maximum number of log files to store on SD 00027 #define DELAY_MS 0.02 // Time between samples (in seconds) 00028 #define NUM_SAMPLES 500 // Number of samples to collect 00029 #define RS 0.5 // Value of RS (sensing resistor) in ohms 00030 #define RL 10000 // Value of RL (output resistor) in ohms 00031 00032 // Digital output (status LED) 00033 DigitalOut status_led(LED1); 00034 00035 // Analog input (pin 15) 00036 AnalogIn ain(p15); 00037 00038 // USB serial (tx, rx) 00039 Serial pc(USBTX, USBRX); 00040 00041 // SD card (SPI pins) 00042 SDFileSystem sd(p5, p6, p7, p8, "sd"); 00043 00044 // Global variables 00045 const char file_root[] = "idata"; 00046 const char file_suffix[] = ".csv"; 00047 char filename[20]; 00048 Timer timer; 00049 00050 // Return true if file (or directory) 'filename' is present in directory 'root' 00051 // Based on: https://developer.mbed.org/forum/mbed/topic/463/ 00052 bool exists(char * root, char *filename) 00053 { 00054 DIR *d = opendir(root); 00055 struct dirent *p; 00056 bool found = false; 00057 if ( d != NULL ) 00058 { 00059 while ( !found && (p = readdir(d)) != NULL ) 00060 { 00061 if ( strcmp(p->d_name, filename) == 0 ) 00062 found = true; 00063 } 00064 } 00065 closedir(d); 00066 return found; 00067 } 00068 00069 // Main 00070 int main( void ) 00071 { 00072 FILE *file; 00073 char str[5]; 00074 int i; 00075 int led_state; 00076 float voltage_in; 00077 float current; 00078 float timestamp; 00079 00080 // Debugging start 00081 #if DEBUG == 1 00082 printf("\n\r"); 00083 printf("-------------------\n\r"); 00084 printf("Current Log-O-Matic\n\r"); 00085 printf("-------------------\n\r"); 00086 #endif 00087 00088 // Create next file in sequence 00089 for ( i = 0; i < MAX_FILES; i++ ) { 00090 00091 // Construct filename 00092 strcpy(filename, file_root); 00093 sprintf(str, "%d", i); 00094 strcat(filename, str); 00095 strcat(filename, file_suffix); 00096 00097 // If file exists, skip to next. If not, create it. 00098 if ( exists("/sd", filename) ) { 00099 #if DEBUG == 1 00100 printf ("%s found\n\r", filename); 00101 #endif 00102 if ( i == (MAX_FILES - 1) ) { 00103 error("ERROR: Too many files!\n\r"); 00104 return -1; 00105 } 00106 } else { 00107 #if DEBUG == 1 00108 printf ("%s not found. Creating.\n\r", filename); 00109 #endif 00110 break; 00111 } 00112 } 00113 00114 // Construct absolute path to file 00115 strcpy(filename, "/sd/"); 00116 strcat(filename, file_root); 00117 sprintf(str, "%d", i); 00118 strcat(filename, str); 00119 strcat(filename, file_suffix); 00120 00121 // Open file for writing 00122 #if DEBUG == 1 00123 printf("Opening %s\n\r", filename); 00124 #endif 00125 file = fopen(filename, "w"); 00126 if ( file == NULL ) { 00127 error("ERROR: Could not open file for writing!\n\r"); 00128 return -1; 00129 } 00130 00131 // Write header to file 00132 fprintf(file, "Time,Current\n\r"); 00133 00134 // Start timer and begin sampling 00135 led_state = 0; 00136 #if DEBUG == 1 00137 printf("Sampling. Do not remove SD card!\n\r"); 00138 #endif 00139 timer.start(); 00140 for ( i = 0; i < NUM_SAMPLES; i++ ) { 00141 00142 // Write data to csv file 00143 timestamp = timer.read(); 00144 voltage_in = ain * 3.3; 00145 current = (voltage_in * 1000) / (RS * RL); 00146 fprintf(file, "%2.2f,%1.3f\n", timestamp, current); 00147 #if DEBUG == 1 00148 printf("%2.2f, %1.3f\n\r", timestamp, current); 00149 #endif 00150 00151 // Toggle LED to show logging 00152 led_state ^= 1; 00153 status_led = led_state; 00154 00155 // Wait until next sample cycle as defined by sample delay 00156 while ( (timer.read() - timestamp) < DELAY_MS ); 00157 } 00158 00159 // Close file 00160 #if DEBUG == 1 00161 printf("Done! Closing file"); 00162 #endif 00163 fclose(file); 00164 00165 // Turn off LED to show done 00166 status_led = 0; 00167 00168 return 0; 00169 }
Generated on Sat Jul 30 2022 09:46:59 by 1.7.2