Browse Source

Using define instead of magic numbers. Started to implement speed vs sleep time logic

tags/v1.2.0
Bertrand Lemasle 7 years ago
parent
commit
0d24ef07e3
8 changed files with 81 additions and 34 deletions
  1. +26
    -0
      GpsTracker/Config.h
  2. +22
    -0
      GpsTracker/Core.cpp
  3. +2
    -0
      GpsTracker/Core.h
  4. +7
    -3
      GpsTracker/Gps.cpp
  5. +2
    -1
      GpsTracker/Gps.h
  6. +1
    -0
      GpsTracker/GpsTracker.h
  7. +6
    -3
      GpsTracker/GpsTracker.ino
  8. +15
    -27
      GpsTracker/Time2.h

+ 26
- 0
GpsTracker/Config.h View File

@@ -0,0 +1,26 @@
#pragma once

struct sleepTimings_t {
uint8_t speed;
uint16_t seconds;
};

#define SLEEP_DEFAULT_TIME_SECONDS 1800

#define GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS 10000
#define GPS_DEFAULT_TOTAL_TIMEOUT_MS 30000

namespace config {

static const sleepTimings_t defaultSleepTimings[] PROGMEM = {
{ 5, SLEEP_DEFAULT_TIME_SECONDS },
{ 10, 1200 },
{ 20, 600 },
{ 30, 540 },
{ 50, 480 },
{ 80, 240 },
{ 100, 210 },
{ 180, 180 },
};

}

+ 22
- 0
GpsTracker/Core.cpp View File

@@ -1,7 +1,29 @@
#include "Core.h" #include "Core.h"
#include "Config.h"


#define LOGGER_NAME "Core" #define LOGGER_NAME "Core"


namespace core { namespace core {
uint16_t sleepTime = SLEEP_DEFAULT_TIME_SECONDS;;


namespace details {
template<typename T, size_t N> size_t getFlashArraySize(T(&)[N]) { return N; }
template<typename T> void readFromFlash(const T *source, T &dest) {
memcpy_P(&dest, source, sizeof(T));
}
}

void setSleepTime(uint8_t velocity) {
for (uint8_t i = 0; i < details::getFlashArraySize(config::defaultSleepTimings); i++) {
sleepTimings_t timing;
details::readFromFlash(&config::defaultSleepTimings[i], timing);

if (velocity > timing.speed) continue;

sleepTime = timing.seconds;
return;
}

sleepTime = SLEEP_DEFAULT_TIME_SECONDS;
}
} }

+ 2
- 0
GpsTracker/Core.h View File

@@ -5,5 +5,7 @@
#include "Debug.h" #include "Debug.h"


namespace core { namespace core {
extern uint16_t sleepTime;


void setSleepTime(uint8_t velocity);
} }

+ 7
- 3
GpsTracker/Gps.cpp View File

@@ -1,10 +1,10 @@
#include "Gps.h" #include "Gps.h"
#include "Config.h"
#include "Debug.h" #include "Debug.h"
#include "Hardware.h" #include "Hardware.h"
#include "MainUnit.h" #include "MainUnit.h"


#define LOGGER_NAME "Gps" #define LOGGER_NAME "Gps"
#define WAIT_FOR_FIX_DELAY 10000


#define TIME_YEAR_OFFSET 0 #define TIME_YEAR_OFFSET 0
#define TIME_MONTH_OFFSET 4 #define TIME_MONTH_OFFSET 4
@@ -27,8 +27,8 @@ namespace gps {
currentStatus = hardware::sim808::device.getGpsStatus(); currentStatus = hardware::sim808::device.getGpsStatus();
if (currentStatus > SIM808_GPS_STATUS::NO_FIX) break; if (currentStatus > SIM808_GPS_STATUS::NO_FIX) break;


mainunit::deepSleep(WAIT_FOR_FIX_DELAY);
timeout -= WAIT_FOR_FIX_DELAY;
mainunit::deepSleep(GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS);
timeout -= GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS;
} while (timeout > 1); } while (timeout > 1);


if (currentStatus > SIM808_GPS_STATUS::NO_FIX) { if (currentStatus > SIM808_GPS_STATUS::NO_FIX) {
@@ -39,6 +39,10 @@ namespace gps {
return currentStatus; return currentStatus;
} }


void getVelocity(uint8_t &velocity) {
hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::SPEED, &velocity);
}

void getTime(tmElements_t &time) { void getTime(tmElements_t &time) {
char *timeStr; char *timeStr;
char buffer[4]; char buffer[4];


+ 2
- 1
GpsTracker/Gps.h View File

@@ -16,6 +16,7 @@ namespace gps {
inline void powerOff() { hardware::sim808::gpsPowerOff(); } inline void powerOff() { hardware::sim808::gpsPowerOff(); }


SIM808_GPS_STATUS acquireCurrentPosition(uint16_t timeout); SIM808_GPS_STATUS acquireCurrentPosition(uint16_t timeout);
void getTime(tmElements_t &time);


void getVelocity(uint8_t &velocity);
void getTime(tmElements_t &time);
} }

+ 1
- 0
GpsTracker/GpsTracker.h View File

@@ -5,6 +5,7 @@
#include <SIM808.h> #include <SIM808.h>


#include "Debug.h" #include "Debug.h"
#include "Config.h"
#include "Core.h" #include "Core.h"


#include "Gps.h" #include "Gps.h"


+ 6
- 3
GpsTracker/GpsTracker.ino View File

@@ -13,9 +13,8 @@ void setup() {
} }


void loop() { void loop() {

gps::powerOn(); gps::powerOn();
SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(30000);
SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_DEFAULT_TOTAL_TIMEOUT_MS);
gps::powerOff(); gps::powerOff();


if (gpsStatus > SIM808_GPS_STATUS::NO_FIX) { if (gpsStatus > SIM808_GPS_STATUS::NO_FIX) {
@@ -26,11 +25,15 @@ void loop() {
rtc::powerOff(); rtc::powerOff();


positions::appendLast(); positions::appendLast();

uint8_t velocity;
gps::getVelocity(velocity);
core::setSleepTime(velocity);
} }


if (positions::needsToSend()) { if (positions::needsToSend()) {
positions::send(); positions::send();
} }


mainunit::deepSleep(10); //TODO : duration
mainunit::deepSleep(core::sleepTime);
} }

+ 15
- 27
GpsTracker/Time2.h View File

@@ -10,30 +10,18 @@


/*============================================================================*/ /*============================================================================*/


typedef unsigned long timestamp_t;

typedef struct {
uint8_t Second;
uint8_t Minute;
uint8_t Hour;
uint8_t Wday; // day of week, sunday is day 1
uint8_t Day;
uint8_t Month;
uint8_t Year; // offset from 1970;
} tmElements_t, TimeElements, *tmElementsPtr_t;

/* low level functions to convert to and from system time */
void breakTime(const timestamp_t time, tmElements_t &tm); // break timestamp_t into elements
timestamp_t makeTime(const tmElements_t &tm); // convert time elements into timestamp_t

tmElements_t operator += (const timestamp_t b) {

}

tmElements_t operator + (const tmElements_t a, const timestamp_t b) {
tmElements_t result;
timestamp_t aTimestamp = makeTime(a);
breakTime(aTimestamp + b, result);

return result;
}
typedef unsigned long timestamp_t;

typedef struct {
uint8_t Second;
uint8_t Minute;
uint8_t Hour;
uint8_t Wday; // day of week, sunday is day 1
uint8_t Day;
uint8_t Month;
uint8_t Year; // offset from 1970;
} tmElements_t, TimeElements, *tmElementsPtr_t;

/* low level functions to convert to and from system time */
void breakTime(const timestamp_t time, tmElements_t &tm); // break timestamp_t into elements
timestamp_t makeTime(const tmElements_t &tm); // convert time elements into timestamp_t

Loading…
Cancel
Save