Subversion Repositories Projects

Compare Revisions

Ignore whitespace Rev 1695 → Rev 1696

/dongfang_FC_rewrite_tool/src/dongfang/mkt/RequestFrameVisitor.java
13,6 → 13,8
import dongfang.mkt.frames.LoopbackTestRequestFrame;
import dongfang.mkt.frames.MotorTestRequestFrame;
import dongfang.mkt.frames.OSDDataRequestFrame;
import dongfang.mkt.frames.ProfilerLabelRequestFrame;
import dongfang.mkt.frames.ProfilerRequestFrame;
import dongfang.mkt.frames.ReadExternalControlRequestFrame;
import dongfang.mkt.frames.ReadIMUConfigurationRequestFrame;
import dongfang.mkt.frames.ReadMotorMixerRequestFrame;
30,7 → 32,9
public interface RequestFrameVisitor {
// void visit(RequestFrame f) throws IOException;
void visit(AnalogDebugLabelRequestFrame f) throws IOException;
void visit(ProfilerLabelRequestFrame f) throws IOException;
void visit(DebugRequestFrame f) throws IOException;
void visit(ProfilerRequestFrame f) throws IOException;
void visit(LoopbackTestRequestFrame f) throws IOException;
void visit(MotorTestRequestFrame f) throws IOException;
void visit(VersionRequestFrame f) throws IOException;
/dongfang_FC_rewrite_tool/src/dongfang/mkt/main/MKProfiler.java
New file
0,0 → 1,216
package dongfang.mkt.main;
 
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
 
import dongfang.mkt.comm.FrameQueue;
import dongfang.mkt.comm.MKConnection;
import dongfang.mkt.comm.serial.RXTXSerialPort;
import dongfang.mkt.frames.Frame;
import dongfang.mkt.frames.ProfilerLabelRequestFrame;
import dongfang.mkt.frames.ProfilerLabelResponseFrame;
import dongfang.mkt.frames.ProfilerRequestFrame;
import dongfang.mkt.frames.ProfilerResponseFrame;
 
public class MKProfiler {
private final static int NUM_ENTRIES = 16;
private static final PrintStream STDERR = System.out;
// private static final FrameFactory ff = MKVersion.getFrameFactory(null);
 
interface ProfilerLogger {
void setLabel(int index, String label);
void start(String timestamp) throws IOException;
void log(ProfilerResponseFrame f, long timestamp) throws IOException;
void end() throws IOException;
}
 
static class ConsoleProfilerLogger implements ProfilerLogger {
String[] labels = new String[NUM_ENTRIES];
 
public void setLabel(int channel, String label) {
labels[channel] = label;
}
public void start(String logId) {}
 
public void log(ProfilerResponseFrame f, long timestamp) {
if (f == null) {
System.out.println("Oops, null response frame.");
return;
}
int l = f.getActivities()==null ? 0 : f.getActivities().length;
for (int i = 0; i < l; i++) {
String label = labels[i] == null ? ("Activity " + i) : labels[i];
System.out.println(label + ":\t" + f.asFraction(i) * 100.0);
}
}
 
public void end() {}
}
 
static class XMLProfilerLogger implements ProfilerLogger {
String[] labels = new String[NUM_ENTRIES];
Writer w;
String encoding;
 
XMLProfilerLogger(OutputStream out, String encoding) throws IOException {
w = new OutputStreamWriter(out, encoding);
this.encoding = encoding;
}
 
public void setLabel(int channel, String label) {
labels[channel] = label.trim();
}
 
public void start(String logId) throws IOException {
w.write("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n");
w.write("<mk-profilerdata time=\"" + logId + "\">\n");
}
 
public void log(ProfilerResponseFrame f, long timestamp)
throws IOException {
if (f==null) return;
w.write(" <dataset timestamp=\"" + timestamp + "\">\n");
for (int i = 0; i < NUM_ENTRIES; i++) {
w.write(" <data offset=\"" + i + "\" label=\"" + labels[i]
+ "\" value=\"" + f.asFraction(i)*100.0 + "\"/>\n");
}
w.write(" </dataset>\n");
}
 
public void end() throws IOException {
w.write("</mk-profilerdata>\n");
w.flush();
w.close();
}
}
 
static class CompositeProfilerLogger implements ProfilerLogger {
List<ProfilerLogger> loggers = new ArrayList<ProfilerLogger>(2);
 
public void setLabel(int index, String label) {
for (ProfilerLogger l : loggers)
l.setLabel(index, label);
}
 
public void log(ProfilerResponseFrame f, long timestamp) throws IOException {
for (ProfilerLogger l : loggers)
l.log(f, timestamp);
}
 
public void start(String logId) throws IOException {
for (ProfilerLogger l : loggers)
l.start(logId);
}
 
public void end() throws IOException {
for (ProfilerLogger l : loggers)
l.end();
}
 
void add(ProfilerLogger l) {
loggers.add(l);
}
}
 
private static ProfilerLogger createLogger(OutputStream out, String encoding)
throws IOException {
ProfilerLogger consoleLogger = new ConsoleProfilerLogger();
XMLProfilerLogger xmlLogger = new XMLProfilerLogger(out, encoding);
CompositeProfilerLogger logger = new CompositeProfilerLogger();
logger.add(consoleLogger);
logger.add(xmlLogger);
return logger;
}
 
private void prepareLoggers(final FrameQueue q, final ProfilerLogger logger)
throws IOException {
new Thread() {
public void run() {
}
}.start();
}
 
private void profile(final FrameQueue q, final ProfilerLogger logger, final int interval) throws IOException {
LinkedList<Integer> missing = new LinkedList<Integer>();
 
for (int i = 0; i < NUM_ENTRIES; i++) {
missing.add(i);
}
 
int tries = 0;
while (!missing.isEmpty() && tries < 300) {
int i = missing.get(0);
tries++;
ProfilerLabelRequestFrame f2 = //ff.createAnalogDebugLabelRequestFrame(Frame.FC_ADDRESS, i);
new ProfilerLabelRequestFrame(Frame.FC_ADDRESS, i);
try {
q.sendRequest(f2);
ProfilerLabelResponseFrame rf = (ProfilerLabelResponseFrame) q.getResponseFor(f2, 5000);
if (rf != null) {
logger.setLabel(i, rf.getLabelAsString());
missing.remove(0);
} else {
String unknown = "activity " + i;
logger.setLabel(i, unknown);
missing.add(i);
}
} catch (IOException ex) {
}
}
 
ProfilerRequestFrame f = // ff.createDebugRequestFrame(Frame.FC_ADDRESS);
new ProfilerRequestFrame(Frame.FC_ADDRESS);
f.setAutoSendInterval(interval);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
q.sendRequest(f);
Long firsttimestamp = null;
while (!in.ready())
try {
// q.output(f);
ProfilerResponseFrame rf = (ProfilerResponseFrame) q.getResponseFor(f, 1000);
// System.out.println(rf);
long timestamp = System.currentTimeMillis();
 
if (firsttimestamp == null) {
firsttimestamp = timestamp;
timestamp = 0;
} else {
timestamp -= firsttimestamp;
}
logger.log(rf, timestamp);
} catch (IOException ex) {
STDERR.println(ex);
}
}
 
public static void main(String[] args) throws IOException {
MKProfiler test = new MKProfiler();
 
MKConnection port = new RXTXSerialPort();
port.init(null);
 
FrameQueue q = new FrameQueue(port);
String encoding = "iso-8859-1";
 
OutputStream fout = new FileOutputStream("profile.xml");
 
ProfilerLogger logger = createLogger(fout, encoding);
logger.start(new Date().toGMTString());
test.profile(q, logger, 10);
logger.end();
fout.close();
}
}
/dongfang_FC_rewrite_tool/src/dongfang/mkt/comm/MKInputStream.java
9,7 → 9,6
import dongfang.mkt.frames.AllDisplaysResponseFrame;
import dongfang.mkt.frames.AnalogDebugLabelResponseFrame;
import dongfang.mkt.frames.AttitudeDataResponseFrame;
import dongfang.mkt.frames.ChangeParameterSetResponseFrame;
import dongfang.mkt.frames.CompassHeadingResponseFrame;
import dongfang.mkt.frames.ConfirmFrame;
import dongfang.mkt.frames.DCMMatrixResponseFrame;
16,6 → 15,8
import dongfang.mkt.frames.DebugResponseFrame;
import dongfang.mkt.frames.MotorTestResponseFrame;
import dongfang.mkt.frames.OSDDataResponseFrame;
import dongfang.mkt.frames.ProfilerLabelResponseFrame;
import dongfang.mkt.frames.ProfilerResponseFrame;
import dongfang.mkt.frames.ReadExternalControlResponseFrame;
import dongfang.mkt.frames.ReadIMUConfigurationResponseFrame;
import dongfang.mkt.frames.ReadMotorMixerResponseFrame;
225,6 → 226,7
result = f;
break;
}
/*
case 'F': {
ChangeParameterSetResponseFrame f = new ChangeParameterSetResponseFrame(address);
f.setParameterSetNumber(base64InputStream.readByte());
231,6 → 233,15
result = f;
break;
}
*/
case 'F': {
ProfilerLabelResponseFrame f = new ProfilerLabelResponseFrame(address);
f.setChannel(base64InputStream.readByte());
f.setLabel(base64InputStream.readChars(16));
result = f;
break;
}
 
case 'G': {
ReadExternalControlResponseFrame f = new ReadExternalControlResponseFrame(address);
f.setDigital(base64InputStream.readBytes(2));
424,6 → 435,14
break;
}
*/
case 'U': {
ProfilerResponseFrame f = new ProfilerResponseFrame(address);
f.setTotalHits(base64InputStream.readSignedDWord());
for (int i=0; i<16; i++)
f.setActivity(i, base64InputStream.readWord());
result = f;
break;
}
case 'V': {
VersionResponseFrame f = new VersionResponseFrame(address);
f.setSWMajor(base64InputStream.readByte());
/dongfang_FC_rewrite_tool/src/dongfang/mkt/comm/MKOutputStream.java
15,6 → 15,8
import dongfang.mkt.frames.LoopbackTestRequestFrame;
import dongfang.mkt.frames.MotorTestRequestFrame;
import dongfang.mkt.frames.OSDDataRequestFrame;
import dongfang.mkt.frames.ProfilerLabelRequestFrame;
import dongfang.mkt.frames.ProfilerRequestFrame;
import dongfang.mkt.frames.ReadExternalControlRequestFrame;
import dongfang.mkt.frames.ReadIMUConfigurationRequestFrame;
import dongfang.mkt.frames.ReadMotorMixerRequestFrame;
133,6 → 135,11
base64OutputStream.writeByte(f.getChannel());
}
 
public void visit(ProfilerLabelRequestFrame f) throws IOException {
writeByte('f');
base64OutputStream.writeByte(f.getChannel());
}
 
public void visit(AttitudeDataRequestFrame f) throws IOException {
writeByte('c');
base64OutputStream.writeByte(f.getAutoSendInterval());
143,6 → 150,11
base64OutputStream.writeByte(f.getAutoSendInterval());
}
 
public void visit(ProfilerRequestFrame f) throws IOException {
writeByte('u');
base64OutputStream.writeByte(f.getAutoSendInterval());
}
 
public void visit(DCMMatrixRequestFrame f) throws IOException {
writeByte('e');
}
/dongfang_FC_rewrite_tool/src/dongfang/mkt/frames/RequestFrameVisitor.java
File deleted
\ No newline at end of file
/dongfang_FC_rewrite_tool/src/dongfang/mkt/frames/ProfilerLabelRequestFrame.java
New file
0,0 → 1,21
package dongfang.mkt.frames;
import java.io.IOException;
 
import dongfang.mkt.RequestFrameVisitor;
 
public class ProfilerLabelRequestFrame extends RequestFrame {
int channel;
public ProfilerLabelRequestFrame(int address, int channel) {
super(address);
this.channel = channel;
}
 
public int getChannel() {
return channel;
}
public void accept(RequestFrameVisitor o) throws IOException {
o.visit(this);
}
}
/dongfang_FC_rewrite_tool/src/dongfang/mkt/frames/ProfilerResponseFrame.java
New file
0,0 → 1,51
package dongfang.mkt.frames;
 
public class ProfilerResponseFrame extends ResponseFrame {
private final static int NUM_ENTRIES = 16;
 
private int[] activities= new int[NUM_ENTRIES];
private int totalHits;
 
public ProfilerResponseFrame(int address) {
super(address);
}
 
public int[] getActivities() {
return activities;
}
 
public void setActivity(int index, int value) {
this.activities[index ]= value;
}
 
@Override
public boolean isResponseTo(RequestFrame r) {
return r instanceof ProfilerRequestFrame;
}
public int getTotalHits() {
return totalHits;
}
 
public void setTotalHits(int totalHits) {
this.totalHits = totalHits;
}
public double asFraction(int activity) {
return (double)activities[activity] / (double)totalHits;
}
 
public String toString() {
StringBuilder result = new StringBuilder();
result.append(getClass().getSimpleName()+"\r");
result.append("activities\r");
for (int i=0; i<activities.length; i++) {
result.append(i);
result.append(':');
result.append(activities[i]);
result.append('\r');
}
return result.toString();
}
}
/dongfang_FC_rewrite_tool/src/dongfang/mkt/frames/ProfilerRequestFrame.java
New file
0,0 → 1,26
package dongfang.mkt.frames;
 
import java.io.IOException;
 
import dongfang.mkt.RequestFrameVisitor;
 
 
public class ProfilerRequestFrame extends RequestFrame {
private int autoSendInterval;
 
public ProfilerRequestFrame(int address) {
super(address);
}
 
public int getAutoSendInterval() {
return autoSendInterval;
}
public void setAutoSendInterval(int autoSendInterval) {
this.autoSendInterval = autoSendInterval;
}
 
public void accept(RequestFrameVisitor o) throws IOException {
o.visit(this);
}
}
/dongfang_FC_rewrite_tool/src/dongfang/mkt/frames/ProfilerLabelResponseFrame.java
New file
0,0 → 1,42
package dongfang.mkt.frames;
 
 
public class ProfilerLabelResponseFrame extends ResponseFrame {
 
private int channel;
private char[] label;
 
public ProfilerLabelResponseFrame(int address) {
super(address);
}
@Override
public boolean isResponseTo(RequestFrame r) {
return r instanceof ProfilerLabelRequestFrame
&& ((ProfilerLabelRequestFrame)r).getChannel() == channel;
}
 
public int getChannel() {
return channel;
}
public void setChannel(int channel) {
this.channel = channel;
}
public char[] getLabel() {
return label;
}
 
public String getLabelAsString() {
return new String(label);
}
 
public void setLabel(char[] label) {
this.label = label;
}
public String toString() {
return getClass().getSimpleName() + ": label=" + getLabelAsString();
}
}