Is it okay to have negative voltage in the motor.spin command?

Hello everyone,

I have a PID drive function that calculates a power variable that is the sum of the P, I, and D terms. I’ve capped the variable so that it is between [-127, 127] and I later convert that into volts using this function:

int convertPowerToVolts (int power)
{
  int maxVolts = 12;
  int maxPower = 127;
  
  int volts = (power * maxVolts) / maxPower;

  return volts;
}

The volts value is then inputted into motor.spin command:

if(!isGoingFwd){
      DriveLF.spin(directionType::rev, volts, voltageUnits::volt);
      // Other motors ignored for this example.
    }
    else{
      DriveLF.spin(directionType::fwd, volts, voltageUnits::volt);
   }
   // isGoingFwd is a bool from the PID drive function's parameters

However, since my power value could be negative, the resulting volts value could also be negative. Would that be a problem? If so, should I cap my power value such that it is [0, 127], thereby not resulting in any negative voltages? Thank you.

Also, is this how to properly insert code into a VEX Forum post or is there some other formatting?

1 Like

Your code format is fine. Negative voltage should make you movebackwards, but I would still when possible.

Do you use cortex or v5? V5 does not need PID. If you are using legacy, that is a whole different story… could you post your PID code, so I could take inspiration from it?

Negative values work.
(I find it annoying that Vexcode does not have functions that assume directionType::fwd.)

The code you posted with isGoingFwd is not neccessary.

Also V5 does have built in PID, though implementing it yourself can give you better and more fine control, which should give better results.

It works but you can also do it like this (which I find easier)…

```
mycode();
```
2 Likes

I’m using V5. It has the built in PID, but that generally isn’t very good. I’m not really doing anything special with my PID function, and it’s mostly the same as found in this guide. The main difference is that my while loop runs until a certain time is reached. However, I can share my code with you via message if you like.

Please do!!

The built-in PID works fine for us.

Thank you for sharing the PDF. This is helpful, for later year autonomous.

Thank you very much :smile:

So I could have the motor.spin command set to fwd by default and the inputted voltage would determine the direction the motors spin, thereby not needing an if/else statement?

DriveLF.spin(directionType::fwd volts, voltageUnits::volt);

So if I wanted my robot to move backwards, I would have to change the distance argument in my drive function so that it is negative, rather than having a positive distance and then specifying the direction?

Yes that is how it is usually done.

1 Like

That should work… We do something similar, just instead of specifying volts, we specify speed in percent, and negative numbers just make us go the other way. Also please PM me the code you have :slight_smile: