Programming Motor to stay in Position it is in

Basically, I’m trying to figure out how can I make a motor stay in its position. For example,
if (joystick.getRawButton(Button: 1))
motor.set(speed: 1);
} else if (joystick.getRawButton(Button: 2))
motor.set(speed: -1);
} else
//making motor stay in its position that it currently is

So, there is a two button system. I press one button the motor spins one way, I press another button the motor spins the opposite direction. Im trying to make it where if i dont press anything the motor is on but not moving. Does anyone have any tips, suggestions, questions?

You should be able to set the speed to zero, and that would work without any load.

The catch is, FRC motors don’t generally operate in no-load environments, and holding position needs some sort of sensor. So, let’s start with: What motor type is it, and is there any sort of encoder in the system (including internal to the motor?

1 Like

I am using a Rev Robotics Neo Motor, yes it does have an encoder.

A motor (or more specifically, its controller) has no inherent knowledge outside of what you tell it to do. With that in mind, you may take the naive approach of figuring out a value that is enough to hold it in place and just use that.

However, a better approach would be to tell the controller more about the system it’s controlling. This is done using sensors. Sensors can be used to measure many things (linear or rotational position/velocity/acceleration, pressure, force, etc), but most relevant for you would be position. Examples of sensors than can measure position would be encoders and potentiometers. I’ll leave the details of these as a homework assignment.

Once the controller has a way of knowing the position, you can change from naive output control (i.e. I want you to use 60% output, or 7 volts, etc), to whats known as closed-loop control. This would then let you tell the controller what position you want to be in, and let it work out how to get there. A common example of closed-loop control is a PID controller (wpilib docs cover this quite well).

ok thank you for the information!

Tuning a Turret Position Controller — FIRST Robotics Competition documentation may be of interest if you choose to go the closed-loop sensor route.

Alternately, check out configuring your motor for “brake” mode when 0 command is applied. Idle Mode - Brake/Coast Mode - SPARK MAX . It’s not perfect, and doesn’t guarantee you stay in the same spot, but it’s pretty stiff, especially when put through a gearbox.

1 Like