-
Notifications
You must be signed in to change notification settings - Fork 1
/
CmdFollowLine.java
200 lines (178 loc) · 6.94 KB
/
CmdFollowLine.java
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright (c) 2018 Titan Robotics Club (http://www.titanrobotics.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package TrcFtcSamples;
import TrcCommonLib.trclib.TrcEvent;
import TrcCommonLib.trclib.TrcRobot;
import TrcCommonLib.trclib.TrcStateMachine;
import TrcCommonLib.trclib.TrcTimer;
/**
* This class implements an autonomous routine that drives forward to find the line. Then it will drive the robot
* following the line using PID controlled drive.
*/
public class CmdFollowLine implements TrcRobot.RobotCommand
{
private enum State
{
DO_DELAY,
FIND_LINE,
TURN_TO_LINE,
FOLLOW_LINE,
DONE
} //enum State
private static final String moduleName = "CmdFollowLine";
private final K9Robot robot;
private final double delay;
private final FtcAutoK9.Alliance alliance;
private final TrcEvent event;
private final TrcTimer timer;
private final TrcStateMachine<State> sm;
/**
* Constructor: Create an instance of the object.
*
* @param robot specifies the robot object for providing access to various global objects.
* @param delay specifies delay in seconds before PID drive starts. 0 means no delay.
* @param alliance specifies the alliance we are on.
*/
public CmdFollowLine(K9Robot robot, double delay, FtcAutoK9.Alliance alliance)
{
robot.globalTracer.traceInfo(moduleName, "delay=%.3f, alliance=%s", delay, alliance);
this.robot = robot;
this.delay = delay;
this.alliance = alliance;
event = new TrcEvent(moduleName);
timer = new TrcTimer(moduleName);
sm = new TrcStateMachine<>(moduleName);
sm.start(State.DO_DELAY);
} //CmdFollowLine
//
// Implements the TrcRobot.RobotCommand interface.
//
/**
* This method checks if the current RobotCommand is running.
*
* @return true if the command is running, false otherwise.
*/
public boolean isActive()
{
return sm.isEnabled();
} //isActive
/**
* This method cancels the command if it is active.
*/
@Override
public void cancel()
{
if (robot.pidDrive.isActive())
{
robot.pidDrive.cancel();
}
if (robot.colorPidLineFollow.isActive())
{
robot.colorPidLineFollow.cancel();
}
sm.stop();
} //cancel
/**
* This method must be called periodically by the caller to drive the command sequence forward.
*
* @param elapsedTime specifies the elapsed time in seconds since the start of the robot mode.
* @return true if the command sequence is completed, false otherwise.
*/
@Override
public boolean cmdPeriodic(double elapsedTime)
{
State state = sm.checkReadyAndGetState();
if (state == null)
{
robot.dashboard.displayPrintf(1, "State: disabled or waiting...");
}
else
{
robot.dashboard.displayPrintf(1, "State: %s", state);
switch (state)
{
case DO_DELAY:
//
// Do delay if any.
//
if (delay == 0.0)
{
sm.setState(State.FIND_LINE);
//
// Intentionally falling through to DO_PID_DRIVE.
//
}
else
{
timer.set(delay, event);
sm.waitForSingleEvent(event, State.FIND_LINE);
break;
}
case FIND_LINE:
//
// Go forward slowly for 3 ft to find the line.
// If line is detected, PID drive will be interrupted.
//
robot.setColorTriggerEnabled(true);
// Drive slowly, limit to half power.
robot.pidDrive.getYPidCtrl().setOutputLimit(0.5);
robot.pidDrive.setRelativeYTarget(36.0, event);
sm.waitForSingleEvent(event, State.TURN_TO_LINE);
break;
case TURN_TO_LINE:
//
// We have past the line slightly, so turn left or right 90 degree
// slowly to find the edge of the line. If the line is detected,
// PID turn will be interrupted.
//
robot.pidDrive.getTurnPidCtrl().setOutputLimit(0.5);
robot.pidDrive.setRelativeTurnTarget(
alliance == FtcAutoK9.Alliance.RED_ALLIANCE? -90.0: 90.0, event);
sm.waitForSingleEvent(event, State.FOLLOW_LINE);
break;
case FOLLOW_LINE:
//
// Slowly follow the line for 5 ft.
//
robot.setColorTriggerEnabled(false);
robot.colorPidLineFollow.getYPidCtrl().setOutputLimit(0.3);
robot.colorPidLineFollow.getTurnPidCtrl().setOutputLimit(0.3);
//
// Follow right edge if red alliance.
// Follow left edge if blue alliance.
//
robot.colorPidLineFollow.getTurnPidCtrl().setInverted(alliance == FtcAutoK9.Alliance.RED_ALLIANCE);
robot.colorPidLineFollow.setSensorTarget(0.0, 60.0, K9Robot.COLOR_LINE_EDGE_DEADBAND, event);
sm.waitForSingleEvent(event, State.DONE);
break;
case DONE:
default:
//
// We are done, restore everything.
//
sm.stop();
break;
}
}
return !sm.isEnabled();
} //cmdPeriodic
} //class CmdFollowLine