• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

controller for magnetorquers

Not open for further replies.


New Member
Can anyone help me. im designing a single-axis control system to control two magnetorquers which are placed on the x & y axes. I want to implement a state-feedback controller on a dsPIC microcontroller. How do i get the controller to determine the required torque and implement the current needed to control the torquer dipole moment

I realize the control law will be in the form as T(toqrue) = M(torquer dipole moment) x B(earths magnetic field) and M = NIA, therefore T = MxBy - MyBx, where Mx is the dipole moment of the x-axis torquer and Bx is the x-component of the earths magnetic field. I am using a magnetometer as the sensing device for angular position.
How does the controller come up with the torque to rotate the cubesat to a desired position, and how does this torque convert to a current to create the torquer's dipole moment. I also have to H bridges to change current direction thru the torquers.


Well-Known Member
Most Helpful Member
The only reference I could find for magnetorquers was to control the orientation of satellites. The OP is either a rocket scientist or a joker, neither of which require our help.

If the OP decides to explain the problem in a more understandable manner then help may be forthcoming.



New Member
Its is a school assignment. Magnetorquers are basically like an electromagnet. I wound copper wire around a ferrite rod. Basically have 1800 turns as per my design specs. When current is put through it a magnetic field is generated. This field interacts with the Earths magnetic field causing the torque rod to rotate.

Its an actuating system for a cubesat. I have 2 H-bridge connected to each rod and the dsPIC30F3011 PWM pins. I want to program the controller with the sate-feedback controller , possibly with a PD controleer if its easier so the cubesat turns to preprogrammed angles. For example when turned on it will use the angular position from the magnetometer to go lets say to 45 degrees than to 90 degrees, etc. I just dont get the controller part.

It must compute the torque necessary. But a controller cant just spit out torque it must spit out a voltage to get the correct amount of current through the rods which will produce the dipole moment (M) used to interact with the earths magnetic field (B) causing the torque.

T = M x B
M = NIA N = coil loops I = current A = coil area
T = NIABSinx(x) x = angle between M and B
T = MxBy - MyBx Mx = dipole moment from rod on x-axis
By = Earths y-component magnetic field
taken from magnetometer


My understanding ... such as it is ....
You can use a PWM scheme by establishing an error signal between the required command angle and the existing angle of the satellite, assuming that these sensor readings are available. This error signal could be incorporated into a controller to adjust the PWM command signal to the H-bridge. Such a design might even include a proportional, integral, and derivative modifier of the error signal in order to produce an acceptable controller.

However, the PWM method would not take into account the inertial qualities of the system .... the satellite's moment of inertia ... and in that sense, the PWM scheme would not be optimal. The PWM controller action would simply be a correction of the angular position error. In a non-damping, frictionless environment such as space, the PWM design might experience extended oscillations about the command set point ....

If you require a precise, time-optimal, angular positioning system, ... generally better than PWM ... You would want to think in terms of driving the magnet coils with a servo amplifier, which could provide a bi-directional analog current. For this technique, you would incorporate the Newtonian equations of motion of the satellite, the amplifier characteristics, and the command signal into a model .... generally using a tool such as Matlab or Simulink .... and obtain the state space formulation, including a set of state space gains.

An additional benefit of the state-space design path is that extensive modeling and simulation tools are available.


New Member
Thank you

Thank you for the answers!

User_88 I will definitely look into the servo amp. The satellite will not really go into space but i need to demonstrate control over it.

Basically I have a magnetometer as my sensor that is giving out the actual angle of the cubesat. Since i am only dealing in a single-axis situation the cube will only rotate about the vertical axis (z-axis). Basically the equation of motion is :
T = JX

where T = torque and will be the control variable (u) spit out from the
J = Moment of inertia
X = the second derivative of theta
theta = the angle position

From our measurement J = 0.002

So basically my magnetometer is connected to dsPIC. So the dsPIC has the value of the current angle in a variable called angle.I can put another variable to determine the error angle (e) as the desired(reference) angle - current angle.

Just so my understanding is correct, I can use a PID controller to output the control variable (u). I can use a tuning method to figure out the Kp, Ki, and Kd gains for the proportional part, integral part, and derivative part respectively, and then just program the equation u = Kp[e] + Ki[e] = Kd[e]?

The problem i have understanding is how the controller determines the current necessary to feed through the magnetorquers, to get the required torque.

I thank you very much for your help and answers and get express how grateful i am for them. I apologize if im asking stupid questions here but im trying hard to understand the control theory here.


I found this link referring to setting up a PID scheme for PWM controlled motors .... which may work for your H-bridge actuator:
Proportional Integral Differential PID Motor Control

However, you still have to find a way to connect the PWM index to the H-bridge.
Take a look at this link on H-bridge drivers:
H-Bridge Driver ICs | New product new technology | ROHM

I'm not saying that all the pieces are there yet, but you might be able to do it.

By using the PWM and the PID method referenced here, you are not using the State Space technique. You are just using a position error metric, consisting of the difference between an existing angle and the desired command angle.

The control vector, u, and any similarity to state space control are just not applicable.

If you want to see an example of actual state space design, see this link:
CTM: State Space Tutorial


New Member
Thanks again User_88.

Im still a little confused about how to get the control signal from the controller into a voltage or current form.

Basically as i said above the equation of motion is T = JX
where X = second derivative of theta
theta = anglular position
J = 0.002

I came up with the transfer function 1/(Js^2)
The controller transfer function is the standard pid Kp + sKd + Ki/s

and i used the ziegler-nichols tuning method to get Kp, Kd, and Ki and plotted in matlab the step response and its ok. The thing I am very confused about is that do i ahve to model the magnetorquers too, like come up with their equations and transfer function?
I was looking how a PID controller controls a motor and the motor's electrical circuit is taken into account to obtain a model of it and a transfer function.Because I need away for the controller to output a voltage or current to get the tourquers fired up to move the satellite to the desired angle.

Basically i am going to do an autonomous control of the satellite where it will be preprogrammed to rotate to specific angles in the program. I also have to show the current and torque plot into MATLAB as the sat is rotating.

I also have two Bd6211 Hbridges i will be using. But like i said before the controller has to control the torque bu controlling the current through the torque rod.

How is the controller output turned into a voltage or current, this is the part i am confused about. Like the output of the controller will be in the form:

T = Kp*e + Kd*e + Ki*e

Does whatever T equal to output a voltage or current on the output pin of the microcontroller?


New Member
Im talking to some ppl i know and i think im confusing them so i must be confusing you as well.

Basically im trying to understand when the PID controller comes up with a control signal (u) which is the controller output I must represent that value in the form of a voltage which must be PWM'ed to represent that voltage, because the dsPIC can only spit out a low which is 0 volts and a high which in my case is 3.3 volts since i am feeding the dsPIC with maximum 3.3 volts.

But the controller u output represents a torque value which must be converted into a current (I) to be used in the equation torque =l to M x B where M uses
I in the equation M = NIA.

Do i make any sense, oh boy im confused


New Member
Im talking to some ppl i know and i think im confusing them so i must be confusing you as well.

Basically im trying to understand when the PID controller comes up with a control signal (u) which is the controller output I must represent that value in the form of a voltage which must be PWM'ed to represent that voltage, because the dsPIC can only spit out a low which is 0 volts and a high which in my case is 3.3 volts since i am feeding the dsPIC with maximum 3.3 volts.

But the controller u output represents a torque value which must be converted into a current (I) to be used in the equation torque =l to M x B where M uses
I in the equation M = NIA.

Do i make any sense, oh boy im confused
That is going to be specific to the actual PID code implementation. What ever the controller software uses for it's output variable (number of bits) it will have to be scaled to pass on to the PWM output code such that 0 = 0% duty and max digital output value equals 100% duty cycle.

I know that's not a specific answer but do you get the concept?



New Member
It is staring to make sense, I spent hours and hours online googling PWM and controllers. I get the concept that the controller output will have to be scaled. Im going to use 8-bits so if the controller spits out zero, then the PWM is scaled to zero duty cycle and since I am using 3.3 volts to power the dsPIC if the controller spits out a maximum value then the PWM will be at 100% duty cycle corresponding to 3.3 Volts.

Im using the dsPIC30F3011 and am looking for some examples of code for PWM using a controller feedback. How do i get the duty cycle register to update with the proper values for controlling the angle position.
Is there a formula that i should use to take in the controller output and scale that for 8 bits which then automatically updates the duty cycle register. For that i would have to find out my minimum possible error which is zero and my maximum possible angle error and say like 10% dutcy cycle would correspond an error 10 percent of max error and so PWM'ed at 10% of 3.3 Volts?

Is this correct in my thinking, if anyone knows where i can get some code examples in C please let me know. Thank you all for your help.


New Member
Ok here what i understand so far. Ive been reading this website which he uses two motors conneceted to two h-bridges. One motor is for left forward and reverse motion. The other motor is for right forward and reverse motion:
PIC Tutorial  Eight - using the PWM hardware

He uses 8 bit resolutuion where 0 - 127 is for forward and 128 - 255 is for reverse where the most significant bit (the 8th bit) determines direction. Therefore for a half speed forward the value for duty cycle is 64 (128/2), and for half speed reverse the duty is 192 (64 + 128). This is kind of on the same analogy as my magnetorquers which are placed on the x and y axis and need to be able to change polarity.

He doesnt use any controller feedback so I am confused how i will take the controller feedback to come up with a duty cycle.


New Member
"He doesnt use any controller feedback so I am confused how i will take the controller feedback to come up with a duty cycle."

In your application you will be using your independent magnometer as your feedback signal and it will be scaled and input to the PID controller as the process input variable (PV). The PID continously takes the difference between the PV and the setpoint value to calculate an error value and generates the new output (duty cycle) value. The PID controller does the heavy lifting as long as you correctly scale the inputs and outputs.

Last edited:


New Member
Ok heres what Ive accomplished so far. I figure that the easiest thing to do for the PWM part is to use complementary mode. So i use the PWM1H and PWM1L pins as one complementary pair to one magnetorquer and PWM2H and PWM2L pins as the other complementary pair to the other magnetorquer, I should say the PWM pins are connected to the h-bridge.

Therefore at 50% duty cycle the average power sent to the torquer is zero because PWM1H pin is going to be high for half the PWM period and the other half will have PWM1L pin high. Since I am using 8-bit resolution I have scaled that at a value of 127 this will represent a 50% duty cycle and cause no power flow. At a byte value of 0 will means maximum power is flowing in a negative polarity (-3.3 volts)while at 255 byte value the power will be maximum in a positive polarity (+3.3 volts)
I charted this and came up with the equation Voltage = 6.6 * (Duty cycle %) - 3.3. Which at 0% duty cycle represents a max voltage of -3.3 volts, at 50% voltage is 0 volts and at 100% voltage is +3.3 volts, and everything in between.
Please tell me if this makes sense?

Now the controller has to determine an appropriate duty cycle needed from the error signal. Just to let you know how i have implemented the controller I have my magnetometer sense the angular position in radians.

I have the controller output (u) is equal to Kp*error + Ki *integral(error) + Kd*derivative(error).
Then to find Kp I let Ki and Kd equal zero, then plotted the controller step response and set Kp to a value where the response started to oscillate. Then i used Zieglar-Nichols tuning method to find Ki and Kd.
The values i got are Kp = 0.000032274, Ki = 0.000001614, and Kd = 0.001613,
and i plotted it and it tracks the step input. Please let me know if this makes sense and if not please let me know what iim doing wrong.
So how do i get the controller output to determine the duty cycle? Lefty when u say i have to scale the magnetometer output is that for an analog output which luckily i dont have and i forgot to mention before, sorry.

Thank you lefty and User_88 for your help, I appreciate it so much. Any further help is greatly appreciated. Thanks so much!


New Member
ok PID is kind of screwed up so im going to use state-space and its going to be in the form of x_dot = Ax + B(-Kx). A = [ 0 1; 0 0]
B = [ 0; 1/J]
C = [ 1 0]
D = 0

my two states are Θ and -dot. Θ will come from the magnetometer now Θ-dot will have to be taken from the derivative of the magnetometer data. Does anyone know how i implement this in C code? And K will be found from MATLAB.

Basically I have determined my control law (u) which will spit out a torque to change the angular position of the satellite. Since Torque will come from the cross product of the dipole moment of the magnetorquers (M) and the earths magnetic field (B), and M = NIA, T = BNIA
= BNA(V/R)
where V is the input to the magnetorquer circuit and R is the circuit resistance which will create a current I. Since T will be the controller output and V = 6.6*(duty cycle) - 3.3, the equation can be rearranged where duty cycle is the dependent variable. The controller will spit out T and this will set a duty cycle. B will come from the magnetometer reading.
Please let me know if this makes sense. After figuring this out i must figure out how to take the derivative of the magnetometer data and put this in the program.


... suggestion ...

If you have a working, open loop prototype of your physical plant, that is, a magnetorquer pair, with a rotational mass, and a magnetometer to provide angular position data, including the h-bridges and power supply, then take a look at the Matlab System Identification Toolbox.

The idea is to use the SI Toolbox to get a Laplace Transform version of the open loop, i.e. non-controlled, plant model.

As I understand it, the basic idea is to use a swept frequency sinusoidal signal as an input to the plant, and obtain the resulting output variable characteristics. If you could find a way to get appropriate data files for the output angle magnitude and phase, that might be sufficient for input to the software. As part of this step, you would have to have a practical means of transforming the sinusoidal signal into a PWM form that could be used by the h-bridge.

A separate approach to input data to SI Toolbox is to use first principles, which consist of equations of motion and mathematical relations. The problem here is that you have certain elements, such as the h-bridge driver, which are not be easily modeled.

If you can obtain the experimental data files resulting from the sinusoid input, then everything would be encompassed, and you don't have to worry about a mathematical model from first principles.

Once you have a Laplace Transform of the open loop plant, you can choose a control law that actually makes sense. It might be some form of PID. It could be a type of pole/zero filter. When you are able to plot the open loop root locus of the actuator and physical plant, you will be able to look at the control situation with insight.

HP used to make a sophisticated machine to accomplish this modeling process.
HP's device used a swept sinusoidal signal.
I think that you can do the same thing with Matlab System Identification Toolbox.
Not open for further replies.

Latest threads

EE World Online Articles