Intake motor coding in Java help

so i need to code a intake/outtake with 2 motors left and right and i need one neg and pos but i dont really know how to go about it heres the code i currently have

private final PWMVictorSPX m_intakerightMotor = new PWMVictorSPX(5);
private final PWMVictorSPX m_intakekleftMotor = new PWMVictorSPX(4);

public void teleopPeriodic() {

m_intakerightMotor.set(m_driverController.getRawButton(4)? 1.0 : 0.0);
m_intakekleftMotor.set(m_driverController.getRawButton(4)? 1.0 : 0.0);

motor outputs go from -1.0 to 1.0.

Checkout: PWMSpeedController (WPILib API 2020.3.2-60-g3011ebe)

I’m not sure I agree. PWMSpeedController should only be used if a more specific answer does not exist.

Assuming OP is indeed already using victor SPX motor controllers, they already have the correct class

The key I think is that valid motor commands are any number in the range between -1.0 and 1.0. Which may be found on that documentation page.

Agree…. SetInverted is on the base class.

1 Like

i currently have updated the code to this but i will not be able to test it until monday

m_intakerightMotor.set(m_driverController.getRawButton(4)? 1.0 : 0.0);
m_intakekleftMotor.set(m_driverController.getRawButton(4)? -1.0 : 0.0);
m_intakerightMotor.set(m_driverController.getRawButton(5)? -1.0 : 0.0);
m_intakekleftMotor.set(m_driverController.getRawButton(5)? 1.0 : 0.0);

So keep in mind: with multiple calls to set() on the same motor, the last one will overwrite anything done prior. IE, only the last call matters.

In cases like this, I usually try to break it down to smaller problems first.

Here, you have two buttons. There are four possible cases.

  1. Button four is depressed
  2. Button five is depressed
  3. Neither are depressed
  4. Both are depressed.

In each case, how do you want the motors to turn?

Write a four-case If/else if/else to express this.

i want both motors to turn while one button is pressed and both to turn in reverse when another is pressed

Sure. You have explicitly covered 2/4 cases. How bout the other two?

when neither are pressed nothing and when both are pressed nothing

Cool, sounds reasonable to me.

So next step… Using the button states, if/else if/else, and boolean operations… How do you express that requirement in code?


Apologies if this seems pedantic. I’m purposely going down the “teach a person to fish” path. Yes, I do know exactly how to solve your problem, but that doesn’t help you for the next problem :).

Take courage, you are really close I think!

Thank you again, both in this post, and in others, for actively engaging in the learning process! You are many steps ahead of your peers simply for being able and proactive in asking the right questions! The pains and mental gymnastics you experience now are just part of the process. You will get past them really soon!

1 Like

i thought the ? 1>0 thingy was supposed to replace the need for the if else boolean operations

It does.
The ternary operator is equivalent to an if/else block.

The problem with your current code is that you have something like:

If button 4 pressed
  Set motor speeds
  Set motors off

If button 5 pressed
  Set motor speeds
  Set motors off

The motors can each only be told one speed to run at.

The logic for button 5 comes last, so whatever it evaluates to will be what the motors end up doing. In other words, the button 5 logic is overriding button 4.

If button 5 is pressed the motors move. If it isn’t they don’t. Their motion is not affected at all by the button 4 logic currently.

Instead of trying to do something fancy with the ternary operator, maybe see if you can construct an if, else if, else block that checks the buttons individually and sets the motor outputs accordingly. The code will likely be easier to follow that way.

1 Like

Yup, otherguy got the meat of it.

Think of the terniary operator as a shorthand for simple if/else, but not a full replacement for longer or more complex calculations. One tool in a toolbox of many tools.

The key why I would advocate moving away from ternary in this case: you have three states you care about. Intake, eject, and stop. While you certainly can get something functional using only ternary operators, most folks would find an if/else if/else solution more intuitive. YMMV.

1 Like

so would this be correct then?

  if (m_driverController.getRawButton(5)) {




else if (m_driverController.getRawButton(1)) {






1 Like

This is pretty close to what I would do. Caveat: my assertion is by visual inspection only, I don’t know for certain if it will work on a real robot. Testing is left as an exercise to the user :slight_smile:

Something to consider: what happens if both buttons are pressed at the same time? Is that acceptable behavior? (I think it is, but you get the final say in declaring it good)

i mean i dont think theyd both get pressed one is l1 and one is a

Have you considered using the command-based framework, which abstracts away the need to roll this kind of input-polling logic?

No one actually likes writing code to parse button inputs, in my experience.

1 Like

Based on this post and others, I’m guessing OP is, for some reason, already steeped in the mentalities required to write code in a TimedRobot base. I think migration might make sense, but I’m not sure of the timing.

That being said, yes, OP, at least for awareness… WPIlib does provide a very good architecture to help you separate the concepts of getting input from the operator, from assigning outputs which correspond to meaningful robot behaviors. Especially as you move into writing autonomous routines, “Wpilib Command based” is a good thing to Google and research for ways to organize and streamline your code.

1 Like

yes i have not dabbled in command based and so dont have any knowledge in it so i will hold of on that because i wont be coding auton until the 2022 frc challenge comes out