6float SensorArray::read(AnalogIn sensor){
return 0;}
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},
15 sens{sens0, sens1, sens2, sens3, sens4, sens5}
24 for (
int i = 0; i <
sizeof(sens_values) /
sizeof(sens_values[0]); i++)
31 prev_filtered_output = 0;
37 for (
int i = 0; i < 6; i++)
46 float sample_total[6] = {0};
48 for (
int i = 0; i < sample_count_; i++)
50 for (
int j = 0; j < 6; j++)
52 sample_total[j] += sens[j].read();
56 float max_reading = 0.0;
57 float min_reading = 1.0;
59 for (
int i = 0; i < 6; i++)
61 sens_values[i] = sample_total[i] / sample_count_;
63 float old_min = cali_min[i];
64 float old_max = cali_max[i];
69 float normalized_value = (sens_values[i] - old_min) / (old_max - old_min);
72 float new_value = new_min + normalized_value * (new_max - new_min);
73 sens_values[i] = new_value;
76 if (sens_values[i] < 0)
80 else if (sens_values[i] > 1)
86 if (sens_values[i] > max_reading)
88 max_reading = sens_values[i];
90 if (sens_values[i] < min_reading)
92 min_reading = sens_values[i];
96 if (max_reading - min_reading <= detect_range_)
98 line_detected =
false;
102 line_detected =
true;
109 output = angle_coeff * 10;
113 output = angle_coeff * -10;
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]);
122 prev_left_true =
true;
126 prev_left_true =
false;
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;
138 return line_detected;
144 if ((index <
sizeof(sens_values) /
sizeof(sens_values[0])) && (index >= 0))
146 return sens_values[index];
163 return filtered_output;
168 float sample_total[6] = {0};
170 for (
int i = 0; i < 100; i++)
172 for (
int j = 0; j < 6; j++)
174 sample_total[j] += sens[j].read();
178 for (
int i = 0; i < 6; i++)
180 sens_values[i] = sample_total[i] / 100;
181 cali_min[i] = sens_values[i];
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