-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmicrocontroller.ino
115 lines (103 loc) · 2.86 KB
/
microcontroller.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//////////////////// Control Systems ////////////////////
#include "NavigationSystem.h"
#include "MotionControlSystem.h"
//////////////////// Initialization ////////////////////
// macro-defined constants for pin values
#define PIN_ENABLE_A 2
#define PIN_ENABLE_B 3
#define PIN_INPUT_1 4
#define PIN_INPUT_2 5
#define PIN_INPUT_3 6
#define PIN_INPUT_4 7
#define TRIG_PIN 9
#define ECHO_PIN 10
// macro-defined constant for delay time (milliseconds) when changing movement mode
#define PING 250
// system instances
NavigationSystem navigationSystem;
MotionControlSystem motionControlSystem;
// initialization
void setup() {
// navigation system initialization
navigationSystem.setupSensorDist(TRIG_PIN, ECHO_PIN);
navigationSystem.setupSensorRefl((const uint8_t[]){A0, A1});
// motion control system initialization
motionControlSystem.setupMotors(PIN_ENABLE_A, PIN_ENABLE_B,
PIN_INPUT_1, PIN_INPUT_2, PIN_INPUT_3, PIN_INPUT_4);
motionControlSystem.scan(ROTATE_CLOCKWISE);
Serial.begin(9600);
}
//////////////////// Main Program ////////////////////
void loop()
{
bool opponentDetected = navigationSystem.getStatusDist();
bool *edgeExceeded = navigationSystem.getStatusRefl();
// both edges exceeding the ring
if (!edgeExceeded[0] && !edgeExceeded[1])
{
if (!opponentDetected)
{
if (motionControlSystem.isTriggered())
{
motionControlSystem.stop();
delay(PING * 0.8);
motionControlSystem.scan(ROTATE_CLOCKWISE);
}
}
else
{
motionControlSystem.dash();
delay(PING * 2);
}
}
// left edge exceeding the ring
else if (!edgeExceeded[0] && edgeExceeded[1])
{
if (!opponentDetected && motionControlSystem.isTriggered())
{
motionControlSystem.stop();
delay(PING);
motionControlSystem.scan(ROTATE_CLOCKWISE);
}
else
{
motionControlSystem.moveForwardCustom(ROTATE_MAX_SPEED, ROTATE_MAX_SPEED * 0.9);
delay(PING * 1.75);
}
}
// right edge exceeding the ring
else if (edgeExceeded[0] && !edgeExceeded[1])
{
if (!opponentDetected && motionControlSystem.isTriggered())
{
motionControlSystem.stop();
delay(PING);
motionControlSystem.scan(ROTATE_COUNTERCLOCKWISE);
}
else
{
motionControlSystem.moveForwardCustom(ROTATE_MAX_SPEED * 0.9, ROTATE_MAX_SPEED);
delay(PING * 1.75);
}
}
// no edge exceeded; perfectly within the ring
else
{
if (opponentDetected)
{
// since dash modifies state, store it in temp for determining delay time
bool temp = motionControlSystem.isTriggered();
if (!temp) motionControlSystem.dash();
delay(temp ? PING * 1.8 : PING * 2);
}
else
{
if (motionControlSystem.isTriggered())
{
motionControlSystem.stop();
delay(PING);
motionControlSystem.scan(ROTATE_COUNTERCLOCKWISE);
}
}
}
}