forked from WPIRoboticsEngineering/RBE2002_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNavigation.cpp
177 lines (167 loc) · 5.32 KB
/
Navigation.cpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* NavigationRoutine.cpp
*
* Created on: Oct 22, 2020
* Author: gentov
*/
#include "Navigation.h"
Navigation::Navigation(DrivingChassis* robotChassis){
chassis = robotChassis;
}
/*
* @enum NavigationStates
* States used in the navigation state machine, not the main robot state machine
*/
void Navigation::setNavGoal(int row, int col){
goalRow = row;
goalCol = col;
}
NavigationStates Navigation::checkNavStatus(){
switch(navState){
case INITIALIZE_NAVIGATION:
//Serial.println("INIT NAV");
if((chassis->myChassisPose.currentColumn == goalCol) && (chassis->myChassisPose.currentRow == goalRow)){
navState = FINISHED_NAVIGATION;
}
// are we in the outer lane?
else if(chassis->myChassisPose.currentColumn == 0){
navState = TURN_TOWARDS_CORRECT_ROW;
}
else{
navState = TURN_TOWARDS_CORRECT_COLUMN;
}
break;
case TURN_TOWARDS_CORRECT_COLUMN:
//Serial.println("TURNING TOWARDS COLUMN");
// determine if we are in the correct row or not
if(chassis->myChassisPose.currentRow != goalRow){
// if our current row isn't our goal row, then we need to navigate to the outer edge
// first. We check to see if we are facing the correct row
if(chassis->myChassisPose.getOrientationToClosest90() != 90){
// in our simplified world, there is only one outer edge, and its on the left side.
// In the future, if there are more out edges, this would need to determine which edge is closest
// and turn towards it.
chassis->turnToHeading(90, 7500);
navStateAfterMotionSetpointReached = FINDING_OUTER_EDGE;
navState = WAIT_FOR_MOTION_SETPOINT_REACHED_NAVIGATION;
}
else{
navState = FINDING_OUTER_EDGE;
}
}
// if we're in the correct row, we need to find the column
else{
// check our orientation first in case we don't need to turn
if(chassis->myChassisPose.currentColumn > goalCol){
if(chassis->myChassisPose.getOrientationToClosest90() != -90){
chassis->turnToHeading(-90, 7500);
navStateAfterMotionSetpointReached = FINDING_COLUMN;
navState = WAIT_FOR_MOTION_SETPOINT_REACHED_NAVIGATION;
}
else{
navState = FINDING_COLUMN;
}
}
else if(chassis->myChassisPose.currentColumn < goalCol){
if(chassis->myChassisPose.getOrientationToClosest90() != 90){
chassis->turnToHeading(90, 7500);
navStateAfterMotionSetpointReached = FINDING_COLUMN;
navState = WAIT_FOR_MOTION_SETPOINT_REACHED_NAVIGATION;
}
else{
navState = FINDING_COLUMN;
}
}
// we're at the right column and row
else{
navState = FINISHED_NAVIGATION;
}
}
break;
case FINDING_OUTER_EDGE:
//Serial.println("FINDING COL: 0, CURRENT COL: " + String(chassis->myChassisPose.currentColumn));
// navigate until column == 0
if(chassis->myChassisPose.currentColumn != 0){
chassis->lineFollowForwards();
}
else{
chassis->stop();
navState = TURN_TOWARDS_CORRECT_ROW;
}
break;
case FINDING_ROW:
//Serial.println("FINDING ROW: " + String(goalRow) + "CURRENT ROW: " + String(chassis->myChassisPose.currentRow));
if(chassis->myChassisPose.currentRow != goalRow){
chassis->lineFollowForwards();
}
else{
chassis->stop();
// if we're at the right column, stop
if(chassis->myChassisPose.currentColumn == goalCol){
navState = FINISHED_NAVIGATION;
}
//otherwise turn towards the right column
else if (goalCol != 0){
navState = TURN_TOWARDS_CORRECT_COLUMN;
}
}
break;
case TURN_TOWARDS_CORRECT_ROW:
//Serial.println("TURNING TOWARDS ROW");
//If we're not in the right row, we need to find the right row
if(chassis->myChassisPose.currentRow > goalRow){
if(chassis->myChassisPose.getOrientationToClosest90() != 180){
chassis->turnToHeading(180, 7500);
navStateAfterMotionSetpointReached = FINDING_ROW;
navState = WAIT_FOR_MOTION_SETPOINT_REACHED_NAVIGATION;
}
else{
navState = FINDING_ROW;
}
}
else if(chassis->myChassisPose.currentRow < goalRow){
if(chassis->myChassisPose.getOrientationToClosest90() != 0){
chassis->turnToHeading(0, 7500);
navStateAfterMotionSetpointReached = FINDING_ROW;
navState = WAIT_FOR_MOTION_SETPOINT_REACHED_NAVIGATION;
}
else{
navState = FINDING_ROW;
}
}
// if we're at the right row
else{
navState = TURN_TOWARDS_CORRECT_COLUMN;;
}
break;
case FINDING_COLUMN:
//Serial.println("FINDING COL: " + String(goalCol) + "CURRENT COL: " + String(chassis->myChassisPose.currentColumn));
if(chassis->myChassisPose.currentColumn != goalCol){
chassis->lineFollowForwards();
}
else{
chassis->stop();
if(goalRow == chassis->myChassisPose.currentRow){
navState = FINISHED_NAVIGATION;
}
}
break;
case WAIT_FOR_MOTION_SETPOINT_REACHED_NAVIGATION:{
DrivingStatus motionStatus = chassis -> statusOfChassisDriving();
if(motionStatus == REACHED_SETPOINT){
navState = navStateAfterMotionSetpointReached;
}
else if(motionStatus == TIMED_OUT){
navState = TIMED_OUT_NAVIGATION;
}
}
break;
case FINISHED_NAVIGATION:
navState = INITIALIZE_NAVIGATION;
break;
case TIMED_OUT_NAVIGATION:
navState = INITIALIZE_NAVIGATION;
break;
}
return navState;
}