Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Datorsmurf committed May 3, 2019
2 parents 8699653 + 676a5a2 commit 2999c9c
Show file tree
Hide file tree
Showing 18 changed files with 822 additions and 509 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
Liam/LocalDefinition.h
.DS_Store
.vscode
.vscode/*
*/.vs/
*/__vm/
*/Debug/
*.sln
*.vcxproj*
*.orig
*/Release/*
node_modules/*
.browse.VC.db
yarn.lock
127 changes: 106 additions & 21 deletions Liam/BWFSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,75 @@

#include "BWFSensor.h"

int BWFSENSOR::outside_code[] = {OUTSIDE_BWF, INSIDE_BWF-OUTSIDE_BWF};
int BWFSENSOR::inside_code[] = {INSIDE_BWF};
int BWFSENSOR::outside_code[] = {OUTSIDE_BWF, INSIDE_BWF - OUTSIDE_BWF, OUTSIDE_BWF, INSIDE_BWF - OUTSIDE_BWF };
int BWFSENSOR::inside_code[] = {INSIDE_BWF, INSIDE_BWF};

int currentSensor = 0;

BWFSENSOR::BWFSENSOR(int selA, int selB) {
selpin_A = selA;
selpin_B = selB;
}


int BWFSENSOR::getCurrentSensor() {
return _currentSensor;
}

void BWFSENSOR::setup() {

//select(0);
}

void BWFSENSOR::selectNext() {
if (_manualSensorSelect) {
return;
}
if (signal_status != NOSIGNAL) {
//Serial.println("Got signal");
select((_currentSensor + 1) % NUMBER_OF_SENSORS);
return;
}

if (millis() - lastSwitch > BWF_COLLECT_SIGNAL_TIME) {
/*Serial.print(millis());
Serial.print(" Signal timout on sensor ");
Serial.print(_currentSensor);
Serial.println(", switching");*/
assignIfNeeded(_currentSensor, NOSIGNAL);
select((_currentSensor + 1) % NUMBER_OF_SENSORS);
}
}

void BWFSENSOR::SetManualSensorSelect(bool useManualMode){
_manualSensorSelect = useManualMode;
}

// Select active sensor
void BWFSENSOR::select(int sensornumber) {
if (currentSensor == sensornumber) {
//Serial.print("Selecting sensor: ");
//Serial.println(sensornumber);

if (_currentSensor == sensornumber) {
return;
}
currentSensor = sensornumber;

_switching = true;
digitalWrite(selpin_A, (sensornumber & 1) > 0 ? HIGH : LOW);
digitalWrite(selpin_B, (sensornumber & 2) > 0 ? HIGH : LOW);
_currentSensor = sensornumber;
clearSignal();
long time = millis();
while (signal_status == NOSIGNAL
&& millis() - time < BWF_COLLECT_SIGNAL_TIME) // max time of 200ms
{
delay(1);
}
lastSwitch = millis();
_switching = false;
// Serial.println(_currentSensor);
// clearSignal();

_switching = false;

//long time = millis();
//while (signal_status == NOSIGNAL
// && millis() - time < BWF_COLLECT_SIGNAL_TIME) // max time of 200ms
//{
// delay(1);
//}

// delay(200);
}
Expand All @@ -78,25 +119,26 @@ void BWFSENSOR::clearSignal() {
for (int i = 0; i < arr_length; i++)
arr[i] = 0;
signal_status = NOSIGNAL;
//sensorValue[_currentSensor] = NOSIGNAL;
pulse_count_inside = 0;
pulse_count_outside = 0;
arr_count = 0;
}


bool BWFSENSOR::isInside() {
return (signal_status == INSIDE);
bool BWFSENSOR::isInside(int sensornumber) {
return (sensorValue[sensornumber] == INSIDE);
}

bool BWFSENSOR::isOutside() {
return (signal_status == OUTSIDE);
bool BWFSENSOR::isOutside(int sensornumber) {
return (sensorValue[sensornumber] == OUTSIDE);
}

bool BWFSENSOR::isOutOfBounds() {
bool BWFSENSOR::isOutOfBounds(int sensornumber) {
if (BWF_DETECTION_ALWAYS)
return !isInside();
return !isInside(sensornumber);
else
return isOutside();
return isOutside(sensornumber);
}

bool BWFSENSOR::isTimedOut() {
Expand All @@ -111,8 +153,15 @@ bool BWFSENSOR::hasNoSignal() {
// This function is run each time the BWF pin gets a pulse
// For accuracy, this function should be kept as fast as possible
void BWFSENSOR::readSensor() {
//bool needOneMoreRun = false;
long now = micros();

if (_switching)
{
last_pulse = now;
Serial.println(">> new puls while still processing.");
return; //Avoid data for undefined state for selection pins
}
char buf[40];
// Calculate the time since last pulse
int time_since_pulse = int(now - last_pulse);
last_pulse = now;
Expand All @@ -126,7 +175,9 @@ void BWFSENSOR::readSensor() {

// Check if the entire pulse train has been batched
if (pulse_count_inside >= sizeof(inside_code)/sizeof(inside_code[0])) {
//sensorValue[_currentSensor] = INSIDE;
signal_status = INSIDE;
assignIfNeeded(_currentSensor, signal_status);
last_match = millis();
pulse_count_inside=0;
}
Expand All @@ -138,20 +189,54 @@ void BWFSENSOR::readSensor() {
if (abs(pulse_length-outside_code[pulse_count_outside]) < 2) {
pulse_count_outside++;
if (pulse_count_outside >= sizeof(outside_code)/sizeof(outside_code[0])) {
//sensorValue[_currentSensor] = OUTSIDE;
signal_status = OUTSIDE;
assignIfNeeded(_currentSensor, signal_status);
last_match = millis();
pulse_count_outside=0;
}
} else {
pulse_count_outside=0;

}



// Store the received code for debug output
arr[arr_count++] = pulse_length;
if (arr_count>arr_length) arr_count=0;
//sensorValue[_currentSensor] = isOutOfBounds();
//select((_currentSensor+1) % NUMBER_OF_SENSORS);
selectNext();
}

void BWFSENSOR::assignIfNeeded(int sensor, int signalStatus) {
if (sensorValue[sensor] != signalStatus) {
sensorValue[_currentSensor] = signalStatus;
//Signal change here if needed
Serial.print(F("Sensor "));
Serial.print(sensor);
Serial.print(F(" "));
Serial.println(getSignalStatusName(signalStatus));
};
}

String BWFSENSOR::getSignalStatusName(int signalStatus) {

switch (signalStatus)
{
case INSIDE:
return F("Inside");
case OUTSIDE:
return F("Outside");
case NOSIGNAL:
return F("No signal");
default:
return F("UNKNOWN");
}
}


void BWFSENSOR::printSignal() {
for (int i = 0; i < arr_length; i++) {
Serial.print(arr[i]);
Expand All @@ -161,4 +246,4 @@ void BWFSENSOR::printSignal() {
bool BWFSENSOR::gotSignal()
{
return arr_count >= BWF_NUMBER_OF_PULSES ? true : false;
}
}
24 changes: 18 additions & 6 deletions Liam/BWFSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,54 @@
class BWFSENSOR {
public:
BWFSENSOR(int selA, int selB);
void setup();

void selectNext();
void select(int sensornumber);
void clearSignal();
void SetManualSensorSelect(bool useManualMode);


bool isInside();
bool isOutside();
bool isInside(int sensornumber);
bool isOutside(int sensornumber);
bool isOutOfBounds(int sensornumber);
bool isTimedOut();
bool isOutOfBounds();
bool hasNoSignal();
bool gotSignal();

void readSensor();


void printSignal();

private:
String getSignalStatusName(int signalStatus);
void assignIfNeeded(int sensor, int signalStatus);

int sensorValue[NUMBER_OF_SENSORS];
int getCurrentSensor();
void clearSignal();
// BWF Code for inside and outside the fence
static int inside_code[];
static int outside_code[];

int _currentSensor = 0;
const static int pulse_unit_length = 100;

int pulse_count_inside;
int pulse_count_outside;

int selpin_A;
int selpin_B;

volatile bool _switching; //volatile since it's share between interrupt and loop
int signal_status;
unsigned long lastSwitch;
long last_match;
long last_pulse;

// Array for debug printing
const static int arr_length=10;
int arr[arr_length];
int arr_count;
bool _manualSensorSelect = false;
};

#endif /* _BWFSENSOR_H_ */
2 changes: 1 addition & 1 deletion Liam/Battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#define LEADACIDEMPTY 12000

// Running average sample size
#define FILTER 5
#define FILTER 40

// Voltage divider factor x10 to avoid decimal ( 4 = 40)
#define VOLTDIVATOR 42
Expand Down
Loading

0 comments on commit 2999c9c

Please sign in to comment.