[go: up one dir, main page]

File: AltosMag.java

package info (click to toggle)
altos 1.9.22-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,712 kB
  • sloc: ansic: 120,853; java: 42,921; makefile: 8,573; sh: 4,995; xml: 2,154; pascal: 2,008
file content (135 lines) | stat: -rw-r--r-- 3,590 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Copyright © 2012 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

package org.altusmetrum.altoslib_14;

import java.util.concurrent.*;
import java.io.*;

public class AltosMag implements Cloneable {
	public int		along = AltosLib.MISSING;
	public int		across = AltosLib.MISSING;
	public int		through = AltosLib.MISSING;

	public static final int model_hmc5883 = 0;
	public static final int model_mmc5983 = 1;

	public int mag_model = AltosLib.MISSING;

	public static final double counts_per_gauss_hmc5883 = 1090;
	public static final double counts_per_gauss_mmc5983 = 4096;

	public static double counts_per_gauss(int imu_type, int mag_model) {
		switch(mag_model) {
		case AltosLib.model_hmc5883:
			return counts_per_gauss_hmc5883;
		case AltosLib.model_mmc5983:
			return counts_per_gauss_mmc5983;
		}
		switch (imu_type) {
		case AltosIMU.imu_type_telemega_v1_v2:
		case AltosIMU.imu_type_easymega_v1:
			return counts_per_gauss_hmc5883;
		case AltosIMU.imu_type_easytimer_v2:
			return counts_per_gauss_mmc5983;
		}

		return AltosIMU.counts_per_gauss(imu_type, mag_model);
	}

	public static double convert_gauss(double counts, int imu_type, int mag_model) {
		double cpg = counts_per_gauss(imu_type, mag_model);

		if (cpg == AltosLib.MISSING)
			return AltosLib.MISSING;

		return counts / cpg;
	}

	public boolean parse_string(String line) {

		if (line.startsWith("X:")) {

			String[] items = line.split("\\s+");

			mag_model = model_hmc5883;

			if (items.length >= 6) {
				across = Integer.parseInt(items[1]);
				through = Integer.parseInt(items[3]);
				along = Integer.parseInt(items[5]);
			}
			return true;
		}
		if (line.startsWith("MMC5983:")) {
			String[] items = line.split("\\s+");

			mag_model = model_mmc5983;

			if (items.length >= 4) {
				along = Integer.parseInt(items[1]);
				across = Integer.parseInt(items[2]);
				through = Integer.parseInt(items[3]);
			}
			return true;
		}

		return false;
	}

	public AltosMag clone() {
		AltosMag n = new AltosMag();

		n.along = along;
		n.across = across;
		n.through = through;
		return n;
	}

	public AltosMag() {
	}

	static public void provide_data(AltosDataListener listener, AltosLink link) throws InterruptedException {
		try {
			AltosMag	mag = new AltosMag(link);
			AltosCalData	cal_data = listener.cal_data();

			if (mag != null) {
				if (mag.mag_model != AltosLib.MISSING)
					cal_data.set_mag_model(mag.mag_model);
				listener.set_mag(cal_data.mag_along(mag.along),
						 cal_data.mag_across(mag.across),
						 cal_data.mag_through(mag.through));
			}
		} catch (TimeoutException te) {
		}
	}

	public AltosMag(AltosLink link) throws InterruptedException, TimeoutException {
		this();
		link.printf("M\n");
		for (;;) {
			String line = link.get_reply_no_dialog(5000);
			if (line == null) {
				throw new TimeoutException();
			}
			if (parse_string(line))
				break;
		}
	}
}