-
Notifications
You must be signed in to change notification settings - Fork 1
/
CmdDriveAndTurn.java
191 lines (168 loc) · 6.24 KB
/
CmdDriveAndTurn.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
/*
* 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 a specified distance and then turn a specified
* degrees using PID controlled drive.
*/
public class CmdDriveAndTurn implements TrcRobot.RobotCommand
{
private static final boolean debugYPid = false;
private static final boolean debugTurnPid = false;
private enum State
{
DO_DELAY,
DO_PID_DRIVE,
DO_PID_TURN,
DONE
} //enum State
private static final String moduleName = "CmdDriveAndTurn";
private final K9Robot robot;
private final double delay;
private final double yDistance;
private final double turnDegrees;
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 yDistance specifies the target distance in inches for the Y direction.
* @param turnDegrees specifies the degrees to turn.
*/
public CmdDriveAndTurn(K9Robot robot, double delay, double yDistance, double turnDegrees)
{
robot.globalTracer.traceInfo(moduleName,
"delay=%.3f, yDist=%.1f, degrees=%.1f", delay, yDistance, turnDegrees);
this.robot = robot;
this.delay = delay;
this.yDistance = yDistance;
this.turnDegrees = turnDegrees;
event = new TrcEvent(moduleName);
timer = new TrcTimer(moduleName);
sm = new TrcStateMachine<>(moduleName);
sm.start(State.DO_DELAY);
} //CmdDriveAndTurn
//
// 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();
}
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.DO_PID_DRIVE);
//
// Intentionally falling through to DO_PID_DRIVE.
//
}
else
{
timer.set(delay, event);
sm.waitForSingleEvent(event, State.DO_PID_DRIVE);
break;
}
case DO_PID_DRIVE:
//
// Drive the set distance and maintain the same heading.
//
robot.pidDrive.setRelativeYTarget(yDistance, event);
sm.waitForSingleEvent(event, State.DO_PID_TURN);
break;
case DO_PID_TURN:
//
// Update the robot target heading with the degrees to turn and turn to the new heading.
//
robot.pidDrive.setRelativeTurnTarget(turnDegrees, event);
sm.waitForSingleEvent(event, State.DONE);
break;
case DONE:
default:
//
// We are done, restore everything.
//
sm.stop();
break;
}
robot.traceStateInfo(elapsedTime, state.toString(), yDistance, turnDegrees);
}
if (robot.pidDrive.isActive() && (debugYPid || debugTurnPid))
{
if (debugYPid)
{
robot.pidDrive.getYPidCtrl().printPidInfo(robot.globalTracer);
}
if (debugTurnPid)
{
robot.pidDrive.getTurnPidCtrl().printPidInfo(robot.globalTracer);
}
}
return !sm.isEnabled();
} //cmdPeriodic
} //class CmdDriveAndTurn