-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDriveTankMT.java
119 lines (89 loc) · 3.47 KB
/
DriveTankMT.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
// simple teleop program that drives bot using controller joysticks in tank mode.
// this code monitors the period and stops when the period is ended.
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.util.Range;
@TeleOp(name="Drive Tank Multi-Thread", group="Exercises")
//@Disabled
public class DriveTankMT extends LinearOpMode
{
DcMotor leftMotor, rightMotor;
float leftY, rightY;
public DriveTankMT() throws Exception
{
Logging.setup();
Logging.log("Starting DriveTankMT");
}
// called when init button is pressed.
@Override
public void runOpMode() throws InterruptedException
{
leftMotor = hardwareMap.dcMotor.get("left_motor");
rightMotor = hardwareMap.dcMotor.get("right_motor");
leftMotor.setDirection(DcMotor.Direction.REVERSE);
// create an instance of the DriveThread.
Thread driveThread = new DriveThread();
telemetry.addData("Mode", "waiting");
telemetry.update();
// wait for start button.
Logging.log("wait for start");
waitForStart();
Logging.log("started");
// start the driving thread.
driveThread.start();
// continue with main thread.
try
{
while (opModeIsActive())
{
telemetry.addData("Mode", "running");
telemetry.addData("Run Time", this.getRuntime());
telemetry.addData("Buttons", "x1=" + gamepad1.x);
telemetry.addData("sticks", " left=" + leftY + " right=" + rightY);
telemetry.update();
idle();
}
}
catch(Exception e) {Logging.log(e.getMessage());}
Logging.log("out of while loop");
// stop the driving thread.
driveThread.interrupt();
Logging.log("end");
}
private class DriveThread extends Thread
{
public DriveThread()
{
this.setName("DriveThread");
Logging.log("%s", this.getName());
}
// called when tread.start is called. thread stays in loop to do what it does until exit is
// signaled by main code calling thread.interrupt.
@Override
public void run()
{
Logging.log("Starting thread %s",this.getName());
try
{
while (!isInterrupted())
{
// we record the Y values in the main class to make showing them in telemetry
// easier.
leftY = gamepad1.left_stick_y * -1;
rightY = gamepad1.right_stick_y * -1;
leftMotor.setPower(Range.clip(leftY, -1.0, 1.0));
rightMotor.setPower(Range.clip(rightY, -1.0, 1.0));
idle();
}
}
// interrupted means time to shutdown. note we can stop by detecting isInterrupted = true
// or by the interrupted exception thrown from the sleep function.
catch (InterruptedException e) {Logging.log("%s interrupted", this.getName());}
// an error occurred in the run loop.
catch (Exception e) {e.printStackTrace(Logging.logPrintStream);}
Logging.log("end of thread %s", this.getName());
}
}
}