-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonMecanum.java
More file actions
79 lines (65 loc) · 3.56 KB
/
CommonMecanum.java
File metadata and controls
79 lines (65 loc) · 3.56 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
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.hardware.HardwareMap;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.hardware.DcMotorEx;
import com.qualcomm.robotcore.hardware.DcMotor;
public class CommonMecanum {
private HardwareMap hardwareMap = null;
private LinearOpMode opMode = null;
private DcMotor motorFrontLeft = null;
private DcMotor motorBackLeft = null;
private DcMotor motorFrontRight = null;
private DcMotor motorBackRight = null;
// These variable are declared here (as class members) so they can be updated in various methods,
// but still be displayed by sendTelemetry()
private double targetHeading = 0;
private double driveSpeed = 0;
private double turnSpeed = 0;
private double leftSpeed = 0;
private double rightSpeed = 0;
private int leftTarget = 0;
private int rightTarget = 0;
// Calculate the COUNTS_PER_INCH for your specific drive train.
// Go to your motor vendor website to determine your motor's COUNTS_PER_MOTOR_REV
// For external drive gearing, set DRIVE_GEAR_REDUCTION as needed.
// For example, use a value of 2.0 for a 12-tooth spur gear driving a 24-tooth spur gear.
// This is gearing DOWN for less speed and more torque.
// For gearing UP, use a gear ratio less than 1.0. Note this will affect the direction of wheel rotation.
static final double COUNTS_PER_MOTOR_REV = 28.0 ; // eg: GoBILDA 312 RPM Yellow Jacket
static final double DRIVE_GEAR_REDUCTION = 20.0 ; // No External Gearing.
// static final double WHEEL_DIAMETER_INCHES = 3.0 ; // For figuring circumference
static final double WHEEL_DIAMETER_INCHES = 3.25 ; // For figuring circumference
static final double COUNTS_PER_INCH = (COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION) /
(WHEEL_DIAMETER_INCHES * 3.1415);
public void initialize (LinearOpMode opMode, HardwareMap hardwareMap)
{
this.opMode = opMode;
this.hardwareMap = hardwareMap;
motorFrontLeft = hardwareMap.dcMotor.get("leftFront");
motorBackLeft = hardwareMap.dcMotor.get("leftBack");
motorFrontRight = hardwareMap.dcMotor.get("rightFront");
motorBackRight = hardwareMap.dcMotor.get("rightBack");
// Reverse the right side motors
// Reverse left motors if you are using NeveRests
motorFrontRight.setDirection(DcMotorSimple.Direction.REVERSE);
motorBackRight.setDirection(DcMotorSimple.Direction.REVERSE);
}
public void driveViaJoystick (double x, double y, double rx)
{
// y is forwards/backwards, x is strafe, rx is rotate
// Denominator is the largest motor power (absolute value) or 1
// This ensures all the powers maintain the same ratio, but only when
// at least one is out of the range [-1, 1]
double denominator = Math.max(Math.abs(y) + Math.abs(x) + Math.abs(rx), 1);
double frontLeftPower = (y + x + rx) / denominator;
double backLeftPower = (y - x + rx) / denominator;
double frontRightPower = (y - x - rx) / denominator;
double backRightPower = (y + x - rx) / denominator;
motorFrontLeft.setPower(frontLeftPower);
motorBackLeft.setPower(backLeftPower);
motorFrontRight.setPower(frontRightPower);
motorBackRight.setPower(backRightPower);
}
}