Dezibot 4
Loading...
Searching...
No Matches
LightDetection.cpp
Go to the documentation of this file.
1#include "LightDetection.h"
2#include <limits.h>
3
8
10 switch(sensor){
11 //Fall Through intended
12 case IR_FRONT:
13 case IR_LEFT:
14 case IR_RIGHT:
15 case IR_BACK:
16 return readIRPT(sensor);
17 case DL_BOTTOM:
18 case DL_FRONT:
19 return readDLPT(sensor);
20 default:
21 //currently not reachable, just if enum will be extended in the future
22 return UINT16_MAX;
23 }
24};
25
27 photoTransistors maxSensor;
28 uint16_t maxReading = 0;
29 uint16_t currentReading = 0;
30
31 if (type == IR){
32 maxSensor = IR_FRONT;
33 for(const auto pt : allIRPTs){
34 currentReading = LightDetection::getValue(pt);
35 if (currentReading > maxReading){
36 maxReading = currentReading;
37 maxSensor = pt;
38 }
39 }
40 } else {
41 maxSensor = DL_FRONT;
42 for(const auto pt : allDLPTs){
43 currentReading = LightDetection::getValue(pt);
44 if (currentReading > maxReading){
45 maxReading = currentReading;
46 maxSensor = pt;
47 }
48 }
49 }
50
51 return maxSensor;
52};
53
54uint32_t LightDetection::getAverageValue(photoTransistors sensor, uint32_t measurments, uint32_t timeBetween){
55
56 TickType_t xLastWakeTime = xTaskGetTickCount();
57 TickType_t frequency = timeBetween / portTICK_PERIOD_MS;
58 uint64_t cumulatedResult = 0;
59 for(int i = 0; i < measurments; i++){
60 cumulatedResult += LightDetection::getValue(sensor);
61 xTaskDelayUntil(&xLastWakeTime,frequency);
62 }
63 return cumulatedResult/measurments;
64};
65
67 digitalWrite(IR_PT_ENABLE,true);
68 pinMode(IR_PT_ENABLE, OUTPUT);
69 pinMode(IR_PT_FRONT_ADC, INPUT);
70 pinMode(IR_PT_LEFT_ADC, INPUT);
71 pinMode(IR_PT_RIGHT_ADC, INPUT);
72 pinMode(IR_PT_BACK_ADC, INPUT);
73};
74
76 digitalWrite(DL_PT_ENABLE,true);
77 pinMode(DL_PT_ENABLE, OUTPUT);
78 pinMode(DL_PT_BOTTOM_ADC, INPUT);
79 pinMode(DL_PT_FRONT_ADC, INPUT );
80};
81
83 //digitalWrite(IR_PT_ENABLE,HIGH);
84 uint16_t result = 0;
85 switch (sensor)
86 {
87 case IR_FRONT:
88 result = analogRead(IR_PT_FRONT_ADC);
89 break;
90 case IR_LEFT:
91 result = analogRead(IR_PT_LEFT_ADC);
92 break;
93 case IR_RIGHT:
94 result = analogRead(IR_PT_RIGHT_ADC);
95 break;
96 case IR_BACK:
97 result = analogRead(IR_PT_BACK_ADC);
98 break;
99 default:
100 break;
101 }
102 //digitalWrite(IR_PT_ENABLE,LOW);
103 return result;
104};
105
107 digitalWrite(DL_PT_ENABLE,HIGH);
108 uint16_t result = 0;
109 switch (sensor)
110 {
111 case DL_FRONT:
112 result = analogRead(DL_PT_FRONT_ADC);
113 break;
114 case DL_BOTTOM:
115 result = analogRead(DL_PT_BOTTOM_ADC);
116 break;
117 default:
118 break;
119 }
120 digitalWrite(DL_PT_ENABLE,LOW);
121 return result;
122};
Class for Reading the values of the different Phototransistors, both IR, and DaylightSensors are supp...
photoTransistors
@ IR_LEFT
@ DL_BOTTOM
@ IR_RIGHT
@ IR_FRONT
@ DL_FRONT
@ IR_BACK
ptType
@ IR
static const uint8_t IR_PT_ENABLE
static void beginDaylight(void)
static void beginInfrared(void)
static const uint8_t DL_PT_FRONT_ADC
static uint16_t readIRPT(photoTransistors sensor)
static uint16_t getValue(photoTransistors sensor)
reads the Value of the specified sensor
static const uint8_t DL_PT_ENABLE
static const uint8_t DL_PT_BOTTOM_ADC
static uint16_t readDLPT(photoTransistors sensor)
static uint32_t getAverageValue(photoTransistors sensor, uint32_t measurments, uint32_t timeBetween)
Get the Average of multiple measurments of a single PT.
static const uint8_t IR_PT_BACK_ADC
static photoTransistors getBrightest(ptType type)
can be used to determine which sensor is exposed to the greatest amount of light Can distingish betwe...
static const uint8_t IR_PT_LEFT_ADC
static const uint8_t IR_PT_FRONT_ADC
static const uint8_t IR_PT_RIGHT_ADC
static void begin(void)
initialize the Lightdetection Compnent, must be called before the other methods are used.