Skip to content

Commit

Permalink
Split code
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNitek committed Jun 7, 2020
1 parent 093a0b5 commit 2abf99f
Show file tree
Hide file tree
Showing 11 changed files with 329 additions and 225 deletions.
239 changes: 14 additions & 225 deletions Cache.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#include <SparkFunLSM6DS3.h>
#include <SPI.h>
#include <MFRC522.h>
#include "Stage.h"
#include "Colors.h"
#include "Tilt.h"
#include "LedNavigation.h"
#include "Rfid.h"
#include "Logbook.h"


/**
Expand All @@ -31,32 +37,13 @@ NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> ring(LED_COUNT, LED_PIN);
TinyGPSPlus gps;
QMC5883LCompass compass;
LSM6DS3 gyro;
MFRC522 mfrc522(RC522_CS_PIN, RC522_RST_PIN); // Create MFRC522 instance
MFRC522 mfrc522(RC522_CS_PIN, RC522_RST_PIN);

#define O_X_DOWN 0x01
#define O_X_UP 0x02
#define O_Y_DOWN 0x04
#define O_Y_UP 0x08
#define O_Z_DOWN 0x10
#define O_Z_UP 0x20

uint8_t currentOrientation;
uint8_t currentStage = 0;
std::vector<Stage*> stages{new Tilt(gyro, ring), new LedNavigation(compass, gps, ring), new Rfid(mfrc522), new Logbook()};
Stage* stage = stages[0];

#define colorSaturation 4
RgbColor red(colorSaturation, 0, 0);
RgbColor green(0, colorSaturation, 0);
RgbColor blue(0, 0, colorSaturation);
RgbColor white(colorSaturation);
RgbColor black(0);

bool blinker = false;

uint8_t stage = 0;

uint8_t tiltSequence[] = {O_Z_DOWN, O_Y_DOWN, O_Z_DOWN, O_X_UP, O_Y_DOWN, O_Z_DOWN, 0x00};
uint8_t tiltPointer = 0;

const double TARGET_LAT = 48.4168602, TARGET_LON = 9.9433157;

void setup()
{
Expand All @@ -69,25 +56,6 @@ void setup()
ring.Begin();
ring.Show();

//Over-ride default settings if desired
gyro.settings.gyroEnabled = 1; //Can be 0 or 1
gyro.settings.gyroRange = 2000; //Max deg/s. Can be: 125, 245, 500, 1000, 2000
gyro.settings.gyroSampleRate = 104; //Hz. Can be: 13, 26, 52, 104, 208, 416, 833, 1666
gyro.settings.gyroBandWidth = 200; //Hz. Can be: 50, 100, 200, 400;

gyro.settings.accelEnabled = 1;
gyro.settings.accelRange = 16; //Max G force readable. Can be: 2, 4, 8, 16
gyro.settings.accelSampleRate = 833; //Hz. Can be: 13, 26, 52, 104, 208, 416, 833, 1666, 3332, 6664, 13330
gyro.settings.accelBandWidth = 200; //Hz. Can be: 50, 100, 200, 400;

// Set orientation threshold
gyro.writeRegister(LSM6DS3_ACC_GYRO_TAP_THS_6D, LSM6DS3_ACC_GYRO_SIXD_THS_60_degree);
// Enable low pass filter 2
gyro.writeRegister(LSM6DS3_ACC_GYRO_TAP_CFG1, 0x10);
gyro.writeRegister(LSM6DS3_ACC_GYRO_CTRL8_XL, 0x01);
// Enable interrupt
gyro.writeRegister(LSM6DS3_ACC_GYRO_MD1_CFG, LSM6DS3_ACC_GYRO_INT1_6D_ENABLED);

gyro.begin();

compass.init();
Expand All @@ -112,189 +80,10 @@ void setup()

void loop()
{
switch(stage) {
case 0:
stageTilt();
break;
case 1:
stageGotoPlayground();
break;
default:
Serial.println("Not implemented!");
}

smartDelay(1000);

if (millis() > 5000 && gps.charsProcessed() < 10)
Serial.println(F("No GPS data received: check wiring"));
}

void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (Serial2.available())
gps.encode(Serial2.read());
stageRfid();
} while (millis() - start < ms);
stage->handle();
}

void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
{
if (d.isValid())
{
char sz[32];
sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
Serial.print(sz);
}

if (t.isValid())
{
char sz[32];
sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
Serial.print(sz);
}
void nextStage() {
stage = stages[currentStage++];
stage->init();
}

void stageTilt() {
uint8_t orientation;
gyro.readRegister(&orientation, LSM6DS3_ACC_GYRO_D6D_SRC);

if(currentOrientation == orientation) {
return;
}

currentOrientation = orientation;

if(orientation == tiltSequence[tiltPointer]) {
tiltPointer++;
if(tiltSequence[tiltPointer] == 0) {
for(uint8_t i = 0; i < 10; i++) {
ring.ClearTo(white);
ring.Show();
smartDelay(200);
ring.ClearTo(green);
ring.Show();
smartDelay(200);
}
return;
}
ring.ClearTo(black);
ring.Show();
smartDelay(500);
ring.ClearTo(green);
ring.Show();
return;
}

tiltPointer = 0;
ring.ClearTo(red);
ring.Show();

/*switch(orientation){
case O_X_DOWN:
Serial.println("X down");
break;
case O_X_UP:
Serial.println("X up");
break;
case O_Y_DOWN:
Serial.println("Y down");
break;
case O_Y_UP:
Serial.println("Y up");
break;
case O_Z_DOWN:
Serial.println("Z down");
break;
case O_Z_UP:
Serial.println("Z up");
break;
default:
Serial.println("Unknown orientation");
}
char orientationString[9];
itoa(orientation, orientationString, 2);
Serial.print("\nOrientation:\n");
Serial.printf("%s", orientationString);*/
}

void stageGotoPlayground() {
compass.read();

printDateTime(gps.date, gps.time);

if(gps.satellites.isValid())
{
Serial.printf("%d ", gps.satellites.value());
}

ring.ClearTo(black);

if(gps.location.isValid()) {
Serial.printf("%f, %f, a: %d", gps.location.lat(), gps.location.lng(), gps.location.age());
Serial.println();

unsigned long distanceMeters =
(unsigned long)TinyGPSPlus::distanceBetween(
gps.location.lat(),
gps.location.lng(),
TARGET_LAT,
TARGET_LON);

int16_t course =
TinyGPSPlus::courseTo(
gps.location.lat(),
gps.location.lng(),
TARGET_LAT,
TARGET_LON);


int16_t azimuth = compass.getAzimuth();

char myArray[3];
compass.getDirection(myArray, azimuth);

Serial.print(" Azimuth: ");
Serial.print(azimuth);
Serial.print(" Direction: ");
Serial.print(myArray[0]);
Serial.print(myArray[1]);
Serial.print(myArray[2]);

// Offset between LED #0 and azimuth == 0
int16_t ledOffset = 90;

Serial.printf(" d: %lu c: %d ang: %d", distanceMeters, course, (uint16_t)round(azimuth + ledOffset + course + 11.25f));

uint16_t ledIndex = (uint16_t)round((azimuth + ledOffset + course + 11.25f)/22.5) % 16;
ring.SetPixelColor(ledIndex, green);
} else {
if(blinker) {
ring.ClearTo(red);
}
blinker = !blinker;
}
Serial.println();
Serial.println();

ring.Show();
}

void stageRfid() {
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
if ( ! mfrc522.PICC_IsNewCardPresent()) {
return;
}

// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial()) {
return;
}

// Dump debug info about the card; PICC_HaltA() is automatically called
mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
}
13 changes: 13 additions & 0 deletions Colors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef Colors_h
#define Colors_h

#include <NeoPixelBus.h>

#define colorSaturation 4
const RgbColor red(colorSaturation, 0, 0);
const RgbColor green(0, colorSaturation, 0);
const RgbColor blue(0, 0, colorSaturation);
const RgbColor white(colorSaturation);
const RgbColor black(0);

#endif
96 changes: 96 additions & 0 deletions LedNavigation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "LedNavigation.h"

void LedNavigation::handle() {
_compass.read();

_printDateTime(_gps.date, _gps.time);

if(_gps.satellites.isValid())
{
Serial.printf("%d ", _gps.satellites.value());
}

_ring.ClearTo(black);

if(_gps.location.isValid()) {
Serial.printf("%f, %f, a: %d", _gps.location.lat(), _gps.location.lng(), _gps.location.age());
Serial.println();

unsigned long distanceMeters =
(unsigned long)TinyGPSPlus::distanceBetween(
_gps.location.lat(),
_gps.location.lng(),
TARGET_LAT,
TARGET_LON);

int16_t course =
TinyGPSPlus::courseTo(
_gps.location.lat(),
_gps.location.lng(),
TARGET_LAT,
TARGET_LON);


int16_t azimuth = _compass.getAzimuth();

char myArray[3];
_compass.getDirection(myArray, azimuth);

Serial.print(" Azimuth: ");
Serial.print(azimuth);
Serial.print(" Direction: ");
Serial.print(myArray[0]);
Serial.print(myArray[1]);
Serial.print(myArray[2]);

// Offset between LED #0 and azimuth == 0
int16_t ledOffset = 90;

Serial.printf(" d: %lu c: %d ang: %d", distanceMeters, course, (uint16_t)round(azimuth + ledOffset + course + 11.25f));

uint16_t ledIndex = (uint16_t)round((azimuth + ledOffset + course + 11.25f)/22.5) % 16;
_ring.SetPixelColor(ledIndex, green);
} else {
if(_blinker) {
_ring.ClearTo(red);
}
_blinker = !_blinker;
}
Serial.println();
Serial.println();

_ring.Show();

_smartDelay(500);

if (millis() > 5000 && _gps.charsProcessed() < 10) {
Serial.println(F("No GPS data received: check wiring"));
}
};


void LedNavigation::_smartDelay(unsigned long ms) {
unsigned long start = millis();
do
{
while (Serial2.available()) {
_gps.encode(Serial2.read());
}
} while (millis() - start < ms);
};

void LedNavigation::_printDateTime(TinyGPSDate &d, TinyGPSTime &t) {
if (d.isValid())
{
char sz[32];
sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
Serial.print(sz);
}

if (t.isValid())
{
char sz[32];
sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
Serial.print(sz);
}
};
Loading

0 comments on commit 2abf99f

Please sign in to comment.