Compass interference, reading dataflash logs

Hi All,

This is my first post here. I am very new to this so please forgive me if I am a little ignorant. I have found the new Ardupilot site fantastic. Great resource.

I would like to use the log captured on my dataflash to see if I have any issues with compass interference. Can someone verify that I have the information correct please.

I am running APM 2.5 on the latest firmware 3.1.2. I have captured the necessary data (IMU and COMPASS) logs and have placed it into a spreadsheet. IMU for vibration COMPASS for compass interference. After capturing the data I turned both IMU and COMPASS off to reduce log file size and free up processor time as per instructions.

When looking this up on the Ardupilot site I noticed that the following was a little misleading:

[color=#400000]Another general thing to check re the compass is the offsets which should bet between -150 ~ 150. These are found in the tlog’s SENSOR_OFFSET group as mag_ofs_x, mag_ofs_y, mag_ofs_z and in the dataflash’s COMPASS message as[/color] [color=#FF0000]OfsX, OfsY, OfsZ[/color]. [color=#400000]They can also be seen in the parameters as COMPASS_OFS_X, COMPASS_OFS_Y, COMPASS_OFS_Z.[/color]

The dataflash COMPASS message is MOfsx, MOfsy and MOfsz. So initially I grabbed the wrong data (I think?) .

In the same section on the web site it also states the following:-
[color=#400000]Dataflash log’s COMPASS message hold the compass’s raw x, y and z axis values (called MagX, MagY, MagZ) which are equivalent to the tlog’s RAW_IMU xmag, ymag and zmag fields. It’s possible to calculate the mag-field length by first loading the dataflash log file into excel, filtering by the COMPASS message and then calculating the mag-field using the formula mag_field = sqrt(MagX^2, MagY^2, MagZ^2). Note that the COMPASS message is not enabled by default in the dataflash logs because it runs at 50hz and does affect CPU performance a bit. [/color]

The formula states “mag_field = sqrt(MagX^2, MagY^2, MagZ^2)” I took all the mag fields and then on each row of a spreadsheet I used the following formula on each “MAG” row. I only show the first row formula here.

=SQRT(POWER(A1,2) + POWER(B1,2) + POWER(C1,2))

Then I take the Max, Median and Minimum values from the column and work out the percentage difference above and below the median value.

Should I be adding the values or multiplying them. The formula given, I guess, is for a function in a programming language of some sort. I am not familiar with the syntax of the statement.

Using the above formula I produced a graph that followed the throttle graph. But I am not sure if the values are correct to calculate the correct percentages. Using the above method I got a completely different value that I received using the compassmot procedure which gave me a reading of over 400%. I think the issue there was that the current readings had not been calibrated as I do not have a way to measure the current at present. So the internal reading could be a mile out.

In the spreadsheet I am not sure if I should take the minimum reading as 0 or to the lowest value once the motors have the craft flying. In the attached image you can see that the blue line (compass Offset) follows the red line (throttle) closely. Should I take the minimum value as zero or should it be around 250. This makes a huge difference the end result. Note: the Red Throttle line was produced in Mission Planner captured and then overlaid onto the Graph in the Spreadsheet.

As the result from this exercise was a figure of around 30% - 35% I have decided to get a GPS/Magnetometer and mount it on the end of a small pole up and away form the electrics. This I hope will prevent any potential issues. It will also lessen any interference with the GPS making it more reliable in Return to Home and Loiter.

The ultimate test will be if it still circles (Toilet Bowls) once the Magnetometer is moved. Though I must admit I was not sure if the circling was due to the wind or to the hardware.

If the above excel formula is correct then I will write a macro that will grab all the necessary data from a log file and process it to give the percentage values. It seems a lot easier and more reliable than the compassmot (rather potentially dangerous) method.

I appreciate any information on this. Thank you.

Regards,

Neil…

Hi again,

Some corrections to the information I posted yesterday which has also raised some more questions.

Firstly I released that I had calculated the incorrect columns. I should have been calculating MagX, MagY and Magz. Which produced the attached graph (Compass1.jpg). I was using the offset figures in the last graph (Not reading instructions properly)

The other odd thing was that most of the data was shown as negative numbers. (Yes I had done a compass calibration before the test)
MagX MagY MagZ
MAG -87 -110 -350
MAG -102 -110 -345
MAG -101 -105 -347
MAG -96 -105 -351
MAG -98 -108 -346

These are the first 5 rows of data. Not quite in the 120 ~ 550 range. So the calculation for the first graph (Compass1.jpg) is not 100 percent correct as the functions treat all the numbers as absolute numbers (No sign). Yet some of the data did climb up to a positive number.

MAG -496 -7 - 245
MAG -511 9 -213
MAG -497 3 -231
MAG -490 0 -239
MAG -504 6 -243
MAG -502 11 -248

In the second attachment (Compass2.jpg) I added 1000 to MagX, MagY, and MagZ did the calculation then subtracted 1000 from the answer.

In the data listed above I would take the first row like this:-

MagX -496 + 1000 = 504
MagY -7 + 1000 = 993
MagZ -245 + 1000 = 755

Then put those figures into the formula:-

345.3959 = (SQRT(POWER(504,2) + POWER(993,2) + POWER(755,2))) - 1000

This produced the graph in the second attachment (Compass2.jpg) which may be a more accurate representation of the data. Series 1 being the Mag_Field and Series 2 being the Throttle In.

This raises two questions:-

  1. Is the formula correct?
  2. Why are my Mag Field readings mostly negative.

My appoligies for this being so long, and for the incorrect information in the first post.

Regards,

Neil…

Where is the log itself?

You would do well to use pymavlink instead of excel. If you want to give me the log I can get you more accurate graphs.

I took a look at the pymavlink web site. As I do not write in Python I would find it difficult to use. That is why I was using Excel, I just need to find a way to handle the negative numbers.

I appreciate the help.

Please find attached the log file.

Neil…

Thanks.

Interestingly, pymavlink needs some work to read this particular text log. I started on that but it just isn’t a great use of time as we are moving to binary logs. A .bin log would be better if you can get one.

I read your post more closely - it looks like you want to compare the length of the magnetic field with the motors. The correct way to do that is not to mess with the individual values, but to take the square root of the sum of their squares. Negative or not, it will get you the right value. So, I believe the “possibly incorrect” graph is correct.

Compassmot should be done on each axis individually, not on the total magnetic field. Take the difference between motor off and motor on each axis, then divide by the difference in current draw, making sure you get the units right (which probably involves looking at the code).

The compassmot calibration procedure is pretty bulletproof. I tend to just hold the copter down or I just don’t throttle up enough to take off. You could ziptie the copter to something really heavy. It needs to not move significantly during the procedure.

If compassmot is giving you 400% values, you have quite a serious problem and APM needs to be mounted further from your power distribution.

Thanks for the reply jschall.

The issue I was having was with having in one axis both positive and negative numbers. By raising them to the power of two it converts them all into a positive number.

-250 and 19 are 269 apart with -250 being smaller. However -250 becomes a much larger positive number when you raise it by the power of two in excel. -205^2 becomes 62500, with 19^2 being 361. Now the smaller -250 is much larger than the 19. This is where my problem came in.

As for compassmot, I am sure that my current readings are out of wack. I have calibrated the voltage but at this stage have no means to adjust the current value. This means that the readings that compassmot is using could be incorrect. I have meter on order so once it arrives I can calibrate the current and re-run compassmot.

I will have to reverse the props and rotate them by one, I don’t want to try and hold 4 x 10" props at the same time try and adjust the settings in mission.

I replaced the GPS yesterday with one that has a compass built into it. The results are very good. The behavior is much better any my hdom has come down significantly, with no GPS glitches. I will be testing Loiter and RTL today and see how they behave.

Thanks for you help. I will have a thing about the formula and how it deals with the negative numbers.

Regards,

Neil…

[quote=“neilhnz”]Thanks for the reply jschall.

The issue I was having was with having in one axis both positive and negative numbers. By raising them to the power of two it converts them all into a positive number.

-250 and 19 are 269 apart with -250 being smaller. However -250 becomes a much larger positive number when you raise it by the power of two in excel. -205^2 becomes 62500, with 19^2 being 361. Now the smaller -250 is much larger than the 19. This is where my problem came in.[/quote]

That is what you want.

Sorry, to clarify, you want to square negative numbers because you’re determining the length of the vector, which can’t be negative. A vector is a line that starts from zero and goes to the point that defines the vector. The length of that line can be determined by the pythagorean theorem (sqrt of the sum of squares of each axis value)

Sorry to interrupt your thread but I was getting ready to post a similar question. I’m also having problems with setting my declination. I regularly fly with declination tuning set to ch6 using values 1-2, 1-3 and as much as 1-5. I’m just not able to get it tuned to a point that I get rid of the problem I’m having.

The problem is when I do big ovals or when I try to make a banking right turn, it seems to resist the turn and giving more and more yaw just causes it to sort of hop around. When I do the same but turning left, it turns easily, in fact too easily and sometimes does an uncommanded 360. I’m sure this is because the declination is set wrong but I’m not sure which direction to change it. Would posting the log help see which way I need to move it?

Here is a flight showing the problem. You can see the bouncing and difficulty to turn left at about 4 minutes.

youtube.com/watch?v=aVi4Vwka … x3e-fXGAPA

[quote=“f11music”]Sorry to interrupt your thread but I was getting ready to post a similar question. I’m also having problems with setting my declination. I regularly fly with declination tuning set to ch6 using values 1-2, 1-3 and as much as 1-5. I’m just not able to get it tuned to a point that I get rid of the problem I’m having.

The problem is when I do big ovals or when I try to make a banking right turn, it seems to resist the turn and giving more and more yaw just causes it to sort of hop around. When I do the same but turning left, it turns easily, in fact too easily and sometimes does an uncommanded 360. I’m sure this is because the declination is set wrong but I’m not sure which direction to change it. Would posting the log help see which way I need to move it?

Here is a flight showing the problem. You can see the bouncing and difficulty to turn left at about 4 minutes.

youtube.com/watch?v=aVi4Vwka … x3e-fXGAPA[/quote]

Please post a log. Declination is not something that needs to be tuned, as it gets set automatically based on your location.

I know it can be auto set by your location (mine is -22° 22’ WEST) but that setting doesn’t work for me and according to the wiki, it can be tuned with ch6. Once the rain stops here, I will post a log. Do I need to enable anything specific in the log?

Is the difficulty I’m having turning left vs turning right an indication that this setting is off? It does seem that way because as I tune it, it gets better but I just can’t get it so the problem completely goes away.

[quote=“f11music”]I know it can be auto set by your location (mine is -22° 22’ WEST) but that setting doesn’t work for me and according to the wiki, it can be tuned with ch6. Once the rain stops here, I will post a log. Do I need to enable anything specific in the log?

Is the difficulty I’m having turning left vs turning right an indication that this setting is off? It does seem that way because as I tune it, it gets better but I just can’t get it so the problem completely goes away.[/quote]

Declination error is tiny. Your problem is almost certainly caused by compass offsets and/or compassmot. If you can send me a log (tlog would be best, else a log with the MAG message enabled), I can take a look.

Hi jschall

Not sure, I am only following the information that is on the Arducopter site. But I see a problem when the log reading fluctuates over the 0 point.

If magY=9 (positive 9) and magX=5 (positive 5) and magZ= 5 (positive 5) then the length is 11.44552
If magY=-9 (negative 9) and magX= 5 (positive 5) and magZ= -5 (negative 5) then the vector length = 11.44552

Seems odd to me.

Any how, I installed an external compass and ran compassmot again and got a reading of 9%. Yet to run a test flight to see if it has fixed the problem.

Thanks for the help.

Neil…
The difference between 9 and 5 is 4, the difference between -9 and 4 is 14

That’s correct, because making those numbers negative only changes the direction of the vector, not the magnitude. The magnetometer outputs the 3-dimensional direction and magnitude of the magnetic field affecting it. Negative numbers are normal.