# control 2 outputs with a single input and pid

Overview

I'm trying to levitate a constrained permanent magnet with 2 electromagnets. I'm having trouble conceptualizing the control system for such an operation.

Setup

The permanent magnet is fixed onto a horizontal arm and is repelled by an electromagnet above and repelled by an electromagnet below. The pendulum is attached to a rotary encoder that provides the absolute angle of the pendulum with respect to the horizontal. Goal

Given a desired angle (relative to the horizontal), I need to change change the voltage going to each electromagnet. For example, if I want the pendulum to be at +X degrees, the control system would increase voltage to the electromagnet below the pendulum and decrease voltage to the electromagnet above the pendulum, resulting in a net upwards force. Problem

I was advised to use a PID controller. I'd heard of, but never used, a PID controller, so I researched them a bit online, and I understand the general idea--it's just like somebody adjusting water temperature before showering. However, my intuition tells me that I cannot use a single PID loop to control the two electromagnets because I'd be using a single input to solve two outputs. It'd be like saying $$f(x) = y_{1}$$ and $$f(x) = y_{2}$$ where f is a linear function. Since that's not possible for a linear function--and PIDs are linear control systems--then I cannot control 2 motors with a single PID controller.

Am I right? If so, could you point me towards how people have solved similar problems? I feel like this is a common problem with control systems.

If I'm wrong, could you provide an example?

Many thanks!

• It is almost trivial for a single signal to contain two (constrained) pieces of information. Simply encode them in the on period and off period of a pulse train. Jun 26, 2015 at 0:33
• Can you add a drawing/schematic of what you want to do? Jun 26, 2015 at 0:40
• By 'horizontal pendulum' do you mean inverted pendulum? As MathieuL says, you need to provide far more detail of the system.
– Chu
Jun 26, 2015 at 7:49
• I've added a visual of my setup. Jun 26, 2015 at 18:23

Your magnet approach is the only one that is likely to work, but there are a number of gotchas. Be aware that stabilizing an attractive magnetic setup (rather than a repulsive one) is not for the faint of heart, and especially not for beginners. The problem is that the attractive force can get very high very quickly if the two magnets get close together, so the magnets tend to "snap" together faster than your system can respond.

First, there is no need to make the magnet currents independent. I'd advise making the two linearly dependent. If the lower coil current varies from 0 to 1 amp, make the top coil vary from 1 to 0 amp, with the sum constant. This is hardly optimal in terms of performance, since the two repulsive forces will not be linearly related with respect to displacement from the center position, but it will be far easier to control and stabilize. Think ahead - upgrading to independent loops will be a no-brainer for further credit.

Second, your encoder scheme, while OK in principle, is unlikely to work well. The problem is that, unless you have access to a really high-resolution encoder your angle measurements will be too coarse to give the sort of accurate position information you need. Not only that, but measuring angular velocity (the D in PID) will be very coarse in granularity, and for low rates will have a very slow update rate. Get hold of your encoder specs and pendulum arm length, and calculate just what your vertical quantization is. You cannot do better than this.

Third, you'll be able to do a simple open-loop calibration of your system simply by putting differing currents into your coils and seeing where the moveable magnet gets to, although you'll have to be careful about damping out residual motion. This is probably a better bet than trying to calculate position from first principles. See https://en.wikipedia.org/wiki/Force_between_magnets for a discussion of inter-magnet forces. Be aware that these results are very sensitive to small changes in geometry, including the size of your magnet cores.

Yes, you should use PID. However, you have choices as to how the PID relates to the current through each electromagnet:

• Use one PID system, driving each electromagnet equally (you might even wire them in parallel or serial and only use a single driver)
• Use two separate PIDs, one to drive each electromagnet

The second option would be more flexible, but a LOT harder to configure, especially when you aren't familiar with PID design. The first design would be much simpler, but wouldn't work with your "multiple repulsion" model.

Since you state that the permanent magnet is on a "pendulum", I'll presume that you don't have a counterweight, and so there will need to be a steady-state upward force to counter gravity in any steady-state position. You might consider reversing your top electromagnet so that it, too, attracts; that way "pendulum too low" would be translated into "make both electromagnets stronger", and "pendulum too high" would be translated into "make both electromagnets weaker".

I expect you will have trouble with the nonlinearity as the permanent magnet approaches each of the electromagnets. If possible, you might consider adjusting the shape of the permanent magnet (perhaps with pole pieces) to moderate the change in force as it approaches each electromagnet.

• "You might consider reversing your top electromagnet so that it, too, attracts". No! Note that both magnets repel, and there is a very good reason for this. If you set up an attracting magnet, it will become extremely unstable since once the magnet gets close to the coil the attractive force increases very rapidly, and the magnet will be uncontrollably attracted. Jun 26, 2015 at 2:35
• @Daniel Griscom: Yeah, I figured I'd need two separate PID loops. However, I have no idea where to start. Any tips? Jun 26, 2015 at 18:30
• @WhatRoughBeast: Do you know of any models that can predict magnetic force strength at a distance?? (such as a magnetic equivalent of Coulomb's Law for electrostatics or Newton's Law of universal gravitation) I looked online, but it looks like no such model exists. If I could predict the forces on the magnet, then I could work backwards to find the necessary magnetic field strength and then the necessary voltage. Jun 26, 2015 at 18:33