![](/media/cache/profiles/2012_sf_avc_095.jpg.50x50_q85.jpg)
2018 revision to classic DataBus AVC code.
Dependencies: LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell
Logger/Logger.cpp@44:0d72a8a1288a, 2019-01-07 (annotated)
- Committer:
- shimniok
- Date:
- Mon Jan 07 16:47:33 2019 +0000
- Revision:
- 44:0d72a8a1288a
- Parent:
- 36:3095e00eef37
Rewrote TinyGPS -> NMEA, GPS::read() now returns struct
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shimniok | 24:a7f92dfc5310 | 1 | #include "Logger.h" |
shimniok | 31:20a95043adb0 | 2 | #include <stdio.h> |
shimniok | 31:20a95043adb0 | 3 | #include <algorithm> |
shimniok | 31:20a95043adb0 | 4 | #include <vector> |
shimniok | 31:20a95043adb0 | 5 | #include <iterator> |
shimniok | 31:20a95043adb0 | 6 | #include <string> |
shimniok | 24:a7f92dfc5310 | 7 | |
shimniok | 31:20a95043adb0 | 8 | Logger::Logger() { |
shimniok | 31:20a95043adb0 | 9 | _file = NULL; |
shimniok | 29:cb2f55fbfe9c | 10 | _fp = NULL; |
shimniok | 29:cb2f55fbfe9c | 11 | } |
shimniok | 29:cb2f55fbfe9c | 12 | |
shimniok | 29:cb2f55fbfe9c | 13 | |
shimniok | 29:cb2f55fbfe9c | 14 | void Logger::start() { |
shimniok | 31:20a95043adb0 | 15 | if (_fp == NULL) { |
shimniok | 31:20a95043adb0 | 16 | DIR *d; |
shimniok | 31:20a95043adb0 | 17 | char file[24]; |
shimniok | 31:20a95043adb0 | 18 | std::vector<string> fl; |
shimniok | 31:20a95043adb0 | 19 | dirent *p; |
shimniok | 31:20a95043adb0 | 20 | |
shimniok | 31:20a95043adb0 | 21 | if ((d = opendir("/log")) != NULL) { |
shimniok | 31:20a95043adb0 | 22 | while ((p = readdir(d)) != NULL) { |
shimniok | 31:20a95043adb0 | 23 | fl.push_back(string(p->d_name)); |
shimniok | 31:20a95043adb0 | 24 | } |
shimniok | 31:20a95043adb0 | 25 | closedir(d); |
shimniok | 31:20a95043adb0 | 26 | |
shimniok | 31:20a95043adb0 | 27 | // Open a new unique log file of the form NNNN.csv |
shimniok | 31:20a95043adb0 | 28 | for (int i=0; i <= 9999; i++) { |
shimniok | 31:20a95043adb0 | 29 | sprintf(file, "%04d.CSV", i); // generate filename |
shimniok | 31:20a95043adb0 | 30 | string x(file); // create string version |
shimniok | 31:20a95043adb0 | 31 | // Is this filename NOT listed in the directory? |
shimniok | 31:20a95043adb0 | 32 | if (std::find(fl.begin(), fl.end(), x) == fl.end()) { |
shimniok | 31:20a95043adb0 | 33 | x.insert(0, "/log/"); // prepend the correct path |
shimniok | 31:20a95043adb0 | 34 | if ((_fp = fopen(x.c_str(), "w")) == NULL) { |
shimniok | 31:20a95043adb0 | 35 | fprintf(stdout, "%s: cannot open\n", file); |
shimniok | 31:20a95043adb0 | 36 | } else { |
shimniok | 31:20a95043adb0 | 37 | fprintf(stdout, "%s: opened\n", file); |
shimniok | 31:20a95043adb0 | 38 | } |
shimniok | 31:20a95043adb0 | 39 | break; |
shimniok | 31:20a95043adb0 | 40 | }// if |
shimniok | 31:20a95043adb0 | 41 | }// for |
shimniok | 31:20a95043adb0 | 42 | }// if |
shimniok | 31:20a95043adb0 | 43 | } // if |
shimniok | 29:cb2f55fbfe9c | 44 | } |
shimniok | 29:cb2f55fbfe9c | 45 | |
shimniok | 29:cb2f55fbfe9c | 46 | |
shimniok | 29:cb2f55fbfe9c | 47 | void Logger::stop() { |
shimniok | 29:cb2f55fbfe9c | 48 | if (_fp && fclose(_fp) != EOF) { |
shimniok | 29:cb2f55fbfe9c | 49 | _fp = NULL; |
shimniok | 29:cb2f55fbfe9c | 50 | } |
shimniok | 29:cb2f55fbfe9c | 51 | } |
shimniok | 29:cb2f55fbfe9c | 52 | |
shimniok | 29:cb2f55fbfe9c | 53 | |
shimniok | 29:cb2f55fbfe9c | 54 | bool Logger::enabled() { |
shimniok | 29:cb2f55fbfe9c | 55 | return (_fp != NULL); |
shimniok | 24:a7f92dfc5310 | 56 | } |
shimniok | 24:a7f92dfc5310 | 57 | |
shimniok | 24:a7f92dfc5310 | 58 | |
shimniok | 30:ed791f1f7f7d | 59 | void Logger::log_gps(GpsData d) { |
shimniok | 29:cb2f55fbfe9c | 60 | if (enabled()) { |
shimniok | 29:cb2f55fbfe9c | 61 | fprintf(_fp, "G,%llu,%3.9f,%3.9f,%3.1f,%2.1f,%2.1f,%d\n", |
shimniok | 30:ed791f1f7f7d | 62 | d.timestamp, |
shimniok | 30:ed791f1f7f7d | 63 | d.latitude, |
shimniok | 30:ed791f1f7f7d | 64 | d.longitude, |
shimniok | 30:ed791f1f7f7d | 65 | d.course, |
shimniok | 30:ed791f1f7f7d | 66 | d.speed, |
shimniok | 30:ed791f1f7f7d | 67 | d.hdop, |
shimniok | 30:ed791f1f7f7d | 68 | d.svcount |
shimniok | 29:cb2f55fbfe9c | 69 | ); |
shimniok | 29:cb2f55fbfe9c | 70 | } |
shimniok | 24:a7f92dfc5310 | 71 | } |
shimniok | 24:a7f92dfc5310 | 72 | |
shimniok | 24:a7f92dfc5310 | 73 | |
shimniok | 30:ed791f1f7f7d | 74 | void Logger::log_sensors(SensorData d) { |
shimniok | 30:ed791f1f7f7d | 75 | if (enabled()) { |
shimniok | 32:eb673f6f5734 | 76 | fprintf(_fp, "S,%llu,%u,%d,%d,%d,%d,%d,%d,%d,%d,%d\n", |
shimniok | 30:ed791f1f7f7d | 77 | d.timestamp, |
shimniok | 30:ed791f1f7f7d | 78 | d.encoder, |
shimniok | 30:ed791f1f7f7d | 79 | d.gyro[0], |
shimniok | 30:ed791f1f7f7d | 80 | d.gyro[1], |
shimniok | 32:eb673f6f5734 | 81 | d.gyro[2], |
shimniok | 32:eb673f6f5734 | 82 | d.accel[0], |
shimniok | 32:eb673f6f5734 | 83 | d.accel[1], |
shimniok | 32:eb673f6f5734 | 84 | d.accel[2], |
shimniok | 32:eb673f6f5734 | 85 | d.mag[0], |
shimniok | 32:eb673f6f5734 | 86 | d.mag[1], |
shimniok | 32:eb673f6f5734 | 87 | d.mag[2] |
shimniok | 30:ed791f1f7f7d | 88 | ); |
shimniok | 30:ed791f1f7f7d | 89 | } |
shimniok | 30:ed791f1f7f7d | 90 | } |
shimniok | 30:ed791f1f7f7d | 91 | |
shimniok | 30:ed791f1f7f7d | 92 | |
shimniok | 24:a7f92dfc5310 | 93 | void Logger::log_estimation() { |
shimniok | 29:cb2f55fbfe9c | 94 | if (enabled()) { |
shimniok | 24:a7f92dfc5310 | 95 | } |
shimniok | 24:a7f92dfc5310 | 96 | } |