OKAPI setup error

I think that I did something wrong when setting up my chasses controller. Whenever I run any command involving it, I get a memory permission error. My full code is In-Progress_Programs/Testing-Prebuild/States · Mages · ACP Robotics / 2021-2022 Season · GitLab, and the chassis controller is

std::shared_ptr<OdomChassisController> drive = //Odometry.
        ChassisControllerBuilder()
        .withMotors(
            { leftBack, leftFront },
            { rightBack, rightFront }
        )
        .withSensors(
            ADIEncoder{ 'A', 'B' }, //Left  Encoder
            ADIEncoder{ 'C', 'D' } //Right Encoder
        )
        .withGains(
            { 0.01, 0, 0 }, // distance controller gains
            { 0.01, 0, 0 }, // turn controller gains
            { 0.01, 0, 0 } // angle controller gains (helps drive straight)
        )
        .withDimensions(AbstractMotor::gearset::green, { {4.125_in, 12_in}, imev5GreenTPR })//Drive wheel size, Drive wheel track
        .withOdometry({ { 4.125_in, 8_in}, quadEncoderTPR })//tracking wheel size, tracking wheel track
        .buildOdometry();

I need to tune the PID, but that is not possible if I can’t run any of the code. Does anyone see a mistake I made? Thanks :slight_smile:

from the code, it seems like the global variable drive was never initialized.


you declared another variable with the same name in initialize, which went out of scope the moment you leave it. The global variable was never initialized, so the null pointer caused a segfault.

To solve this, just paste the builder code into the global scope, and don’t redeclare drive objects everywhere.

3 Likes

So, I should make:

std::shared_ptr<OdomChassisController> drive;

/**
 * Runs initialization code. This occurs as soon as the program is started.
 *
 * All other competition modes are blocked by initialize; it is recommended
 * to keep execution time for this mode under a few seconds.
 */
void initialize() {
    std::shared_ptr<OdomChassisController> drive = //Odometry.
        ChassisControllerBuilder()
        .withMotors(
            { leftBack, leftFront },
            { rightBack, rightFront }
        )
        .withSensors(
            ADIEncoder{ 'A', 'B' }, //Left  Encoder
            ADIEncoder{ 'C', 'D' } //Right Encoder
        )
        .withGains(
            { 0.01, 0, 0 }, // distance controller gains
            { 0.01, 0, 0 }, // turn controller gains
            { 0.01, 0, 0 } // angle controller gains (helps drive straight)
        )
        .withDimensions(AbstractMotor::gearset::green, { {4.125_in, 12_in}, imev5GreenTPR })//Drive wheel size, Drive wheel track
        .withOdometry({ { 4.125_in, 8_in}, quadEncoderTPR })//tracking wheel size, tracking wheel track
        .buildOdometry();

    lift.setBrakeMode(AbstractMotor::brakeMode::brake);
    frontGrabber.setBrakeMode(AbstractMotor::brakeMode::brake);
    backGrabber.setBrakeMode(AbstractMotor::brakeMode::brake);

    lift.tarePosition();

}

into:

std::shared_ptr<OdomChassisController> drive = //Odometry.
ChassisControllerBuilder()
.withMotors(
    { leftBack, leftFront },
    { rightBack, rightFront }
)
.withSensors(
    ADIEncoder{ 'A', 'B' }, //Left  Encoder
    ADIEncoder{ 'C', 'D' } //Right Encoder
)
.withGains(
    { 0.01, 0, 0 }, // distance controller gains
    { 0.01, 0, 0 }, // turn controller gains
    { 0.01, 0, 0 } // angle controller gains (helps drive straight)
)
.withDimensions(AbstractMotor::gearset::green, { {4.125_in, 12_in}, imev5GreenTPR })//Drive wheel size, Drive wheel track
.withOdometry({ { 4.125_in, 8_in}, quadEncoderTPR })//tracking wheel size, tracking wheel track
.buildOdometry();

/**
 * Runs initialization code. This occurs as soon as the program is started.
 *
 * All other competition modes are blocked by initialize; it is recommended
 * to keep execution time for this mode under a few seconds.
 */
void initialize() {
    lift.setBrakeMode(AbstractMotor::brakeMode::brake);
    frontGrabber.setBrakeMode(AbstractMotor::brakeMode::brake);
    backGrabber.setBrakeMode(AbstractMotor::brakeMode::brake);

    lift.tarePosition();
}

Also, I would still need extern std::shared_ptr<OdomChassisController> drive; in include/OurFunctions.hpp, right?