From 90408278b746efee064e6a43ab7fc7c535007965 Mon Sep 17 00:00:00 2001 From: Thomas Reto Strub Date: Fri, 28 Apr 2017 22:59:22 +0200 Subject: [PATCH 01/18] add TinyShield GPS project source: https://tinycircuits.com/products/gps-tinyshield --- TinyDuino/TinyGPS.cpp | 515 +++++++++++++++++++++++++++++++++++ TinyDuino/TinyGPS.h | 177 ++++++++++++ TinyDuino/TinyShield_GPS.ino | 160 +++++++++++ 3 files changed, 852 insertions(+) create mode 100644 TinyDuino/TinyGPS.cpp create mode 100644 TinyDuino/TinyGPS.h create mode 100644 TinyDuino/TinyShield_GPS.ino diff --git a/TinyDuino/TinyGPS.cpp b/TinyDuino/TinyGPS.cpp new file mode 100644 index 0000000..14f8e52 --- /dev/null +++ b/TinyDuino/TinyGPS.cpp @@ -0,0 +1,515 @@ +/* +TinyGPS - a small GPS library for Arduino providing basic NMEA parsing +Based on work by and "distance_to" and "course_to" courtesy of Maarten Lamers. +Suggestion to add satellites(), course_to(), and cardinal(), by Matt Monson. +Precision improvements suggested by Wayne Holder. +Copyright (C) 2008-2013 Mikal Hart +All rights reserved. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "TinyGPS.h" + +#define _GPRMC_TERM "GPRMC" +#define _GPGGA_TERM "GPGGA" +#define _GPGSA_TERM "GPGSA" +#define _GNRMC_TERM "GNRMC" +#define _GNGNS_TERM "GNGNS" +#define _GNGSA_TERM "GNGSA" +#define _GPGSV_TERM "GPGSV" +#define _GLGSV_TERM "GLGSV" + +TinyGPS::TinyGPS() + : _time(GPS_INVALID_TIME) + , _date(GPS_INVALID_DATE) + , _latitude(GPS_INVALID_ANGLE) + , _longitude(GPS_INVALID_ANGLE) + , _altitude(GPS_INVALID_ALTITUDE) + , _speed(GPS_INVALID_SPEED) + , _course(GPS_INVALID_ANGLE) + , _hdop(GPS_INVALID_HDOP) + , _numsats(GPS_INVALID_SATELLITES) + , _last_time_fix(GPS_INVALID_FIX_TIME) + , _last_position_fix(GPS_INVALID_FIX_TIME) + , _parity(0) + , _is_checksum_term(false) + , _sentence_type(_GPS_SENTENCE_OTHER) + , _term_number(0) + , _term_offset(0) + , _gps_data_good(false) +#ifndef _GPS_NO_STATS + , _encoded_characters(0) + , _good_sentences(0) + , _failed_checksum(0) +#endif +{ + _term[0] = '\0'; +} + +// +// public methods +// + +bool TinyGPS::encode(char c) +{ + bool valid_sentence = false; + +#ifndef _GPS_NO_STATS + ++_encoded_characters; +#endif + switch(c) + { + case ',': // term terminators + _parity ^= c; + case '\r': + case '\n': + case '*': + if (_term_offset < sizeof(_term)) + { + _term[_term_offset] = 0; + valid_sentence = term_complete(); + } + ++_term_number; + _term_offset = 0; + _is_checksum_term = c == '*'; + return valid_sentence; + + case '$': // sentence begin + _term_number = _term_offset = 0; + _parity = 0; + _sentence_type = _GPS_SENTENCE_OTHER; + _is_checksum_term = false; + _gps_data_good = false; + return valid_sentence; + } + + // ordinary characters + if (_term_offset < sizeof(_term) - 1) + _term[_term_offset++] = c; + if (!_is_checksum_term) + _parity ^= c; + + return valid_sentence; +} + +#ifndef _GPS_NO_STATS +void TinyGPS::stats(unsigned long *chars, unsigned short *sentences, unsigned short *failed_cs) +{ + if (chars) *chars = _encoded_characters; + if (sentences) *sentences = _good_sentences; + if (failed_cs) *failed_cs = _failed_checksum; +} +#endif + +// +// internal utilities +// +int TinyGPS::from_hex(char a) +{ + if (a >= 'A' && a <= 'F') + return a - 'A' + 10; + else if (a >= 'a' && a <= 'f') + return a - 'a' + 10; + else + return a - '0'; +} + +unsigned long TinyGPS::parse_decimal() +{ + char *p = _term; + bool isneg = *p == '-'; + if (isneg) ++p; + unsigned long ret = 100UL * gpsatol(p); + while (gpsisdigit(*p)) ++p; + if (*p == '.') + { + if (gpsisdigit(p[1])) + { + ret += 10 * (p[1] - '0'); + if (gpsisdigit(p[2])) + ret += p[2] - '0'; + } + } + return isneg ? -ret : ret; +} + +// Parse a string in the form ddmm.mmmmmmm... +unsigned long TinyGPS::parse_degrees() +{ + char *p; + unsigned long left_of_decimal = gpsatol(_term); + unsigned long hundred1000ths_of_minute = (left_of_decimal % 100UL) * 100000UL; + for (p=_term; gpsisdigit(*p); ++p); + if (*p == '.') + { + unsigned long mult = 10000; + while (gpsisdigit(*++p)) + { + hundred1000ths_of_minute += mult * (*p - '0'); + mult /= 10; + } + } + return (left_of_decimal / 100) * 1000000 + (hundred1000ths_of_minute + 3) / 6; +} + +#define COMBINE(sentence_type, term_number) (((unsigned)(sentence_type) << 5) | term_number) + +// Processes a just-completed term +// Returns true if new sentence has just passed checksum test and is validated +bool TinyGPS::term_complete() +{ + if (_is_checksum_term) + { + byte checksum = 16 * from_hex(_term[0]) + from_hex(_term[1]); + if (checksum == _parity) + { + if(_sentence_type == _GPS_SENTENCE_GPRMC) //set the time and date even if not tracking + { + _time = _new_time; + _date = _new_date; + } + if (_gps_data_good) + { +#ifndef _GPS_NO_STATS + ++_good_sentences; +#endif + _last_time_fix = _new_time_fix; + _last_position_fix = _new_position_fix; + + switch(_sentence_type) + { + case _GPS_SENTENCE_GPRMC: + _time = _new_time; + _date = _new_date; + _latitude = _new_latitude; + _longitude = _new_longitude; + _speed = _new_speed; + _course = _new_course; + break; + case _GPS_SENTENCE_GPGGA: + _altitude = _new_altitude; + _time = _new_time; + _latitude = _new_latitude; + _longitude = _new_longitude; + _numsats = _new_numsats; + _hdop = _new_hdop; + break; + } + + return true; + } + } + +#ifndef _GPS_NO_STATS + else + ++_failed_checksum; +#endif + return false; + } + + // the first term determines the sentence type + if (_term_number == 0) + { + if (!gpsstrcmp(_term, _GPRMC_TERM) || !gpsstrcmp(_term, _GNRMC_TERM)) + _sentence_type = _GPS_SENTENCE_GPRMC; + else if (!gpsstrcmp(_term, _GPGGA_TERM)) + _sentence_type = _GPS_SENTENCE_GPGGA; + else if (!gpsstrcmp(_term, _GNGNS_TERM)) + _sentence_type = _GPS_SENTENCE_GNGNS; + else if (!gpsstrcmp(_term, _GNGSA_TERM) || !gpsstrcmp(_term, _GPGSA_TERM)) + _sentence_type = _GPS_SENTENCE_GNGSA; + else if (!gpsstrcmp(_term, _GPGSV_TERM)) + _sentence_type = _GPS_SENTENCE_GPGSV; + else if (!gpsstrcmp(_term, _GLGSV_TERM)) + _sentence_type = _GPS_SENTENCE_GLGSV; + else + _sentence_type = _GPS_SENTENCE_OTHER; + return false; + } + + if (_sentence_type != _GPS_SENTENCE_OTHER && _term[0]) + switch(COMBINE(_sentence_type, _term_number)) + { + case COMBINE(_GPS_SENTENCE_GPRMC, 1): // Time in both sentences + case COMBINE(_GPS_SENTENCE_GPGGA, 1): + case COMBINE(_GPS_SENTENCE_GNGNS, 1): + _new_time = parse_decimal(); + _new_time_fix = millis(); + break; + case COMBINE(_GPS_SENTENCE_GPRMC, 2): // GPRMC validity + _gps_data_good = _term[0] == 'A'; + break; + case COMBINE(_GPS_SENTENCE_GPRMC, 3): // Latitude + case COMBINE(_GPS_SENTENCE_GPGGA, 2): + case COMBINE(_GPS_SENTENCE_GNGNS, 2): + _new_latitude = parse_degrees(); + _new_position_fix = millis(); + break; + case COMBINE(_GPS_SENTENCE_GPRMC, 4): // N/S + case COMBINE(_GPS_SENTENCE_GPGGA, 3): + case COMBINE(_GPS_SENTENCE_GNGNS, 3): + if (_term[0] == 'S') + _new_latitude = -_new_latitude; + break; + case COMBINE(_GPS_SENTENCE_GPRMC, 5): // Longitude + case COMBINE(_GPS_SENTENCE_GPGGA, 4): + case COMBINE(_GPS_SENTENCE_GNGNS, 4): + _new_longitude = parse_degrees(); + break; + case COMBINE(_GPS_SENTENCE_GPRMC, 6): // E/W + case COMBINE(_GPS_SENTENCE_GPGGA, 5): + case COMBINE(_GPS_SENTENCE_GNGNS, 5): + if (_term[0] == 'W') + _new_longitude = -_new_longitude; + break; + case COMBINE(_GPS_SENTENCE_GNGNS, 6): + strncpy(_constellations, _term, 5); + break; + case COMBINE(_GPS_SENTENCE_GPRMC, 7): // Speed (GPRMC) + _new_speed = parse_decimal(); + break; + case COMBINE(_GPS_SENTENCE_GPRMC, 8): // Course (GPRMC) + _new_course = parse_decimal(); + break; + case COMBINE(_GPS_SENTENCE_GPRMC, 9): // Date (GPRMC) + _new_date = gpsatol(_term); + break; + case COMBINE(_GPS_SENTENCE_GPGGA, 6): // Fix data (GPGGA) + _gps_data_good = _term[0] > '0'; + break; + case COMBINE(_GPS_SENTENCE_GPGGA, 7): // Satellites used (GPGGA): GPS only + case COMBINE(_GPS_SENTENCE_GNGNS, 7): // GNGNS counts-in all constellations + _new_numsats = (unsigned char)atoi(_term); + break; + case COMBINE(_GPS_SENTENCE_GPGGA, 8): // HDOP + _new_hdop = parse_decimal(); + break; + case COMBINE(_GPS_SENTENCE_GPGGA, 9): // Altitude (GPGGA) + _new_altitude = parse_decimal(); + break; + case COMBINE(_GPS_SENTENCE_GNGSA, 3): //satellites used in solution: 3 to 15 + //_sats_used[ + break; + case COMBINE(_GPS_SENTENCE_GPGSV, 2): //beginning of sequence + case COMBINE(_GPS_SENTENCE_GLGSV, 2): //beginning of sequence + { + uint8_t msgId = atoi(_term)-1; //start from 0 + if(msgId == 0) { + //http://geostar-navigation.com/file/geos3/geos_nmea_protocol_v3_0_eng.pdf + if(_sentence_type == _GPS_SENTENCE_GPGSV) { + //reset GPS & WAAS trackedSatellites + for(uint8_t x=0;x<12;x++) + { + tracked_sat_rec[x] = 0; + } + } else { + //reset GLONASS trackedSatellites: range starts with 23 + for(uint8_t x=12;x<24;x++) + { + tracked_sat_rec[x] = 0; + } + } + } + _sat_index = msgId*4; //4 sattelites/line + if(_sentence_type == _GPS_SENTENCE_GLGSV) + { + _sat_index = msgId*4 + 12; //Glonass offset by 12 + } + break; + } + case COMBINE(_GPS_SENTENCE_GPGSV, 4): //satellite # + case COMBINE(_GPS_SENTENCE_GPGSV, 8): + case COMBINE(_GPS_SENTENCE_GPGSV, 12): + case COMBINE(_GPS_SENTENCE_GPGSV, 16): + case COMBINE(_GPS_SENTENCE_GLGSV, 4): + case COMBINE(_GPS_SENTENCE_GLGSV, 8): + case COMBINE(_GPS_SENTENCE_GLGSV, 12): + case COMBINE(_GPS_SENTENCE_GLGSV, 16): + _tracked_satellites_index = atoi(_term); + break; + case COMBINE(_GPS_SENTENCE_GPGSV, 7): //strength + case COMBINE(_GPS_SENTENCE_GPGSV, 11): + case COMBINE(_GPS_SENTENCE_GPGSV, 15): + case COMBINE(_GPS_SENTENCE_GPGSV, 19): + case COMBINE(_GPS_SENTENCE_GLGSV, 7): //strength + case COMBINE(_GPS_SENTENCE_GLGSV, 11): + case COMBINE(_GPS_SENTENCE_GLGSV, 15): + case COMBINE(_GPS_SENTENCE_GLGSV, 19): + uint8_t stren = (uint8_t)atoi(_term); + if(stren == 0) //remove the record, 0dB strength + { + tracked_sat_rec[_sat_index + (_term_number-7)/4] = 0; + } + else + { + tracked_sat_rec[_sat_index + (_term_number-7)/4] = _tracked_satellites_index<<8 | stren<<1; + } + break; + } + + return false; +} + +long TinyGPS::gpsatol(const char *str) +{ + long ret = 0; + while (gpsisdigit(*str)) + ret = 10 * ret + *str++ - '0'; + return ret; +} + +int TinyGPS::gpsstrcmp(const char *str1, const char *str2) +{ + while (*str1 && *str1 == *str2) + ++str1, ++str2; + return *str1; +} + +/* static */ +float TinyGPS::distance_between (float lat1, float long1, float lat2, float long2) +{ + // returns distance in meters between two positions, both specified + // as signed decimal-degrees latitude and longitude. Uses great-circle + // distance computation for hypothetical sphere of radius 6372795 meters. + // Because Earth is no exact sphere, rounding errors may be up to 0.5%. + // Courtesy of Maarten Lamers + float delta = radians(long1-long2); + float sdlong = sin(delta); + float cdlong = cos(delta); + lat1 = radians(lat1); + lat2 = radians(lat2); + float slat1 = sin(lat1); + float clat1 = cos(lat1); + float slat2 = sin(lat2); + float clat2 = cos(lat2); + delta = (clat1 * slat2) - (slat1 * clat2 * cdlong); + delta = sq(delta); + delta += sq(clat2 * sdlong); + delta = sqrt(delta); + float denom = (slat1 * slat2) + (clat1 * clat2 * cdlong); + delta = atan2(delta, denom); + return delta * 6372795; +} + +float TinyGPS::course_to (float lat1, float long1, float lat2, float long2) +{ + // returns course in degrees (North=0, West=270) from position 1 to position 2, + // both specified as signed decimal-degrees latitude and longitude. + // Because Earth is no exact sphere, calculated course may be off by a tiny fraction. + // Courtesy of Maarten Lamers + float dlon = radians(long2-long1); + lat1 = radians(lat1); + lat2 = radians(lat2); + float a1 = sin(dlon) * cos(lat2); + float a2 = sin(lat1) * cos(lat2) * cos(dlon); + a2 = cos(lat1) * sin(lat2) - a2; + a2 = atan2(a1, a2); + if (a2 < 0.0) + { + a2 += TWO_PI; + } + return degrees(a2); +} + +const char *TinyGPS::cardinal (float course) +{ + static const char* directions[] = {"N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"}; + + int direction = (int)((course + 11.25f) / 22.5f); + return directions[direction % 16]; +} + +// lat/long in MILLIONTHs of a degree and age of fix in milliseconds +// (note: versions 12 and earlier gave this value in 100,000ths of a degree. +void TinyGPS::get_position(long *latitude, long *longitude, unsigned long *fix_age) +{ + if (latitude) *latitude = _latitude; + if (longitude) *longitude = _longitude; + if (fix_age) *fix_age = _last_position_fix == GPS_INVALID_FIX_TIME ? + GPS_INVALID_AGE : millis() - _last_position_fix; +} + +// date as ddmmyy, time as hhmmsscc, and age in milliseconds +void TinyGPS::get_datetime(unsigned long *date, unsigned long *time, unsigned long *age) +{ + if (date) *date = _date; + if (time) *time = _time; + if (age) *age = _last_time_fix == GPS_INVALID_FIX_TIME ? + GPS_INVALID_AGE : millis() - _last_time_fix; +} + +void TinyGPS::f_get_position(float *latitude, float *longitude, unsigned long *fix_age) +{ + long lat, lon; + get_position(&lat, &lon, fix_age); + *latitude = lat == GPS_INVALID_ANGLE ? GPS_INVALID_F_ANGLE : (lat / 1000000.0); + *longitude = lat == GPS_INVALID_ANGLE ? GPS_INVALID_F_ANGLE : (lon / 1000000.0); +} + +void TinyGPS::crack_datetime(int *year, byte *month, byte *day, + byte *hour, byte *minute, byte *second, byte *hundredths, unsigned long *age) +{ + unsigned long date, time; + get_datetime(&date, &time, age); + if (year) + { + *year = date % 100; + *year += *year > 80 ? 1900 : 2000; + } + if (month) *month = (date / 100) % 100; + if (day) *day = date / 10000; + if (hour) *hour = time / 1000000; + if (minute) *minute = (time / 10000) % 100; + if (second) *second = (time / 100) % 100; + if (hundredths) *hundredths = time % 100; +} + +float TinyGPS::f_altitude() +{ + return _altitude == GPS_INVALID_ALTITUDE ? GPS_INVALID_F_ALTITUDE : _altitude / 100.0; +} + +float TinyGPS::f_course() +{ + return _course == GPS_INVALID_ANGLE ? GPS_INVALID_F_ANGLE : _course / 100.0; +} + +float TinyGPS::f_speed_knots() +{ + return _speed == GPS_INVALID_SPEED ? GPS_INVALID_F_SPEED : _speed / 100.0; +} + +float TinyGPS::f_speed_mph() +{ + float sk = f_speed_knots(); + return sk == GPS_INVALID_F_SPEED ? GPS_INVALID_F_SPEED : _GPS_MPH_PER_KNOT * sk; +} + +float TinyGPS::f_speed_mps() +{ + float sk = f_speed_knots(); + return sk == GPS_INVALID_F_SPEED ? GPS_INVALID_F_SPEED : _GPS_MPS_PER_KNOT * sk; +} + +float TinyGPS::f_speed_kmph() +{ + float sk = f_speed_knots(); + return sk == GPS_INVALID_F_SPEED ? GPS_INVALID_F_SPEED : _GPS_KMPH_PER_KNOT * sk; +} + +const float TinyGPS::GPS_INVALID_F_ANGLE = 1000.0; +const float TinyGPS::GPS_INVALID_F_ALTITUDE = 1000000.0; +const float TinyGPS::GPS_INVALID_F_SPEED = -1.0; \ No newline at end of file diff --git a/TinyDuino/TinyGPS.h b/TinyDuino/TinyGPS.h new file mode 100644 index 0000000..b541de6 --- /dev/null +++ b/TinyDuino/TinyGPS.h @@ -0,0 +1,177 @@ +/* +TinyGPS - a small GPS library for Arduino providing basic NMEA parsing +Based on work by and "distance_to" and "course_to" courtesy of Maarten Lamers. +Suggestion to add satellites(), course_to(), and cardinal(), by Matt Monson. +Precision improvements suggested by Wayne Holder. +Copyright (C) 2008-2013 Mikal Hart +All rights reserved. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef TinyGPS_h +#define TinyGPS_h + +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif + +#include + +#define _GPS_VERSION 13 // software version of this library +#define _GPS_MPH_PER_KNOT 1.15077945 +#define _GPS_MPS_PER_KNOT 0.51444444 +#define _GPS_KMPH_PER_KNOT 1.852 +#define _GPS_MILES_PER_METER 0.00062137112 +#define _GPS_KM_PER_METER 0.001 +// #define _GPS_NO_STATS + +class TinyGPS +{ +public: + enum { + GPS_INVALID_AGE = 0xFFFFFFFF, GPS_INVALID_ANGLE = 999999999, + GPS_INVALID_ALTITUDE = 999999999, GPS_INVALID_DATE = 0, + GPS_INVALID_TIME = 0xFFFFFFFF, GPS_INVALID_SPEED = 999999999, + GPS_INVALID_FIX_TIME = 0xFFFFFFFF, GPS_INVALID_SATELLITES = 0xFF, + GPS_INVALID_HDOP = 0xFFFFFFFF + }; + + static const float GPS_INVALID_F_ANGLE, GPS_INVALID_F_ALTITUDE, GPS_INVALID_F_SPEED; + + TinyGPS(); + bool encode(char c); // process one character received from GPS + TinyGPS &operator << (char c) {encode(c); return *this;} + + // lat/long in MILLIONTHs of a degree and age of fix in milliseconds + // (note: versions 12 and earlier gave lat/long in 100,000ths of a degree. + void get_position(long *latitude, long *longitude, unsigned long *fix_age = 0); + + // date as ddmmyy, time as hhmmsscc, and age in milliseconds + void get_datetime(unsigned long *date, unsigned long *time, unsigned long *age = 0); + + // signed altitude in centimeters (from GPGGA sentence) + inline long altitude() { return _altitude; } + + // course in last full GPRMC sentence in 100th of a degree + inline unsigned long course() { return _course; } + + // speed in last full GPRMC sentence in 100ths of a knot + inline unsigned long speed() { return _speed; } + + // satellites used in last full GPGGA sentence + inline unsigned short satellites() { return _numsats; } + + // horizontal dilution of precision in 100ths + inline unsigned long hdop() { return _hdop; } + + inline char* constellations() { return _constellations; } + inline uint32_t* trackedSatellites() { return tracked_sat_rec; } + + void f_get_position(float *latitude, float *longitude, unsigned long *fix_age = 0); + void crack_datetime(int *year, byte *month, byte *day, + byte *hour, byte *minute, byte *second, byte *hundredths = 0, unsigned long *fix_age = 0); + float f_altitude(); + float f_course(); + float f_speed_knots(); + float f_speed_mph(); + float f_speed_mps(); + float f_speed_kmph(); + + static int library_version() { return _GPS_VERSION; } + + static float distance_between (float lat1, float long1, float lat2, float long2); + static float course_to (float lat1, float long1, float lat2, float long2); + static const char *cardinal(float course); + +#ifndef _GPS_NO_STATS + void stats(unsigned long *chars, unsigned short *good_sentences, unsigned short *failed_cs); +#endif + +private: + enum {_GPS_SENTENCE_GPGGA, _GPS_SENTENCE_GPRMC, _GPS_SENTENCE_GNGNS, _GPS_SENTENCE_GNGSA, + _GPS_SENTENCE_GPGSV, _GPS_SENTENCE_GLGSV, _GPS_SENTENCE_OTHER}; + + // properties + unsigned long _time, _new_time; + unsigned long _date, _new_date; + long _latitude, _new_latitude; + long _longitude, _new_longitude; + long _altitude, _new_altitude; + unsigned long _speed, _new_speed; + unsigned long _course, _new_course; + unsigned long _hdop, _new_hdop; + unsigned short _numsats, _new_numsats; + + unsigned long _last_time_fix, _new_time_fix; + unsigned long _last_position_fix, _new_position_fix; + + // parsing state variables + byte _parity; + bool _is_checksum_term; + char _term[15]; + byte _sentence_type; + byte _term_number; + byte _term_offset; + bool _gps_data_good; + + struct TrackedSattelites { + uint8_t prn; //"pseudo-random noise" sequences, or Gold codes. GPS sats are listed here http://en.wikipedia.org/wiki/List_of_GPS_satellites + uint8_t strength; //in dB + }; + + char _constellations[5]; + uint8_t _sat_used_count; + + //format: + //bit 0-7: sat ID + //bit 8-14: snr (dB), max 99dB + //bit 15: used in solution (when tracking) + uint32_t tracked_sat_rec[24]; //TODO: externalize array size + int _tracked_satellites_index; + uint8_t _sat_index; + +#ifndef _GPS_NO_STATS + // statistics + unsigned long _encoded_characters; + unsigned short _good_sentences; + unsigned short _failed_checksum; + unsigned short _passed_checksum; +#endif + + // internal utilities + int from_hex(char a); + unsigned long parse_decimal(); + unsigned long parse_degrees(); + bool term_complete(); + bool gpsisdigit(char c) { return c >= '0' && c <= '9'; } + long gpsatol(const char *str); + int gpsstrcmp(const char *str1, const char *str2); +}; + +#if !defined(ARDUINO) +// Arduino 0012 workaround +#undef int +#undef char +#undef long +#undef byte +#undef float +#undef abs +#undef round +#endif + +#endif \ No newline at end of file diff --git a/TinyDuino/TinyShield_GPS.ino b/TinyDuino/TinyShield_GPS.ino new file mode 100644 index 0000000..c497f25 --- /dev/null +++ b/TinyDuino/TinyShield_GPS.ino @@ -0,0 +1,160 @@ +//------------------------------------------------------------------------------- +// TinyCircuits GPS TinyShield Display Example +// Last updated 7 April 2016 +// +// This example uses the TinyGPS library, originally written by Mikal Hart and +// currently forked and updated at https://github.com/florind/TinyGPS to support +// Glonass, slightly modified to work with the GPS TinyShield. +// +// Some GPS modules have been shipped with 4800 baud instead of 9600- try this if +// you see bad data. +// +// TinyCircuits, http://TinyCircuits.com +// +//------------------------------------------------------------------------------- + + +#include "TinyGPS.h" +#include + +const int GPS_ONOFFPin = A3; +const int GPS_SYSONPin = A2; +const int RXPin = A1, TXPin = A0; +const uint32_t GPSBaud = 9600; + +// The TinyGPS++ object +TinyGPS gps; + +// The serial connection to the GPS device +SoftwareSerial ss(RXPin, TXPin); + +void setup() +{ + Serial.begin(115200); + ss.begin(GPSBaud); + pinMode(GPS_SYSONPin, INPUT); + digitalWrite(GPS_ONOFFPin, LOW); + pinMode(GPS_ONOFFPin, OUTPUT); + delay(100); + Serial.print("Attempting to wake GPS module.. "); + while (digitalRead( GPS_SYSONPin ) == LOW ) + { + // Need to wake the module + digitalWrite( GPS_ONOFFPin, HIGH ); + delay(5); + digitalWrite( GPS_ONOFFPin, LOW ); + delay(100); + } + Serial.println("done."); + + Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version()); + Serial.println("by Mikal Hart"); + Serial.println(); + Serial.println("Sats HDOP Latitude Longitude Fix Date Time Date Alt Course Speed Card Distance Course Card Chars Sentences Checksum"); + Serial.println(" (deg) (deg) Age Age (m) --- from GPS ---- ---- to London ---- RX RX Fail"); + Serial.println("-------------------------------------------------------------------------------------------------------------------------------------"); +} + +void loop() +{ + float flat, flon; + unsigned long age, date, time, chars = 0; + unsigned short sentences = 0, failed = 0; + static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002; + + print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5); + print_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP, 5); + gps.f_get_position(&flat, &flon, &age); + print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 10, 6); + print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 11, 6); + print_int(age, TinyGPS::GPS_INVALID_AGE, 5); + print_date(gps); + print_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 7, 2); + print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2); + print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2); + print_str(gps.f_course() == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(gps.f_course()), 6); + print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, LONDON_LAT, LONDON_LON) / 1000, 0xFFFFFFFF, 9); + print_float(flat == TinyGPS::GPS_INVALID_F_ANGLE ? TinyGPS::GPS_INVALID_F_ANGLE : TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2); + print_str(flat == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON)), 6); + + gps.stats(&chars, &sentences, &failed); + print_int(chars, 0xFFFFFFFF, 6); + print_int(sentences, 0xFFFFFFFF, 10); + print_int(failed, 0xFFFFFFFF, 9); + Serial.println(); + + smartdelay(1000); +} + +static void smartdelay(unsigned long ms) +{ + unsigned long start = millis(); + do + { + while (ss.available()) + gps.encode(ss.read()); + } while (millis() - start < ms); +} + +static void print_float(float val, float invalid, int len, int prec) +{ + if (val == invalid) + { + while (len-- > 1) + Serial.print('*'); + Serial.print(' '); + } + else + { + Serial.print(val, prec); + int vi = abs((int)val); + int flen = prec + (val < 0.0 ? 2 : 1); // . and - + flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1; + for (int i=flen; i 0) + sz[len-1] = ' '; + Serial.print(sz); + smartdelay(0); +} + +static void print_date(TinyGPS &gps) +{ + int year; + byte month, day, hour, minute, second, hundredths; + unsigned long age; + gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age); + if (age == TinyGPS::GPS_INVALID_AGE) + Serial.print("********** ******** "); + else + { + char sz[32]; + sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ", + month, day, year, hour, minute, second); + Serial.print(sz); + } + print_int(age, TinyGPS::GPS_INVALID_AGE, 5); + smartdelay(0); +} + +static void print_str(const char *str, int len) +{ + int slen = strlen(str); + for (int i=0; i Date: Fri, 28 Apr 2017 23:51:16 +0200 Subject: [PATCH 02/18] read gps data (integer) --- TinyDuino/TinyDuino.ino | 102 ++++++++++++++++++++++ TinyDuino/TinyShield_GPS.ino | 160 ----------------------------------- 2 files changed, 102 insertions(+), 160 deletions(-) delete mode 100644 TinyDuino/TinyShield_GPS.ino diff --git a/TinyDuino/TinyDuino.ino b/TinyDuino/TinyDuino.ino index 1a08dd7..611aa2d 100644 --- a/TinyDuino/TinyDuino.ino +++ b/TinyDuino/TinyDuino.ino @@ -9,6 +9,21 @@ void ledShowChar(char c); /// GPS /// GPS /// GPS /// +#define _GPS_NO_STATS + +#include +#include "TinyGPS.h" + +typedef struct { + unsigned short nofSatellites; + long latitude, longitude, altitude; + unsigned long date, time; +} GpsData; + +void gpsWakeUp(); +bool gpsTryReadData(GpsData* data); +void gpsShutDown(); + /// SD /// SD /// SD /// /// WIFI /// WIFI /// WIFI /// @@ -21,8 +36,25 @@ void setup() { Serial.begin(115200); ledSetup(); + gpsSetup(); ledShowString("running"); + + GpsData data; + gpsWakeUp(); + auto success = gpsTryReadData(&data); + gpsShutDown(); + + if (!success) + Serial.println("error!"); + else { + Serial.println(data.nofSatellites); + Serial.println(data.latitude); + Serial.println(data.longitude); + Serial.println(data.altitude); + Serial.println(data.date); + Serial.println(data.time); + } } ///////////////////////////////// @@ -84,6 +116,76 @@ void ledShowChar(const char c) { /// GPS /// GPS /// GPS /// ///////////////////////////////// +const auto gpsOnOffPin = A3; +const auto gpsSysOnPin = A2; +const uint32_t gpsBaud = 9600; + +SoftwareSerial gpsSerial(A1, A0); +TinyGPS gps; + +void gpsSetup() { + + gpsSerial.begin(gpsBaud); + + digitalWrite(gpsOnOffPin, LOW); + pinMode(gpsOnOffPin, OUTPUT); + pinMode(gpsSysOnPin, INPUT); + delay(100); +} + +void gpsDelay(const unsigned long ms) { + + const unsigned long start = millis(); + do { + while (gpsSerial.available()) + gps.encode(gpsSerial.read()); + } while (millis() - start < ms); +} + +void gpsWakeUp() { + + Serial.print("Waking up GPS module.."); + while (digitalRead(gpsSysOnPin) == LOW) { + Serial.print("."); + digitalWrite(gpsOnOffPin, HIGH); + delay(5); + digitalWrite(gpsOnOffPin, LOW); + delay(100); + } + Serial.println(" done."); + + Serial.print("Calibrating GPS module..."); + gpsDelay(20000); + Serial.println(" done."); +} + +bool gpsTryReadData(GpsData* data) { + + unsigned long age; + + data->nofSatellites = gps.satellites(); + gps.get_position(&data->latitude, &data->longitude, &age); + data->altitude = gps.altitude(); + gps.get_datetime(&data->date, &data->time, &age); + + return data->latitude != TinyGPS::GPS_INVALID_F_ANGLE + && data->altitude != TinyGPS::GPS_INVALID_ALTITUDE + && age != TinyGPS::GPS_INVALID_AGE; +} + +void gpsShutDown() { + + Serial.print("Shutting down GPS module.."); + while (digitalRead(gpsSysOnPin) == HIGH) { + Serial.print("."); + digitalWrite(gpsOnOffPin, LOW); + delay(5); + digitalWrite(gpsOnOffPin, HIGH); + delay(100); + } + Serial.println(" done."); +} + ///////////////////////////////// /// SD /// SD /// SD /// ///////////////////////////////// diff --git a/TinyDuino/TinyShield_GPS.ino b/TinyDuino/TinyShield_GPS.ino deleted file mode 100644 index c497f25..0000000 --- a/TinyDuino/TinyShield_GPS.ino +++ /dev/null @@ -1,160 +0,0 @@ -//------------------------------------------------------------------------------- -// TinyCircuits GPS TinyShield Display Example -// Last updated 7 April 2016 -// -// This example uses the TinyGPS library, originally written by Mikal Hart and -// currently forked and updated at https://github.com/florind/TinyGPS to support -// Glonass, slightly modified to work with the GPS TinyShield. -// -// Some GPS modules have been shipped with 4800 baud instead of 9600- try this if -// you see bad data. -// -// TinyCircuits, http://TinyCircuits.com -// -//------------------------------------------------------------------------------- - - -#include "TinyGPS.h" -#include - -const int GPS_ONOFFPin = A3; -const int GPS_SYSONPin = A2; -const int RXPin = A1, TXPin = A0; -const uint32_t GPSBaud = 9600; - -// The TinyGPS++ object -TinyGPS gps; - -// The serial connection to the GPS device -SoftwareSerial ss(RXPin, TXPin); - -void setup() -{ - Serial.begin(115200); - ss.begin(GPSBaud); - pinMode(GPS_SYSONPin, INPUT); - digitalWrite(GPS_ONOFFPin, LOW); - pinMode(GPS_ONOFFPin, OUTPUT); - delay(100); - Serial.print("Attempting to wake GPS module.. "); - while (digitalRead( GPS_SYSONPin ) == LOW ) - { - // Need to wake the module - digitalWrite( GPS_ONOFFPin, HIGH ); - delay(5); - digitalWrite( GPS_ONOFFPin, LOW ); - delay(100); - } - Serial.println("done."); - - Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version()); - Serial.println("by Mikal Hart"); - Serial.println(); - Serial.println("Sats HDOP Latitude Longitude Fix Date Time Date Alt Course Speed Card Distance Course Card Chars Sentences Checksum"); - Serial.println(" (deg) (deg) Age Age (m) --- from GPS ---- ---- to London ---- RX RX Fail"); - Serial.println("-------------------------------------------------------------------------------------------------------------------------------------"); -} - -void loop() -{ - float flat, flon; - unsigned long age, date, time, chars = 0; - unsigned short sentences = 0, failed = 0; - static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002; - - print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5); - print_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP, 5); - gps.f_get_position(&flat, &flon, &age); - print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 10, 6); - print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 11, 6); - print_int(age, TinyGPS::GPS_INVALID_AGE, 5); - print_date(gps); - print_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 7, 2); - print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2); - print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2); - print_str(gps.f_course() == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(gps.f_course()), 6); - print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0xFFFFFFFF : (unsigned long)TinyGPS::distance_between(flat, flon, LONDON_LAT, LONDON_LON) / 1000, 0xFFFFFFFF, 9); - print_float(flat == TinyGPS::GPS_INVALID_F_ANGLE ? TinyGPS::GPS_INVALID_F_ANGLE : TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2); - print_str(flat == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON)), 6); - - gps.stats(&chars, &sentences, &failed); - print_int(chars, 0xFFFFFFFF, 6); - print_int(sentences, 0xFFFFFFFF, 10); - print_int(failed, 0xFFFFFFFF, 9); - Serial.println(); - - smartdelay(1000); -} - -static void smartdelay(unsigned long ms) -{ - unsigned long start = millis(); - do - { - while (ss.available()) - gps.encode(ss.read()); - } while (millis() - start < ms); -} - -static void print_float(float val, float invalid, int len, int prec) -{ - if (val == invalid) - { - while (len-- > 1) - Serial.print('*'); - Serial.print(' '); - } - else - { - Serial.print(val, prec); - int vi = abs((int)val); - int flen = prec + (val < 0.0 ? 2 : 1); // . and - - flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1; - for (int i=flen; i 0) - sz[len-1] = ' '; - Serial.print(sz); - smartdelay(0); -} - -static void print_date(TinyGPS &gps) -{ - int year; - byte month, day, hour, minute, second, hundredths; - unsigned long age; - gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age); - if (age == TinyGPS::GPS_INVALID_AGE) - Serial.print("********** ******** "); - else - { - char sz[32]; - sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ", - month, day, year, hour, minute, second); - Serial.print(sz); - } - print_int(age, TinyGPS::GPS_INVALID_AGE, 5); - smartdelay(0); -} - -static void print_str(const char *str, int len) -{ - int slen = strlen(str); - for (int i=0; i Date: Sat, 29 Apr 2017 21:24:04 +0200 Subject: [PATCH 03/18] compress morse, add more GPS info --- TinyDuino/TinyDuino.ino | 191 +++++++++++++++++++++++++++------------- 1 file changed, 128 insertions(+), 63 deletions(-) diff --git a/TinyDuino/TinyDuino.ino b/TinyDuino/TinyDuino.ino index 611aa2d..04ef4cc 100644 --- a/TinyDuino/TinyDuino.ino +++ b/TinyDuino/TinyDuino.ino @@ -10,19 +10,25 @@ void ledShowChar(char c); /// GPS /// GPS /// GPS /// #define _GPS_NO_STATS +#define _GPS_MIN_INFO #include #include "TinyGPS.h" typedef struct { unsigned short nofSatellites; - long latitude, longitude, altitude; - unsigned long date, time; + float latitude, longitude; +#ifndef _GPS_MIN_INFO + float altitude, course, speedKmph, fromHome; +#endif + int year; + byte month, day, hour, minute, second; } GpsData; void gpsWakeUp(); -bool gpsTryReadData(GpsData* data); void gpsShutDown(); +void gpsDelay(const unsigned long ms); +bool gpsTryReadData(GpsData* data); /// SD /// SD /// SD /// @@ -48,12 +54,37 @@ void setup() { if (!success) Serial.println("error!"); else { - Serial.println(data.nofSatellites); - Serial.println(data.latitude); - Serial.println(data.longitude); + Serial.print("GPS result: # satellites: "); + Serial.print(data.nofSatellites); + Serial.print(" latitude: "); + Serial.print(data.latitude); + Serial.print(" longitude: "); + Serial.print(data.longitude); +#ifdef _GPS_MIN_INFO + Serial.println(); +#else + Serial.print(" altitude: "); Serial.println(data.altitude); - Serial.println(data.date); - Serial.println(data.time); + Serial.print("course: "); + Serial.print(data.course); + Serial.print(" speed (km/h): "); + Serial.print(data.speedKmph); + Serial.print(" from home (m): "); + Serial.println(data.fromHome); +#endif + Serial.print("date: "); + Serial.print(data.year); + Serial.print('-'); + Serial.print(data.month); + Serial.print('-'); + Serial.print(data.day); + Serial.print('T'); + Serial.print(data.hour); + Serial.print(':'); + Serial.print(data.minute); + Serial.print(':'); + Serial.print(data.second); + Serial.println('Z'); } } @@ -69,16 +100,15 @@ void loop() { /// LED /// LED /// LED /// ///////////////////////////////// -const int32_t morseSpeed = 100; -const int32_t charMin = 'a'; -const int32_t charMax = 'z'; -const int32_t morse[26][4] = { - /*A*/ { 1, 3 }, /*B*/ { 3, 1, 1, 1 }, /*C*/ { 3, 1, 3, 1 }, /*D*/ { 3, 1, 1 }, /*E*/ { 1 }, - /*F*/ { 1, 1, 3, 1 }, /*G*/ { 3, 3, 1 }, /*H*/ { 1, 1, 1, 1 }, /*I*/ { 1, 1 }, /*J*/ { 1, 3, 3, 3 }, - /*K*/ { 3, 1, 3 }, /*L*/ { 1, 3, 1, 1 }, /*M*/ { 3, 3 }, /*N*/ { 3, 1 }, /*O*/ { 3, 3, 3 }, - /*P*/ { 1, 3, 3, 1 }, /*Q*/ { 3, 3, 1, 3 }, /*R*/ { 1, 3, 1 }, /*S*/ { 1, 1, 1 }, /*T*/ { 3 }, - /*U*/ { 1, 1, 3 }, /*V*/ { 1, 1, 1, 3 }, /*W*/ { 1, 3, 3 }, /*X*/ { 3, 1, 1, 3 }, /*Y*/ { 3, 1, 3, 3 }, - /*Z*/ { 3, 3, 1, 1 } +#define MORSE_SPEED (100) +#define CHAR_FIRST ('a') +#define CHAR_LAST ('z') +const uint8_t morseCodes[] = { + /*A*/ (uint8_t)0x70, /*B*/ (uint8_t)0xD5, /*C*/ (uint8_t)0xDD, /*D*/ (uint8_t)0xD4, /*E*/ (uint8_t)0x40, /*F*/ (uint8_t)0x5D, + /*G*/ (uint8_t)0xF4, /*H*/ (uint8_t)0x55, /*I*/ (uint8_t)0x50, /*J*/ (uint8_t)0x7F, /*K*/ (uint8_t)0xDC, /*L*/ (uint8_t)0x75, + /*M*/ (uint8_t)0xF0, /*N*/ (uint8_t)0xD0, /*O*/ (uint8_t)0xFC, /*P*/ (uint8_t)0x7D, /*Q*/ (uint8_t)0xF7, /*R*/ (uint8_t)0x74, + /*S*/ (uint8_t)0x54, /*T*/ (uint8_t)0xC0, /*U*/ (uint8_t)0x5C, /*V*/ (uint8_t)0x57, /*W*/ (uint8_t)0x7C, /*X*/ (uint8_t)0xD7, + /*Y*/ (uint8_t)0xDF, /*Z*/ (uint8_t)0xF5 }; void ledSetup() { pinMode(LED_BUILTIN, OUTPUT); } @@ -86,13 +116,13 @@ void ledSetup() { pinMode(LED_BUILTIN, OUTPUT); } void ledOn() { digitalWrite(LED_BUILTIN, HIGH); } void ledOff() { digitalWrite(LED_BUILTIN, LOW); } -void ledShowString(const char *s) { +void ledShowString(const char* s) { for (auto i = 0; s[i] != 0; i++) { if (s[i] == ' ') - delay(morseSpeed * (7 - 3)); //exclude the next three units + delay(MORSE_SPEED * (7 - 3)); //exclude the next three units else { - if (i > 0) delay(morseSpeed * 3); + if (i > 0) delay(MORSE_SPEED * 3); ledShowChar(s[i]); } } @@ -100,14 +130,16 @@ void ledShowString(const char *s) { void ledShowChar(const char c) { - if (charMin > c || c > charMax) + if (CHAR_FIRST > c || c > CHAR_LAST) return; - auto m = morse[c - charMin]; - for (auto i = 0; i < 4 && m[i] != 0; i++) { - if (i > 0) delay(morseSpeed); + auto m = morseCodes[c - CHAR_FIRST]; + for (uint8_t i = 0, j = 0b11000000, k = 6; i < 4; i++, j >>= 2, k -= 2) { + auto n = (m & j) >> k; + if (n == 0) break; + if (i > 0) delay(MORSE_SPEED); ledOn(); - delay(morseSpeed * m[i]); + delay(MORSE_SPEED * n); ledOff(); } } @@ -116,76 +148,109 @@ void ledShowChar(const char c) { /// GPS /// GPS /// GPS /// ///////////////////////////////// -const auto gpsOnOffPin = A3; -const auto gpsSysOnPin = A2; -const uint32_t gpsBaud = 9600; +#define GPS_TX_PIN (A0) +#define GPS_RX_PIN (A1) +#define GPS_SYS_ON_PIN (A2) +#define GPS_ON_OFF_PIN (A3) + +#define GPS_CALIBRATION_TIME (20000) -SoftwareSerial gpsSerial(A1, A0); +#define HOME_LATITUDE (47.16628) +#define HOME_LONGITUDE (7.63498) + +SoftwareSerial gpsSerial(GPS_RX_PIN, GPS_TX_PIN); TinyGPS gps; void gpsSetup() { - gpsSerial.begin(gpsBaud); + gpsSerial.begin(9600); - digitalWrite(gpsOnOffPin, LOW); - pinMode(gpsOnOffPin, OUTPUT); - pinMode(gpsSysOnPin, INPUT); + digitalWrite(GPS_ON_OFF_PIN, LOW); + pinMode(GPS_ON_OFF_PIN, OUTPUT); + pinMode(GPS_SYS_ON_PIN, INPUT); delay(100); } -void gpsDelay(const unsigned long ms) { - - const unsigned long start = millis(); - do { - while (gpsSerial.available()) - gps.encode(gpsSerial.read()); - } while (millis() - start < ms); -} - void gpsWakeUp() { Serial.print("Waking up GPS module.."); - while (digitalRead(gpsSysOnPin) == LOW) { + while (digitalRead(GPS_SYS_ON_PIN) == LOW) { Serial.print("."); - digitalWrite(gpsOnOffPin, HIGH); + digitalWrite(GPS_ON_OFF_PIN, HIGH); delay(5); - digitalWrite(gpsOnOffPin, LOW); + digitalWrite(GPS_ON_OFF_PIN, LOW); delay(100); } Serial.println(" done."); Serial.print("Calibrating GPS module..."); - gpsDelay(20000); + gpsDelay(GPS_CALIBRATION_TIME); Serial.println(" done."); -} - -bool gpsTryReadData(GpsData* data) { - - unsigned long age; - data->nofSatellites = gps.satellites(); - gps.get_position(&data->latitude, &data->longitude, &age); - data->altitude = gps.altitude(); - gps.get_datetime(&data->date, &data->time, &age); - - return data->latitude != TinyGPS::GPS_INVALID_F_ANGLE - && data->altitude != TinyGPS::GPS_INVALID_ALTITUDE - && age != TinyGPS::GPS_INVALID_AGE; +#ifndef _GPS_NO_STATS + unsigned long encodedChars; + unsigned short goodSentences, failedChecksums; + gps.stats(&encodedChars, &goodSentences, &failedChecksums); + + Serial.print("GPS statistics: chars: "); + Serial.print(encodedChars); + Serial.print(", good sentences: "); + Serial.print(goodSentences); + Serial.print(", failed checksums: "); + Serial.print(failedChecksums); + Serial.println('.'); +#endif } void gpsShutDown() { Serial.print("Shutting down GPS module.."); - while (digitalRead(gpsSysOnPin) == HIGH) { + while (digitalRead(GPS_SYS_ON_PIN) == HIGH) { Serial.print("."); - digitalWrite(gpsOnOffPin, LOW); + digitalWrite(GPS_ON_OFF_PIN, LOW); delay(5); - digitalWrite(gpsOnOffPin, HIGH); + digitalWrite(GPS_ON_OFF_PIN, HIGH); delay(100); } Serial.println(" done."); } +void gpsDelay(const unsigned long ms) { + + const unsigned long start = millis(); + do { + while (gpsSerial.available()) + gps.encode(gpsSerial.read()); + } while (millis() - start < ms); +} + +bool gpsTryReadData(GpsData* data) { + + byte hundreth; + unsigned long age1, age2; + + data->nofSatellites = gps.satellites(); + gps.f_get_position(&data->latitude, &data->longitude, &age1); +#ifndef _GPS_MIN_INFO + data->altitude = gps.f_altitude(); + data->course = gps.f_course(); + data->speedKmph = gps.f_speed_kmph(); + data->fromHome = gps.distance_between(data->latitude, data->longitude, HOME_LATITUDE, HOME_LONGITUDE); +#endif + gps.crack_datetime(&data->year, &data->month, &data->day, &data->hour, &data->minute, &data->second, &hundreth, &age2); + + return data->nofSatellites != TinyGPS::GPS_INVALID_SATELLITES + && data->latitude != TinyGPS::GPS_INVALID_F_ANGLE + && data->longitude != TinyGPS::GPS_INVALID_F_ANGLE +#ifndef _GPS_MIN_INFO + && data->altitude != TinyGPS::GPS_INVALID_F_ALTITUDE + && data->course != TinyGPS::GPS_INVALID_F_ANGLE + && data->speedKmph != TinyGPS::GPS_INVALID_F_SPEED +#endif + && age1 != TinyGPS::GPS_INVALID_AGE + && age2 != TinyGPS::GPS_INVALID_AGE; +} + ///////////////////////////////// /// SD /// SD /// SD /// ///////////////////////////////// From f046bb53b09929b2eeb3f6050731a5700264ec94 Mon Sep 17 00:00:00 2001 From: Thomas Reto Strub Date: Sat, 29 Apr 2017 23:29:40 +0200 Subject: [PATCH 04/18] add SD functionality, remove LED (conflict with SD) --- TinyDuino/TinyDuino.ino | 191 +++++++++++++++++++++++++++------------- 1 file changed, 131 insertions(+), 60 deletions(-) diff --git a/TinyDuino/TinyDuino.ino b/TinyDuino/TinyDuino.ino index 04ef4cc..5aec331 100644 --- a/TinyDuino/TinyDuino.ino +++ b/TinyDuino/TinyDuino.ino @@ -1,20 +1,14 @@ -/// LED /// LED /// LED /// +/*/ LED /// LED /// LED /// void ledSetup(); void ledOn(); void ledOff(); -void ledShowString(char* s); -void ledShowChar(char c); +void ledShowString(const char* s); +void ledShowChar(const char c);*/ /// GPS /// GPS /// GPS /// -#define _GPS_NO_STATS -#define _GPS_MIN_INFO - -#include -#include "TinyGPS.h" - typedef struct { unsigned short nofSatellites; float latitude, longitude; @@ -25,67 +19,52 @@ typedef struct { byte month, day, hour, minute, second; } GpsData; +void gpsSetup(); void gpsWakeUp(); void gpsShutDown(); void gpsDelay(const unsigned long ms); -bool gpsTryReadData(GpsData* data); +bool gpsTryReadData(const GpsData* data); +void gpsPrintData(const GpsData* data); /// SD /// SD /// SD /// +bool sdSetup(); +bool sdWriteData(const char* s); + /// WIFI /// WIFI /// WIFI /// ///////////////////////////////// /// SETUP /// SETUP /// SETUP /// ///////////////////////////////// +bool error = false; + void setup() { Serial.begin(115200); - ledSetup(); + //ledSetup(); gpsSetup(); - - ledShowString("running"); + if ((error = !sdSetup())) { + Serial.println("Could not setup SD card!"); + return; + } GpsData data; gpsWakeUp(); - auto success = gpsTryReadData(&data); + error = !gpsTryReadData(&data); gpsShutDown(); + if (error) { + Serial.println("Could not read valid GPS data!"); + return; + } - if (!success) - Serial.println("error!"); - else { - Serial.print("GPS result: # satellites: "); - Serial.print(data.nofSatellites); - Serial.print(" latitude: "); - Serial.print(data.latitude); - Serial.print(" longitude: "); - Serial.print(data.longitude); -#ifdef _GPS_MIN_INFO - Serial.println(); -#else - Serial.print(" altitude: "); - Serial.println(data.altitude); - Serial.print("course: "); - Serial.print(data.course); - Serial.print(" speed (km/h): "); - Serial.print(data.speedKmph); - Serial.print(" from home (m): "); - Serial.println(data.fromHome); -#endif - Serial.print("date: "); - Serial.print(data.year); - Serial.print('-'); - Serial.print(data.month); - Serial.print('-'); - Serial.print(data.day); - Serial.print('T'); - Serial.print(data.hour); - Serial.print(':'); - Serial.print(data.minute); - Serial.print(':'); - Serial.print(data.second); - Serial.println('Z'); + gpsPrintData(&data); + Serial.println("GPS printed."); + if ((error = !sdWriteData(&data))) { + Serial.println("Could not write GPS data!"); + return; } + Serial.println("GPS written."); } ///////////////////////////////// @@ -94,9 +73,11 @@ void setup() { void loop() { + if (error) + return; } -///////////////////////////////// +/*/////////////////////////////// /// LED /// LED /// LED /// ///////////////////////////////// @@ -104,16 +85,14 @@ void loop() { #define CHAR_FIRST ('a') #define CHAR_LAST ('z') const uint8_t morseCodes[] = { - /*A*/ (uint8_t)0x70, /*B*/ (uint8_t)0xD5, /*C*/ (uint8_t)0xDD, /*D*/ (uint8_t)0xD4, /*E*/ (uint8_t)0x40, /*F*/ (uint8_t)0x5D, - /*G*/ (uint8_t)0xF4, /*H*/ (uint8_t)0x55, /*I*/ (uint8_t)0x50, /*J*/ (uint8_t)0x7F, /*K*/ (uint8_t)0xDC, /*L*/ (uint8_t)0x75, - /*M*/ (uint8_t)0xF0, /*N*/ (uint8_t)0xD0, /*O*/ (uint8_t)0xFC, /*P*/ (uint8_t)0x7D, /*Q*/ (uint8_t)0xF7, /*R*/ (uint8_t)0x74, - /*S*/ (uint8_t)0x54, /*T*/ (uint8_t)0xC0, /*U*/ (uint8_t)0x5C, /*V*/ (uint8_t)0x57, /*W*/ (uint8_t)0x7C, /*X*/ (uint8_t)0xD7, - /*Y*/ (uint8_t)0xDF, /*Z*/ (uint8_t)0xF5 + (uint8_t)0x70, (uint8_t)0xD5, (uint8_t)0xDD, (uint8_t)0xD4, (uint8_t)0x40, (uint8_t)0x5D, (uint8_t)0xF4, (uint8_t)0x55, (uint8_t)0x50, (uint8_t)0x7F, + (uint8_t)0xDC, (uint8_t)0x75, (uint8_t)0xF0, (uint8_t)0xD0, (uint8_t)0xFC, (uint8_t)0x7D, (uint8_t)0xF7, (uint8_t)0x74, (uint8_t)0x54, (uint8_t)0xC0, + (uint8_t)0x5C, (uint8_t)0x57, (uint8_t)0x7C, (uint8_t)0xD7, (uint8_t)0xDF, (uint8_t)0xF5 }; void ledSetup() { pinMode(LED_BUILTIN, OUTPUT); } -void ledOn() { digitalWrite(LED_BUILTIN, HIGH); } +void ledOn() { digitalWrite(LED_BUILTIN, HIGH); } void ledOff() { digitalWrite(LED_BUILTIN, LOW); } void ledShowString(const char* s) { @@ -133,7 +112,7 @@ void ledShowChar(const char c) { if (CHAR_FIRST > c || c > CHAR_LAST) return; - auto m = morseCodes[c - CHAR_FIRST]; + const auto m = morseCodes[c - CHAR_FIRST]; for (uint8_t i = 0, j = 0b11000000, k = 6; i < 4; i++, j >>= 2, k -= 2) { auto n = (m & j) >> k; if (n == 0) break; @@ -142,22 +121,28 @@ void ledShowChar(const char c) { delay(MORSE_SPEED * n); ledOff(); } -} +}*/ ///////////////////////////////// /// GPS /// GPS /// GPS /// ///////////////////////////////// +#define _GPS_NO_STATS +#define _GPS_MIN_INFO + #define GPS_TX_PIN (A0) #define GPS_RX_PIN (A1) #define GPS_SYS_ON_PIN (A2) #define GPS_ON_OFF_PIN (A3) -#define GPS_CALIBRATION_TIME (20000) +#define GPS_CALIBRATION_TIME (15000) #define HOME_LATITUDE (47.16628) #define HOME_LONGITUDE (7.63498) +#include +#include "TinyGPS.h" + SoftwareSerial gpsSerial(GPS_RX_PIN, GPS_TX_PIN); TinyGPS gps; @@ -217,14 +202,14 @@ void gpsShutDown() { void gpsDelay(const unsigned long ms) { - const unsigned long start = millis(); + const auto start = millis(); do { while (gpsSerial.available()) gps.encode(gpsSerial.read()); } while (millis() - start < ms); } -bool gpsTryReadData(GpsData* data) { +bool gpsTryReadData(GpsData* const data) { byte hundreth; unsigned long age1, age2; @@ -251,10 +236,96 @@ bool gpsTryReadData(GpsData* data) { && age2 != TinyGPS::GPS_INVALID_AGE; } +void gpsPrintData(const GpsData* const data) { + + Serial.print("GPS result: # satellites: "); + Serial.print(data->nofSatellites); + Serial.print(" latitude: "); + Serial.print(data->latitude); + Serial.print(" longitude: "); + Serial.print(data->longitude); +#ifdef _GPS_MIN_INFO + Serial.println(); +#else + Serial.print(" altitude: "); + Serial.println(data->altitude); + Serial.print("course: "); + Serial.print(data->course); + Serial.print(" speed (km/h): "); + Serial.print(data->speedKmph); + Serial.print(" from home (m): "); + Serial.println(data->fromHome); +#endif + Serial.print("date: "); + Serial.print(data->year); + Serial.print('-'); + Serial.print(data->month); + Serial.print('-'); + Serial.print(data->day); + Serial.print('T'); + Serial.print(data->hour); + Serial.print(':'); + Serial.print(data->minute); + Serial.print(':'); + Serial.print(data->second); + Serial.println('Z'); +} + ///////////////////////////////// /// SD /// SD /// SD /// ///////////////////////////////// +#define SD_PIN (10) +#define SD_FILE ("gps.csv") + +#include + +bool sdSetup() { + + pinMode(SD_PIN, OUTPUT); + return SD.begin(SD_PIN); +} + +bool sdWriteData(const GpsData* const data) { + + const auto init = SD.exists(SD_FILE); + auto file = SD.open(SD_FILE, FILE_WRITE); + if (file); + else + return false; + + if (!init) { + file.print("# Satellites,Latitude,Longitude"); +#ifndef _GPS_MIN_INFO + file.print(",Altitude,Course,Speed (km/h),From Home (m)"); +#endif + file.println(",Date/Time"); + } + + char dateTime[20]; + sprintf(dateTime, "%02d-%02d-%02dT%02d:%02d:%02dZ", data->year, data->month, data->day, data->hour, data->minute, data->second); + + file.print(data->nofSatellites); + file.print(','); + file.print(data->latitude); + file.print(','); + file.print(data->longitude); + file.print(','); +#ifndef _GPS_MIN_INFO + file.print(data->altitude); + file.print(','); + file.print(data->course); + file.print(','); + file.print(data->speedKmph); + file.print(','); + file.print(data->fromHome); + file.print(','); +#endif + file.println(dateTime); + file.close(); + return true; +} + ///////////////////////////////// /// WIFI /// WIFI /// WIFI /// ///////////////////////////////// From 5baa6e2f2b8d12f77ff31b4437b71b4cbf8f4bac Mon Sep 17 00:00:00 2001 From: Thomas Reto Strub Date: Thu, 4 May 2017 17:48:33 +0200 Subject: [PATCH 05/18] Update .gitignore GitHub Template 'Visual Studio' --- .gitignore | 254 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 254 insertions(+) diff --git a/.gitignore b/.gitignore index fcacd6e..e717e33 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,255 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +### CUSTOM ### _resources From 174b1475ecb506b6b624a7cabca9a6a5d273cde3 Mon Sep 17 00:00:00 2001 From: Thomas Reto Strub Date: Tue, 9 May 2017 23:07:39 +0200 Subject: [PATCH 06/18] create visual studio project --- .gitignore | 1 + TinyDuino.sln | 22 ++ TinyDuino/TinyDuino.ino | 420 ++++++++++++++-------------- TinyDuino/TinyDuino.vcxproj | 107 +++++++ TinyDuino/TinyDuino.vcxproj.filters | 33 +++ 5 files changed, 376 insertions(+), 207 deletions(-) create mode 100644 TinyDuino.sln create mode 100644 TinyDuino/TinyDuino.vcxproj create mode 100644 TinyDuino/TinyDuino.vcxproj.filters diff --git a/.gitignore b/.gitignore index e717e33..06e5ac9 100644 --- a/.gitignore +++ b/.gitignore @@ -253,3 +253,4 @@ paket-files/ ### CUSTOM ### _resources +__vm diff --git a/TinyDuino.sln b/TinyDuino.sln new file mode 100644 index 0000000..9b2b902 --- /dev/null +++ b/TinyDuino.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26403.7 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TinyDuino", "TinyDuino\TinyDuino.vcxproj", "{C5F80730-F44F-4478-BDAE-6634EFC2CA88}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C5F80730-F44F-4478-BDAE-6634EFC2CA88}.Debug|x86.ActiveCfg = Debug|Win32 + {C5F80730-F44F-4478-BDAE-6634EFC2CA88}.Debug|x86.Build.0 = Debug|Win32 + {C5F80730-F44F-4478-BDAE-6634EFC2CA88}.Release|x86.ActiveCfg = Release|Win32 + {C5F80730-F44F-4478-BDAE-6634EFC2CA88}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/TinyDuino/TinyDuino.ino b/TinyDuino/TinyDuino.ino index 5aec331..6991def 100644 --- a/TinyDuino/TinyDuino.ino +++ b/TinyDuino/TinyDuino.ino @@ -1,93 +1,106 @@ -/*/ LED /// LED /// LED /// +/** LED *** LED *** LED **/ + +#define _LED_DISABLED + +#ifndef _LED_DISABLED void ledSetup(); void ledOn(); void ledOff(); void ledShowString(const char* s); -void ledShowChar(const char c);*/ +void ledShowChar(const char c); +#endif + +/** GPS *** GPS *** GPS **/ -/// GPS /// GPS /// GPS /// +//#define _GPS_NO_STATS +#define _GPS_MIN_INFO typedef struct { - unsigned short nofSatellites; - float latitude, longitude; #ifndef _GPS_MIN_INFO - float altitude, course, speedKmph, fromHome; + unsigned short nofSatellites; +#endif + float latitude, longitude; +#ifndef _GPS_MIN_INFO + float altitude, course, speedKmph, fromHomeM; #endif - int year; - byte month, day, hour, minute, second; + int year; + byte month, day, hour, minute, second; } GpsData; void gpsSetup(); void gpsWakeUp(); void gpsShutDown(); void gpsDelay(const unsigned long ms); -bool gpsTryReadData(const GpsData* data); -void gpsPrintData(const GpsData* data); +bool gpsTryReadData(GpsData* const data); +void gpsPrintData(const GpsData* const data); -/// SD /// SD /// SD /// +/** SD *** SD *** SD **/ bool sdSetup(); -bool sdWriteData(const char* s); +bool sdWriteData(const GpsData* const data); -/// WIFI /// WIFI /// WIFI /// +/** WIFI *** WIFI *** WIFI **/ -///////////////////////////////// -/// SETUP /// SETUP /// SETUP /// -///////////////////////////////// +/******************************** +*** SETUP *** SETUP *** SETUP *** +********************************/ bool error = false; void setup() { - Serial.begin(115200); - //ledSetup(); - gpsSetup(); - if ((error = !sdSetup())) { - Serial.println("Could not setup SD card!"); - return; - } - - GpsData data; - gpsWakeUp(); - error = !gpsTryReadData(&data); - gpsShutDown(); - if (error) { - Serial.println("Could not read valid GPS data!"); - return; - } - - gpsPrintData(&data); - Serial.println("GPS printed."); - if ((error = !sdWriteData(&data))) { - Serial.println("Could not write GPS data!"); - return; - } - Serial.println("GPS written."); + Serial.begin(115200); +#ifndef _LED_DISABLED + ledSetup(); +#endif + gpsSetup(); + if ((error = !sdSetup())) { + Serial.println("Could not setup SD card!"); + return; + } } -///////////////////////////////// -/// LOOP /// LOOP /// LOOP /// -///////////////////////////////// +/******************************** +*** LOOP *** LOOP *** LOOP *** +********************************/ void loop() { - if (error) - return; + if (error) + return; + + GpsData data; + gpsWakeUp(); + //gpsShutDown(); + if (!gpsTryReadData(&data)) { + Serial.println("Could not read valid GPS data!"); + return; + } + + gpsPrintData(&data); + Serial.println("GPS printed."); + if ((error = !sdWriteData(&data))) { + Serial.println("Could not write GPS data!"); + return; + } + Serial.println("GPS written."); } -/*/////////////////////////////// -/// LED /// LED /// LED /// -///////////////////////////////// +/******************************** +*** LED *** LED *** LED *** +********************************/ + +#ifndef _LED_DISABLED #define MORSE_SPEED (100) #define CHAR_FIRST ('a') #define CHAR_LAST ('z') const uint8_t morseCodes[] = { - (uint8_t)0x70, (uint8_t)0xD5, (uint8_t)0xDD, (uint8_t)0xD4, (uint8_t)0x40, (uint8_t)0x5D, (uint8_t)0xF4, (uint8_t)0x55, (uint8_t)0x50, (uint8_t)0x7F, - (uint8_t)0xDC, (uint8_t)0x75, (uint8_t)0xF0, (uint8_t)0xD0, (uint8_t)0xFC, (uint8_t)0x7D, (uint8_t)0xF7, (uint8_t)0x74, (uint8_t)0x54, (uint8_t)0xC0, - (uint8_t)0x5C, (uint8_t)0x57, (uint8_t)0x7C, (uint8_t)0xD7, (uint8_t)0xDF, (uint8_t)0xF5 + (uint8_t)0x70, (uint8_t)0xD5, (uint8_t)0xDD, (uint8_t)0xD4, (uint8_t)0x40, (uint8_t)0x5D, (uint8_t)0xF4, (uint8_t)0x55, (uint8_t)0x50, (uint8_t)0x7F, + (uint8_t)0xDC, (uint8_t)0x75, (uint8_t)0xF0, (uint8_t)0xD0, (uint8_t)0xFC, (uint8_t)0x7D, (uint8_t)0xF7, (uint8_t)0x74, (uint8_t)0x54, (uint8_t)0xC0, + (uint8_t)0x5C, (uint8_t)0x57, (uint8_t)0x7C, (uint8_t)0xD7, (uint8_t)0xDF, (uint8_t)0xF5 }; void ledSetup() { pinMode(LED_BUILTIN, OUTPUT); } @@ -97,38 +110,37 @@ void ledOff() { digitalWrite(LED_BUILTIN, LOW); } void ledShowString(const char* s) { - for (auto i = 0; s[i] != 0; i++) { - if (s[i] == ' ') - delay(MORSE_SPEED * (7 - 3)); //exclude the next three units - else { - if (i > 0) delay(MORSE_SPEED * 3); - ledShowChar(s[i]); - } - } + for (auto i = 0; s[i] != 0; i++) { + if (s[i] == ' ') + delay(MORSE_SPEED * (7 - 3)); //exclude the next three units + else { + if (i > 0) delay(MORSE_SPEED * 3); + ledShowChar(s[i]); + } + } } void ledShowChar(const char c) { - if (CHAR_FIRST > c || c > CHAR_LAST) - return; - - const auto m = morseCodes[c - CHAR_FIRST]; - for (uint8_t i = 0, j = 0b11000000, k = 6; i < 4; i++, j >>= 2, k -= 2) { - auto n = (m & j) >> k; - if (n == 0) break; - if (i > 0) delay(MORSE_SPEED); - ledOn(); - delay(MORSE_SPEED * n); - ledOff(); - } -}*/ - -///////////////////////////////// -/// GPS /// GPS /// GPS /// -///////////////////////////////// - -#define _GPS_NO_STATS -#define _GPS_MIN_INFO + if (CHAR_FIRST > c || c > CHAR_LAST) + return; + + const auto m = morseCodes[c - CHAR_FIRST]; + for (uint8_t i = 0, j = 0b11000000, k = 6; i < 4; i++, j >>= 2, k -= 2) { + auto n = (m & j) >> k; + if (n == 0) break; + if (i > 0) delay(MORSE_SPEED); + ledOn(); + delay(MORSE_SPEED * n); + ledOff(); + } +} + +#endif + +/******************************** +*** GPS *** GPS *** GPS *** +********************************/ #define GPS_TX_PIN (A0) #define GPS_RX_PIN (A1) @@ -136,6 +148,7 @@ void ledShowChar(const char c) { #define GPS_ON_OFF_PIN (A3) #define GPS_CALIBRATION_TIME (15000) +#define GPS_LAT_LONG_PRECISION (6) #define HOME_LATITUDE (47.16628) #define HOME_LONGITUDE (7.63498) @@ -148,132 +161,124 @@ TinyGPS gps; void gpsSetup() { - gpsSerial.begin(9600); + gpsSerial.begin(9600); - digitalWrite(GPS_ON_OFF_PIN, LOW); - pinMode(GPS_ON_OFF_PIN, OUTPUT); - pinMode(GPS_SYS_ON_PIN, INPUT); - delay(100); + digitalWrite(GPS_ON_OFF_PIN, LOW); + pinMode(GPS_ON_OFF_PIN, OUTPUT); + pinMode(GPS_SYS_ON_PIN, INPUT); + delay(100); } void gpsWakeUp() { - Serial.print("Waking up GPS module.."); - while (digitalRead(GPS_SYS_ON_PIN) == LOW) { - Serial.print("."); - digitalWrite(GPS_ON_OFF_PIN, HIGH); - delay(5); - digitalWrite(GPS_ON_OFF_PIN, LOW); - delay(100); - } - Serial.println(" done."); + Serial.print("Waking up GPS module.."); + while (digitalRead(GPS_SYS_ON_PIN) == LOW) { + Serial.print("."); + digitalWrite(GPS_ON_OFF_PIN, HIGH); + delay(5); + digitalWrite(GPS_ON_OFF_PIN, LOW); + delay(100); + } + Serial.println(" done."); - Serial.print("Calibrating GPS module..."); - gpsDelay(GPS_CALIBRATION_TIME); - Serial.println(" done."); + Serial.print("Calibrating GPS module..."); + gpsDelay(GPS_CALIBRATION_TIME); + Serial.println(" done."); #ifndef _GPS_NO_STATS - unsigned long encodedChars; - unsigned short goodSentences, failedChecksums; - gps.stats(&encodedChars, &goodSentences, &failedChecksums); - - Serial.print("GPS statistics: chars: "); - Serial.print(encodedChars); - Serial.print(", good sentences: "); - Serial.print(goodSentences); - Serial.print(", failed checksums: "); - Serial.print(failedChecksums); - Serial.println('.'); + unsigned long encodedChars; + unsigned short goodSentences, failedChecksums; + gps.stats(&encodedChars, &goodSentences, &failedChecksums); + + Serial.print("GPS statistics: chars: "); + Serial.print(encodedChars); + Serial.print(", good sentences: "); + Serial.print(goodSentences); + Serial.print(", failed checksums: "); + Serial.print(failedChecksums); + Serial.println('.'); #endif } void gpsShutDown() { - Serial.print("Shutting down GPS module.."); - while (digitalRead(GPS_SYS_ON_PIN) == HIGH) { - Serial.print("."); - digitalWrite(GPS_ON_OFF_PIN, LOW); - delay(5); - digitalWrite(GPS_ON_OFF_PIN, HIGH); - delay(100); - } - Serial.println(" done."); + Serial.print("Shutting down GPS module.."); + while (digitalRead(GPS_SYS_ON_PIN) == HIGH) { + Serial.print("."); + digitalWrite(GPS_ON_OFF_PIN, LOW); + delay(5); + digitalWrite(GPS_ON_OFF_PIN, HIGH); + delay(100); + } + Serial.println(" done."); } void gpsDelay(const unsigned long ms) { - const auto start = millis(); - do { - while (gpsSerial.available()) - gps.encode(gpsSerial.read()); - } while (millis() - start < ms); + const auto start = millis(); + do { + while (gpsSerial.available()) + gps.encode(gpsSerial.read()); + } while (millis() - start < ms); } bool gpsTryReadData(GpsData* const data) { - byte hundreth; - unsigned long age1, age2; + byte hundreth; + unsigned long age1, age2; - data->nofSatellites = gps.satellites(); - gps.f_get_position(&data->latitude, &data->longitude, &age1); + gps.f_get_position(&data->latitude, &data->longitude, &age1); + gps.crack_datetime(&data->year, &data->month, &data->day, &data->hour, &data->minute, &data->second, &hundreth, &age2); #ifndef _GPS_MIN_INFO - data->altitude = gps.f_altitude(); - data->course = gps.f_course(); - data->speedKmph = gps.f_speed_kmph(); - data->fromHome = gps.distance_between(data->latitude, data->longitude, HOME_LATITUDE, HOME_LONGITUDE); + data->nofSatellites = gps.satellites(); + data->altitude = gps.f_altitude(); + data->course = gps.f_course(); + data->speedKmph = gps.f_speed_kmph(); + data->fromHomeM = gps.distance_between(data->latitude, data->longitude, HOME_LATITUDE, HOME_LONGITUDE); #endif - gps.crack_datetime(&data->year, &data->month, &data->day, &data->hour, &data->minute, &data->second, &hundreth, &age2); - return data->nofSatellites != TinyGPS::GPS_INVALID_SATELLITES - && data->latitude != TinyGPS::GPS_INVALID_F_ANGLE - && data->longitude != TinyGPS::GPS_INVALID_F_ANGLE + return data->latitude != TinyGPS::GPS_INVALID_F_ANGLE + && data->longitude != TinyGPS::GPS_INVALID_F_ANGLE + && age1 != TinyGPS::GPS_INVALID_AGE + && age2 != TinyGPS::GPS_INVALID_AGE #ifndef _GPS_MIN_INFO - && data->altitude != TinyGPS::GPS_INVALID_F_ALTITUDE - && data->course != TinyGPS::GPS_INVALID_F_ANGLE - && data->speedKmph != TinyGPS::GPS_INVALID_F_SPEED + && data->nofSatellites != TinyGPS::GPS_INVALID_SATELLITES + && data->altitude != TinyGPS::GPS_INVALID_F_ALTITUDE + && data->course != TinyGPS::GPS_INVALID_F_ANGLE + && data->speedKmph != TinyGPS::GPS_INVALID_F_SPEED #endif - && age1 != TinyGPS::GPS_INVALID_AGE - && age2 != TinyGPS::GPS_INVALID_AGE; + ; } void gpsPrintData(const GpsData* const data) { - Serial.print("GPS result: # satellites: "); - Serial.print(data->nofSatellites); - Serial.print(" latitude: "); - Serial.print(data->latitude); - Serial.print(" longitude: "); - Serial.print(data->longitude); -#ifdef _GPS_MIN_INFO - Serial.println(); -#else - Serial.print(" altitude: "); - Serial.println(data->altitude); - Serial.print("course: "); - Serial.print(data->course); - Serial.print(" speed (km/h): "); - Serial.print(data->speedKmph); - Serial.print(" from home (m): "); - Serial.println(data->fromHome); + char dateTime[20]; + sprintf(dateTime, "%02d-%02d-%02dT%02d:%02d:%02dZ", data->year, data->month, data->day, data->hour, data->minute, data->second); + + Serial.print("Date/time: "); + Serial.print(dateTime); + Serial.print(" latitude: "); + Serial.print(data->latitude, GPS_LAT_LONG_PRECISION); + Serial.print(" longitude: "); + Serial.print(data->longitude, GPS_LAT_LONG_PRECISION); +#ifndef _GPS_MIN_INFO + Serial.print(" altitude: "); + Serial.print(data->altitude); + Serial.print(" course: "); + Serial.print(data->course); + Serial.print(" speed (km/h): "); + Serial.print(data->speedKmph); + Serial.print(" from home (m): "); + Serial.print(data->fromHomeM); + Serial.print(" # satellites: "); + Serial.print(data->nofSatellites); #endif - Serial.print("date: "); - Serial.print(data->year); - Serial.print('-'); - Serial.print(data->month); - Serial.print('-'); - Serial.print(data->day); - Serial.print('T'); - Serial.print(data->hour); - Serial.print(':'); - Serial.print(data->minute); - Serial.print(':'); - Serial.print(data->second); - Serial.println('Z'); + Serial.println(); } -///////////////////////////////// -/// SD /// SD /// SD /// -///////////////////////////////// +/******************************** +*** SD *** SD *** SD *** +********************************/ #define SD_PIN (10) #define SD_FILE ("gps.csv") @@ -282,51 +287,52 @@ void gpsPrintData(const GpsData* const data) { bool sdSetup() { - pinMode(SD_PIN, OUTPUT); - return SD.begin(SD_PIN); + pinMode(SD_PIN, OUTPUT); + return SD.begin(SD_PIN); } bool sdWriteData(const GpsData* const data) { - const auto init = SD.exists(SD_FILE); - auto file = SD.open(SD_FILE, FILE_WRITE); - if (file); - else - return false; + const auto init = SD.exists(SD_FILE); + auto file = SD.open(SD_FILE, FILE_WRITE); + if (file); + else + return false; - if (!init) { - file.print("# Satellites,Latitude,Longitude"); + if (!init) { + file.print("Date/Time,Latitude,Longitude"); #ifndef _GPS_MIN_INFO - file.print(",Altitude,Course,Speed (km/h),From Home (m)"); + file.print(",Altitude,Course,Speed (km/h),From Home (m),# Satellites"); #endif - file.println(",Date/Time"); - } - - char dateTime[20]; - sprintf(dateTime, "%02d-%02d-%02dT%02d:%02d:%02dZ", data->year, data->month, data->day, data->hour, data->minute, data->second); - - file.print(data->nofSatellites); - file.print(','); - file.print(data->latitude); - file.print(','); - file.print(data->longitude); - file.print(','); + file.println(); + } + + char dateTime[20]; + sprintf(dateTime, "%02d-%02d-%02dT%02d:%02d:%02dZ", data->year, data->month, data->day, data->hour, data->minute, data->second); + + file.print(dateTime); + file.print(','); + file.print(data->latitude, GPS_LAT_LONG_PRECISION); + file.print(','); + file.print(data->longitude, GPS_LAT_LONG_PRECISION); + file.print(','); #ifndef _GPS_MIN_INFO - file.print(data->altitude); - file.print(','); - file.print(data->course); - file.print(','); - file.print(data->speedKmph); - file.print(','); - file.print(data->fromHome); - file.print(','); + file.print(data->altitude); + file.print(','); + file.print(data->course); + file.print(','); + file.print(data->speedKmph); + file.print(','); + file.print(data->fromHomeM); + file.print(','); + file.print(data->nofSatellites); #endif - file.println(dateTime); - file.close(); - return true; + file.println(); + file.close(); + return true; } -///////////////////////////////// -/// WIFI /// WIFI /// WIFI /// -///////////////////////////////// +/******************************** +*** WIFI *** WIFI *** WIFI *** +********************************/ diff --git a/TinyDuino/TinyDuino.vcxproj b/TinyDuino/TinyDuino.vcxproj new file mode 100644 index 0000000..dab519b --- /dev/null +++ b/TinyDuino/TinyDuino.vcxproj @@ -0,0 +1,107 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {C5F80730-F44F-4478-BDAE-6634EFC2CA88} + + + TinyDuino + 10.0.15063.0 + + + + Application + true + MultiByte + + + + + Application + false + + + true + MultiByte + + + + + + + + + + + + + + + Level3 + Disabled + true + C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial\src;C:\Program Files (x86)\Arduino\libraries\SD\src;C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI\src;C:\Program Files (x86)\Arduino\libraries\SD\src\utility;C:\Program Files (x86)\Arduino\libraries;C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries;C:\Users\ThS\Documents\Arduino\libraries;C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino;C:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\eightanaloginputs;$(ProjectDir)..\TinyDuino;C:\Program Files (x86)\Arduino\hardware\tools\avr\avr\include\;C:\Program Files (x86)\Arduino\hardware\tools\avr\avr\include\avr\;C:\Program Files (x86)\Arduino\hardware\tools\avr\lib\gcc\avr\4.8.1\include;C:\Program Files (x86)\Arduino\hardware\tools\avr\lib\gcc\avr\4.9.2\include;C:\Program Files (x86)\Arduino\hardware\tools\avr\lib\gcc\avr\4.9.3\include;%(AdditionalIncludeDirectories) + $(ProjectDir)__vm\.TinyDuino.vsarduino.h;%(ForcedIncludeFiles) + false + __AVR_ATmega328p__;__AVR_ATmega328P__;_VMDEBUG=1;F_CPU=8000000L;ARDUINO=10802;ARDUINO_AVR_PRO;ARDUINO_ARCH_AVR;__cplusplus=201103L;_VMICRO_INTELLISENSE;%(PreprocessorDefinitions) + + + true + + + + + Level3 + Disabled + true + true + true + C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial\src;C:\Program Files (x86)\Arduino\libraries\SD\src;C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SPI\src;C:\Program Files (x86)\Arduino\libraries\SD\src\utility;C:\Program Files (x86)\Arduino\libraries;C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries;C:\Users\ThS\Documents\Arduino\libraries;C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino;C:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\eightanaloginputs;$(ProjectDir)..\TinyDuino;C:\Program Files (x86)\Arduino\hardware\tools\avr\avr\include\;C:\Program Files (x86)\Arduino\hardware\tools\avr\avr\include\avr\;C:\Program Files (x86)\Arduino\hardware\tools\avr\lib\gcc\avr\4.8.1\include;C:\Program Files (x86)\Arduino\hardware\tools\avr\lib\gcc\avr\4.9.2\include;C:\Program Files (x86)\Arduino\hardware\tools\avr\lib\gcc\avr\4.9.3\include;%(AdditionalIncludeDirectories) + $(ProjectDir)__vm\.TinyDuino.vsarduino.h;%(ForcedIncludeFiles) + false + __AVR_ATmega328p__;__AVR_ATmega328P__;F_CPU=8000000L;ARDUINO=10802;ARDUINO_AVR_PRO;ARDUINO_ARCH_AVR;__cplusplus=201103L;_VMICRO_INTELLISENSE;%(PreprocessorDefinitions) + + + true + true + true + + + + + + + + CppCode + + + + + + + + CppCode + + + + + VisualMicroDebugger + + + + + + + + + + \ No newline at end of file diff --git a/TinyDuino/TinyDuino.vcxproj.filters b/TinyDuino/TinyDuino.vcxproj.filters new file mode 100644 index 0000000..32040d3 --- /dev/null +++ b/TinyDuino/TinyDuino.vcxproj.filters @@ -0,0 +1,33 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + + + + Source Files + + + + + Header Files + + + Header Files + + + \ No newline at end of file From c2a93743778437e71dba411b13a50665dd3f4813 Mon Sep 17 00:00:00 2001 From: Thomas Reto Strub Date: Thu, 11 May 2017 17:50:06 +0200 Subject: [PATCH 07/18] minimise program code size --- TinyDuino/TinyDuino.ino | 145 ++++++++++++++++++++++++++++------------ 1 file changed, 104 insertions(+), 41 deletions(-) diff --git a/TinyDuino/TinyDuino.ino b/TinyDuino/TinyDuino.ino index 6991def..0863f4d 100644 --- a/TinyDuino/TinyDuino.ino +++ b/TinyDuino/TinyDuino.ino @@ -1,9 +1,11 @@ +#define DEBUG_DISABLE + /** LED *** LED *** LED **/ -#define _LED_DISABLED +#define LED_DISABLED -#ifndef _LED_DISABLED +#ifndef LED_DISABLED void ledSetup(); void ledOn(); @@ -14,19 +16,26 @@ void ledShowChar(const char c); /** GPS *** GPS *** GPS **/ -//#define _GPS_NO_STATS -#define _GPS_MIN_INFO +//#define GPS_FLOAT +#define GPS_NO_STATS +#define GPS_MIN_INFO -typedef struct { -#ifndef _GPS_MIN_INFO - unsigned short nofSatellites; -#endif - float latitude, longitude; -#ifndef _GPS_MIN_INFO - float altitude, course, speedKmph, fromHomeM; +#ifdef GPS_FLOAT +typedef float GPS_LAT_LONG_ALT_TYPE, GPS_COURSE_SPEED_TYPE; +#else +typedef long GPS_LAT_LONG_ALT_TYPE; +typedef unsigned long GPS_COURSE_SPEED_TYPE; #endif + +typedef struct { int year; byte month, day, hour, minute, second; + unsigned short nofSatellites; + GPS_LAT_LONG_ALT_TYPE latitude, longitude; +#ifndef GPS_MIN_INFO + GPS_LAT_LONG_ALT_TYPE altitude, fromHomeM; + GPS_COURSE_SPEED_TYPE course, speed; +#endif } GpsData; void gpsSetup(); @@ -34,7 +43,9 @@ void gpsWakeUp(); void gpsShutDown(); void gpsDelay(const unsigned long ms); bool gpsTryReadData(GpsData* const data); +#ifndef DEBUG_DISABLE void gpsPrintData(const GpsData* const data); +#endif /** SD *** SD *** SD **/ @@ -51,13 +62,17 @@ bool error = false; void setup() { +#ifndef DEBUG_DISABLE Serial.begin(115200); -#ifndef _LED_DISABLED +#endif +#ifndef LED_DISABLED ledSetup(); #endif gpsSetup(); if ((error = !sdSetup())) { +#ifndef DEBUG_DISABLE Serial.println("Could not setup SD card!"); +#endif return; } } @@ -75,24 +90,32 @@ void loop() { gpsWakeUp(); //gpsShutDown(); if (!gpsTryReadData(&data)) { +#ifndef DEBUG_DISABLE Serial.println("Could not read valid GPS data!"); +#endif return; } +#ifndef DEBUG_DISABLE gpsPrintData(&data); Serial.println("GPS printed."); +#endif if ((error = !sdWriteData(&data))) { +#ifndef DEBUG_DISABLE Serial.println("Could not write GPS data!"); +#endif return; } +#ifndef DEBUG_DISABLE Serial.println("GPS written."); +#endif } /******************************** *** LED *** LED *** LED *** ********************************/ -#ifndef _LED_DISABLED +#ifndef LED_DISABLED #define MORSE_SPEED (100) #define CHAR_FIRST ('a') @@ -148,7 +171,20 @@ void ledShowChar(const char c) { #define GPS_ON_OFF_PIN (A3) #define GPS_CALIBRATION_TIME (15000) + +#ifdef GPS_FLOAT #define GPS_LAT_LONG_PRECISION (6) +#define GPS_LAT_LONG_FUNCTION f_get_position +#define GPS_ALT_FUNCTION f_altitude +#define GPS_COURSE_FUNCTION f_course +#define GPS_SPEED_FUNCTION f_speed_kmph +#else +#define GPS_LAT_LONG_PRECISION (10) +#define GPS_LAT_LONG_FUNCTION get_position +#define GPS_ALT_FUNCTION altitude +#define GPS_COURSE_FUNCTION course +#define GPS_SPEED_FUNCTION speed +#endif #define HOME_LATITUDE (47.16628) #define HOME_LONGITUDE (7.63498) @@ -171,21 +207,28 @@ void gpsSetup() { void gpsWakeUp() { +#ifndef DEBUG_DISABLE Serial.print("Waking up GPS module.."); +#endif while (digitalRead(GPS_SYS_ON_PIN) == LOW) { +#ifndef DEBUG_DISABLE Serial.print("."); +#endif digitalWrite(GPS_ON_OFF_PIN, HIGH); delay(5); digitalWrite(GPS_ON_OFF_PIN, LOW); delay(100); } +#ifndef DEBUG_DISABLE Serial.println(" done."); Serial.print("Calibrating GPS module..."); +#endif gpsDelay(GPS_CALIBRATION_TIME); +#ifndef DEBUG_DISABLE Serial.println(" done."); -#ifndef _GPS_NO_STATS +#ifndef GPS_NO_STATS unsigned long encodedChars; unsigned short goodSentences, failedChecksums; gps.stats(&encodedChars, &goodSentences, &failedChecksums); @@ -198,19 +241,26 @@ void gpsWakeUp() { Serial.print(failedChecksums); Serial.println('.'); #endif +#endif } void gpsShutDown() { +#ifndef DEBUG_DISABLE Serial.print("Shutting down GPS module.."); +#endif while (digitalRead(GPS_SYS_ON_PIN) == HIGH) { +#ifndef DEBUG_DISABLE Serial.print("."); +#endif digitalWrite(GPS_ON_OFF_PIN, LOW); delay(5); digitalWrite(GPS_ON_OFF_PIN, HIGH); delay(100); } +#ifndef DEBUG_DISABLE Serial.println(" done."); +#endif } void gpsDelay(const unsigned long ms) { @@ -227,29 +277,34 @@ bool gpsTryReadData(GpsData* const data) { byte hundreth; unsigned long age1, age2; - gps.f_get_position(&data->latitude, &data->longitude, &age1); - gps.crack_datetime(&data->year, &data->month, &data->day, &data->hour, &data->minute, &data->second, &hundreth, &age2); -#ifndef _GPS_MIN_INFO + gps.crack_datetime(&data->year, &data->month, &data->day, &data->hour, &data->minute, &data->second, &hundreth, &age1); data->nofSatellites = gps.satellites(); - data->altitude = gps.f_altitude(); - data->course = gps.f_course(); - data->speedKmph = gps.f_speed_kmph(); + gps.GPS_LAT_LONG_FUNCTION(&data->latitude, &data->longitude, &age2); + +#ifndef GPS_MIN_INFO + data->altitude = gps.GPS_ALT_FUNCTION(); + data->course = gps.GPS_COURSE_FUNCTION(); + data->speed = gps.GPS_SPEED_FUNCTION(); + +#ifdef GPS_FLOAT data->fromHomeM = gps.distance_between(data->latitude, data->longitude, HOME_LATITUDE, HOME_LONGITUDE); +#endif #endif - return data->latitude != TinyGPS::GPS_INVALID_F_ANGLE + return age1 != TinyGPS::GPS_INVALID_AGE + && data->nofSatellites != TinyGPS::GPS_INVALID_SATELLITES + && data->latitude != TinyGPS::GPS_INVALID_F_ANGLE && data->longitude != TinyGPS::GPS_INVALID_F_ANGLE - && age1 != TinyGPS::GPS_INVALID_AGE && age2 != TinyGPS::GPS_INVALID_AGE -#ifndef _GPS_MIN_INFO - && data->nofSatellites != TinyGPS::GPS_INVALID_SATELLITES +#ifndef GPS_MIN_INFO && data->altitude != TinyGPS::GPS_INVALID_F_ALTITUDE && data->course != TinyGPS::GPS_INVALID_F_ANGLE - && data->speedKmph != TinyGPS::GPS_INVALID_F_SPEED + && data->speed != TinyGPS::GPS_INVALID_F_SPEED #endif ; } +#ifndef DEBUG_DISABLE void gpsPrintData(const GpsData* const data) { char dateTime[20]; @@ -257,24 +312,30 @@ void gpsPrintData(const GpsData* const data) { Serial.print("Date/time: "); Serial.print(dateTime); + Serial.print(" # satellites: "); + Serial.print(data->nofSatellites); Serial.print(" latitude: "); Serial.print(data->latitude, GPS_LAT_LONG_PRECISION); Serial.print(" longitude: "); Serial.print(data->longitude, GPS_LAT_LONG_PRECISION); -#ifndef _GPS_MIN_INFO +#ifndef GPS_MIN_INFO Serial.print(" altitude: "); Serial.print(data->altitude); Serial.print(" course: "); Serial.print(data->course); +#ifndef GPS_FLOAT + Serial.print(" speed (kt/100): "); + Serial.print(data->speed); +#else Serial.print(" speed (km/h): "); - Serial.print(data->speedKmph); + Serial.print(data->speed); Serial.print(" from home (m): "); Serial.print(data->fromHomeM); - Serial.print(" # satellites: "); - Serial.print(data->nofSatellites); +#endif #endif Serial.println(); } +#endif /******************************** *** SD *** SD *** SD *** @@ -299,33 +360,36 @@ bool sdWriteData(const GpsData* const data) { else return false; - if (!init) { - file.print("Date/Time,Latitude,Longitude"); -#ifndef _GPS_MIN_INFO - file.print(",Altitude,Course,Speed (km/h),From Home (m),# Satellites"); -#endif - file.println(); - } + if (!init) + file.println("Date/Time,# Satellites,Latitude,Longitude,Altitude,Course,Speed (kt/100),Speed (km/h),From Home (m)"); char dateTime[20]; sprintf(dateTime, "%02d-%02d-%02dT%02d:%02d:%02dZ", data->year, data->month, data->day, data->hour, data->minute, data->second); file.print(dateTime); file.print(','); + file.print(data->nofSatellites); + file.print(','); file.print(data->latitude, GPS_LAT_LONG_PRECISION); file.print(','); file.print(data->longitude, GPS_LAT_LONG_PRECISION); file.print(','); -#ifndef _GPS_MIN_INFO +#ifndef GPS_MIN_INFO file.print(data->altitude); file.print(','); file.print(data->course); +#ifdef GPS_FLOAT file.print(','); - file.print(data->speedKmph); +#endif file.print(','); - file.print(data->fromHomeM); + file.print(data->speed); +#ifndef GPS_FLOAT file.print(','); - file.print(data->nofSatellites); +#endif +#ifdef GPS_FLOAT + file.print(','); + file.print(data->fromHomeM); +#endif #endif file.println(); file.close(); @@ -335,4 +399,3 @@ bool sdWriteData(const GpsData* const data) { /******************************** *** WIFI *** WIFI *** WIFI *** ********************************/ - From 428ea48a0b62ca0d1b832734a22a6076fe105eef Mon Sep 17 00:00:00 2001 From: Thomas Reto Strub Date: Thu, 11 May 2017 17:54:41 +0200 Subject: [PATCH 08/18] update lat/long to match precision --- TinyDuino/TinyDuino.ino | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/TinyDuino/TinyDuino.ino b/TinyDuino/TinyDuino.ino index 0863f4d..9622f39 100644 --- a/TinyDuino/TinyDuino.ino +++ b/TinyDuino/TinyDuino.ino @@ -6,7 +6,6 @@ #define LED_DISABLED #ifndef LED_DISABLED - void ledSetup(); void ledOn(); void ledOff(); @@ -186,8 +185,8 @@ void ledShowChar(const char c) { #define GPS_SPEED_FUNCTION speed #endif -#define HOME_LATITUDE (47.16628) -#define HOME_LONGITUDE (7.63498) +#define HOME_LATITUDE (47.166320) +#define HOME_LONGITUDE (7.635007) #include #include "TinyGPS.h" From 159f200a7dd2141979ad14934c86518412800d5d Mon Sep 17 00:00:00 2001 From: Thomas Reto Strub Date: Thu, 11 May 2017 18:01:56 +0200 Subject: [PATCH 09/18] re-add morse messages (disabled) --- TinyDuino/TinyDuino.ino | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/TinyDuino/TinyDuino.ino b/TinyDuino/TinyDuino.ino index 9622f39..9e679cb 100644 --- a/TinyDuino/TinyDuino.ino +++ b/TinyDuino/TinyDuino.ino @@ -74,6 +74,10 @@ void setup() { #endif return; } + +#ifndef LED_DISABLED + ledShowString("running"); +#endif } /******************************** @@ -108,6 +112,10 @@ void loop() { #ifndef DEBUG_DISABLE Serial.println("GPS written."); #endif + +#ifndef LED_DISABLED + ledShowString("logged"); +#endif } /******************************** @@ -372,8 +380,8 @@ bool sdWriteData(const GpsData* const data) { file.print(data->latitude, GPS_LAT_LONG_PRECISION); file.print(','); file.print(data->longitude, GPS_LAT_LONG_PRECISION); - file.print(','); #ifndef GPS_MIN_INFO + file.print(','); file.print(data->altitude); file.print(','); file.print(data->course); From cbf6362974eb3238bdb8cb58f29c50752633078b Mon Sep 17 00:00:00 2001 From: Thomas Reto Strub Date: Thu, 11 May 2017 18:14:53 +0200 Subject: [PATCH 10/18] bugfixes - fix date format (4-digit year) - rename to _GPS_NO_STATS (used in library) - float / long invalid values --- TinyDuino/TinyDuino.ino | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/TinyDuino/TinyDuino.ino b/TinyDuino/TinyDuino.ino index 9e679cb..cc67ff0 100644 --- a/TinyDuino/TinyDuino.ino +++ b/TinyDuino/TinyDuino.ino @@ -16,7 +16,7 @@ void ledShowChar(const char c); /** GPS *** GPS *** GPS **/ //#define GPS_FLOAT -#define GPS_NO_STATS +#define _GPS_NO_STATS #define GPS_MIN_INFO #ifdef GPS_FLOAT @@ -181,15 +181,23 @@ void ledShowChar(const char c) { #ifdef GPS_FLOAT #define GPS_LAT_LONG_PRECISION (6) +#define GPS_LAT_LONG_INVALID (TinyGPS::GPS_INVALID_ANGLE) #define GPS_LAT_LONG_FUNCTION f_get_position +#define GPS_ALT_INVALID (TinyGPS::GPS_INVALID_ALTITUDE) #define GPS_ALT_FUNCTION f_altitude +#define GPS_COURSE_INVALID (TinyGPS::GPS_INVALID_ANGLE) #define GPS_COURSE_FUNCTION f_course +#define GPS_SPEED_INVALID (TinyGPS::GPS_INVALID_SPEED) #define GPS_SPEED_FUNCTION f_speed_kmph #else #define GPS_LAT_LONG_PRECISION (10) +#define GPS_LAT_LONG_INVALID (TinyGPS::GPS_INVALID_F_ANGLE) #define GPS_LAT_LONG_FUNCTION get_position +#define GPS_ALT_INVALID (TinyGPS::GPS_INVALID_F_ALTITUDE) #define GPS_ALT_FUNCTION altitude +#define GPS_COURSE_INVALID (TinyGPS::GPS_INVALID_F_ANGLE) #define GPS_COURSE_FUNCTION course +#define GPS_SPEED_INVALID (TinyGPS::GPS_INVALID_F_SPEED) #define GPS_SPEED_FUNCTION speed #endif @@ -235,7 +243,7 @@ void gpsWakeUp() { #ifndef DEBUG_DISABLE Serial.println(" done."); -#ifndef GPS_NO_STATS +#ifndef _GPS_NO_STATS unsigned long encodedChars; unsigned short goodSentences, failedChecksums; gps.stats(&encodedChars, &goodSentences, &failedChecksums); @@ -300,13 +308,13 @@ bool gpsTryReadData(GpsData* const data) { return age1 != TinyGPS::GPS_INVALID_AGE && data->nofSatellites != TinyGPS::GPS_INVALID_SATELLITES - && data->latitude != TinyGPS::GPS_INVALID_F_ANGLE - && data->longitude != TinyGPS::GPS_INVALID_F_ANGLE + && data->latitude != GPS_LAT_LONG_INVALID + && data->longitude != GPS_LAT_LONG_INVALID && age2 != TinyGPS::GPS_INVALID_AGE #ifndef GPS_MIN_INFO - && data->altitude != TinyGPS::GPS_INVALID_F_ALTITUDE - && data->course != TinyGPS::GPS_INVALID_F_ANGLE - && data->speed != TinyGPS::GPS_INVALID_F_SPEED + && data->altitude != GPS_ALT_INVALID + && data->course != GPS_COURSE_INVALID + && data->speed != GPS_SPEED_INVALID #endif ; } @@ -371,7 +379,7 @@ bool sdWriteData(const GpsData* const data) { file.println("Date/Time,# Satellites,Latitude,Longitude,Altitude,Course,Speed (kt/100),Speed (km/h),From Home (m)"); char dateTime[20]; - sprintf(dateTime, "%02d-%02d-%02dT%02d:%02d:%02dZ", data->year, data->month, data->day, data->hour, data->minute, data->second); + sprintf(dateTime, "%04d-%02d-%02dT%02d:%02d:%02dZ", data->year, data->month, data->day, data->hour, data->minute, data->second); file.print(dateTime); file.print(','); From 431413505333f705fced7283beadb26684cc9b28 Mon Sep 17 00:00:00 2001 From: Thomas Reto Strub Date: Fri, 12 May 2017 09:35:47 +0200 Subject: [PATCH 11/18] rename typedefs --- TinyDuino/TinyDuino.ino | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/TinyDuino/TinyDuino.ino b/TinyDuino/TinyDuino.ino index cc67ff0..ac40ae5 100644 --- a/TinyDuino/TinyDuino.ino +++ b/TinyDuino/TinyDuino.ino @@ -20,20 +20,20 @@ void ledShowChar(const char c); #define GPS_MIN_INFO #ifdef GPS_FLOAT -typedef float GPS_LAT_LONG_ALT_TYPE, GPS_COURSE_SPEED_TYPE; +typedef float GpsLatLongAltType, GpsCourseSpeedType; #else -typedef long GPS_LAT_LONG_ALT_TYPE; -typedef unsigned long GPS_COURSE_SPEED_TYPE; +typedef long GpsLatLongAltType; +typedef unsigned long GpsCourseSpeedType; #endif typedef struct { int year; byte month, day, hour, minute, second; unsigned short nofSatellites; - GPS_LAT_LONG_ALT_TYPE latitude, longitude; + GpsLatLongAltType latitude, longitude; #ifndef GPS_MIN_INFO - GPS_LAT_LONG_ALT_TYPE altitude, fromHomeM; - GPS_COURSE_SPEED_TYPE course, speed; + GpsLatLongAltType altitude, fromHomeM; + GpsCourseSpeedType course, speed; #endif } GpsData; From 7772561e1d818e9021000bbe6e90d2b2eb82f6c8 Mon Sep 17 00:00:00 2001 From: Thomas Reto Strub Date: Fri, 12 May 2017 10:17:54 +0200 Subject: [PATCH 12/18] maximise functionality --- TinyDuino/TinyDuino.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/TinyDuino/TinyDuino.ino b/TinyDuino/TinyDuino.ino index ac40ae5..7a52334 100644 --- a/TinyDuino/TinyDuino.ino +++ b/TinyDuino/TinyDuino.ino @@ -1,5 +1,5 @@ -#define DEBUG_DISABLE +//#define DEBUG_DISABLE /** LED *** LED *** LED **/ @@ -15,9 +15,9 @@ void ledShowChar(const char c); /** GPS *** GPS *** GPS **/ -//#define GPS_FLOAT -#define _GPS_NO_STATS -#define GPS_MIN_INFO +#define GPS_FLOAT +//#define _GPS_NO_STATS +//#define GPS_MIN_INFO #ifdef GPS_FLOAT typedef float GpsLatLongAltType, GpsCourseSpeedType; From cf13449350f5f7a5b858a9e8939bce8649f37b11 Mon Sep 17 00:00:00 2001 From: Thomas Reto Strub Date: Fri, 12 May 2017 10:18:49 +0200 Subject: [PATCH 13/18] always wake up / shut down --- TinyDuino/TinyDuino.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/TinyDuino/TinyDuino.ino b/TinyDuino/TinyDuino.ino index 7a52334..1b3fdcf 100644 --- a/TinyDuino/TinyDuino.ino +++ b/TinyDuino/TinyDuino.ino @@ -225,7 +225,7 @@ void gpsWakeUp() { #ifndef DEBUG_DISABLE Serial.print("Waking up GPS module.."); #endif - while (digitalRead(GPS_SYS_ON_PIN) == LOW) { + do { #ifndef DEBUG_DISABLE Serial.print("."); #endif @@ -233,7 +233,7 @@ void gpsWakeUp() { delay(5); digitalWrite(GPS_ON_OFF_PIN, LOW); delay(100); - } + } while (digitalRead(GPS_SYS_ON_PIN) == LOW); #ifndef DEBUG_DISABLE Serial.println(" done."); @@ -264,7 +264,7 @@ void gpsShutDown() { #ifndef DEBUG_DISABLE Serial.print("Shutting down GPS module.."); #endif - while (digitalRead(GPS_SYS_ON_PIN) == HIGH) { + do { #ifndef DEBUG_DISABLE Serial.print("."); #endif @@ -272,7 +272,7 @@ void gpsShutDown() { delay(5); digitalWrite(GPS_ON_OFF_PIN, HIGH); delay(100); - } + } while (digitalRead(GPS_SYS_ON_PIN) == HIGH); #ifndef DEBUG_DISABLE Serial.println(" done."); #endif From 1a8a6e416e16efb5323086cd27ff9222993809dc Mon Sep 17 00:00:00 2001 From: Thomas Reto Strub Date: Fri, 12 May 2017 10:24:57 +0200 Subject: [PATCH 14/18] split gpsWakeUp --- TinyDuino/TinyDuino.ino | 56 ++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/TinyDuino/TinyDuino.ino b/TinyDuino/TinyDuino.ino index 1b3fdcf..48c4bdd 100644 --- a/TinyDuino/TinyDuino.ino +++ b/TinyDuino/TinyDuino.ino @@ -44,6 +44,9 @@ void gpsDelay(const unsigned long ms); bool gpsTryReadData(GpsData* const data); #ifndef DEBUG_DISABLE void gpsPrintData(const GpsData* const data); +#ifndef _GPS_NO_STATS +void gpsPrintStatistics(); +#endif #endif /** SD *** SD *** SD **/ @@ -89,9 +92,21 @@ void loop() { if (error) return; - GpsData data; gpsWakeUp(); + +#ifndef DEBUG_DISABLE + Serial.print("Reading GPS data..."); +#endif + gpsDelay(29000); +#ifndef DEBUG_DISABLE + Serial.println(" done."); +#endif + //gpsShutDown(); + + gpsPrintStatistics(); + + GpsData data; if (!gpsTryReadData(&data)) { #ifndef DEBUG_DISABLE Serial.println("Could not read valid GPS data!"); @@ -177,8 +192,6 @@ void ledShowChar(const char c) { #define GPS_SYS_ON_PIN (A2) #define GPS_ON_OFF_PIN (A3) -#define GPS_CALIBRATION_TIME (15000) - #ifdef GPS_FLOAT #define GPS_LAT_LONG_PRECISION (6) #define GPS_LAT_LONG_INVALID (TinyGPS::GPS_INVALID_ANGLE) @@ -236,26 +249,6 @@ void gpsWakeUp() { } while (digitalRead(GPS_SYS_ON_PIN) == LOW); #ifndef DEBUG_DISABLE Serial.println(" done."); - - Serial.print("Calibrating GPS module..."); -#endif - gpsDelay(GPS_CALIBRATION_TIME); -#ifndef DEBUG_DISABLE - Serial.println(" done."); - -#ifndef _GPS_NO_STATS - unsigned long encodedChars; - unsigned short goodSentences, failedChecksums; - gps.stats(&encodedChars, &goodSentences, &failedChecksums); - - Serial.print("GPS statistics: chars: "); - Serial.print(encodedChars); - Serial.print(", good sentences: "); - Serial.print(goodSentences); - Serial.print(", failed checksums: "); - Serial.print(failedChecksums); - Serial.println('.'); -#endif #endif } @@ -350,6 +343,23 @@ void gpsPrintData(const GpsData* const data) { #endif Serial.println(); } + +#ifndef _GPS_NO_STATS +void gpsPrintStatistics() { + + unsigned long encodedChars; + unsigned short goodSentences, failedChecksums; + gps.stats(&encodedChars, &goodSentences, &failedChecksums); + + Serial.print("GPS statistics: chars: "); + Serial.print(encodedChars); + Serial.print(", good sentences: "); + Serial.print(goodSentences); + Serial.print(", failed checksums: "); + Serial.print(failedChecksums); + Serial.println('.'); +} +#endif #endif /******************************** From 85f56c6fae21e20f0bd6d097e4ca90c4b205cef1 Mon Sep 17 00:00:00 2001 From: Thomas Reto Strub Date: Fri, 12 May 2017 10:33:20 +0200 Subject: [PATCH 15/18] cleanup superfluous parenthesis --- TinyDuino/TinyDuino.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TinyDuino/TinyDuino.ino b/TinyDuino/TinyDuino.ino index 48c4bdd..e487bee 100644 --- a/TinyDuino/TinyDuino.ino +++ b/TinyDuino/TinyDuino.ino @@ -71,7 +71,7 @@ void setup() { ledSetup(); #endif gpsSetup(); - if ((error = !sdSetup())) { + if (error = !sdSetup()) { #ifndef DEBUG_DISABLE Serial.println("Could not setup SD card!"); #endif @@ -118,7 +118,7 @@ void loop() { gpsPrintData(&data); Serial.println("GPS printed."); #endif - if ((error = !sdWriteData(&data))) { + if (error = !sdWriteData(&data)) { #ifndef DEBUG_DISABLE Serial.println("Could not write GPS data!"); #endif From ad2fd7d9adbeb941406f052a9260471494c2e1a7 Mon Sep 17 00:00:00 2001 From: Thomas Reto Strub Date: Fri, 12 May 2017 10:55:05 +0200 Subject: [PATCH 16/18] add hdop --- TinyDuino/TinyDuino.ino | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/TinyDuino/TinyDuino.ino b/TinyDuino/TinyDuino.ino index e487bee..dfccac4 100644 --- a/TinyDuino/TinyDuino.ino +++ b/TinyDuino/TinyDuino.ino @@ -30,6 +30,7 @@ typedef struct { int year; byte month, day, hour, minute, second; unsigned short nofSatellites; + unsigned long hdop; GpsLatLongAltType latitude, longitude; #ifndef GPS_MIN_INFO GpsLatLongAltType altitude, fromHomeM; @@ -287,6 +288,7 @@ bool gpsTryReadData(GpsData* const data) { gps.crack_datetime(&data->year, &data->month, &data->day, &data->hour, &data->minute, &data->second, &hundreth, &age1); data->nofSatellites = gps.satellites(); + data->hdop = gps.hdop(); gps.GPS_LAT_LONG_FUNCTION(&data->latitude, &data->longitude, &age2); #ifndef GPS_MIN_INFO @@ -301,6 +303,7 @@ bool gpsTryReadData(GpsData* const data) { return age1 != TinyGPS::GPS_INVALID_AGE && data->nofSatellites != TinyGPS::GPS_INVALID_SATELLITES + && data->hdop != TinyGPS::GPS_INVALID_HDOP && data->latitude != GPS_LAT_LONG_INVALID && data->longitude != GPS_LAT_LONG_INVALID && age2 != TinyGPS::GPS_INVALID_AGE @@ -322,6 +325,8 @@ void gpsPrintData(const GpsData* const data) { Serial.print(dateTime); Serial.print(" # satellites: "); Serial.print(data->nofSatellites); + Serial.print(" horizontal DOP: "); + Serial.print(data->hdop); Serial.print(" latitude: "); Serial.print(data->latitude, GPS_LAT_LONG_PRECISION); Serial.print(" longitude: "); @@ -386,7 +391,7 @@ bool sdWriteData(const GpsData* const data) { return false; if (!init) - file.println("Date/Time,# Satellites,Latitude,Longitude,Altitude,Course,Speed (kt/100),Speed (km/h),From Home (m)"); + file.println("Date/Time,# Satellites,Horizontal DOP,Latitude,Longitude,Altitude,Course,Speed (kt/100),Speed (km/h),From Home (m)"); char dateTime[20]; sprintf(dateTime, "%04d-%02d-%02dT%02d:%02d:%02dZ", data->year, data->month, data->day, data->hour, data->minute, data->second); @@ -395,6 +400,8 @@ bool sdWriteData(const GpsData* const data) { file.print(','); file.print(data->nofSatellites); file.print(','); + file.print(data->hdop); + file.print(','); file.print(data->latitude, GPS_LAT_LONG_PRECISION); file.print(','); file.print(data->longitude, GPS_LAT_LONG_PRECISION); From 5a0982e6e2ef271798486e46b249154995eddc58 Mon Sep 17 00:00:00 2001 From: Thomas Reto Strub Date: Fri, 12 May 2017 11:06:59 +0200 Subject: [PATCH 17/18] add missing directives, use standard order --- TinyDuino/TinyDuino.ino | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/TinyDuino/TinyDuino.ino b/TinyDuino/TinyDuino.ino index dfccac4..472c675 100644 --- a/TinyDuino/TinyDuino.ino +++ b/TinyDuino/TinyDuino.ino @@ -105,7 +105,11 @@ void loop() { //gpsShutDown(); +#ifndef DEBUG_DISABLE +#ifndef _GPS_NO_STATS gpsPrintStatistics(); +#endif +#endif GpsData data; if (!gpsTryReadData(&data)) { @@ -228,9 +232,9 @@ void gpsSetup() { gpsSerial.begin(9600); - digitalWrite(GPS_ON_OFF_PIN, LOW); - pinMode(GPS_ON_OFF_PIN, OUTPUT); pinMode(GPS_SYS_ON_PIN, INPUT); + pinMode(GPS_ON_OFF_PIN, OUTPUT); + digitalWrite(GPS_ON_OFF_PIN, LOW); delay(100); } From c34ffed993cf39a5a3dbc5b506e73f737d48d92d Mon Sep 17 00:00:00 2001 From: Thomas Reto Strub Date: Sun, 28 May 2017 00:33:31 +0200 Subject: [PATCH 18/18] try to fix errors --- TinyDuino/TinyDuino.ino | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/TinyDuino/TinyDuino.ino b/TinyDuino/TinyDuino.ino index 472c675..fc55405 100644 --- a/TinyDuino/TinyDuino.ino +++ b/TinyDuino/TinyDuino.ino @@ -52,8 +52,12 @@ void gpsPrintStatistics(); /** SD *** SD *** SD **/ +//#define SD_DISABLED + +#ifndef SD_DISABLED bool sdSetup(); bool sdWriteData(const GpsData* const data); +#endif /** WIFI *** WIFI *** WIFI **/ @@ -68,19 +72,24 @@ void setup() { #ifndef DEBUG_DISABLE Serial.begin(115200); #endif + #ifndef LED_DISABLED ledSetup(); #endif + gpsSetup(); + +#ifndef SD_DISABLED if (error = !sdSetup()) { #ifndef DEBUG_DISABLE Serial.println("Could not setup SD card!"); #endif return; } +#endif #ifndef LED_DISABLED - ledShowString("running"); + ledShowString("l"); // . - . . #endif } @@ -112,9 +121,12 @@ void loop() { #endif GpsData data; - if (!gpsTryReadData(&data)) { + if (/*error =*/ !gpsTryReadData(&data)) { #ifndef DEBUG_DISABLE Serial.println("Could not read valid GPS data!"); +#endif +#ifndef LED_DISABLED + ledShowString("p"); // . - - . #endif return; } @@ -123,18 +135,24 @@ void loop() { gpsPrintData(&data); Serial.println("GPS printed."); #endif - if (error = !sdWriteData(&data)) { + +#ifndef SD_DISABLED + if (/*error =*/ !sdWriteData(&data)) { #ifndef DEBUG_DISABLE Serial.println("Could not write GPS data!"); +#endif +#ifndef LED_DISABLED + ledShowString("p"); // . - - . #endif return; } #ifndef DEBUG_DISABLE Serial.println("GPS written."); #endif +#endif #ifndef LED_DISABLED - ledShowString("logged"); + ledShowString("q"); // - - . - #endif } @@ -241,9 +259,9 @@ void gpsSetup() { void gpsWakeUp() { #ifndef DEBUG_DISABLE - Serial.print("Waking up GPS module.."); + Serial.print("Waking up GPS module..."); #endif - do { + while (digitalRead(GPS_SYS_ON_PIN) == LOW) { #ifndef DEBUG_DISABLE Serial.print("."); #endif @@ -251,7 +269,7 @@ void gpsWakeUp() { delay(5); digitalWrite(GPS_ON_OFF_PIN, LOW); delay(100); - } while (digitalRead(GPS_SYS_ON_PIN) == LOW); + } #ifndef DEBUG_DISABLE Serial.println(" done."); #endif @@ -260,9 +278,9 @@ void gpsWakeUp() { void gpsShutDown() { #ifndef DEBUG_DISABLE - Serial.print("Shutting down GPS module.."); + Serial.print("Shutting down GPS module..."); #endif - do { + while (digitalRead(GPS_SYS_ON_PIN) == HIGH) { #ifndef DEBUG_DISABLE Serial.print("."); #endif @@ -270,7 +288,7 @@ void gpsShutDown() { delay(5); digitalWrite(GPS_ON_OFF_PIN, HIGH); delay(100); - } while (digitalRead(GPS_SYS_ON_PIN) == HIGH); + } #ifndef DEBUG_DISABLE Serial.println(" done."); #endif @@ -375,6 +393,8 @@ void gpsPrintStatistics() { *** SD *** SD *** SD *** ********************************/ +#ifndef SD_DISABLED + #define SD_PIN (10) #define SD_FILE ("gps.csv") @@ -431,6 +451,7 @@ bool sdWriteData(const GpsData* const data) { file.close(); return true; } +#endif /******************************** *** WIFI *** WIFI *** WIFI ***