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
aeffe10a
Commit
aeffe10a
authored
1 year ago
by
Daniel Ye
Browse files
Options
Downloads
Patches
Plain Diff
prototype testing code with IR sensors
parent
55b21026
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/settings.h
+28
-0
28 additions, 0 deletions
include/settings.h
src/main.cpp
+76
-36
76 additions, 36 deletions
src/main.cpp
with
104 additions
and
36 deletions
include/settings.h
0 → 100644
+
28
−
0
View file @
aeffe10a
// From top to bottom of motor driver (bottom is big capacitor)
#define MOTOR_LEFT_PIN_2 D5
#define MOTOR_LEFT_PIN_1 D6
#define MOTOR_RIGHT_PIN_2 D9
#define MOTOR_RIGHT_PIN_1 D10
#define IR_SENSOR_LEFT_PIN A0
#define IR_SENSOR_RIGHT_PIN A1
#define DEBOUNCE_TIME_MS 50
#define LEFT_MOTOR_SPEED 50
#define RIGHT_MOTOR_SPEED 50
#define LEFT_IR_THRESHOLD 500
#define RIGHT_IR_THRESHOLD 500
// mode 0(default) - break, 1 - coast
void
motorLeftForward
(
int
speed
,
bool
mode
=
0
);
void
motorLeftBackwards
(
int
speed
,
bool
mode
=
0
);
void
motorRightForward
(
int
speed
,
bool
mode
=
0
);
void
motorRightBackwards
(
int
speed
,
bool
mode
=
0
);
typedef
enum
{
MODE_BREAK
,
MODE_COAST
}
MotorMode
;
This diff is collapsed.
Click to expand it.
src/main.cpp
+
76
−
36
View file @
aeffe10a
#include
<Arduino.h>
#include
<Wire.h>
#include
"settings.h"
#include
"Adafruit_TCS34725.h"
#define MOTOR_PIN_1 D6
#define MOTOR_PIN_2 D5
#define DEBOUNCE_TIME_MS 50
// mode 0(default) - break, 1 - coast
void
motorForward
(
int
speed
,
bool
mode
=
0
);
void
motorBackwards
(
int
speed
,
bool
mode
=
0
);
// Color sensor
// Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
bool
forward
=
false
;
bool
prev_button_state
=
true
;
// nominal high
int
prev_button_press_time
=
0
;
...
...
@@ -20,14 +14,18 @@ int prev_button_press_time = 0;
void
setup
()
{
Serial
.
begin
(
115200
);
pinMode
(
A0
,
INPUT_ANALOG
);
pinMode
(
MOTOR_PIN_1
,
OUTPUT
);
pinMode
(
MOTOR_PIN_2
,
OUTPUT
);
pinMode
(
IR_SENSOR_LEFT_PIN
,
INPUT_ANALOG
);
pinMode
(
IR_SENSOR_RIGHT_PIN
,
INPUT_ANALOG
);
pinMode
(
MOTOR_LEFT_PIN_1
,
OUTPUT
);
pinMode
(
MOTOR_LEFT_PIN_2
,
OUTPUT
);
pinMode
(
MOTOR_RIGHT_PIN_1
,
OUTPUT
);
pinMode
(
MOTOR_RIGHT_PIN_2
,
OUTPUT
);
pinMode
(
USER_BTN
,
INPUT
);
// Color sensor setup
// if (tcs.begin()) {
//
//
Serial.println("Found sensor");
// Serial.println("Found sensor");
// } else {
// Serial.println("No TCS34725 found ... check your connections");
// while (1); // halt!
...
...
@@ -46,14 +44,41 @@ void loop() {
}
prev_button_state
=
current_button_state
;
int
motor_speed
=
map
(
analogRead
(
A0
),
0
,
1023
,
0
,
255
);
// uint16_t ir_left_value = analogRead(IR_SENSOR_LEFT_PIN);
// uint16_t ir_right_value = analogRead(IR_SENSOR_RIGHT_PIN);
// Serial.print(ir_left_value);
// Serial.print(" ");
// Serial.println(ir_right_value);
// For testing motor
// int motor_speed = map(ir_left_value, 0, 1023, 0, 255);
// Serial.println(motor_speed);
if
(
forward
)
{
motorForward
(
motor_speed
);
// Enable line following
/*
if (ir_left_value < LEFT_IR_THRESHOLD) {
motorLeftForward(LEFT_MOTOR_SPEED);
} else {
motor
Backwards
(
motor_speed
);
motor
LeftForward(0
);
}
if (ir_right_value < RIGHT_IR_THRESHOLD) {
motorRightForward(RIGHT_MOTOR_SPEED);
} else {
motorRightForward(0);
}
*/
// For testing motor
// if (forward) {
// motorLeftForward(motor_speed);
// motorRightForward(motor_speed);
// } else {
// motorLeftBackwards(motor_speed);
// motorRightBackwards(motor_speed);
// }
// Color sensor code
// float red, green, blue;
// tcs.setInterrupt(false); // turn on LED
...
...
@@ -64,40 +89,55 @@ void loop() {
// Serial.print("R:"); Serial.print(int(red));
// Serial.print("\tG:"); Serial.print(int(green));
// Serial.print("\tB:"); Serial.print(int(blue));
// Serial.print("\n");
}
void
motorForward
(
int
speed
,
bool
mode
)
{
// Left motor
void
motorLeftForward
(
int
speed
,
bool
mode
)
{
if
(
!
mode
)
{
// Break
analogWrite
(
MOTOR_PIN_2
,
255
);
analogWrite
(
MOTOR_PIN_1
,
speed
);
analogWrite
(
MOTOR_
LEFT_
PIN_2
,
255
);
analogWrite
(
MOTOR_
LEFT_
PIN_1
,
speed
);
}
else
{
// Coast
analogWrite
(
MOTOR_PIN_2
,
0
);
analogWrite
(
MOTOR_PIN_1
,
255
-
speed
);
analogWrite
(
MOTOR_LEFT_PIN_2
,
0
);
analogWrite
(
MOTOR_LEFT_PIN_1
,
255
-
speed
);
}
}
void
motorLeftBackwards
(
int
speed
,
bool
mode
)
{
if
(
!
mode
)
{
// Break
analogWrite
(
MOTOR_LEFT_PIN_1
,
255
);
analogWrite
(
MOTOR_LEFT_PIN_2
,
speed
);
}
else
{
// Coast
analogWrite
(
MOTOR_LEFT_PIN_1
,
0
);
analogWrite
(
MOTOR_LEFT_PIN_2
,
255
-
speed
);
}
Serial
.
print
(
"Forward: "
);
Serial
.
print
(
speed
);
Serial
.
print
(
" "
);
Serial
.
println
(
mode
);
}
void
motorBackwards
(
int
speed
,
bool
mode
)
{
// Right motor
void
motorRightForward
(
int
speed
,
bool
mode
)
{
if
(
!
mode
)
{
// Break
analogWrite
(
MOTOR_PIN_
1
,
255
);
analogWrite
(
MOTOR_PIN_
2
,
speed
);
analogWrite
(
MOTOR_
RIGHT_
PIN_
2
,
255
);
analogWrite
(
MOTOR_
RIGHT_
PIN_
1
,
speed
);
}
else
{
// Coast
analogWrite
(
MOTOR_PIN_
1
,
0
);
analogWrite
(
MOTOR_PIN_
2
,
255
-
speed
);
analogWrite
(
MOTOR_
RIGHT_
PIN_
2
,
0
);
analogWrite
(
MOTOR_
RIGHT_
PIN_
1
,
255
-
speed
);
}
}
Serial
.
print
(
"Backwards: "
);
Serial
.
print
(
speed
);
Serial
.
print
(
" "
);
Serial
.
println
(
mode
);
void
motorRightBackwards
(
int
speed
,
bool
mode
)
{
if
(
!
mode
)
{
// Break
analogWrite
(
MOTOR_RIGHT_PIN_1
,
255
);
analogWrite
(
MOTOR_RIGHT_PIN_2
,
speed
);
}
else
{
// Coast
analogWrite
(
MOTOR_RIGHT_PIN_1
,
0
);
analogWrite
(
MOTOR_RIGHT_PIN_2
,
255
-
speed
);
}
}
\ No newline at end of file
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