Tuesday, October 9, 2012

Inertial measuring experiment

I just posted a video on YouTube, and wanted to comment on that shortly.

What I did was that I connected my navigation board (more on that in a post later) to my receiver. The receiver was sending data from the navigation board down as telemetry, which I recorded. At the same time, I also recorded video using an 808 #11 key chain camera. As the actual test, I just walked around our house holding the device in my hand and pretending it was a space ship.

Here is a picture of the sophisticated sensor instrument:



I used MATLAB to produce a visualization of the measured attitude. This visualization was then synchronized with the video and overlaid to produce the clip uploaded to YouTube. The whole visualization process is very naive, but as a first try it'll do :-).

Saturday, October 6, 2012

Attitude inference

In my grand plan of things, I want to infer both the attitude and location of the plane. Location is somewhat easy to figure out thanks to GPS, but attitude is really a problem. Now, you might think that using an accelerometer or an inclinometer would be the solution. Unfortunately this is not the case, and the reason for this is very closely related to the so-called pendulum rocket fallacy.

The fact that accelerometers can be used to infer the orientation of a cell phone follows directly from the fact, that you can (for the most part) assume that the phone is held stationary in a users hand. The moment this assumption is no longer true, you can't directly measure the direction of gravity. To me this was quite a difficult thing to understand. My intuition originally was that you could always measure gravity as you did in the cell phone. It wasn't until later, when I did a simulation of the attitude measurements, that I realized the acceleration measurements didn't give any information on the direction of gravity. Anyway, if you don't believe me on this, there are some good explanations out there.

Okay. So how do you know which way is up? Very few methods allow you to measure your absolute attitude directly. Many of the methods give you only a part of your attitude and you have to combine many methods, to get your absolute attitude (roll, yaw, pitch). An incomplete list of possible methods:
  1. Guide stars. This is the only method I can think of right now which will actually give your absolute orientation. It only works at night. Requires you to know where you are and what time it is (GPS). Very difficult to measure.
  2. Horizon measurement. This could be either optical or thermal. Thermal works also during night, but not as well in the winter or in heavy overcast conditions. Can at most give you roll and pitch. Combined with GPS heading readings will give full absolute attitude. Sensors are quite expensive. Easy to measure.
  3. Earth's magnetic field measurements. Gives two components of the attitude. On the magnetic poles these components will correspond with roll and pitch. Combined with suitable other means of attitude inference can give full absolute attitude. Easy to measure.
  4. Polarization of the sky. Can be used to infer the components of the attitude, if you know where you are and what time it is (GPS). Combined with e.g. magnetic field measurements can give absolute attitude. Difficult to measure.
  5. Gyroscopes. Gyroscopes can be used to measure the angular velocity at which the plane is rotating. Hence they give you the changing rate of attitude. If you knew your attitude at start, and you had a perfect gyroscope, you could track your changes in attitude and thus know your current orientation. Unfortunately, there is no such thing as a perfect gyroscope (plus you'll have integration error anyway) and you end up with a drift. However, the drift can be quite low even with cheap solid state gyroscopes (in the order of a fraction of a degree per minute). Combined with e.g. magnetic field measurements can give absolute attitude (with a caveat). Easy to measure.
I want to only do easy stuff, so I can only really consider horizon measurement, magnetic field measurement and gyroscopes. Horizon measurements are a bit on the expensive side, and they also have problems during overcast conditions and winter (which sums up to about 90% of the time in Finland), so I'll have to go with magnetic fields and gyroscopes.

As luck would have it, some nice Chinese folks are selling small IMU boards on ebay. They have everything I need for attitude measurement, as well as an accelerometer :-).

Let's begin with the first thing. How to represent the attitude of the plane? The first thing that comes to mind is the classic roll, yaw, pitch formulation, also known as Tait-Bryan angles, or improper Euler angles. This formulation is ultimately the form I would like to look at the attitude, so it would make sense to store them internally in this form as well. This formulation however has a nasty feature known as gimbal locking. Also, the differential of the rotation has a form, which is heavy on computation (a lot of trigonometric functions, that would need to be evaluated at each time step). So this is not the way to go. I hate to say it (because I've been against the idea in the past), but a better solution is to use Euler rotation parameters, also known as unit quaternions. This formulation is minimal on computational complexity and it easily converts to Tait-Bryan angles when needed. See these lecture notes (starting from about lecture 7) for a very good tutorial on different rotation formalisms and their uses. Wikipedia also has an article about this, but it is not as good in my opinion.

The most important reason for using the quaternion formulation of rotations is the absolute simplicity of the rotation time derivative. It is given simply as
,
where q is the rotation quaternion, \omega_x, \omega_y and \omega_z are angular velocities, which are measured by the gyroscope and the \otimes is the quaternion product. Using explicit Euler integration, a pseudo code implementation of a rotation update given the instantaneous angular velocity measurement (wx, wy, wz) is

updaterotation(wx,wy,wz) {
   /* old rotation defined by rqs, rqx, rqy, rqz
      length of time step dt */
   rqs_up=rqs+dt*0.5f*(-rqx*wx-rqy*wy-rqz*wz);
   rqx_up=rqx+dt*0.5f*(+rqs*wx-rqz*wy+rqy*wz);
   rqy_up=rqy+dt*0.5f*(+rqz*wx+rqs*wy-rqx*wz);
   rqz_up=rqz+dt*0.5f*(-rqy*wx+rqx*wy+rqs*wz);
   /* normalize the quaternion */
   rqinvnorm = 1.0f/sqrtf(rqx_up*rqx_up+
                          rqy_up*rqy_up+
                          rqz_up*rqz_up+
                          rqs_up*rqs_up);
   rqx=rqx_up*rqinvnorm;
   rqy=rqy_up*rqinvnorm;
   rqz=rqz_up*rqinvnorm;
   rqs=rqs_up*rqinvnorm;
}

That takes care of integrating the angular velocity measurements to approximate our attitude. Now to get rid of the gyroscope drift by using measurements on the absolute direction of the magnetic field. As I only have very limited computational power on board, I had to come up with a really simple way to correct for the drift. I originally wanted to do something along the steps of the extended Kalman filter (extended Kalman filter because it was the least computationally expensive non-linear flavour I could think of), but it would end up using quite a deal of memory since in addition to the current best estimate of the attitude, the covariance matrix of the estimate must also be stored. Most of all, it would also use quite a bit of CPU time, since for each update we need to solve a general 3 unknown linear equation. It probably would work though, but I didn't want to go through the hassle of testing how close it would be.

What I ended up doing can in some sense be explained as approximating the extended Kalman filter, by using a constant scalar covariance for the states. More precisely I did the following. When the plane is in its starting position and we know its attitude, we take a measurement of the magnetic field. This magnetic field direction, as well as the initial attitude, are stored. In the future, since we know what the magnetic field was at the initial attitude, we can compute what the magnetic field should be if we knew our attitude. Let's call the magnetic field measurement m, the actual attitude q and the function which connects the two h. Hence


However, we don't know our actual attitude, we know only an estimate q_est, so in general


As the equality should hold for the correct attitude, it would make sense to change the estimate so that the equality would hold. This however is not a good idea. The solution to the equation lacks uniqueness, and even if we had uniqueness, any measurement errors in the magnetic field would be amplified. In addition, the exact solution takes a lot of computational power, which we don't have. What we do instead, is that we simply take a small step in the negative gradient direction. If you don't know what that means, it just means that we change the estimate only by a little amount, but so that the new estimate is slightly better. The update is then


where \alpha is a relaxation parameter, which defines how long the steps should be that the algorithm takes. The lower the value, the slower the estimate converges. The higher it is, the more unstable the update is.

The algorithm to correct for a magnetic field measurement (mx, my, mz) in pseudocode
correctrotation(mx,my,mz) {
   /* old rotation estimate defined by rqs, rqx, rqy, rqz
      north direction in initial coordinates is gmx, gmy, gmz. */
   /* compute difference between estimated and measured north direction */
   mx_diff=mx-(+rqs*(gmx*rqs+gmy*rqz-gmz*rqy)
               +rqx*(gmx*rqx+gmy*rqy+gmz*rqz)
               -rqy*(gmz*rqs+gmx*rqy-gmy*rqx)
               +rqz*(gmy*rqs-gmx*rqz+gmz*rqx));
   my_diff=my-(+rqs*(gmy*rqs-gmx*rqz+gmz*rqx)
               +rqx*(gmz*rqs+gmx*rqy-gmy*rqx)
               +rqy*(gmx*rqx+gmy*rqy+gmz*rqz)
               -rqz*(gmx*rqs+gmy*rqz-gmz*rqy));
   mz_diff=mz-(+rqs*(gmz*rqs+gmx*rqy-gmy*rqx)
               -rqx*(gmy*rqs-gmx*rqz+gmz*rqx)
               +rqy*(gmx*rqs+gmy*rqz-gmz*rqy)
               +rqz*(gmx*rqx+gmy*rqy+gmz*rqz));
   /* update rotation based on measured north direction error */
   rqs_up=rqs+alpha*(+mx_diff*(+gmx*rqs+gmy*rqz-gmz*rqy)
                     +my_diff*(-gmx*rqz+gmy*rqs+gmz*rqx)
                     +mz_diff*(+gmx*rqy-gmy*rqx+gmz*rqs));
   rqx_up=rqx+alpha*(+mx_diff*(+gmx*rqx+gmy*rqy+gmz*rqz)
                     +my_diff*(+gmx*rqy-gmy*rqx+gmz*rqs)
                     -mz_diff*(-gmx*rqz+gmy*rqs+gmz*rqx));
   rqy_up=rqy+alpha*(-mx_diff*(+gmx*rqy-gmy*rqx+gmz*rqs)
                     +my_diff*(+gmx*rqx+gmy*rqy+gmz*rqz)
                     +mz_diff*(+gmx*rqs+gmy*rqz-gmz*rqy));
   rqz_up=rqz+alpha*(+mx_diff*(-gmx*rqz+gmy*rqs+gmz*rqx)
                     -my_diff*(+gmx*rqs+gmy*rqz-gmz*rqy)
                     +mz_diff*(+gmx*rqx+gmy*rqy+gmz*rqz));
   /* normalize the rotation */
   rqinvnorm = 1.0f/sqrtf(rqx_up*rqx_up+
                          rqy_up*rqy_up+
                          rqz_up*rqz_up+
                          rqs_up*rqs_up);
   rqx=rqx_up*rqinvnorm;
   rqy=rqy_up*rqinvnorm;
   rqz=rqz_up*rqinvnorm;
   rqs=rqs_up*rqinvnorm;
}

These algorithms are easily light enough so that they can run on a low-end microcontroller, such as the ATmega328, over 200 times per second. In my tests, even after a long period (hours) of rotating the sensors around, the accumulated error is in the order of 5 degrees, which is about the accuracy of the magnetometer. However, I haven't as of yet done any in flight tests. I'm a bit worried of how much the large currents of the motor affects the magnetic field measurements.

Friday, October 5, 2012

Receiver v2 hardware design & build

As of this writing the receiver v2 is still somewhat of a work in progress, but the design of the main hardware is mostly done. In this new revision I wanted to address a couple of issues that I had with the old receiver, mainly that the old receiver was large, heavy, fragile and complex. The new receiver was thus supposed to be small, light, sturdy and simple :-). I also wanted to finally implement antenna diversity, which I never got around to do in the old receiver. In addition, I decided to dump the analog inputs altogether in favor of an external battery management board connected via I2C and to only have 8 servo channels.

The target size was 2 x 1 inches, which is about the footprint of the regular commercially available receivers. I originally hoped to get everything to fit on one board of that size, but it soon became apparent that if I were to manufacture the board myself using photoresist methods (two-sided this time, however), I would need to split the design in two. I might still do a single board version of the receiver, which could be manufactured by a commercial board manufacturer, for instance through ITEAD studio and their cheap prototyping service. Anyway, the boards of this new design are simply
  1. Connector board (schematic, layout)
  2. Main board (schematic, layout top, bottom, both)
The connector board is completely passive. As its name suggests, it only contains connectors. It mounts on the main board through a pin header and breaks out the servo connectors as well as I2C and UART.

Like before, I didn't want to buy too many parts specifically. So, many of the choices of components were dictated by what I had in my collection. In the end, I only had to order the AS169 antenna selector switch. A local electronics store Partco offers a quasi-cheap way to order a low quantity of components from wholesalers like Farnell. Normally for small orders Farnell would charge a fixed price for shipping and handling, which is in the order of tens of euros. Partco makes larger orders once per week, which make the handling fees more reasonable per item. It still ends up costing me around 25-50% more per component than if I ordered in bulk, but as we're talking about a couple of 0.50€ components for prototyping, it doesn't really matter.

For the RF design, as I am working with 868 MHz (~345mm) and the RF signal path lengths on the board are in the order of 20 millimeters, I was confident I didn't need to worry about impedance matching. Assuming perfect ground, the characteristic impedance of the signal paths would be around 75 ohm, which is way off the target of 50 ohm.

I took some photos along the way, as I was assembling the main board

Bare board, bottom side. Right after drilling and removing the photoresist.
The board looks covered in holes, but those are actually drops of water.
This board was my second ever two-sided board. The first was a success, so I was not too worried about this one.

Top side, with vias soldered in place.
Getting via holes copperized is something I don't know how to do at home. I understand that in commercial fabrication they actually begin (I mean even before applying the photoresist) by drilling the holes and then building a copper layer inside the holes by electrochemical methods. What I do is I push a copper wire through the via hole and cut it almost flush with the board. It leaves quite a bump on the board after soldering, so I can't have vias under components. This makes the board quite a deal bigger than what it could be.

Top side. Much of the components soldered.

Bottom side. Everything except the pin header and output series resistors in place.
As I got most of the stuff in place I tested that I could talk with the processor. This was just to make sure everything was working correctly before attaching the radio module, which is the most expensive component in the board by a factor of two. In the pictures, along with the radio module the top side is also missing the antenna selector switch, which I had to order specifically (I didn't have a hand-solder friendly version in my junk box). The bottom side is still missing the large female header as well as output channel protection resistors. I didn't want to end up killing the receiver if I accidentally shorted the servo connectors or if I plugged them the wrong way around. They limit the short-circuit current to the absolute maximum rating of the processor GPIOs. None of the servos I am using seem to have any ill effects due to the resistors or due to the fact that the signal fed to them is only 3.3V high. Getting rid of the resistors and instead being really careful, or using 0603 size instead of 0805 could help make this board still smaller, if I ever go for that.

Top side. Everything except for the antenna selector switch soldered.

Bottom side. Everything in place.
Here the board is otherwise complete, but I'm still waiting for my order of the antenna selector to arrive. I also haven't yet attached the antennas.


While waiting for the antenna selector, I assembled the connector board and tried it on. The two white connectors in the picture below are (from left to right) the I2C interface and the UART interface. The I2C is for connection to a battery management board, which will contain a high side battery current and voltage measurement IC (I found a suitable one from Texas Instruments, but haven't yet ordered it). In the current plan, the UART will be used to interface a navigation board that infers the attitude and the position of the plane (or other platform) from inertial measurements (gyroscopes), Earth's magnetic field (magnetometers) and GPS. More on that in a later post.



Connector board is completed and tested on.

View from the back.

The receiver is looking pretty good. I'm a bit worried about the strength of the connection between the main board and the connector board. My plan is to stick the connector board down to the main board with double sided foam tape. That way there is less stress on the header.

A close up, now with the antenna selector and the antennas connected.

The AS169 comes in a SOT-23-6 package,
which is among the largest cases for these things.

The antennas are just RG-174 coax, which have ~1/4 wavelength (82 millimeters) of the shield stripped at the end. These are by no means optimal antennas, and it is something I still need to look into. The idea anyway is to have one of the antennas vertically polarized and the other horizontal. This way I'm hopefully less likely to lose the connection due to orientation, or at least have less variation in the received signal strength. However, there still will be an orientation at which both of the antenna polarizations are orthogonal to the transmitter antenna. This is probably just a theoretical problem of ideal antennas, since with my old receiver I've been flying with just one vertically polarized antenna at a distance of 1.5km without any problems. Another choice would be to go with circular polarized antennas, but the size of them at 868 MHz seems a bit intimidating.

The bounding box of the entire assembly (not including the antennas) is approx. 51 x 26 x 22 millimeters. A plastic case would be very nice. I'll probably investigate getting one printed at Aalto University's fablab in the near future.