-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearMotor.java
More file actions
69 lines (65 loc) · 2.04 KB
/
Copy pathLinearMotor.java
File metadata and controls
69 lines (65 loc) · 2.04 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
package com.walnuthillseagles.walnutlibrary;
/**
* Created by Yan Vologzhanin on 1/2/2016.
*/
import com.qualcomm.robotcore.hardware.DcMotor;
public class LinearMotor extends SimpleMotor implements Runnable, Auto {
//Field
protected double speedLimit;
protected boolean isDone;
//Value for a full encoder rotation
protected int encoderRot;
protected int orientation;
protected Thread runner;
//Encoder Stuff
protected int target;
//Constants
public static final double WHEEL_SIZE =4;
public static final double GEAR_RATIO = 1;
public static final int TOLERANCE = 5;
//Constructor
LinearMotor(DcMotor myMotor, String myName, boolean encoderCheck, boolean isReversed){
super(myMotor, myName, encoderCheck);
orientation = 1;
if(isReversed)
orientation = -1;
runner = new Thread(this);
target = 0;
speedLimit = 0;
}
//Val = inches
public void operate(double val, double mySpeedLimit){
speedLimit = Math.abs(mySpeedLimit);
if(val < 0)
speedLimit = -speedLimit;
if(hasEncoders)
target = (int)((val / WHEEL_SIZE) * GEAR_RATIO * orientation);
runner.start();
}
public void operate(double val){
operate(val, 1);
}
public void run(){
motor.setPower(speedLimit);
boolean notStopped = true;
while(!inRange(target, motor.getCurrentPosition()) && notStopped){
try{
motor.wait(WAITRESOLUTION);
}
catch(InterruptedException e){
this.fullStop();
notStopped = false;
}
}
this.fullStop();
}
protected boolean inRange(int target, int current){
return (current > target-TOLERANCE) && (current < target+TOLERANCE);
}
public void waitForCompletion() throws InterruptedException{
runner.join();
}
public DcMotor getMotor(){
return motor;
}
}