Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mte380_robot
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Daniel Ye
mte380_robot
Commits
ca113ec4
Commit
ca113ec4
authored
1 year ago
by
Daniel Qu
Browse files
Options
Downloads
Patches
Plain Diff
Address PR comments - move calibration_state to color module
parent
6e2bfef2
No related branches found
No related tags found
1 merge request
!1
Refactor color sensor code to separate file
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
include/color.h
+2
-3
2 additions, 3 deletions
include/color.h
src/color.cpp
+19
-7
19 additions, 7 deletions
src/color.cpp
src/main.cpp
+11
-22
11 additions, 22 deletions
src/main.cpp
with
32 additions
and
32 deletions
include/color.h
+
2
−
3
View file @
ca113ec4
...
...
@@ -23,15 +23,14 @@ typedef enum
CALIBRATE_READY
,
}
CalibrationState
;
extern
CalibrationState
calibration_state
;
void
setupColorSensor
();
void
printRGB
(
const
Rgb
&
value
);
void
get
ColorValues
(
Rgb
&
left_value
,
Rgb
&
right_value
);
void
read
ColorValues
(
Rgb
&
left_value
,
Rgb
&
right_value
);
float
computeHue
(
const
Rgb
&
rgb
);
CalibrationState
getCalibrationState
();
void
calibrateBlack
();
void
calibrateWhite
();
This diff is collapsed.
Click to expand it.
src/color.cpp
+
19
−
7
View file @
ca113ec4
...
...
@@ -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_
4
X
);
static
Adafruit_TCS34725
color_sensor_right
=
Adafruit_TCS34725
(
TCS34725_INTEGRATIONTIME_24MS
,
TCS34725_GAIN_
4
X
);
static
Adafruit_TCS34725
color_sensor_left
=
Adafruit_TCS34725
(
TCS34725_INTEGRATIONTIME_24MS
,
TCS34725_GAIN_
60
X
);
static
Adafruit_TCS34725
color_sensor_right
=
Adafruit_TCS34725
(
TCS34725_INTEGRATIONTIME_24MS
,
TCS34725_GAIN_
60
X
);
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
get
ColorValues
(
Rgb
&
left_value
,
Rgb
&
right_value
)
void
read
ColorValues
(
Rgb
&
left_value
,
Rgb
&
right_value
)
{
// Turn on LEDs
color_sensor_left
.
setInterrupt
(
false
);
color_sensor_right
.
setInterrupt
(
false
);
// Delay
50
ms to read
// Delay
24
ms 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
This diff is collapsed.
Click to expand it.
src/main.cpp
+
11
−
22
View file @
ca113ec4
...
...
@@ -29,7 +29,7 @@ void loop()
// Color sensor code
Rgb
left_color
=
{};
Rgb
right_color
=
{};
get
ColorValues
(
left_color
,
right_color
);
read
ColorValues
(
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
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment