Okapilib autonomous issue

When I run my code the operator control runs fine. However, in the autonomous the robot drives forward and then the left side stops when it is supposed to but the right keeps going forever. I even tried adding a stop afterwards. Here’s my code. Do you guys know what the issue is?

#include "main.h"
using namespace okapi;
Motor frontLeftMotor(1, false, AbstractMotor::gearset::green, AbstractMotor::encoderUnits::degrees);
Motor frontRightMotor(2, true, AbstractMotor::gearset::green, AbstractMotor::encoderUnits::degrees);
Motor backRightMotor(3, true, AbstractMotor::gearset::green, AbstractMotor::encoderUnits::degrees);
Motor backLeftMotor(4, false, AbstractMotor::gearset::green, AbstractMotor::encoderUnits::degrees);

ADIEncoder leftEncoder('C', 'D');
ADIEncoder rightEncoder('A', 'B');
ADIEncoder middleEncoder('E', 'F');

std::shared_ptr<OdomChassisController> chassis = ChassisControllerBuilder()
.withMotors(frontLeftMotor, frontRightMotor, backRightMotor, backLeftMotor)
.withMaxVelocity(150)
.withSensors(leftEncoder, rightEncoder, middleEncoder)
.withGains(
		{0.0035, 0, 0}, // Distance controller gains
		{0.003, 0, 0}, // Turn controller gains
		{0.002, 0, 0.00006}  // Angle controller gains (helps drive straight)
)
.withDimensions(AbstractMotor::gearset::green, {{4.0_in, 9.0_in}, imev5GreenTPR})//gearset, diameter, track, ticks
.withOdometry() // use the same scales as the chassis (above)
.buildOdometry(); // build an odometry chassis

std::shared_ptr<okapi::XDriveModel> driveTrain = std::dynamic_pointer_cast<XDriveModel>(chassis->getModel());

Controller master;

void initialize() {//runs when program starts
	pros::lcd::initialize();
	pros::lcd::set_text(1, "Hello PROS User!");
}

void disabled() {
	chassis->moveDistance(0_in);
}//runs when disabled

void competition_initialize() {}//after initialize before auton

void autonomous() {//after start comp
	chassis->moveDistance(3_in);
	chassis->stop();
}

void opcontrol() {//after auton
	chassis->moveDistance(0_in);
	// assigning the chassis to a X-drive model
	driveTrain->setBrakeMode(AbstractMotor::brakeMode::hold);
	while (true) {
			driveTrain->xArcade(master.getAnalog(ControllerAnalog::leftX),
	   	master.getAnalog(ControllerAnalog::leftY),
	    master.getAnalog(ControllerAnalog::rightX),
			0.3);

    pros::delay(20);
	}
}

Try removing the external encoders. If one is bad it can cause this problem.

2 Likes

Thanks that worked! Do you know a way to check which encoder is messing it up and how to fix it? Or is OkapiLib accurate enough with just the integrated motor encoders? Thank you for helping me with this and my other post, my team and I greatly appreciate it.

I like to use external encoders. It depends on your robot if it’s heavy, uses chain, or gears I would definitely use external ones. To test your external encoders first initialize the pros lcd which should already be done. Than in you opcontrol loop put this replacing Encoder with your encoder object. pros::lcd::print("%d", Encoder.get());

2 Likes