VexCode DriveTrain Help

Is this is right way to write the code to make my drivetrain have a set speed and brake type? I don’t really know what the right way is and if it is going to work. And for the axis on the controller is there anything that can specify the exact position of the joystick on the axis?

Here is my original code:

leftFront.spin(directionType::fwd, Controller1.Axis3.position(), pct);
leftBack.spin(directionType::fwd, Controller1.Axis3.position(), pct);
rightFront.spin(directionType::fwd, Controller1.Axis2.position(), pct);
rightBack.spin(directionType::fwd, Controller1.Axis2.position(), pct);

And this is what I want it to be but I don’t know if it’s the best way to write it:

if(Controller1.Axis3.position()) {
     leftFront.setBrake(brakeType::coast);
     leftBack.setBrake(brakeType::coast);
     leftFront.spin(directionType::fwd, 75, velocityUnits::pct);
     leftBack.spin(directionType::fwd, 75, velocityUnits::pct);
}
if(Controller1.Axis2.position()) {
     rightFront.setBrake(brakeType::coast);
     rightBack.setBrake(brakeType::coast);
     rightFront.spin(directionType::fwd, 75, velocityUnits::pct);
     rightBack.spin(directionType::fwd, 75, velocityUnits::pct);
}

And would this code work as well?

if(Controller1.Axis3.position()) {
   leftFront.spin(directionType::fwd, 75, velocityUnits::pct);
   leftBack.spin(directionType::fwd, 75, velocityUnits::pct);
} else {
   leftFront.stop(brakeType::coast);
   leftBack.stop(brakeType::coast);

if(Controller1.Axis2.position()) {
   rightFront.spin(directionType::fwd, 75, velocityUnits::pct);
   rightBack.spin(directionType::fwd, 75, velocityUnits::pct);
} else {
   rightFront.stop(brakeType::coast);
   rightBack.stop(brakeType::coast);

The reason why I want to have a set speed and have a specific brake type is because my robot keeps abruptly tilting back and forth when I’m moving the joysticks back and forth without giving the robot time to stop. For example, when I’m driving the robot forward and then I quickly drive backward, the robot will tilt forward a lot, and vice versa for when I’m driving the robot backward and quickly drive forward.

Also btw is this a code problem or a weight issue? Because my robot has most of its weight near the front since I have an intake and flywheel in the middle to front area.

Hey, this kind of sounds like a weight distribution, center of mass, and momentum related issue.

I think your original code should work fine

leftFront.spin(directionType::fwd, Controller1.Axis3.position(), pct);
leftBack.spin(directionType::fwd, Controller1.Axis3.position(), pct);
rightFront.spin(directionType::fwd, Controller1.Axis2.position(), pct);
rightBack.spin(directionType::fwd, Controller1.Axis2.position(), pct);

The jerking your robot experiences is probably a result of your robot driving full speed forward and then you immediately telling your motors to go backward when your jerk the joystick down. I think there might be benefit in either reapproaching the design to better balance your weight.

If you wanted to try and have a code fix for this, you could try and add some sort of curve to your speed. A simple example would be to do leftFront.spin(directionType::fwd, Controller1.Axis3.position()/2, pct); so that instead of a -100 being sent to the motor when you flick the stick, it would send a -50 which might reduce the amount of jumping. Thats not the ideal fix but an example on how you can change the input/output relationship between your joystick and motors. maybe doing some reshaping on that input/output relationship could make it so you need to flick the joysticks less too

2 Likes

It is a bad idea to limit the speed of your motors in code because you will not be able to get their full power. If you want a slower drivebase you should use a gear ratio.
The first problem I see is that your if statements are wrong. .position returns a number, not a boolean. To make this work you would have to use a comparison. I recommend checking if the absolute value is less than a number (or greater than for the second code).
Another problem is you need to make the motors spin the value of the controller position in percent, which you currently do not have implemented.

But then is there still a way to have a coast brake when I stop pushing the joystick? Because I don’t want the robot to just halt abruptly. Or unless that’s not necessary or it’s a bad idea…

Also wouldn’t this work?

   leftFront.setBrake(brakeType::coast);
   leftBack.setBrake(brakeType::coast);
   rightFront.setBrake(brakeType::coast);
   rightBack.setBrake(brakeType::coast);
   leftFront.spin(directionType::fwd, Controller1.Axis3.position(), pct); //set a max speed?
   leftBack.spin(directionType::fwd, Controller1.Axis3.position(), pct);
   rightFront.spin(directionType::fwd, Controller1.Axis2.position(), pct);
   rightBack.spin(directionType::fwd, Controller1.Axis2.position(), pct);

This would work, however I don’t think it will trigger the brake because telling the drive to spin at zero may be not the same as telling the motor to stop. I don’t exactly remember so it’s the best to test. If it doesn’t work just check whether the joystick values and see if they are zero.