Motor Forward and Reverse SOLVED

I have an elevator with a motor and i cant get it to go in reverse the line for the motor control is
m_motor.set(m_driverController.getRawAxis(kMotorPort));

I was also trying to use this

m_motor.(
m_driverController.getTriggerAxis(Hand.kLeft), m_driverController.getTriggerAxis(Hand.kRight));

Are you trying to drive the motor with the triggers on a gamepad? One for forward and one for reverse?

im trying to use the triggers just cause thats what it defaulted to

Triggers are a little bit weird because they’re two halves of a single axis on most gamepads. So where most axes are -1 to +1, the triggers are usually just 0 to +1. You usually have to do some of your own logic to mix the two in a useful way.

I would do it this way:

double leftTrigVal = m_driverController.getRawAxis(/*axis number for left trigger*/); 
double rightTrigVal = m_driverController.getRawAxis(/*axis number for right trigger*/) ;
double motorPwr = (leftTrigVal > 0)? -leftTrigVal : (rightTrigVal > 0)? rightTrigVal : 0;
m_motor.set(motorPwr);

In case you’re not familiar with the syntax, the motorPwr = etc. etc. bit is equivalent to:

if (leftTrigVal > 0)
{
    motorPwr = -leftTrigVal; //negate for down direction
}
else if (rightTrigVal > 0)
{
    motorPwr = rightTrigVal;
}
else
{
    motorPwr = 0;
}

so i used the code but both triggers are up now

PSA: please use code blocks when sharing code. You just have to wrap the code in triple backticks, and you can even specify a language and it’ll lex it for you.

image

console.log('See how much better this is?');
1 Like

Negate either the rightTrigVal or the leftTrigVal to get the down direction you want.

Sorry about that. Need to negate either left or right trigger to have one go down. Fixed in the original post.

Thank you. Corrected in the original post.

1 Like

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