implemented basic ligthdetection

This commit is contained in:
hhaupt 2024-04-27 23:05:03 +02:00
parent 1d074f26d1
commit 96a7efdb6b
5 changed files with 147 additions and 35 deletions

View File

@ -135,6 +135,7 @@ For instance, in `src/Dezibot.h`, to include `src/motion/Motion.h`, you should w
* Upload Mode: "UART0 / Hardware CDC" * Upload Mode: "UART0 / Hardware CDC"
* USB Mode: "Hardware CDC and JTAG" * USB Mode: "Hardware CDC and JTAG"
* Programmer: "Esptool" * Programmer: "Esptool"
* USB CDC on Boot: Enabled
#### Display #### Display

View File

@ -1,18 +1,15 @@
#include <Adafruit_NeoPixel.h>
#include <Dezibot.h> #include <Dezibot.h>
#define GPIO_LED 48
Dezibot dezibot = Dezibot(); Dezibot dezibot = Dezibot();
void setup() { void setup() {
dezibot.begin(); Serial.begin(115200);
Serial.begin(9600); Serial.println("Started");
dezibot.begin();
Serial.println("Inited");
} }
void loop() { void loop() {
Serial.println("FooBarBaz");
Serial.println(dezibot.lightDetection.getValue(DL_FRONT)); Serial.println(dezibot.lightDetection.getValue(DL_FRONT));
delay(1000); delay(1000);
} }

View File

@ -4,26 +4,6 @@
#include "Dezibot.h" #include "Dezibot.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define GPIO_LED 48
void Dezibot::begin(void) { void Dezibot::begin(void) {
lightDetection.begin(); lightDetection.begin();

View 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;
};

View File

@ -14,6 +14,8 @@
#include <stdint.h> #include <stdint.h>
#include <Arduino.h> #include <Arduino.h>
enum photoTransistors{ enum photoTransistors{
IR_LEFT, IR_LEFT,
IR_RIGHT, IR_RIGHT,
@ -23,10 +25,21 @@ enum photoTransistors{
DL_BOTTOM DL_BOTTOM
}; };
struct averageMeasurement {
photoTransistors sensor;
uint32_t measurementAmount;
uint32_t timeBetween;
uint16_t result;
bool done;
};
enum ptType{ enum ptType{
IR, IR,
DAYLIGHT DAYLIGHT
}; };
static const photoTransistors allIRPTs[] = {IR_FRONT,IR_LEFT,IR_RIGHT,IR_BACK};
static const photoTransistors allDLPTs[] = {DL_BOTTOM,DL_FRONT};
class LightDetection{ class LightDetection{
public: public:
@ -34,7 +47,7 @@ public:
* @brief initialize the Lightdetection Compnent, must be called before the other methods are used. * @brief initialize the Lightdetection Compnent, must be called before the other methods are used.
* *
*/ */
void begin(void); static void begin(void);
/** /**
* @brief reads the Value of the specified sensor * @brief reads the Value of the specified sensor
@ -42,7 +55,7 @@ public:
* @param sensor which sensor to read * @param sensor which sensor to read
* @return uint the reading of the sensor. between 0-4095 * @return uint the reading of the sensor. between 0-4095
*/ */
uint16_t getValue(photoTransistors sensor); static uint16_t getValue(photoTransistors sensor);
/** /**
* @brief can be used to determine which sensor is exposed to the greatest amount of light * @brief can be used to determine which sensor is exposed to the greatest amount of light
@ -51,7 +64,7 @@ public:
* @param type select which PTTransistors to compare * @param type select which PTTransistors to compare
* @return photoTransistors which sensor is exposed to the greatest amount of light * @return photoTransistors which sensor is exposed to the greatest amount of light
*/ */
photoTransistors getBrightest(ptType type); static photoTransistors getBrightest(ptType type);
/** /**
* @brief Get the Average of multiple measurments of a single PT * @brief Get the Average of multiple measurments of a single PT
@ -61,7 +74,7 @@ public:
* @param timeBetween which time should elapse between * @param timeBetween which time should elapse between
* @return the average of all taken meaurments * @return the average of all taken meaurments
*/ */
uint32_t getAverageValue(photoTransistors sensor, uint32_t measurments, uint32_t timeBetween); static uint32_t getAverageValue(photoTransistors sensor, uint32_t measurments, uint32_t timeBetween);
protected: protected:
static const uint8_t IR_PT_FRONT_ADC = 3; static const uint8_t IR_PT_FRONT_ADC = 3;
static const uint8_t IR_PT_LEFT_ADC = 4; static const uint8_t IR_PT_LEFT_ADC = 4;
@ -73,10 +86,17 @@ protected:
static const uint8_t DL_PT_ENABLE = 41; static const uint8_t DL_PT_ENABLE = 41;
static const uint8_t IR_PT_ENABLE = 40; static const uint8_t IR_PT_ENABLE = 40;
void beginInfrared(void);
void beginDaylight(void);
uint16_t readIRPT(photoTransistors sensor); static void beginInfrared(void);
uint16_t readDLPT(photoTransistors sensor); static void beginDaylight(void);
static uint16_t readIRPT(photoTransistors sensor);
static uint16_t readDLPT(photoTransistors sensor);
static void averageMeasurmentTask(void * args);
}; };