Dezibot 4
LightDetection.cpp
Go to the documentation of this file.
1 #include "LightDetection.h"
2 #include <limits.h>
3 
7 };
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 
54 uint32_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 };
IR_RIGHT
@ IR_RIGHT
Definition: LightDetection.h:21
LightDetection::IR_PT_BACK_ADC
static const uint8_t IR_PT_BACK_ADC
Definition: LightDetection.h:82
LightDetection::begin
static void begin(void)
initialize the Lightdetection Compnent, must be called before the other methods are used.
Definition: LightDetection.cpp:4
LightDetection::IR_PT_LEFT_ADC
static const uint8_t IR_PT_LEFT_ADC
Definition: LightDetection.h:80
LightDetection::beginDaylight
static void beginDaylight(void)
Definition: LightDetection.cpp:75
LightDetection::DL_PT_BOTTOM_ADC
static const uint8_t DL_PT_BOTTOM_ADC
Definition: LightDetection.h:85
LightDetection::getValue
static uint16_t getValue(photoTransistors sensor)
reads the Value of the specified sensor
Definition: LightDetection.cpp:9
IR
@ IR
Definition: LightDetection.h:37
LightDetection::DL_PT_FRONT_ADC
static const uint8_t DL_PT_FRONT_ADC
Definition: LightDetection.h:84
IR_LEFT
@ IR_LEFT
Definition: LightDetection.h:20
IR_BACK
@ IR_BACK
Definition: LightDetection.h:23
IR_FRONT
@ IR_FRONT
Definition: LightDetection.h:22
LightDetection::IR_PT_RIGHT_ADC
static const uint8_t IR_PT_RIGHT_ADC
Definition: LightDetection.h:81
LightDetection::IR_PT_FRONT_ADC
static const uint8_t IR_PT_FRONT_ADC
Definition: LightDetection.h:79
ptType
ptType
Definition: LightDetection.h:36
DL_FRONT
@ DL_FRONT
Definition: LightDetection.h:24
LightDetection::beginInfrared
static void beginInfrared(void)
Definition: LightDetection.cpp:66
photoTransistors
photoTransistors
Definition: LightDetection.h:19
LightDetection::getBrightest
static photoTransistors getBrightest(ptType type)
can be used to determine which sensor is exposed to the greatest amount of light Can distingish betwe...
Definition: LightDetection.cpp:26
LightDetection::readIRPT
static uint16_t readIRPT(photoTransistors sensor)
Definition: LightDetection.cpp:82
LightDetection.h
Class for Reading the values of the different Phototransistors, both IR, and DaylightSensors are supp...
LightDetection::IR_PT_ENABLE
static const uint8_t IR_PT_ENABLE
Definition: LightDetection.h:88
DL_BOTTOM
@ DL_BOTTOM
Definition: LightDetection.h:25
LightDetection::getAverageValue
static uint32_t getAverageValue(photoTransistors sensor, uint32_t measurments, uint32_t timeBetween)
Get the Average of multiple measurments of a single PT.
Definition: LightDetection.cpp:54
LightDetection::readDLPT
static uint16_t readDLPT(photoTransistors sensor)
Definition: LightDetection.cpp:106
LightDetection::DL_PT_ENABLE
static const uint8_t DL_PT_ENABLE
Definition: LightDetection.h:87