2024-03-07 15:15:33 +01:00
|
|
|
package org.firstinspires.ftc.teamcode;
|
2024-03-07 14:54:46 +01:00
|
|
|
|
2024-03-07 15:15:33 +01:00
|
|
|
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
|
|
|
|
|
|
|
|
import com.qualcomm.robotcore.hardware.IMU;
|
|
|
|
import org.firstinspires.ftc.robotcore.external.navigation.YawPitchRollAngles;
|
|
|
|
import com.qualcomm.robotcore.hardware.ImuOrientationOnRobot;
|
|
|
|
import com.qualcomm.hardware.rev.RevHubOrientationOnRobot;
|
|
|
|
|
|
|
|
import org.firstinspires.ftc.robotcore.external.navigation.Orientation;
|
|
|
|
import org.firstinspires.ftc.robotcore.external.navigation.AxesOrder;
|
|
|
|
import org.firstinspires.ftc.robotcore.external.navigation.AxesReference;
|
|
|
|
import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
|
|
|
|
|
|
|
|
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
|
|
|
|
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
|
|
|
|
import com.qualcomm.robotcore.hardware.DcMotor;
|
|
|
|
import com.qualcomm.robotcore.util.ElapsedTime;
|
|
|
|
|
|
|
|
@Autonomous
|
|
|
|
|
|
|
|
public class ftc2024_autonome_api extends LinearOpMode {
|
|
|
|
private DcMotor rm;
|
|
|
|
private DcMotor lm;
|
2024-03-07 16:38:24 +01:00
|
|
|
private DcMotor harvestmotor;
|
2024-03-07 15:15:33 +01:00
|
|
|
private IMU imu;
|
2024-03-07 16:28:23 +01:00
|
|
|
private final double wheel_width = 9.0e-2; // metres
|
|
|
|
private final double wheel_perimeter = Math.PI * wheel_width;
|
|
|
|
private final double tour_par_minutes = 300.0;
|
|
|
|
private final double ground_tiles_width = 61.0e-2; // metres
|
2024-03-07 15:15:33 +01:00
|
|
|
/*
|
|
|
|
* return a metre/sec speed
|
|
|
|
* @param motor_speed = double between 0 and 1
|
|
|
|
*/
|
|
|
|
public double getSpeedFromMotorSpeed(double motor_speed = 1.0){
|
|
|
|
double speed_tour_par_minutes = this.tour_par_minutes*motor_speed;
|
|
|
|
double speed = (speed_tour_par_minutes/60)*this.wheel_perimeter;
|
|
|
|
return speed;
|
|
|
|
}
|
|
|
|
public double time_for_dist(double dist, double motor_speed=1.0){
|
|
|
|
double speed = getSpeedFromMotorSpeed(motor_speed);
|
|
|
|
return (dist/speed);
|
|
|
|
}
|
|
|
|
public void forward(double n_tiles, double motor_speed = 1.0){
|
2024-03-07 16:28:23 +01:00
|
|
|
double total_time = time_for_dist(n_tiles*ground_tiles_width, motor_speed);
|
2024-03-07 15:15:33 +01:00
|
|
|
double start_time = runtime.seconds();
|
|
|
|
while( opModeIsActive() && ((runtime.seconds()-start_time)<total_time)){
|
|
|
|
lm.setPower(motor_speed);
|
|
|
|
rm.setPower(motor_speed);
|
|
|
|
}
|
|
|
|
lm.setPower(0);
|
|
|
|
rm.setPower(0);
|
|
|
|
}
|
2024-03-07 16:38:24 +01:00
|
|
|
public void backward(double n_tiles, double motor_speed= 1.0){
|
|
|
|
forward(n_tiles, -motor_speed);
|
|
|
|
}
|
|
|
|
public void harvest(double motor_speed=0.0){
|
|
|
|
harvestmotor.setPower(motor_speed);
|
|
|
|
}
|
2024-03-07 15:38:51 +01:00
|
|
|
public void rotate(double angle, double motor_speed=1.0){
|
|
|
|
double start_yaw = robotOrientation.getYaw(AngleUnit.DEGREES);
|
2024-03-07 16:21:56 +01:00
|
|
|
angle = 200.0;
|
|
|
|
double anglerad = Math.toRadians(angle);
|
|
|
|
angle = Math.toDegrees(Math.atan2(Math.sin(anglerad),Math.cos(anglerad)));
|
2024-03-07 15:38:51 +01:00
|
|
|
double left_multiplier = -( (double) Math.signum(angle));
|
|
|
|
double right_multiplier = ((double) Math.signum(angle));
|
|
|
|
double m_power = motor_speed;
|
|
|
|
while(opModeIsActive() && (Math.abs(robotOrientation.getYaw(AngleUnit.DEGREES) - start_yaw) < Math.abs(angle)){
|
2024-03-07 15:50:00 +01:00
|
|
|
m_power = (Math.abs(robotOrientation.getYaw(AngleUnit.DEGREES)-start_yaw));//relative
|
2024-03-07 15:38:51 +01:00
|
|
|
lm.setPower(left_multiplier*m_power);
|
|
|
|
rm.setPower(right_multiplier*m_power);
|
|
|
|
}
|
|
|
|
lm.setPower(0);
|
|
|
|
rm.setPower(0);
|
|
|
|
}
|
2024-03-07 15:15:33 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void runOpMode() {
|
|
|
|
lm = hardwareMap.get (DcMotor.class, "blm");
|
|
|
|
rm = hardwareMap.get (DcMotor.class, "brm");
|
2024-03-07 16:38:24 +01:00
|
|
|
harvestmotor = hardwareMap.get(DcMotor.class, "flm");
|
2024-03-07 15:15:33 +01:00
|
|
|
|
|
|
|
rm.setDirection(DcMotor.Direction.REVERSE);
|
|
|
|
|
|
|
|
imu = hardwareMap.get(IMU.class, "imu");
|
|
|
|
imu.initialize(
|
|
|
|
new IMU.Parameters(
|
|
|
|
new RevHubOrientationOnRobot(
|
|
|
|
RevHubOrientationOnRobot.LogoFacingDirection.UP,
|
|
|
|
RevHubOrientationOnRobot.UsbFacingDirection.FORWARD
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
imu.resetYaw();
|
|
|
|
YawPitchRollAngles robotOrientation;
|
|
|
|
robotOrientation = imu.getRobotYawPitchRollAngles();
|
|
|
|
double Yaw = robotOrientation.getYaw(AngleUnit.DEGREES);
|
|
|
|
double yaw_sortie = 0.0;
|
|
|
|
|
|
|
|
waitForStart();
|
|
|
|
|
2024-03-07 16:28:23 +01:00
|
|
|
if(opModeIsRunning()){
|
2024-03-07 16:31:51 +01:00
|
|
|
forward(0.5);
|
2024-03-07 16:38:24 +01:00
|
|
|
rotate(-90.0);
|
|
|
|
forward(1.5);
|
|
|
|
harvest(-1);
|
|
|
|
backward(1);
|
|
|
|
harvest(0);
|
2024-03-07 15:15:33 +01:00
|
|
|
}
|
|
|
|
}
|