Skip to content

Commit

Permalink
pid loop and new
Browse files Browse the repository at this point in the history
  • Loading branch information
ntt305 committed Nov 5, 2024
1 parent 5249744 commit 0bf1ff1
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

@Config
@TeleOp

public class nickpidloop extends LinearOpMode {

private DcMotor leftFrontMotor;
Expand All @@ -19,7 +20,7 @@ public class nickpidloop extends LinearOpMode {
private DcMotor Arm;
double refrence;

public void runOpMode() throws InterruptedException {
public void runOpMode() throws InterruptedException {

leftFrontMotor = hardwareMap.get(DcMotor.class, "front_left");
rightFrontMotor = hardwareMap.get(DcMotor.class, "front_right");
Expand All @@ -32,10 +33,10 @@ public void runOpMode() throws InterruptedException {
double power = 0;
double difference = 0;
double preverror = 0;
double k = 0;
double kp = 0;
double Ki = 0;
double kg = 0;
double k = 0;



Expand All @@ -45,16 +46,13 @@ public void runOpMode() throws InterruptedException {

while (opModeIsActive()) {

while (gamepad1.b);
while (gamepad1.b) ;

error = preverror + error;
difference = error -preverror;
preverror = error;
power = k*(error) + Ki*(error)+ kg;

}
error = preverror + error;
difference = error - preverror;
preverror = error;
power = k * (error) + Ki * (error) + kg;

}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.firstinspires.ftc.teamcode.opmode;

import com.acmerobotics.dashboard.config.Config;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;

@Config
@TeleOp
public class workingpidloop18284 extends LinearOpMode {


private DcMotor leftFrontMotor;
private DcMotor rightFrontMotor;
private DcMotor leftBackMotor;
private DcMotor rightBackMotor;
private DcMotor Arm;
double reference;

@Override
public void runOpMode() throws InterruptedException {

leftFrontMotor = hardwareMap.get(DcMotor.class, "front_left");
rightFrontMotor = hardwareMap.get(DcMotor.class, "front_right");
leftBackMotor = hardwareMap.get(DcMotor.class, "back_left");
rightBackMotor = hardwareMap.get(DcMotor.class, "back_right");
Arm = hardwareMap.get(DcMotor.class, "arm");
double encoderPosition;
waitForStart();
double error;
error = 1.1;
double power = 0;


reference = 300;
Arm.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
Arm.setMode(DcMotor.RunMode.RUN_USING_ENCODER);

while (opModeIsActive()) {

while (gamepad1.b) {


// obtain the encoder position
encoderPosition = Arm.getCurrentPosition();
// calculate the error
error = reference - encoderPosition;
power = (error/250)*.7;
if(power>0.7){
power = 0.7;
}
else if(power<-0.7){
power = -0.7;
}

Arm.setPower(power);

telemetry.addData("arm", encoderPosition);
telemetry.addData("error", error);
telemetry.addData("stick", gamepad2.left_stick_y);
telemetry.addData("power", power);
telemetry.update();


}
Arm.setPower(0);
leftFrontMotor.setDirection(DcMotorSimple.Direction.REVERSE);
leftBackMotor.setDirection(DcMotorSimple.Direction.REVERSE);
double y = -gamepad1.left_stick_y; // Remember, Y stick is reversed!
double x = gamepad1.left_stick_x;
double rx = gamepad1.right_stick_x;

leftFrontMotor.setPower(y + x + rx);
leftBackMotor.setPower(y - x + rx);
rightFrontMotor.setPower(y - x - rx);
rightBackMotor.setPower(y + x - rx);

//oops i didn't push right :P
}
}
}


//3 hours for 1 bracket... always remember to click code->reformat code









0 comments on commit 0bf1ff1

Please sign in to comment.