Go to most recent revision | Details | 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(); |
||
56 | mm.parseXMLMotorMixer(inputStream); |
||
57 | WriteMotorMixerRequestFrame frame = new WriteMotorMixerRequestFrame(1, mm.toBinary()); |
||
58 | configure(portIdentifier, frame); |
||
59 | } |
||
60 | |||
61 | private static MotorMixer readMotorMixer(String portIdentifier) throws IOException { |
||
62 | MKConnection port = new RXTXSerialPort(); |
||
63 | port.init(portIdentifier); |
||
64 | FrameQueue q = new FrameQueue(port); |
||
65 | MotorMixer mm = new MotorMixer(); |
||
66 | |||
67 | ReadMotorMixerRequestFrame frame = new ReadMotorMixerRequestFrame(); |
||
68 | q.sendRequest(frame); |
||
69 | ReadMotorMixerResponseFrame r = (ReadMotorMixerResponseFrame) q.getResponseFor(frame, 5000); |
||
70 | if (r == null) { |
||
71 | System.err.println("ERROR. Timeout waiting for response."); |
||
72 | } else { |
||
73 | int[][] matrix = r.getMatrix(); |
||
74 | mm.setMatrix(matrix); |
||
75 | System.out.println(mm.toXML()); |
||
76 | } |
||
77 | q.kill(); |
||
78 | return mm; |
||
79 | } |
||
80 | |||
81 | private static void readMotorMixer(String portIdentifier, String fileName) throws IOException { |
||
82 | MotorMixer mm = readMotorMixer(portIdentifier); |
||
83 | if (mm != null) { |
||
84 | FileWriter fw = new FileWriter(fileName); |
||
85 | fw.write(mm.toXML()); |
||
86 | fw.close(); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | static void help() { |
||
91 | System.err.println("Usage: MotorMixerConfigurator r [filename to write to]"); |
||
92 | System.err.println("Usage: MotorMixerConfigurator w [filename to read from]"); |
||
93 | System.exit(-1); |
||
94 | } |
||
95 | |||
96 | public static void main(String[] args) throws IOException { |
||
97 | if (!"r".equals(args[0]) && !"w".equals(args[0])) help(); |
||
98 | if ("w".equals(args[0]) && (args.length!=2)) help(); |
||
99 | if ("r".equals(args[0]) && (args.length!=2)) help(); |
||
100 | |||
101 | String portIdentifier = null; |
||
102 | |||
103 | if ("r".equals(args[0])) { |
||
104 | readMotorMixer(portIdentifier, args[1]); |
||
105 | } else { |
||
106 | writeConfiguration(portIdentifier, args[1]); |
||
107 | } |
||
108 | System.exit(0); |
||
109 | } |
||
110 | } |