Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1688 | - | 1 | package dongfang.mkt.configuration; |
2 | |||
3 | import java.io.File; |
||
4 | import java.io.IOException; |
||
5 | import java.io.InputStream; |
||
6 | |||
7 | import javax.xml.parsers.DocumentBuilder; |
||
8 | import javax.xml.parsers.DocumentBuilderFactory; |
||
9 | import javax.xml.parsers.ParserConfigurationException; |
||
10 | import javax.xml.xpath.XPath; |
||
11 | import javax.xml.xpath.XPathConstants; |
||
12 | import javax.xml.xpath.XPathExpressionException; |
||
13 | import javax.xml.xpath.XPathFactory; |
||
14 | |||
15 | import org.w3c.dom.Document; |
||
16 | import org.w3c.dom.Element; |
||
17 | import org.w3c.dom.NodeList; |
||
18 | import org.xml.sax.SAXException; |
||
19 | |||
20 | import dongfang.mkt.frames.WriteParamSetRequestFrame; |
||
21 | |||
22 | public class MotorMixer { |
||
23 | public static final int EEPROMVERSION = 11; |
||
24 | |||
25 | public static final int MAX_MOTORS = 12; |
||
26 | public static final int MIX_PITCH = 0; |
||
27 | public static final int MIX_ROLL = 1; |
||
28 | public static final int MIX_THROTTLE = 2; |
||
29 | public static final int MIX_YAW = 3; |
||
30 | public static final int MIX_OPPOSITE_MOTOR = 4; |
||
31 | |||
32 | private int getByteCount() { |
||
33 | return MAX_MOTORS * 4 + MAX_MOTORS; |
||
34 | } |
||
35 | |||
36 | void rowToXML(int[] row, StringBuilder out) { |
||
37 | out.append("<motor throttlePart=\"" + row[MIX_THROTTLE] + "\" pitchPart=\"" + row[MIX_PITCH] + "\" rollPart=\"" + row[MIX_ROLL] + "\" yawPart=\"" + row[MIX_YAW] + "\" oppositeMotor=\"" + row[MIX_OPPOSITE_MOTOR] + "\"/>"); |
||
38 | } |
||
39 | |||
40 | private int[][] matrix = new int[MAX_MOTORS][]; |
||
41 | |||
42 | public void setMatrix(int[][] matrix) { |
||
43 | this.matrix = matrix; |
||
44 | } |
||
45 | |||
46 | public int[][] toBinary() { |
||
47 | return matrix; |
||
48 | } |
||
49 | |||
50 | public String toXML() { |
||
51 | StringBuilder result = new StringBuilder(); |
||
52 | result.append("<motorMixer eepromVersion=\"" + EEPROMVERSION + "\" length=\"" + getByteCount() + "\">\n"); |
||
53 | for (int i = 0; i < MAX_MOTORS; i++) { |
||
54 | int[] row = matrix[i]; |
||
55 | result.append(" "); |
||
56 | this.rowToXML(row, result); |
||
57 | result.append("\n"); |
||
58 | } |
||
59 | result.append("</motorMixer>\n"); |
||
60 | return result.toString(); |
||
61 | } |
||
62 | |||
63 | private void parseXMLParameterSet(InputStream input) throws IOException { |
||
64 | DocumentBuilderFactory saxfac = DocumentBuilderFactory.newInstance(); |
||
65 | saxfac.setValidating(false); |
||
66 | try { |
||
67 | DocumentBuilder bldr = saxfac.newDocumentBuilder(); |
||
68 | Document doc = bldr.parse(input); |
||
69 | |||
70 | XPath xpath = XPathFactory.newInstance().newXPath(); |
||
71 | String s_eepromVersion = xpath.evaluate("/motorMixer/@eepromVersion", doc); |
||
72 | int eepromVersion; |
||
73 | |||
74 | try { |
||
75 | eepromVersion = Integer.parseInt(s_eepromVersion); |
||
76 | } catch (NumberFormatException ex) { |
||
77 | System.err |
||
78 | .println("The motorMixer element must have an 'eepromVersion' attribute with a numerical value (eg.<motorMixer eepromVersion='1'>)"); |
||
79 | System.exit(-1); |
||
80 | } |
||
81 | |||
82 | NodeList rowNodes = (NodeList) xpath.evaluate( |
||
83 | "/motorMixer/motor", doc, XPathConstants.NODESET); |
||
84 | |||
85 | for (int i = 0; i < rowNodes.getLength(); i++) { |
||
86 | Element e = (Element) rowNodes.item(i); |
||
87 | int[] row = new int[5]; |
||
88 | |||
89 | String s_value = e.getAttribute("throttle"); |
||
90 | row[MIX_THROTTLE] = Integer.parseInt(s_value); |
||
91 | |||
92 | s_value = e.getAttribute("pitch"); |
||
93 | row[MIX_PITCH] = Integer.parseInt(s_value); |
||
94 | |||
95 | s_value = e.getAttribute("roll"); |
||
96 | row[MIX_ROLL] = Integer.parseInt(s_value); |
||
97 | |||
98 | s_value = e.getAttribute("yaw"); |
||
99 | row[MIX_YAW] = Integer.parseInt(s_value); |
||
100 | |||
101 | s_value = e.getAttribute("oppositeMotor"); |
||
102 | row[MIX_OPPOSITE_MOTOR] = Integer.parseInt(s_value); |
||
103 | |||
104 | matrix[i] = row; |
||
105 | } |
||
106 | input.close(); |
||
107 | } catch (ParserConfigurationException ex) { |
||
108 | // Should never happen. |
||
109 | throw new RuntimeException(ex); |
||
110 | } catch (SAXException ex) { |
||
111 | System.err.println(ex); |
||
112 | System.err |
||
113 | .println("There is something screwed with your XML document. It is not well-formed and won't parse."); |
||
114 | throw new RuntimeException("Parse error."); |
||
115 | } catch (XPathExpressionException ex) { |
||
116 | // Should never happen. |
||
117 | throw new RuntimeException(ex); |
||
118 | } |
||
119 | } |
||
120 | } |