We need to get a motor to have a set value that allows it to stay in place and not move the object it’s connected to. We want the motor to move when the joystick is pulled, but stay in place otherwise. How would we program this?
More information on the mechanism would help. In short, to do that you need to either implement a mechanical brake of some sort to hold it in place when you cut power to the motor (window motors or the Bosch seat motor are great for this application, as they can’t be back driven.), or put a sensor on the shaft (potentiometer or encoder, most likely) and utilize a PID loop. If you search on here for PID, you’ll get a lot of hits that can be useful in figuring out how to use it! However, depending on the setup, a PID loop may not be a good idea. It could essentially mean stalling the motor to hold the mechanism in place, and many of the FRC motors don’ like to be stalled. You may risk burning out the motor. More information would be needed to help you evaluate that risk!
The braking feature on the motor controller only works when the motor is moving. Simply put it provides torque in opposition to the movement proportional to motor speed. IE the faster it turns the more force it supplies. 0 speed=0 braking force. The only electrical solution is to have position feedback and use a positioning control loop.
Most motor controllers have a way to set the controller to lock when no signal is provided. Look into the settings for your motor controller. Most of the time is is as simple as moving a jumper or pressing a button on the controller itself.
There is no “lock” feature on motor controllers. That would require a PID loop with a sensor on the motor output… There is a “brake” feature which, as FrankJ said, can apply some torque to a moving motor… but that is not nearly enough to hold the motor in place as the OP wants.
The best solution in my opinion would be a mechanical brake.
The simplest way to do it with the motor and programming is to experimentally determine the motor power required to hold the robot in position. Then in code you could have the joy stick give you variable power and a separate button that sends the signal of that fixed power level if there is no joy stick input. However there is a big IF doing it that way. If the power level required to hold the robot in position is too high that can cause heat to build up rapidly in the motor. Too much heat and you will let the magic smoke out. Fan cooled motors are more likely to have a problem operating this way than a sealed motor that relies on a large thermal mass to control temps. With Fan cooled motors no rpm equals no cooling.
The brake/coast modes on motor controllers only affects what happens when the motor isn’t being commanded to move. If set to coast mode, then when no signal is applied, the motor is allowed to spin naturally. In brake mode: “When a neutral signal is applied to the Talon SRX in Brake mode, the motor will resist rotation, especially high speed rotation. This is accomplished by essentially shorting the motor leads together, which causes a Back Electromotive Force (Back-EMF) to resist the rotation of the motor.” (from the Talon SRX manual)
On a drivetrain, this will cause the robot to coast to a stop when you let go of the joystick. If set to brake mode, the robot will come to a stop much more quickly.
This is different from commanding a motor (or mechanism) to stay in one place. As others have mentioned, a mechanical brake might be what you want, or a PID loop.
For the benefit of students just learning this stuff:
[meticulous]The spinning rotor generates Back EMF (voltage). If the leads are open, this Back EMF voltage cannot cause current in the coils, so it does not generate a torque to resist the rotation.
But if the leads are shorted, this Back EMF generates current through the motor coils, and this current generates torque opposing the rotation.[/meticulous]
We return you now to your regularly scheduled programming…
If this is for a one-shot mechanism, like climbing up the rope with a winch and then hanging there until your crew comes out to retrieve the bot, then you could buy a cheap ratcheting wrench and fasten it to the spool.