Okapi GearsetRatioPair Issue

Hi everyone,

I’m trying to set up my chassis in Okapi, but I keep getting error when using GearsetRatioPair. Here is my code:

AbstractMotor::GearsetRatioPair drive_ratio = AbstractMotor::gearset::green * (1/3);

ChassisControllerIntegrated drive = ChassisControllerFactory::create(
  1, -2, 
  -9, 10,
  drive_ratio,
  {4.0_in, 9.5_in}
);

And when executed, all robot functionality stops. To clarify, the issue only occurs when using GearsetRatioPair, and I am trying to set the gear ratio on my drive. Any idea what’s going on?

Ah, you have been got by the finicky floating-point math logic in C :wink:.

You should read https://www.learncpp.com/cpp-tutorial/4-4a-explicit-type-conversion-casting/ and Integer and floating point division from https://www.learncpp.com/cpp-tutorial/arithmetic-operators/ .

Basically, when you do 1/3, C looks at the two values, and sees that they are both integers. Then, it does integer division on the two numbers, which returns another integer. Since 1/3 = 0.33, it will truncate the number to 0, which is why the base does not move.

To solve it, simply change your code to (1.0/3.0), so that it does floating point math.

Btw, you are currently calling the X-Base factory, change your motor parameters to {1, -2}, {-9, 10} in order to create a skid-steer chassis.

8 Likes

I find it funny the PROS team shows the correct usage in the text description but not in the example.
https://pros.cs.purdue.edu/v5/okapi/api/device/motor/abstract-abstract-motor.html?highlight=gearsetratiopair#gearsetratiopair

@theol0403 good catch on the x-drive. I totally missed that.

1 Like

Oops, I will fix that. Also worth noting that if OP had the PROS terminal open, they would have gotten an error message stating The gear ratio cannot be zero! Check if you are using integer division..

7 Likes