Rev 1688 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1688 | - | 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.configuration.MotorMixer; |
||
26 | import dongfang.mkt.frames.ReadMotorMixerRequestFrame; |
||
27 | import dongfang.mkt.frames.ReadMotorMixerResponseFrame; |
||
28 | import dongfang.mkt.frames.WriteIMUConfigurationRequestFrame; |
||
29 | import dongfang.mkt.frames.WriteMotorMixerRequestFrame; |
||
30 | import dongfang.mkt.frames.WriteMotorMixerResponseFrame; |
||
31 | |||
32 | public class MotorMixerConfigurator { |
||
33 | private static void configure(String portIdentifier, WriteMotorMixerRequestFrame frame) throws IOException { |
||
34 | MKConnection port = new RXTXSerialPort(); |
||
35 | port.init(portIdentifier); |
||
36 | FrameQueue q = new FrameQueue(port); |
||
37 | |||
38 | q.sendRequest(frame); |
||
39 | WriteMotorMixerResponseFrame r = (WriteMotorMixerResponseFrame) q.getResponseFor(frame, 5000); |
||
40 | if (r == null) { |
||
41 | System.err.println("ERROR. Timeout waiting for response."); |
||
42 | } else if (!r.wasAccepted()) { |
||
43 | System.err |
||
44 | .println("ERROR. Motor mixer not accepted. Check version against MK firmware EEPROM mixer version."); |
||
45 | } else { |
||
46 | System.out.println("Saved motor mixer."); |
||
47 | } |
||
48 | |||
49 | q.kill(); |
||
50 | } |
||
51 | |||
52 | private static void writeConfiguration(String portIdentifier, String fileName) throws IOException { |
||
53 | System.out.println("Writing motor mixer from file: " + fileName); |
||
54 | InputStream inputStream = new FileInputStream(fileName); |
||
55 | MotorMixer mm = new MotorMixer(); |
||
1689 | - | 56 | mm.parseXML(inputStream); |
57 | char[] name = new char[12]; |
||
58 | mm.getName().getChars(0, mm.getName().length(), name, 0); |
||
59 | WriteMotorMixerRequestFrame frame = new WriteMotorMixerRequestFrame(1, name, mm.toBinary()); |
||
60 | |||
1688 | - | 61 | configure(portIdentifier, frame); |
62 | } |
||
63 | |||
64 | private static MotorMixer readMotorMixer(String portIdentifier) throws IOException { |
||
65 | MKConnection port = new RXTXSerialPort(); |
||
66 | port.init(portIdentifier); |
||
67 | FrameQueue q = new FrameQueue(port); |
||
68 | MotorMixer mm = new MotorMixer(); |
||
69 | |||
70 | ReadMotorMixerRequestFrame frame = new ReadMotorMixerRequestFrame(); |
||
71 | q.sendRequest(frame); |
||
72 | ReadMotorMixerResponseFrame r = (ReadMotorMixerResponseFrame) q.getResponseFor(frame, 5000); |
||
73 | if (r == null) { |
||
74 | System.err.println("ERROR. Timeout waiting for response."); |
||
75 | } else { |
||
1689 | - | 76 | mm.setMatrix(r.getMatrix()); |
77 | char[] name = r.getName(); |
||
78 | int firstNulPos = name.length; |
||
79 | for (int i=0; i<name.length; i++) { |
||
80 | if (name[i] == 0) { |
||
81 | firstNulPos = i; |
||
82 | break; |
||
83 | } |
||
84 | } |
||
85 | String s_name = new String(name, 0, firstNulPos); |
||
86 | mm.setName(s_name); |
||
1688 | - | 87 | } |
88 | q.kill(); |
||
89 | return mm; |
||
90 | } |
||
91 | |||
92 | private static void readMotorMixer(String portIdentifier, String fileName) throws IOException { |
||
93 | MotorMixer mm = readMotorMixer(portIdentifier); |
||
94 | if (mm != null) { |
||
1689 | - | 95 | if (fileName != null) { |
96 | FileWriter fw = new FileWriter(fileName); |
||
97 | fw.write(mm.toXML()); |
||
98 | fw.close(); |
||
99 | } |
||
100 | System.out.println(mm.toXML()); |
||
1688 | - | 101 | } |
102 | } |
||
103 | |||
104 | static void help() { |
||
1689 | - | 105 | System.err.println("Usage: MotorMixerConfigurator r <filename to write to>"); |
1688 | - | 106 | System.err.println("Usage: MotorMixerConfigurator w [filename to read from]"); |
107 | System.exit(-1); |
||
108 | } |
||
109 | |||
110 | public static void main(String[] args) throws IOException { |
||
1689 | - | 111 | if (args.length < 1) help(); |
1688 | - | 112 | if (!"r".equals(args[0]) && !"w".equals(args[0])) help(); |
1689 | - | 113 | if ("w".equals(args[0]) && args.length < 2) help(); |
1688 | - | 114 | |
115 | String portIdentifier = null; |
||
116 | |||
117 | if ("r".equals(args[0])) { |
||
1689 | - | 118 | readMotorMixer(portIdentifier, args.length < 2 ? null : args[1]); |
1688 | - | 119 | } else { |
120 | writeConfiguration(portIdentifier, args[1]); |
||
121 | } |
||
122 | System.exit(0); |
||
123 | } |
||
124 | } |