-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRRBotMecanumDrive.java
154 lines (134 loc) · 4.36 KB
/
RRBotMecanumDrive.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
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.util.ElapsedTime;
/**
* Controls the robot's mecanum drive base
* @author John Brereton
* @since 2021-10-08
*/
public class RRBotMecanumDrive
{
RRBotHardware robot;
private ElapsedTime autoMoveTime = new ElapsedTime();
private boolean isAutoMove = false;
private double autoTime;
/**
* Constructor gets hardware object from teleop class
* @param robot contains the hardware elements of the robot
*/
public RRBotMecanumDrive(RRBotHardware robot)
{
this.robot = robot;
}
/**
* Calculates the velocity values for the drive motors in a mecanum configuration.
* @param leftX X position of left joystick
* @param leftY Y position of left joystick
* @param rightX X position of right joystick
* @param rightY Y position of right joystick
* @param doFunction whether a function should be used on the input values
* @return velocities - array of motor velocities
*/
public double[] calcVelocities(double leftX, double leftY, double rightX, double rightY, boolean doFunction)
{
double moveX = rightX;
double moveY1 = leftY;
double turn = leftX;
double moveY2 = rightY;
//remap input values using a function
if(doFunction)
{
moveX = inputFunction(moveX);
moveY1 = inputFunction(moveY1);
turn = inputFunction(turn);
moveY2 = inputFunction(moveY2);
}
double v1 = moveY1 + moveX + turn + moveY2;
double v2 = moveY1 - moveX - turn + moveY2;
double v3 = moveY1 + moveX - turn + moveY2;
double v4 = moveY1 - moveX + turn + moveY2;
double max = Math.abs(v1);
if(Math.abs(v2) > max)
max = Math.abs(v2);
if(Math.abs(v3) > max)
max = Math.abs(v3);
if(Math.abs(v4) > max)
max = Math.abs(v4);
if(max > 1)
{
v1 /= max;
v2 /= max;
v3 /= max;
v4 /= max;
}
double[] velocities = {v1, v2, v3, v4};
return velocities;
}
/**
* Sets the motor power for manual drive. The parameters are sent to calcVelocities.
* @param leftX X position of left joystick
* @param leftY Y position of left joystick
* @param rightX X position of right joystick
* @param rightY Y position of right joystick
* @param doFunction whether a function should be used on the input values
*/
public void setMotorPower(double leftX, double leftY, double rightX, double rightY, boolean doFunction)
{
//calculate the velocities
double[] velocities = calcVelocities(leftX, leftY, rightX, rightY, doFunction);
//set the motor power
robot.frontLeftDrive.setPower(velocities[0]);
robot.frontRightDrive.setPower(velocities[1]);
robot.rearLeftDrive.setPower(velocities[2]);
robot.rearRightDrive.setPower(velocities[3]);
}
/**
* Function to be executed on the joystick input values. Not currently used
* @param input joystick value
* @return value - input value scaled by function
*/
public double inputFunction(double input)
{
double value = input * input;
if(input < 0)
{
return value * -1;
}
else
{
return value;
}
}
/**
* Automatically moves the robot based on a set speed and time. Meant to be used in teleop.
* @param speed speed of movement
* @param time how long the movement should take
*/
public void AutoMove(double speed, double time)
{
isAutoMove = true;
autoTime = time;
autoMoveTime.reset();
robot.rearRightDrive.setPower(speed);
robot.rearLeftDrive.setPower(speed);
robot.frontRightDrive.setPower(speed);
robot.frontLeftDrive.setPower(speed);
}
/**
* Checks if the movement is done by comparing the time elapsed and the time the movement is set to take.
*/
public void AutoMoveEndCheck()
{
if(autoMoveTime.milliseconds() >= autoTime)
{
isAutoMove = false;
}
}
/**
* Returns whether an auto move is currently occurring
* @return isAutoMove
*/
public boolean getIsAutoMove()
{
return isAutoMove;
}
}