-
Notifications
You must be signed in to change notification settings - Fork 1
/
CmdSeekIR.java
157 lines (138 loc) · 4.84 KB
/
CmdSeekIR.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
/*
* 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 the IR beacon using PID controlled drive.
*/
public class CmdSeekIR implements TrcRobot.RobotCommand
{
private enum State
{
DO_DELAY,
SEEK_IR,
DONE
} //enum State
private static final String moduleName = "CmdSeekIR";
private final K9Robot robot;
private final double delay;
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.
*/
public CmdSeekIR(K9Robot robot, double delay)
{
robot.globalTracer.traceInfo(moduleName, "delay=%.3f", delay);
this.robot = robot;
this.delay = delay;
event = new TrcEvent(moduleName);
timer = new TrcTimer(moduleName);
sm = new TrcStateMachine<>(moduleName);
sm.start(State.DO_DELAY);
} //CmdSeekIR
//
// 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.irPidDrive.isActive())
{
robot.irPidDrive.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.SEEK_IR);
//
// Intentionally falling through to DO_PID_DRIVE.
//
}
else
{
timer.set(delay, event);
sm.waitForSingleEvent(event, State.SEEK_IR);
break;
}
case SEEK_IR:
//
// Go towards IR beacon until IR strength reaches 0.8.
//
robot.irPidDrive.setSensorTarget(0.0, 0.8, 0.0, event);
sm.waitForSingleEvent(event, State.DONE);
break;
case DONE:
default:
//
// We are done, restore everything.
//
sm.stop();
break;
}
}
return !sm.isEnabled();
} //cmdPeriodic
} //class CmdSeekIR