ESP Line Following Buggy
2nd Year Embedded System Project (Group 48 - 2023/24)
Loading...
Searching...
No Matches
sensor_array.cpp
Go to the documentation of this file.
1#include "mbed.h"
2
3#include "sensor_array.h"
4
5
6float SensorArray::read(AnalogIn sensor){return 0;}
7
8
9SensorArray::SensorArray(PinName sens0, PinName sens1, PinName sens2, PinName sens3, PinName sens4, PinName sens5,
10 PinName led0, PinName led1, PinName led2, PinName led3, PinName led4, PinName led5, int sample_count, float detect_range, float angle_coefficient):
11 sample_count_(sample_count),
12 detect_range_(detect_range),
13 angle_coeff(angle_coefficient),
14 led{led0, led1, led2, led3, led4, led5}, // Initialize led array
15 sens{sens0, sens1, sens2, sens3, sens4, sens5} // Initialize sens array
16 {
17 reset();
18 set_all_led_on(false);
19 };
20
21
23{
24 for (int i = 0; i < sizeof(sens_values) / sizeof(sens_values[0]); i++)
25 {
26 sens_values[i] = 0;
27 }
28 output = 0;
29 prev_output = 0;
30 filtered_output = 0;
31 prev_filtered_output = 0;
32}
33
34
36{
37 for (int i = 0; i < 6; i++)
38 {
39 led[i] = status;
40 }
41}
42
43
45{
46 float sample_total[6] = {0};
47
48 for (int i = 0; i < sample_count_; i++)
49 {
50 for (int j = 0; j < 6; j++)
51 {
52 sample_total[j] += sens[j].read();
53 }
54 }
55
56 float max_reading = 0.0;
57 float min_reading = 1.0;
58
59 for (int i = 0; i < 6; i++)
60 {
61 sens_values[i] = sample_total[i] / sample_count_;
62
63 float old_min = cali_min[i];
64 float old_max = cali_max[i];
65 float new_min = 0;
66 float new_max = 1;
67
68 // Calculate the normalized value
69 float normalized_value = (sens_values[i] - old_min) / (old_max - old_min);
70
71 // Map the normalized value to the new range
72 float new_value = new_min + normalized_value * (new_max - new_min);
73 sens_values[i] = new_value;
74
75 // clamping the output
76 if (sens_values[i] < 0)
77 {
78 sens_values[i] = 0;
79 }
80 else if (sens_values[i] > 1)
81 {
82 sens_values[i] = 1;
83 }
84
85 // Find min and Max Value
86 if (sens_values[i] > max_reading)
87 {
88 max_reading = sens_values[i];
89 }
90 if (sens_values[i] < min_reading)
91 {
92 min_reading = sens_values[i];
93 }
94 }
95
96 if (max_reading - min_reading <= detect_range_)
97 {
98 line_detected = false;
99 }
100 else
101 {
102 line_detected = true;
103 }
104
105 if (!line_detected)
106 {
107 if (prev_left_true)
108 {
109 output = angle_coeff * 10;
110 }
111 else
112 {
113 output = angle_coeff * -10;
114 }
115 }
116 else
117 {
118 output = angle_coeff * (sens_values[0] * coef[0] + sens_values[1] * coef[1] + sens_values[2] * coef[2] + sens_values[3] * coef[3] + sens_values[4] * coef[4] + sens_values[5] * coef[5]);
119
120 if (output > 0)
121 {
122 prev_left_true = true;
123 }
124 else
125 {
126 prev_left_true = false;
127 }
128 }
129
130 filtered_output = (prev_filtered_output * LP_a0) + (output * LP_b0) + (prev_output * LP_b1);
131 prev_filtered_output = filtered_output;
132 prev_output = output;
133}
134
135
137{
138 return line_detected;
139}
140
141
143{
144 if ((index < sizeof(sens_values) / sizeof(sens_values[0])) && (index >= 0))
145 {
146 return sens_values[index];
147 }
148 return -1;
149}
150
152{
153 return sens_values;
154}
155
157{
158 return output;
159}
160
162{
163 return filtered_output;
164}
165
167{
168 float sample_total[6] = {0};
169
170 for (int i = 0; i < 100; i++)
171 {
172 for (int j = 0; j < 6; j++)
173 {
174 sample_total[j] += sens[j].read();
175 }
176 }
177
178 for (int i = 0; i < 6; i++)
179 {
180 sens_values[i] = sample_total[i] / 100;
181 cali_min[i] = sens_values[i];
182 }
183}
184
186{
187 return cali_min;
188}
void calibrate_sensors(void)
SensorArray(PinName sens0, PinName sens1, PinName sens2, PinName sens3, PinName sens4, PinName sens5, PinName led0, PinName led1, PinName led2, PinName led3, PinName led4, PinName led5, int sample_count, float detect_range, float angle_coefficient)
Constructs a new SensorArray object.
float get_array_output(void)
Gets the output value of the sensor array.
float * get_sens_output_array(void)
Gets an array of sensor output values.
float get_filtered_output(void)
void update(void)
Updates the sensor array.
float get_sens_output(int index)
Gets the output value of a sensor at the specified index.
float * get_calibration_constants(void)
bool is_line_detected(void)
Checks if a line is detected (in the last update).
void set_all_led_on(bool status)
Sets the status of all LEDs.
void reset(void)
Resets the sensor array.
Sensor Array PCB interface class library