mirror of
https://gitlab.dit.htwk-leipzig.de/phillip.kuehne/dezibot.git
synced 2025-05-21 20:11:46 +02:00
Merge branch 'feature/#6-lightdetection' into feature/#16-find-a-friend
This commit is contained in:
commit
907228ba79
@ -135,6 +135,7 @@ For instance, in `src/Dezibot.h`, to include `src/motion/Motion.h`, you should w
|
||||
* Upload Mode: "UART0 / Hardware CDC"
|
||||
* USB Mode: "Hardware CDC and JTAG"
|
||||
* Programmer: "Esptool"
|
||||
* USB CDC on Boot: Enabled
|
||||
|
||||
#### Display
|
||||
|
||||
|
@ -8,4 +8,5 @@
|
||||
|
||||
void Dezibot::begin(void) {
|
||||
infraredLight.begin();
|
||||
lightDetection.begin();
|
||||
}
|
@ -18,6 +18,7 @@
|
||||
#include "motionDetection/MotionDetection.h"
|
||||
#include "infraredLight/InfraredLight.h"
|
||||
|
||||
|
||||
class Dezibot {
|
||||
protected:
|
||||
|
||||
|
114
src/lightDetection/LightDetection.cpp
Normal file
114
src/lightDetection/LightDetection.cpp
Normal file
@ -0,0 +1,114 @@
|
||||
#include "LightDetection.h"
|
||||
|
||||
void LightDetection::begin(void){
|
||||
LightDetection::beginInfrared();
|
||||
LightDetection::beginDaylight();
|
||||
};
|
||||
|
||||
uint16_t LightDetection::getValue(photoTransistors sensor){
|
||||
uint16_t result;
|
||||
switch(sensor){
|
||||
//Fall Through intended
|
||||
case IR_FRONT:
|
||||
case IR_LEFT:
|
||||
case IR_RIGHT:
|
||||
case IR_BACK:
|
||||
return readIRPT(sensor);
|
||||
case DL_BOTTOM:
|
||||
case DL_FRONT:
|
||||
return readDLPT(sensor);
|
||||
}
|
||||
};
|
||||
|
||||
photoTransistors LightDetection::getBrightest(ptType type){
|
||||
photoTransistors maxSensor;
|
||||
uint16_t maxReading = 0;
|
||||
uint16_t currentReading = 0;
|
||||
|
||||
if (type == IR){
|
||||
for(const auto pt : allIRPTs){
|
||||
currentReading = LightDetection::getValue(pt);
|
||||
if (currentReading > maxReading){
|
||||
maxReading = currentReading;
|
||||
maxSensor = pt;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for(const auto pt : allDLPTs){
|
||||
currentReading = LightDetection::getValue(pt);
|
||||
if (currentReading > maxReading){
|
||||
maxReading = currentReading;
|
||||
maxSensor = pt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return maxSensor;
|
||||
};
|
||||
|
||||
uint32_t LightDetection::getAverageValue(photoTransistors sensor, uint32_t measurments, uint32_t timeBetween){
|
||||
TickType_t xLastWakeTime = xTaskGetTickCount();
|
||||
TickType_t frequency = timeBetween / portTICK_PERIOD_MS;
|
||||
uint64_t cumulatedResult = 0;
|
||||
for(int i = 0; i < measurments; i++){
|
||||
cumulatedResult += LightDetection::getValue(sensor);
|
||||
xTaskDelayUntil(&xLastWakeTime,frequency);
|
||||
}
|
||||
return cumulatedResult/measurments;
|
||||
};
|
||||
|
||||
void LightDetection::beginInfrared(void){
|
||||
pinMode(IR_PT_ENABLE, OUTPUT);
|
||||
pinMode(IR_PT_FRONT_ADC, INPUT);
|
||||
pinMode(IR_PT_LEFT_ADC, INPUT);
|
||||
pinMode(IR_PT_RIGHT_ADC, INPUT);
|
||||
pinMode(IR_PT_BACK_ADC, INPUT);
|
||||
};
|
||||
|
||||
void LightDetection::beginDaylight(void){
|
||||
pinMode(DL_PT_ENABLE, OUTPUT);
|
||||
pinMode(DL_PT_BOTTOM_ADC, INPUT);
|
||||
pinMode(DL_PT_FRONT_ADC, INPUT );
|
||||
};
|
||||
|
||||
uint16_t LightDetection::readIRPT(photoTransistors sensor){
|
||||
digitalWrite(IR_PT_ENABLE,HIGH);
|
||||
uint16_t result = 0;
|
||||
switch (sensor)
|
||||
{
|
||||
case IR_FRONT:
|
||||
result = analogRead(IR_PT_FRONT_ADC);
|
||||
break;
|
||||
case IR_LEFT:
|
||||
result = analogRead(IR_PT_LEFT_ADC);
|
||||
break;
|
||||
case IR_RIGHT:
|
||||
result = analogRead(IR_PT_RIGHT_ADC);
|
||||
break;
|
||||
case IR_BACK:
|
||||
result = analogRead(IR_PT_BACK_ADC);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
digitalWrite(IR_PT_ENABLE,LOW);
|
||||
return result;
|
||||
};
|
||||
|
||||
uint16_t LightDetection::readDLPT(photoTransistors sensor){
|
||||
digitalWrite(DL_PT_ENABLE,HIGH);
|
||||
uint16_t result = 0;
|
||||
switch (sensor)
|
||||
{
|
||||
case DL_FRONT:
|
||||
result = analogRead(DL_PT_FRONT_ADC);
|
||||
break;
|
||||
case DL_BOTTOM:
|
||||
result = analogRead(DL_PT_BOTTOM_ADC);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
digitalWrite(DL_PT_ENABLE,LOW);
|
||||
return result;
|
||||
};
|
@ -1,8 +1,97 @@
|
||||
/**
|
||||
* @file LightDetection.h
|
||||
* @author Hans Haupt (hans.haupt@dezibot.de)
|
||||
* @brief Class for Reading the values of the different Phototransistors, both IR, and DaylightSensors are supported.
|
||||
* @version 0.1
|
||||
* @date 2024-04-26
|
||||
*
|
||||
* @copyright Copyright (c) 2024
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef LightDetection_h
|
||||
#define LightDetection_h
|
||||
#include <stdint.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
|
||||
|
||||
enum photoTransistors{
|
||||
IR_LEFT,
|
||||
IR_RIGHT,
|
||||
IR_FRONT,
|
||||
IR_BACK,
|
||||
DL_FRONT,
|
||||
DL_BOTTOM
|
||||
};
|
||||
|
||||
struct averageMeasurement {
|
||||
photoTransistors sensor;
|
||||
uint32_t measurementAmount;
|
||||
uint32_t timeBetween;
|
||||
uint16_t result;
|
||||
bool done;
|
||||
};
|
||||
|
||||
enum ptType{
|
||||
IR,
|
||||
DAYLIGHT
|
||||
};
|
||||
static const photoTransistors allIRPTs[] = {IR_FRONT,IR_LEFT,IR_RIGHT,IR_BACK};
|
||||
static const photoTransistors allDLPTs[] = {DL_BOTTOM,DL_FRONT};
|
||||
|
||||
|
||||
//beinhaltet IR + Tageslicht
|
||||
class LightDetection{
|
||||
public:
|
||||
/**
|
||||
* @brief initialize the Lightdetection Compnent, must be called before the other methods are used.
|
||||
*
|
||||
*/
|
||||
static void begin(void);
|
||||
|
||||
/**
|
||||
* @brief reads the Value of the specified sensor
|
||||
*
|
||||
* @param sensor which sensor to read
|
||||
* @return uint the reading of the sensor. between 0-4095
|
||||
*/
|
||||
static uint16_t getValue(photoTransistors sensor);
|
||||
|
||||
/**
|
||||
* @brief can be used to determine which sensor is exposed to the greatest amount of light
|
||||
* Can distingish between IR and Daylight
|
||||
*
|
||||
* @param type select which PTTransistors to compare
|
||||
* @return photoTransistors which sensor is exposed to the greatest amount of light
|
||||
*/
|
||||
static photoTransistors getBrightest(ptType type);
|
||||
|
||||
/**
|
||||
* @brief Get the Average of multiple measurments of a single PT
|
||||
*
|
||||
* @param sensor Which Phototransistor should be read
|
||||
* @param measurments how many measurements should be taken
|
||||
* @param timeBetween which time should elapse between
|
||||
* @return the average of all taken meaurments
|
||||
*/
|
||||
static uint32_t getAverageValue(photoTransistors sensor, uint32_t measurments, uint32_t timeBetween);
|
||||
protected:
|
||||
static const uint8_t IR_PT_FRONT_ADC = 3;
|
||||
static const uint8_t IR_PT_LEFT_ADC = 4;
|
||||
static const uint8_t IR_PT_RIGHT_ADC = 5;
|
||||
static const uint8_t IR_PT_BACK_ADC = 6;
|
||||
|
||||
static const uint8_t DL_PT_FRONT_ADC = 7;
|
||||
static const uint8_t DL_PT_BOTTOM_ADC = 8;
|
||||
|
||||
static const uint8_t DL_PT_ENABLE = 41;
|
||||
static const uint8_t IR_PT_ENABLE = 40;
|
||||
|
||||
|
||||
static void beginInfrared(void);
|
||||
static void beginDaylight(void);
|
||||
static uint16_t readIRPT(photoTransistors sensor);
|
||||
static uint16_t readDLPT(photoTransistors sensor);
|
||||
|
||||
};
|
||||
#endif //LightDetection_h
|
Loading…
x
Reference in New Issue
Block a user