Rev 1613 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1611 | - | 1 | package dongfang.mkt.main; |
2 | |||
3 | import java.io.FileInputStream; |
||
4 | import java.io.FileWriter; |
||
5 | import java.io.IOException; |
||
6 | import java.io.InputStream; |
||
7 | |||
8 | import javax.xml.parsers.DocumentBuilder; |
||
9 | import javax.xml.parsers.DocumentBuilderFactory; |
||
10 | import javax.xml.parsers.ParserConfigurationException; |
||
11 | import javax.xml.xpath.XPath; |
||
12 | import javax.xml.xpath.XPathConstants; |
||
13 | import javax.xml.xpath.XPathExpressionException; |
||
14 | import javax.xml.xpath.XPathFactory; |
||
15 | |||
16 | import org.w3c.dom.Document; |
||
17 | import org.w3c.dom.Element; |
||
18 | import org.w3c.dom.NodeList; |
||
19 | import org.xml.sax.SAXException; |
||
20 | |||
21 | import dongfang.mkt.comm.FrameQueue; |
||
22 | import dongfang.mkt.comm.MKConnection; |
||
23 | import dongfang.mkt.comm.serial.RXTXSerialPort; |
||
24 | import dongfang.mkt.configuration.IMUConfiguration; |
||
25 | import dongfang.mkt.frames.ReadIMUConfigurationRequestFrame; |
||
26 | import dongfang.mkt.frames.ReadIMUConfigurationResponseFrame; |
||
27 | import dongfang.mkt.frames.WriteIMUConfigurationRequestFrame; |
||
28 | import dongfang.mkt.frames.WriteIMUConfigurationResponseFrame; |
||
29 | |||
30 | public class IMUConfigurator { |
||
31 | private static void configure(String portIdentifier, WriteIMUConfigurationRequestFrame frame) throws IOException { |
||
32 | MKConnection port = new RXTXSerialPort(); |
||
33 | port.init(portIdentifier); |
||
34 | FrameQueue q = new FrameQueue(port); |
||
35 | |||
36 | q.sendRequest(frame); |
||
37 | WriteIMUConfigurationResponseFrame r = (WriteIMUConfigurationResponseFrame) q.getResponseFor(frame, 5000); |
||
38 | if (r == null) { |
||
39 | System.err.println("ERROR. Timeout waiting for response."); |
||
40 | } else if (!r.wasAccepted()) { |
||
41 | System.err |
||
42 | .println("ERROR. Parameter set not accepted. Check version against MK firmware EEPROM configuration version."); |
||
43 | } else { |
||
44 | System.out.println("Saved IMU configuration."); |
||
45 | } |
||
46 | |||
47 | q.kill(); |
||
48 | } |
||
49 | |||
50 | private static void writeConfiguration(String portIdentifier, String fileName) throws IOException { |
||
51 | System.out.println("Writing IMU configuration from file: " + fileName); |
||
52 | InputStream inputStream = new FileInputStream(fileName); |
||
53 | WriteIMUConfigurationRequestFrame frame = parseXMLIMUConfiguration(inputStream); |
||
54 | configure(portIdentifier, frame); |
||
55 | } |
||
56 | |||
57 | private static IMUConfiguration readConfiguration(String portIdentifier) throws IOException { |
||
58 | MKConnection port = new RXTXSerialPort(); |
||
59 | port.init(portIdentifier); |
||
60 | FrameQueue q = new FrameQueue(port); |
||
61 | IMUConfiguration cs = null; |
||
62 | |||
63 | ReadIMUConfigurationRequestFrame frame = new ReadIMUConfigurationRequestFrame(); |
||
64 | q.sendRequest(frame); |
||
65 | ReadIMUConfigurationResponseFrame r = (ReadIMUConfigurationResponseFrame) q.getResponseFor(frame, 5000); |
||
66 | if (r == null) { |
||
67 | System.err.println("ERROR. Timeout waiting for response."); |
||
68 | } else { |
||
69 | int paramSetVersion = r.getConfigurationVersion(); |
||
70 | int[] data = r.getData(); |
||
71 | cs = IMUConfiguration.parseXMLIMUConfiguration(paramSetVersion); |
||
72 | cs.setData(data); |
||
73 | System.out.println(cs.toXML()); |
||
74 | } |
||
75 | q.kill(); |
||
76 | return cs; |
||
77 | } |
||
78 | |||
79 | private static void readConfiguration(String portIdentifier, String fileName) throws IOException { |
||
80 | IMUConfiguration cs = readConfiguration(portIdentifier); |
||
81 | if (cs != null) { |
||
82 | FileWriter fw = new FileWriter(fileName); |
||
83 | fw.write(cs.toXML()); |
||
84 | fw.close(); |
||
85 | } |
||
86 | } |
||
87 | |||
88 | private static WriteIMUConfigurationRequestFrame parseXMLIMUConfiguration(InputStream input) throws IOException { |
||
89 | DocumentBuilderFactory saxfac = DocumentBuilderFactory.newInstance(); |
||
90 | saxfac.setValidating(false); |
||
91 | try { |
||
92 | DocumentBuilder bldr = saxfac.newDocumentBuilder(); |
||
93 | Document doc = bldr.parse(input); |
||
94 | |||
95 | XPath xpath = XPathFactory.newInstance().newXPath(); |
||
96 | |||
97 | String s_eepromVersion = xpath.evaluate( |
||
98 | "/imuconfiguration/@eepromVersion", doc); |
||
99 | String ss_length = xpath.evaluate("/imuconfiguration/@length", doc); |
||
100 | |||
101 | int s_length = 0; |
||
102 | try { |
||
103 | s_length = Integer.parseInt(ss_length); |
||
104 | } catch (NumberFormatException ex) { |
||
105 | // ignore. |
||
106 | } |
||
107 | |||
108 | int eepromVersion = -1; |
||
109 | |||
110 | try { |
||
111 | eepromVersion = Integer.parseInt(s_eepromVersion); |
||
112 | } catch (NumberFormatException ex) { |
||
113 | System.err |
||
114 | .println("The imuconfiguration element must have an 'eepromVersion' attribute with a numerical value (eg.<parameterset eepromVersion='74'>)"); |
||
115 | System.exit(-1); |
||
116 | } |
||
117 | |||
118 | NodeList parameterNodes = (NodeList) xpath.evaluate( |
||
119 | "/imuconfiguration/parameter", doc, XPathConstants.NODESET); |
||
120 | int[] parameters = new int[parameterNodes.getLength()]; |
||
121 | |||
122 | NodeList allChildNodes = (NodeList) xpath.evaluate("/imuconfiguration/*", doc, XPathConstants.NODESET); |
||
123 | |||
124 | if (parameterNodes.getLength() != allChildNodes.getLength()) { |
||
125 | System.err.println("There seems to be a child element of imuconfiguration whose name is not \"parameter\"."); |
||
126 | // System.exit(-1); |
||
127 | } |
||
128 | |||
129 | if (s_length >=0 && s_length != parameterNodes.getLength()) { |
||
130 | System.err.println("The number of parameters ("+parameterNodes.getLength()+") does not match the number in the length attribute of the imuconfiguration element ("+s_length+")."); |
||
131 | // System.exit(-1); |
||
132 | } |
||
133 | |||
134 | for (int i = 0; i < parameterNodes.getLength(); i++) { |
||
135 | Element e = (Element) parameterNodes.item(i); |
||
136 | int value = 0; |
||
137 | if (e.hasAttribute("value")) { |
||
138 | String s_value = e.getAttribute("value"); |
||
139 | value = Integer.parseInt(s_value); |
||
140 | if (value < 0) |
||
141 | try { |
||
142 | value = Integer.parseInt(s_value); |
||
143 | } catch (NumberFormatException ex) { |
||
144 | throw new NumberFormatException( |
||
145 | "Value is not a number and not a variable name."); |
||
146 | } |
||
147 | } else { |
||
148 | NodeList bitNodes = (NodeList) xpath.evaluate("bit", e, |
||
149 | XPathConstants.NODESET); |
||
150 | for (int j = 0; j < 8; j++) { |
||
151 | Element bitElement = (Element) bitNodes.item(j); |
||
152 | if (bitElement != null) { |
||
153 | String s_bitValue = bitElement |
||
154 | .getAttribute("value"); |
||
155 | if ("1".equals(s_bitValue)) |
||
156 | value |= (1 << j); |
||
157 | else if (!"0".equals(s_bitValue)) |
||
158 | throw new NumberFormatException( |
||
159 | "Bit value was not 0 or 1."); |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | parameters[i] = value; |
||
164 | } |
||
165 | input.close(); |
||
166 | return new WriteIMUConfigurationRequestFrame(eepromVersion, parameters); |
||
167 | } catch (ParserConfigurationException ex) { |
||
168 | // Should never happen. |
||
169 | throw new RuntimeException(ex); |
||
170 | } catch (SAXException ex) { |
||
171 | System.err.println(ex); |
||
172 | System.err |
||
173 | .println("There is something screwed with your XML document. It is not well-formed and won't parse."); |
||
174 | throw new RuntimeException("Parse error."); |
||
175 | } catch (XPathExpressionException ex) { |
||
176 | // Should never happen. |
||
177 | throw new RuntimeException(ex); |
||
178 | } |
||
179 | } |
||
180 | |||
181 | static void help() { |
||
182 | System.err.println("Usage: IMUConfigurator r [filename to write to]"); |
||
183 | System.err.println("Usage: IMUConfigurator w [filename to read from]"); |
||
184 | System.exit(-1); |
||
185 | } |
||
186 | |||
187 | public static void main(String[] args) throws IOException { |
||
188 | if (!"r".equals(args[0]) && !"w".equals(args[0])) help(); |
||
189 | if ("w".equals(args[0]) && (args.length!=2)) help(); |
||
190 | if ("r".equals(args[0]) && (args.length!=2)) help(); |
||
191 | |||
192 | String portIdentifier = null; |
||
193 | |||
194 | if ("r".equals(args[0])) { |
||
195 | readConfiguration(portIdentifier, args[1]); |
||
196 | } else { |
||
197 | writeConfiguration(portIdentifier, args[1]); |
||
198 | } |
||
199 | System.exit(0); |
||
200 | } |
||
201 | } |