Okapi V4 H-Drive programming?

I got the left and right motors working but when I try to initiate a third motor it doesn’t work. I know I’m not doing something right but I don’t know what.

#include "main.h"
using namespace okapi;

void opcontrol() {
  auto drive = ChassisControllerBuilder().withMotors(10, -9).build();
  auto Model = std::dynamic_pointer_cast<SkidSteerModel>(drive->getModel());
  Controller controller;

  while (true) {
      Model->setBrakeMode(AbstractMotor::brakeMode::hold);
      Model->arcade(controller.getAnalog(ControllerAnalog::leftY),controller.getAnalog(ControllerAnalog::rightX), 0.3);
      pros::delay(20);
  }
} 

OkapiLib does not support h-drives at this time.

So how do I get the third motor working? Just initialize it as a standalone motor?

That would work fine.

Is there a better way?

Better is subjective. If you wanted, you could put the work in to make a PR to add h-drive support to OkapiLib. Otherwise, that is probably the best you are going to get.

2 Likes

@lerker100 If you would be so kind, please test this PR on your h-drive. You will find the artifact in the comments.

4 Likes

I have verified that it does work. I had to use

driveTrain->middle(controller.getAnalog(ControllerAnalog::leftX));

to make the middle wheel spin because arcade only accepted 2 controller inputs.

I have read through the okapi docs here https://okapilib.github.io/OkapiLib/index.html and couldn’t find anyway to change the speed of the chassis in auton. I also couldn’t find or figure out any way to put this into a .hpp file. I’ve been testing out okapi V4 and quite like it but these things are giving me some troubles. They probably should go in the documentation.

auto chassis = ChassisControllerBuilder()
  .withMotors(10, -9, 8)
  .withGearset(AbstractMotor::gearset::green)
  .withDimensions({{4_in, 10_in}, imev5GreenTPR})
  .build();

Does setMaxVelocity(double) or setMaxVoltage(double) not work?

If you look at the documentation, you will find the .withMaxVelocity() and .withMaxVoltage() methods in ChassisControllerBuilder.
You can also find the setMaxVelocity() method that belongs to a ChassisModel, which you can call using chassis->getModel()->setMaxVelocity().

Also, the okapi docs are not meant to teach you c++. If you want to extern your chassis in a header file, you need c++ knowledge to do that, not okapi knowledge.
Here is how you would do the forward declaration, since you can’t use auto for declarations (as opposed to definitions):

extern std::shared_ptr<ChassisController> chassis;

You then have two options for a source file:

// globally inititalize
std::shared_ptr<ChassisController> chassis = ChassisControllerBuilder()
  // ...

or

// global pointer, initialize later
std::shared_ptr<ChassisController> chassis;

void initialize() {
  chassis = ChassisControllerBuilder()
    // ...
}
3 Likes

@theol0403 does chassis out live the scope of initialize()?

The Okapi V3 example I showed lerker had the chassis defined outside the scope of a function.

1 Like

He is using okapi v4, which is the only version which has the chassis builder and h-drive support.
But you are right, I made a mistake by trying to create a global in local scope.

There are two ways to do it, either you have a global shared_ptr that also gets initialized in global scope, or you can also initialize the global pointer in initialize().

I will update my example.

2 Likes

chassis->getModel()->setMaxVelocity(20); Doesn’t change the speed. This is the code.

#include "main.h"

void autonomous() {
  std::shared_ptr<okapi::ChassisController> chassis = ChassisControllerBuilder()
  .withMotors(10, -9, 8)
  .withGearset(AbstractMotor::gearset::green)
  .withDimensions({{4_in, 10_in}, imev5GreenTPR})
  .build();
  auto driveTrain = std::dynamic_pointer_cast<HDriveModel>(chassis->getModel());

  chassis->getModel()->setMaxVelocity(20);
  chassis->moveDistance(6_in);
}
1 Like

That code doesn’t use setMaxVelocity. Please give us an example that you think should work, but does not.

2 Likes

I copied the wrong code. I edited my post with the right code

That code should not compile, because setMaxVelocity does not belong to ChassisController

Yes, your right. I fixed it now and checked that it does compile. Sorry, I didn’t have my laptop in front of me.

1 Like