Skip to content
Snippets Groups Projects
Commit ca113ec4 authored by Daniel Qu's avatar Daniel Qu
Browse files

Address PR comments - move calibration_state to color module

parent 6e2bfef2
No related branches found
No related tags found
1 merge request!1Refactor color sensor code to separate file
......@@ -23,15 +23,14 @@ typedef enum
CALIBRATE_READY,
} CalibrationState;
extern CalibrationState calibration_state;
void setupColorSensor();
void printRGB(const Rgb &value);
void getColorValues(Rgb &left_value, Rgb &right_value);
void readColorValues(Rgb &left_value, Rgb &right_value);
float computeHue(const Rgb &rgb);
CalibrationState getCalibrationState();
void calibrateBlack();
void calibrateWhite();
......@@ -3,13 +3,13 @@
#include "Adafruit_TCS34725.h"
// Initiate color sensor instances
static Adafruit_TCS34725 color_sensor_left = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_24MS, TCS34725_GAIN_4X);
static Adafruit_TCS34725 color_sensor_right = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_24MS, TCS34725_GAIN_4X);
static Adafruit_TCS34725 color_sensor_left = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_24MS, TCS34725_GAIN_60X);
static Adafruit_TCS34725 color_sensor_right = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_24MS, TCS34725_GAIN_60X);
static TwoWire Wire2 = TwoWire(PC9, PA8); // I2C channel 3 for right sensor
// Change this to CALIBRATE_READY if no calibration is required
CalibrationState calibration_state = CALIBRATE_BLACK;
static CalibrationState calibration_state = CALIBRATE_BLACK;
static ColorSensorCal color_cal_left;
static ColorSensorCal color_cal_right;
......@@ -24,12 +24,12 @@ void printRGB(const Rgb &value)
Serial.print(int(value.b));
}
void getColorValues(Rgb &left_value, Rgb &right_value)
void readColorValues(Rgb &left_value, Rgb &right_value)
{
// Turn on LEDs
color_sensor_left.setInterrupt(false);
color_sensor_right.setInterrupt(false);
// Delay 50ms to read
// Delay 24ms to read
delay(24);
color_sensor_left.getRGB(&left_value.r, &left_value.g, &left_value.b);
color_sensor_right.getRGB(&right_value.r, &right_value.g, &right_value.b);
......@@ -108,12 +108,24 @@ void setupColorSensor()
}
};
CalibrationState getCalibrationState() {
return calibration_state;
}
void calibrateBlack()
{
getColorValues(color_cal_left.min, color_cal_right.max);
// Calibrate black
Serial.print("Calibrating black");
readColorValues(color_cal_left.min, color_cal_right.max);
// Transition to calibrate white
calibration_state = CALIBRATE_WHITE;
}
void calibrateWhite()
{
getColorValues(color_cal_left.max, color_cal_right.max);
// Calibrate white
Serial.print("Calibrating white");
readColorValues(color_cal_left.max, color_cal_right.max);
// Transition to ready state
calibration_state = CALIBRATE_READY;
}
\ No newline at end of file
......@@ -29,7 +29,7 @@ void loop()
// Color sensor code
Rgb left_color = {};
Rgb right_color = {};
getColorValues(left_color, right_color);
readColorValues(left_color, right_color);
float left_hue = computeHue(left_color);
float right_hue = computeHue(right_color);
......@@ -82,27 +82,16 @@ void checkButtonPress()
{
prev_button_press_time = millis();
Serial.print("Pressed: ");
if (calibration_state == CALIBRATE_READY)
{
enable_line_follow = !enable_line_follow;
}
// calibration code
if (calibration_state == CALIBRATE_BLACK)
{
// Calibrate black
Serial.print("Calibrating black");
calibrateBlack();
// Transition to calibrate white
calibration_state = CALIBRATE_WHITE;
}
else if (calibration_state == CALIBRATE_WHITE)
{
// Calibrate white
Serial.print("Calibrating white");
calibrateWhite();
// Transition to ready state
calibration_state = CALIBRATE_READY;
switch (getCalibrationState()) {
case CALIBRATE_READY:
enable_line_follow = !enable_line_follow;
break;
case CALIBRATE_BLACK:
calibrateBlack();
break;
case CALIBRATE_WHITE:
calibrateWhite();
break;
}
}
prev_button_state = current_button_state;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment