How to make a motor hold it's position

Hello There,

We are using an elevator to lift our cargo in this years challenge but every time we move the elevator up it slips back down, how can we get it to hold it’s position? Here is our current code:
if (m_driveStick.getPOV() == 0) {
m_Elevator.set(0.7);
System.out.println(“The elevator goes up.”);

	}
	
	if (m_driveStick.getPOV() == 180) {
		m_Elevator.set(-0.5);
		System.out.println("The elevator goes down.");
	}

Thanks in advance!

The ideal way would be to implement some sort of feedback. This is typically done in FRC with a PID controller, which reads a sensor to detect the current position of your system (in this case, the height of your elevator) and then generates an error based on that and your setpoint (the desired position for your system). There are a lot of tutorials concerning PID on Chief Delphi and on the internet in general. Additionally, you can find implementations of PID on the WPILib libraries and built into certain speed controllers, like the Talon SRX.

If you want a quicker solution, I’d imagine that using a feed-forward term could help to maintain the carriage position. You’d have to calculate the necessary torque to hold the carriage still, transform that to voltage using a DC motor model (look into 971’s Modelling workshop or the State Space Guide by 3512) and scale that to units between -1 and 1 for the code (divide by the battery voltage). Then, whenever you want to hold the motor still, you’d send that value to the speed controllers. Be careful that the holding voltage will change based on the carriage mass.

2 Likes

We are using a Talon SRX, so how would we set up PID on it? Can you link a tutorial or something?

1 Like

There are numerous threads on CD about setting up/tuning PID on the Talon, and I’d suggest you go and search around a bit for some information that suits your needs. Here’s the official documentation for PID on the Talon SRX:

https://phoenix-documentation.readthedocs.io/en/latest/ch16_ClosedLoop.html

1 Like

PID is one path. You could also try a mechanical solution with elastics, like a constant force spring

2 Likes

Counterbalancing helps a ton. This season and last we have been able to perfectly counter balance our elevator (plus resistance in system) to hold it at a fixed height. Constant force springs are incredibly useful, and relatively simple to design for.

2 Likes

Using PID requires sensor feedback, meaning that you have to add an encoder of some type to the system. Which type is easiest to add, kind of depends on what gearbox you are using to actuate the elevator.

Pictures of your robot will let folks be able to help you more easily, as we won’t have to guess about things.

1 Like

In PID control, the I term is what will compensate your droop.

If you want a quicker solution, just drop your drive voltage/current to a non zero value when you stop the motor. You’ll probably want this parameter accessible from the driver station, because it might change from match to match.

Also be careful that not all motors can hold a position. If you try it with a redline motor at more than around 3V you’ll damage the motor. There is a CD thread that started studying this topic.

1 Like

We like our programmers. That’s why we put constant force springs in our elevator design. A ten pounder on each side. With them, and setting the lift motor controller in brake mode, our elevator stays where we put it. Now our programmers can tune their PID for motion control, not to hold up the mechanism.

4 Likes

Note: this is easy for cascade elevators, harder for continuous. We build a pneumatic brake into the elevator gearbox to keep the manipulator in place.

1 Like

Do you have an encoder on the motor?
That would be necessary to feed any kind of control loop.

1 Like

we do have an encoder on the motor, what is the code to set up PID on the motor?

1 Like

I have also seen elsewhere that putting the Talon SRX in break mode would have the same effect, how would I do that?

You should read the CTRE Phoenix documentation and then come back with specific questions.

brake. as in “resist motion”

Brake mode on all FRC brushed motor controllers works by “shunting” together the two sides of the motor. Essentially, this makes the motor “fight” any velocity you put into it (a motor is also a generator, connecting the leads together puts that generated power back into the motor resisting rotation). However, depending on gearing/weight of your mechanism, it might not be enough to hold it in place, and if enough external force is applied it will move. The advantage of brake mode is that it can be active even when the robot is disabled.

TL;DR brake mode doesn’t actually hold a motor in place actively, but it might be enough to do what you’re trying to do.

1 Like

Since you are late in the game, MotionMagic is probably your quickest path to success:

Start here and follow each direction, do not skip steps.

Then you can follow the steps here to have your elevator work with smooth motion to the target.

You have two different choices for PID control in the talon to do what you want to do: MotionMagic, or Position (Closed-loop). Both of them work, but they operate slightly differently. Again, since you are brand new to it, I would highly suggest MotionMagic, it is the easiest (and safest for the robot) method to having smooth motion to the position you desire.

If you follow my advice and choose MotionMagic, for the positions where you need the elevator to hold against gravity you would then need to calculate the Arbitrary Feed Forward value and include it with your MotionMagic mode.

CTRE provides examples for MotionMagic with Auxillary (or Arbitrary) Feed Forward in their documentation.

If you want to see how we use it, our code is public. Here is our Arm subsystem using MotionMagic for control (check out the move_MM method).

Thanks to everyone who replied, we were able to find a solution thanks to all of your suggestions.

1 Like

What solution did you settle on ?

Please give a link to the thread about redlines motors you mentioned