Skip to content

Commit 5ce30cb

Browse files
committed
Merge pull request #4 from DynamicPerception/cherry_pick_test
Added timeout and debug commands
2 parents 2819cd6 + 16e1f74 commit 5ce30cb

3 files changed

Lines changed: 164 additions & 50 deletions

File tree

src/com/dynamicperception/nmxcommandline/main/NMXCommandLine.java

Lines changed: 104 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.dynamicperception.nmxcommandline.main;
22

3+
import java.io.File;
34
import java.io.IOException;
5+
import java.io.PrintStream;
46
import java.nio.charset.Charset;
57
import java.nio.charset.StandardCharsets;
68
import java.nio.file.Files;
@@ -15,15 +17,16 @@
1517
import com.dynamicperception.nmxcommandline.helpers.Consts;
1618
import com.dynamicperception.nmxcommandline.models.Command;
1719
import com.dynamicperception.nmxcommandline.models.NMXComs;
20+
import com.dynamicperception.nmxcommandline.models.Command.Names.General;
1821

1922
public class NMXCommandLine {
2023

2124
private static boolean execute;
2225
private static Serial serial;
2326
final static String DELIMITER = " ";
2427
//private static long lastTime;
25-
private static String version = "0.2-beta";
26-
28+
private static String version = "0.3-beta";
29+
2730
public static void main(String[] args) {
2831
// Create serial object
2932
serial = new Serial();
@@ -38,7 +41,7 @@ public static void main(String[] args) {
3841
}
3942

4043
try{
41-
serial.openPort(args[0]);
44+
serial.openPort(Integer.parseInt(args[0]));
4245
}catch(RuntimeException e){
4346
Console.pln("Invalid port! Either you picked the wrong number or you have the wrong syntax.\n"
4447
+ "A full one-time execution should look something like this: "
@@ -60,6 +63,27 @@ public static void main(String[] args) {
6063
// Print help
6164
printHelp();
6265

66+
/*
67+
* Send an arbitrary command to clear initial connection hiccup. Also
68+
* reroute system out to temporary file to avoid printing the error
69+
* this will likely generate.
70+
*/
71+
try {
72+
PrintStream oldOut = System.out;
73+
File tempFile = new File("temp.txt");
74+
tempFile.createNewFile();
75+
PrintStream tempOut = new PrintStream(tempFile);
76+
System.setOut(tempOut);
77+
Command.execute(General.GET_FIRMWARE);
78+
System.setOut(oldOut);
79+
tempOut.close();
80+
tempFile.delete();
81+
} catch (IOException e) {
82+
// TODO Auto-generated catch block
83+
e.printStackTrace();
84+
}
85+
86+
6387
// Enter program loop
6488
execute = true;
6589
while(execute){
@@ -74,7 +98,7 @@ private static void printTerminalHelp(){
7498
Console.pln("\nIn order to enter the interactive command line tool, run this JAR file without arguments.\n"
7599
+ "Alternatively, you may run it with the following arguments to execute a single NMX command and then\n"
76100
+ "immediately disconnect from the controller and close this application:\n\n"
77-
+ "\"NMXCommander.jar <PORT NAME> <COMMAND TYPE>.<COMMAND NAME> <DATA or MOTOR # (if needed)> <MOTOR DATA (if needed)>\"\n\n"
101+
+ "\"NMXCommander.jar [PORT NAME] [COMMAND TYPE].[COMMAND NAME] [DATA or MOTOR # (if required)] [MOTOR DATA (if required)]\"\n\n"
78102
+ "If you're not familiar with the command types, names, data, etc., open the application in interactive\n"
79103
+ "mode first and read the extended help there.");
80104
}
@@ -86,16 +110,32 @@ private static void printHelp(){
86110
Console.pln("\n\n******** NMX Commander " + version + " Overview ********\n\n"
87111
+ "This command line tool allows you to manually send single instuctions to the NMX controller.\n"
88112
+ "This is done by giving input with the following syntax:\n\n"
89-
+ "Non-motor commands -- \"<COMMAND TYPE>.<COMMAND NAME> <DATA (if required)>\"\n"
90-
+ "Motor commands -- \"m.<COMMAND NAME> <MOTOR #> <DATA (if required)>\"\n\n"
113+
+ "Non-motor commands -- \"[COMMAND TYPE].[COMMAND NAME] [DATA (if required)]\"\n"
114+
+ "Motor commands -- \"m.[COMMAND NAME] [MOTOR #] [DATA (if required)]\"\n\n"
91115
+ "Non-motor command types include \"g\" (general), \"c\" (camera), and \"k\" (key frame)\n"
92116
+ "When specifying the motor number for motor commands, counting starts at 0 (i.e. valid motor #s are 0, 1, 2)\n\n\n"
117+
+ "******** NMX Commander Configuration Commands ********\n\n"
118+
+ "* \"outputAddress [ADDR # optional argument]\" -- without optional parameter, reports the current command address. With the\n"
119+
+ "optional parameter, sets the address to which the command will be sent. This is 3 by default and should not be changed unless\n"
120+
+ "you have manually reassigned the address of one or more of your controllers. !!!Sending commands to an incorrect address will\n"
121+
+ "cause you lots of headaches!!!\n\n"
122+
+ "* \"commandDetail [0/1 optional argument]\" -- without optional parameter, reports whether commandDetail is enabled. With the\n"
123+
+ "optional parameter, sets commandDetail enabled. If true, NMX Commander will print the command name and any additional data\n"
124+
+ "output to the NMX\n\n"
125+
+ "* \"serialDetail [0/1 optional argument]\" -- without optional parameter, reports whether serialDetail is enabled. With the\n"
126+
+ "optional parameter, sets serialDetail enabled. If true, NMX Commander will print the raw packet received from the NMX for\n"
127+
+ "each command\n\n\n"
128+
+ "* \"responseTimout [TIMEOUT optional argument]\" -- without optional parameter, reports current response timeout in milliseconds\n"
129+
+ "With optional parameter, sets timeout length. If you find that you encounter a situation where you are getting many timeouts that\n"
130+
+ "are not causing problems (e.g. you don't care about the response contents, but the command seems to be executing properly), you\n"
131+
+ "may want to shorten the timout in order to increase command throughput.\n\n\n"
93132
+ "******** Other Useful Commands ********\n\n"
94133
+ "* \"help\" -- prints this information again\n\n"
95-
+ "* \"<COMMAND TYPE>.<COMMAND NAME> -h\" -- prints command-specific help\n\n"
96-
+ "* \"list <COMMAND TYPE>\" -- lists all commands of that type\n\n"
97-
+ "* \"find <COMMAND TYPE>.<SEARCH TERM>\" -- returns all commands of that type containing the search term\n\n"
98-
+ "* \"runMacro <PATH>\" -- runs a command macro list from a text file. Type \"runMacro\" without arguments\n"
134+
+ "* \"[COMMAND TYPE].[COMMAND NAME] -h\" -- prints command-specific help\n\n"
135+
+ "* \"list [COMMAND TYPE]\" -- lists all commands of that type\n\n"
136+
+ "* \"find [COMMAND TYPE].[SEARCH TERM]\" -- returns all commands of that type containing the search term\n\n"
137+
+ "* \"repeat [N] [COMMAND TYPE].[COMMAND NAME]\" -- repeats the given command N times\n\n"
138+
+ "* \"runMacro [PATH]\" -- runs a command macro list from a text file. Type \"runMacro\" without arguments\n"
99139
+ "for macro file syntax\n\n"
100140
+ "* \"exit\" -- closes the serial port and exits the application\n\n\n"
101141
+ "******** Some Important Tips ********\n\n"
@@ -110,7 +150,7 @@ private static void printHelp(){
110150
+ "BE CAREFUL ABOUT THIS! If you accidentally send your rig two or four times as far as you intended, you can break\n"
111151
+ "your valueable equipment!\n\n"
112152
+ "* If you try to send a motor to a position and it either does not move or does not go all the way to where you sent it,\n"
113-
+ "try running the command \"m.resetLimits <MOTOR #>\" to clear any end limits that may be restricting movement.");
153+
+ "try running the command \"m.resetLimits [MOTOR #]\" to clear any end limits that may be restricting movement.");
114154
}
115155

116156
/**
@@ -175,14 +215,52 @@ else if(args.get(0).indexOf("//") != -1){
175215
else if(args.get(0).equals("help")){
176216
printHelp();
177217
}
218+
else if(args.get(0).equals("commandDetail")){
219+
try{
220+
boolean enabled = args.get(1).equals("0") ? false : true;
221+
Command.setCommandDetail(enabled);
222+
}catch(IndexOutOfBoundsException e){
223+
System.out.println("Command detail enabled? : " + Command.getCommandDetail());
224+
}
225+
}
226+
else if(args.get(0).equals("serialDetail")){
227+
try{
228+
boolean enabled = args.get(1).equals("0") ? false : true;
229+
NMXComs.setSerialDetail(enabled);
230+
}catch(IndexOutOfBoundsException e){
231+
System.out.println("Serial detail enabled? : " + NMXComs.getSerialDetail());
232+
}
233+
}
234+
else if(args.get(0).equals("outputAddress")){
235+
try{
236+
int addr = Integer.parseInt(args.get(1));
237+
Command.setAddr(addr);
238+
}catch(IndexOutOfBoundsException e){
239+
System.out.println("Current command address: " + Command.getAddr());
240+
}catch(NumberFormatException e){
241+
System.out.println("Invalid address. Must be an integer.");
242+
}
243+
244+
}
245+
else if(args.get(0).equals("responseTimeout")){
246+
try{
247+
int timeout = Integer.parseInt(args.get(1));
248+
NMXComs.setResponseTimeout(timeout);
249+
}catch(IndexOutOfBoundsException e){
250+
System.out.println("Current timout in milliseconds: " + NMXComs.getResponseTimeout());
251+
}catch(NumberFormatException e){
252+
System.out.println("Invalid address. Must be an integer.");
253+
}
254+
255+
}
178256
// Run command list from file
179257
else if(args.get(0).equals("runMacro")){
180258
try {
181259
if(args.size() == 1){
182-
Console.pln("\nrunMacro syntax -- \"runMacro <PATH>\"");
260+
Console.pln("\nrunMacro syntax -- \"runMacro [PATH]\"");
183261
Console.pln("Example -> \"runMacro c:\\NMXmacro.txt\"\n");
184262
Console.pln("The text file should have one command on each line with the following syntax:\n\n"
185-
+ "\"<DELAY TIME> <COMMAND TYPE>.<COMMAND NAME> <DATA or MOTOR # (if needed)> <MOTOR DATA (if needed)>\"\n\n"
263+
+ "\"[DELAY TIME] [COMMAND TYPE].[COMMAND NAME] [DATA or MOTOR # (if required)] [MOTOR DATA (if required)]\"\n\n"
186264
+ "The following example enables the camera, sets the focus time to 600ms, trigger time to 100ms, sets home\n"
187265
+ "for each of the motors (e.g. sets current position to 0), immediately takes an exposure, commands the motors to\n"
188266
+ "a new position, waits 5000ms, takes another exposure, waits 1000ms, commands the motors back to their original\n"
@@ -231,28 +309,32 @@ else if(args.get(0).equals("replayLog")){
231309
// Help request
232310
else if(args.get(0).toLowerCase().equals("list")){
233311
if(args.size() < 2){
234-
Console.pln("List syntax -- \"list <COMMAND TYPE>\"");
312+
Console.pln("List syntax -- \"list [COMMAND TYPE]\"");
235313
Console.pln("Example -> \"list c\" prints the list of valid camera commands");
236314
return;
237315
}
238316
Command.printList(Command.getType(args.get(1)));
239317
return;
240318
}
241319
// Repeat the following command n times. (e.g. "repeat 2 m.getMS 0")
242-
else if(args.get(0).equals("repeat")){
243-
int count = Integer.parseInt(args.get(1));
244-
for(int i = 0; i < 2; i++){
245-
args.remove(0);
320+
else if(args.get(0).equals("repeat")){
321+
try{
322+
int count = Integer.parseInt(args.get(1));
323+
for(int i = 0; i < 2; i++){
324+
args.remove(0);
325+
}
326+
for(int i = 0; i < count; i++){
327+
parseCommand(args);
328+
}
329+
}catch(IndexOutOfBoundsException | NumberFormatException e){
330+
Console.pln("Invalid syntax. Try \"repeat [#] [command]\"");
246331
}
247-
for(int i = 0; i < count; i++){
248-
parseCommand(args);
249-
}
250332
return;
251333
}
252334
// Find command name
253335
else if(args.get(0).equals("find")){
254336
if(args.size() < 2){
255-
Console.pln("Find syntax -- \"find <COMMAND TYPE>.<SEARCH TERM>\"");
337+
Console.pln("Find syntax -- \"find [COMMAND TYPE].[SEARCH TERM]\"");
256338
Console.pln("Example -> \"find m.speed\" prints the following:");
257339
List <String> exampleArgs = Arrays.asList("find", "m.speed");
258340
findCommand(exampleArgs);

src/com/dynamicperception/nmxcommandline/models/Command.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Command {
1414
private final static int MOTOR_COUNT = 3;
1515
private static int addr = 3;
1616
private static int currentControllerNum = 0;
17-
private static boolean debug = true;
17+
private static boolean commandDetail = false;
1818
private static List<Command> generalList = new ArrayList<Command>();
1919
private static List<Command> motorList = new ArrayList<Command>();
2020
private static List<Command> cameraList = new ArrayList<Command>();
@@ -157,10 +157,6 @@ else if(returnType == Boolean.class){
157157
ret = (T) Void.class.cast(null);
158158
}
159159

160-
// Print debug if necessary
161-
if(debug){
162-
System.out.println("Command: " + thisCommand.name);
163-
}
164160
if(ret != null)
165161
System.out.println(ret);
166162
else
@@ -966,14 +962,22 @@ public static Command get(Type type, int command){
966962
throw new UnsupportedOperationException();
967963
}
968964

969-
public static void setDebug(boolean debug){
970-
Command.debug = debug;
965+
public static void setCommandDetail(boolean enabled){
966+
Command.commandDetail = enabled;
967+
}
968+
969+
public static boolean getCommandDetail(){
970+
return Command.commandDetail;
971971
}
972972

973973
public static void setAddr(int addr){
974974
Command.addr = addr;
975975
}
976976

977+
public static int getAddr(){
978+
return Command.addr;
979+
}
980+
977981
public static int getControllerNum(){
978982
return currentControllerNum;
979983
}
@@ -1117,18 +1121,20 @@ private <T>T executeThis(int subAddr, String dataStr, boolean hasData){
11171121
data = (int) Math.round(Float.parseFloat(dataStr));
11181122
}
11191123
}
1120-
1121-
// Print debug if necessary
1122-
if(debug){
1123-
System.out.println("Command: " + this.name + " Input data: " + dataStr);
1124-
}
11251124

1125+
String commandPacket = "";
1126+
11261127
// Send the command to the NMX
11271128
if(hasData){
1128-
NMXComs.cmd(addr, subAddr, this.command, this.dataLength, data);
1129+
commandPacket = NMXComs.cmd(addr, subAddr, this.command, this.dataLength, data);
11291130
}
11301131
else{
1131-
NMXComs.cmd(addr, subAddr, this.command);
1132+
commandPacket = NMXComs.cmd(addr, subAddr, this.command);
1133+
}
1134+
1135+
// Print debug if necessary
1136+
if(commandDetail){
1137+
System.out.println("Command: " + commandPacket + " Output data: " + dataStr);
11321138
}
11331139

11341140
// Wait for the NMX to clear

0 commit comments

Comments
 (0)