Rev 1613 | Details | Compare with Previous | 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) { |
||
1690 | - | 82 | if (fileName != null) { |
83 | FileWriter fw = new FileWriter(fileName); |
||
84 | fw.write(cs.toXML()); |
||
85 | fw.close(); |
||
86 | } |
||
1611 | - | 87 | } |
1690 | - | 88 | System.out.println(cs.toXML()); |
1611 | - | 89 | } |
90 | |||
91 | private static WriteIMUConfigurationRequestFrame parseXMLIMUConfiguration(InputStream input) throws IOException { |
||
92 | DocumentBuilderFactory saxfac = DocumentBuilderFactory.newInstance(); |
||
93 | saxfac.setValidating(false); |
||
94 | try { |
||
95 | DocumentBuilder bldr = saxfac.newDocumentBuilder(); |
||
96 | Document doc = bldr.parse(input); |
||
97 | |||
98 | XPath xpath = XPathFactory.newInstance().newXPath(); |
||
99 | |||
100 | String s_eepromVersion = xpath.evaluate( |
||
101 | "/imuconfiguration/@eepromVersion", doc); |
||
102 | String ss_length = xpath.evaluate("/imuconfiguration/@length", doc); |
||
103 | |||
104 | int s_length = 0; |
||
105 | try { |
||
106 | s_length = Integer.parseInt(ss_length); |
||
107 | } catch (NumberFormatException ex) { |
||
108 | // ignore. |
||
109 | } |
||
110 | |||
111 | int eepromVersion = -1; |
||
112 | |||
113 | try { |
||
114 | eepromVersion = Integer.parseInt(s_eepromVersion); |
||
115 | } catch (NumberFormatException ex) { |
||
116 | System.err |
||
1613 | - | 117 | .println("The imuconfiguration element must have an 'eepromVersion' attribute with a numerical value (eg.<imuconfiguration eepromVersion='74'>)"); |
1611 | - | 118 | System.exit(-1); |
119 | } |
||
120 | |||
121 | NodeList parameterNodes = (NodeList) xpath.evaluate( |
||
122 | "/imuconfiguration/parameter", doc, XPathConstants.NODESET); |
||
123 | int[] parameters = new int[parameterNodes.getLength()]; |
||
124 | |||
125 | NodeList allChildNodes = (NodeList) xpath.evaluate("/imuconfiguration/*", doc, XPathConstants.NODESET); |
||
126 | |||
127 | if (parameterNodes.getLength() != allChildNodes.getLength()) { |
||
128 | System.err.println("There seems to be a child element of imuconfiguration whose name is not \"parameter\"."); |
||
129 | // System.exit(-1); |
||
130 | } |
||
131 | |||
132 | if (s_length >=0 && s_length != parameterNodes.getLength()) { |
||
133 | System.err.println("The number of parameters ("+parameterNodes.getLength()+") does not match the number in the length attribute of the imuconfiguration element ("+s_length+")."); |
||
134 | // System.exit(-1); |
||
135 | } |
||
136 | |||
137 | for (int i = 0; i < parameterNodes.getLength(); i++) { |
||
138 | Element e = (Element) parameterNodes.item(i); |
||
139 | int value = 0; |
||
140 | if (e.hasAttribute("value")) { |
||
141 | String s_value = e.getAttribute("value"); |
||
142 | value = Integer.parseInt(s_value); |
||
143 | if (value < 0) |
||
144 | try { |
||
145 | value = Integer.parseInt(s_value); |
||
146 | } catch (NumberFormatException ex) { |
||
147 | throw new NumberFormatException( |
||
148 | "Value is not a number and not a variable name."); |
||
149 | } |
||
150 | } else { |
||
151 | NodeList bitNodes = (NodeList) xpath.evaluate("bit", e, |
||
152 | XPathConstants.NODESET); |
||
153 | for (int j = 0; j < 8; j++) { |
||
154 | Element bitElement = (Element) bitNodes.item(j); |
||
155 | if (bitElement != null) { |
||
156 | String s_bitValue = bitElement |
||
157 | .getAttribute("value"); |
||
158 | if ("1".equals(s_bitValue)) |
||
159 | value |= (1 << j); |
||
160 | else if (!"0".equals(s_bitValue)) |
||
161 | throw new NumberFormatException( |
||
162 | "Bit value was not 0 or 1."); |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | parameters[i] = value; |
||
167 | } |
||
168 | input.close(); |
||
169 | return new WriteIMUConfigurationRequestFrame(eepromVersion, parameters); |
||
170 | } catch (ParserConfigurationException ex) { |
||
171 | // Should never happen. |
||
172 | throw new RuntimeException(ex); |
||
173 | } catch (SAXException ex) { |
||
174 | System.err.println(ex); |
||
175 | System.err |
||
176 | .println("There is something screwed with your XML document. It is not well-formed and won't parse."); |
||
177 | throw new RuntimeException("Parse error."); |
||
178 | } catch (XPathExpressionException ex) { |
||
179 | // Should never happen. |
||
180 | throw new RuntimeException(ex); |
||
181 | } |
||
182 | } |
||
183 | |||
184 | static void help() { |
||
185 | System.err.println("Usage: IMUConfigurator r [filename to write to]"); |
||
186 | System.err.println("Usage: IMUConfigurator w [filename to read from]"); |
||
187 | System.exit(-1); |
||
188 | } |
||
189 | |||
190 | public static void main(String[] args) throws IOException { |
||
191 | if (!"r".equals(args[0]) && !"w".equals(args[0])) help(); |
||
192 | if ("w".equals(args[0]) && (args.length!=2)) help(); |
||
1690 | - | 193 | if ("r".equals(args[0]) && (args.length>2)) help(); |
1611 | - | 194 | |
195 | String portIdentifier = null; |
||
196 | |||
197 | if ("r".equals(args[0])) { |
||
1690 | - | 198 | readConfiguration(portIdentifier, args.length<2 ? null : args[1]); |
1611 | - | 199 | } else { |
200 | writeConfiguration(portIdentifier, args[1]); |
||
201 | } |
||
202 | System.exit(0); |
||
203 | } |
||
204 | } |