Encoder Returning Garbage Values When Used in Task

Hi everyone, I’m currently working on programming my robot’s odometry, and I’m having a bit of trouble getting my encoders to work. My plan is to have a task running a member function of my drivetrain class, keeping track of where the robot’s been.

In this task, I need to be able to read the encoder values, but every time I do that I just get the number 2147483647, or the maximum value of an int. However, when running the same function in the main task, everything works perfectly fine.

class Testing {
    private:
        pros::ADIEncoder encoderX;
    public:
        Testing(): encoderX('G', 'H', false) {
            pros::Task odom([this] { this->test(); });
        }
        void test() {
            while (true) {
                std::cout << encoderX.get_value() << std::endl;
                pros::delay(20);
            }
        }
};

void opcontrol() {
    Testing test;
}

Thanks for your help!

Key questions here for you to think about:

  • What happens when the Testing object that this refers to is destroyed?
  • When does test get destroyed?
3 Likes