-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteToOtherFileV3.java
More file actions
219 lines (172 loc) · 6.5 KB
/
WriteToOtherFileV3.java
File metadata and controls
219 lines (172 loc) · 6.5 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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.hardware.GyroSensor;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileOutputStream;
import android.content.Context;
import android.os.Environment;
import android.widget.Toast;
/**
* Created by sidharth on 10/10/16.
*/
@TeleOp(name="Writer Tele Op - v3", group="Linear Opmode")
public class WriteToOtherFileV3 extends LinearOpMode{
DcMotor frontRight;
DcMotor frontLeft;
DcMotor backRight;
DcMotor backLeft;
GyroSensor gyro;
File root = new File(Environment.getExternalStorageDirectory() + File.separator + "Auto", "Report Files");
String fileName = "file.txt";
//Instantiating all variables including the new text file root
public void runOpMode() throws InterruptedException {
myinit();
waitForStart();
while (gyro.isCalibrating()) {
Thread.sleep(50);
}
if (!root.exists())
{
root.mkdirs();
}
while (opModeIsActive()) {
float leftThrottle;
float rightThrottle;
boolean a;
boolean b;
boolean x;
boolean y;
//Getting gyro values and encoder values
int currentHeading = gyro.getHeading();
double leftPos = backLeft.getCurrentPosition();
double rightPos = frontRight.getCurrentPosition();
leftThrottle = -gamepad1.left_stick_y;
rightThrottle = -gamepad1.right_stick_y;
a = gamepad1.a;
b = gamepad1.b;
x = gamepad1.x;
y = gamepad1.y;
float right = rightThrottle;
float left = leftThrottle;
right = (float) scaleInput(right);
left = (float) scaleInput(left);
//Drive motors
if(left > 0 && right > 0 || left < 0 && right < 0){
frontLeft.setPower(left * 0.5);
backLeft.setPower(left * 0.5);
frontRight.setPower(-left * 0.5);
backRight.setPower(-left * 0.5);
}
else if(left > 0 && right < 0 || left < 0 && right > 0){
frontLeft.setPower(left * 0.5);
backLeft.setPower(left * 0.5);
frontRight.setPower(-right * 0.5);
backRight.setPower(-right * 0.5);
}
else if(left == 0 && right == 0){
frontLeft.setPower(0);
backLeft.setPower(0);
frontRight.setPower(0);
backRight.setPower(0);
}
if(a){
Thread.sleep(500);
writeToFile("leftThrottle-" + String.format("%.2f", leftPos));
writeToFile("rightThrottle-" + String.format("%.2f", -rightPos));
writeToFile("gyro-" + String.format("%d", currentHeading));
writeToFile("/");
reset_encoders();
revert_encoders();
//Writing to file, resetting and reverting encoders
//writeToFile("a");
}
if(y){
Thread.sleep(500);
writeToFile("wait");
writeToFile("/");
}
if(b){
Thread.sleep(500);
writeToFile("beacon_senseR");
writeToFile("/");
}
if(x){
Thread.sleep(500);
writeToFile("beacon_senseB");
writeToFile("/");
}
telemetry.addData("leftpos",leftPos);
telemetry.addData("rightpos",-rightPos);
telemetry.addData("gyro",currentHeading);
telemetry.update();
}
}
public void myinit(){
frontRight = hardwareMap.dcMotor.get("motor_3");
frontLeft = hardwareMap.dcMotor.get("motor_1");
backRight = hardwareMap.dcMotor.get("motor_4");
backLeft = hardwareMap.dcMotor.get("motor_2");
gyro = hardwareMap.gyroSensor.get("gyro");
//frontRight.setDirection(DcMotor.Direction.REVERSE);
//frontLeft.setDirection(DcMotor.Direction.REVERSE);
frontLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
frontLeft.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
frontRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
frontRight.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
backLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
backLeft.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
backRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
backRight.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
//frontLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION);
gyro.calibrate();
}
public void writeToFile(String note) {
//File writing
try {
File gpxfile = new File(root, fileName);
FileWriter writer = new FileWriter(gpxfile,true);
String body = note + "\n\n";
writer.append(body);
writer.flush();
writer.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
public void reset_encoders(){
frontRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
frontLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
backRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
backLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
}
public void revert_encoders(){
frontLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
frontRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
backLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
backRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
}
double scaleInput(double dVal) {
double[] scaleArray = {0.0, 0.05, 0.09, 0.1, 0.12, 0.15, 0.18, 0.24, 0.30, 0.36, 0.43, 0.50, 0.60, 0.72, 0.85, 1.00, 1.00};
int index = (int) (dVal * 16.0);
if(index < 0){
index = -index;
} else if(index > 16){
index = 16;
}
double dScale = 0.0;
if(dVal < 0){
dScale = -scaleArray[index];
} else{
dScale = scaleArray[index];
}
return dScale;
}
}