-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAutoMovement.kt
More file actions
160 lines (137 loc) · 4.92 KB
/
AutoMovement.kt
File metadata and controls
160 lines (137 loc) · 4.92 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
package org.firstinspires.ftc.teamcode
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode
import org.firstinspires.ftc.robotcore.external.navigation.DistanceUnit
import kotlin.math.*
class AutoMovement(private val robot: Hardware, private val opMode: LinearOpMode) {
inner class AutonomousAutoMovement(private val vuforia: Vuforia) {
fun moveDistance(angle: Double, speed: Double, distance: Double) {
vuforia.getPosition()
val point0 = vuforia.lastLocation!!.translation
robotMap(
speed * sin(angle),
speed * cos(angle),
speed * sin(angle),
speed * cos(angle)
)
while (hypot(vuforia.lastLocation!!.translation[0] - point0[0], point0[0] - vuforia.lastLocation!!.translation[1]) < distance) {
vuforia.getPosition()
}
robotStop()
}
fun moveToCoords(x: Double, y: Double, speed: Double) {
val angle: Double = Math.toDegrees(atan2(x - vuforia.lastLocation!!.translation[0], y - vuforia.lastLocation!!.translation[1]))
val distance: Double = hypot(x - vuforia.lastLocation!!.translation[0], y - vuforia.lastLocation!!.translation[1])
moveDistance(angle, speed, distance)
}
}
fun moveToDistance(distance: Double, speed: Double) {
val goingBk = getDistance() > distance
robotTranslate(speed, if(goingBk) Direction.BACKWARD else Direction.FORWARD)
while(opMode.opModeIsActive() &&
((goingBk && getDistance() >= distance) ||
(!goingBk && getDistance() <= distance))) {
opMode.telemetry.addData("goingBk", goingBk)
opMode.telemetry.addData("distance", getDistance())
opMode.telemetry.addData("target distance", distance)
opMode.telemetry.update()
}
robotStop()
}
fun getDistance() : Double = round(robot.distanceSensorFront.getDistance(DistanceUnit.CM))
/* fun armGrab() {
robot.grabberServo.position = 0.0
}
fun armRelease() {
robot.grabberServo.position = 1.0
}*/
fun ducksStart(speed: Double) {
robot.motorDucks.power = speed
}
fun ducksStop() {
robot.motorDucks.power = 0.0
}
enum class Position {
TOP, BOTTOM, MIDDLE
}
fun armRaise(position: Position) {
when(position) {
Position.TOP -> {
robot.motorArm.power = -1.0
Thread.sleep(1500)
robot.motorArm.power = 0.0
}
Position.BOTTOM -> {
robot.motorArm.power = 1.0
Thread.sleep(1500)
robot.motorArm.power = 0.0
}
Position.MIDDLE -> {
/*
val time = measureTimeMillis {
armRaise(Position.TOP)
}
armRaise(Position.BOTTOM)
robot.motorArm?.power = -1.0
Thread.sleep(time / 2)
robot.motorArm?.power = 0.0 */
}
}
}
enum class Direction {
FORWARD, BACKWARD, LEFT, RIGHT
}
fun robotTranslate(speed: Double, direction: Direction) {
when(direction) {
Direction.FORWARD -> {
robotMap(speed, speed, speed, speed)
}
Direction.BACKWARD -> {
robotMap(-speed, -speed, -speed, -speed)
}
Direction.LEFT -> {
robotMap(speed, -speed, -speed, speed)
}
Direction.RIGHT -> {
robotMap(-speed, speed, speed, -speed)
}
}
}
fun robotRotateLeft(speed: Double) {
robotMap(speed, speed, -speed, -speed)
}
fun robotRotateRight(speed: Double) {
robotMap(-speed, -speed, speed, speed)
}
fun rotate90(speed: Double) {
val startAngle = angle()
robotRotateRight(speed)
while(abs(angle() - startAngle) < 90) {
// compute the meaning of life, do a backflip, etc.
println(42.0f)
}
}
fun rotateR90(speed: Double) {
val startAngle = angle()
robotRotateLeft(speed)
while(abs(angle() - startAngle) < 90) {
// compute the meaning of life, do a backflip, etc.
println(42.0f)
}
}
private fun avg(x: Float, y: Float): Float {
return (x + y) / 2
}
fun avgAngles(): Float {
return avg(-robot.controlHubIMU.angularOrientation.firstAngle, robot.expansionHubIMU.angularOrientation.firstAngle)
}
fun angle(): Float = robot.controlHubIMU.angularOrientation.firstAngle + 180
fun robotStop() {
robotMap(0.0,0.0,0.0,0.0)
}
fun robotMap(BL: Double, FL: Double, BR: Double, FR: Double) {
robot.leftMotor.power = BL
robot.motorFL.power = FL
robot.rightMotor.power = -BR
robot.motorFR.power = -FR
}
}