Issue Setting Conversion Factors on Spark Flex

I was attepting to show a new student some of the basics of using the integrated encoder on a NEO (Vortex in this instance because we plan to use them), and when trying to set a position or velocity conversion factor it would not work. I confirmed that there was an error, but could not narrow it down to which with the code below.

void Robot::RobotInit() override {
  rev::REVLibError err;
  err = m_encoder.SetPositionConversionFactor(1/5);
  if (err != rev::REVLibError::kOk) {
    std::puts("error present");
  }
}

This would print “error present” in the RIOLog and the conversion would not work. Is there something I am doing wrong? The encoder read accurately so I don’t think I’ve done anything odd there. It just would not apply a conversion factor.

Try 1.0/5.0, but it could certainly be something else.

I can try that, but that still doesn’t explain the function returning an error.

Looking at the firmware changelog the conversion factor is mentioned, I will just try updating that.

A conversion factor of 0 is invalid. 1 / 5 is 0 which is invalid, where 1.0 / 5.0 is 0.2 which is valid.

3 Likes

Makes sense. I thought it would implicitly cast this to a double, I guess not. Thanks for the explanation.

“Integer division” is the search term to explain this. Heres the first result on Google involving C++: https://www.cs.mtsu.edu/~xyang/2170/typeConversion.html

It does in Python, but not C++ or Java.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.