From 69e4480feb3e0451bb80d04c038ff7f066155949 Mon Sep 17 00:00:00 2001 From: raceimaztion Date: Fri, 27 May 2011 19:08:36 -0700 Subject: [PATCH] Did some more work on the RobotRepeater class, fixed a bunch of errors in the embedded version of the internal library, and started work on the embedded compilation code. --- create/simulator/RobotRepeater.java | 13 +- create/simulator/window/CreateProject.java | 186 ++++++++++++++++++++- create/simulator/window/EditorWindow.java | 11 +- include/cm.h | 5 +- include/main.cc | 20 ++- 5 files changed, 216 insertions(+), 19 deletions(-) diff --git a/create/simulator/RobotRepeater.java b/create/simulator/RobotRepeater.java index 1cda66a..2ffcf5a 100644 --- a/create/simulator/RobotRepeater.java +++ b/create/simulator/RobotRepeater.java @@ -27,7 +27,7 @@ public class RobotRepeater protected BufferedInputStream realIn; protected OutputStream realOut; - public RobotRepeater(Process master) + private RobotRepeater(Process master) { masterProcess = master; @@ -36,6 +36,11 @@ public RobotRepeater(Process master) coreOut = new PrintStream(master.getOutputStream()); } + /** + * Create a new RobotRepeater with a controlling process and a simulated robot. + * @param master The controlling process. + * @param robot The simulated robot. + */ public RobotRepeater(Process master, SimulatedRobot robot) { this(master); @@ -43,6 +48,12 @@ public RobotRepeater(Process master, SimulatedRobot robot) simulatedRobot = robot; } + /** + * Create a new RobotRepeater with a controlling process and a serial port to control a real robot. + * @param master The controlling proces. + * @param port The serial port that connects to the real robot. + * @throws IOException + */ public RobotRepeater(Process master, SerialPort port) throws IOException { this(master); diff --git a/create/simulator/window/CreateProject.java b/create/simulator/window/CreateProject.java index 3df30b1..a90f4ee 100644 --- a/create/simulator/window/CreateProject.java +++ b/create/simulator/window/CreateProject.java @@ -49,10 +49,38 @@ protected CreateProject(File projectFolder) /** * Compiles the current code for execution on this computer. + * @return If there's a problem, returns a BuildProblem object. */ public BuildProblem buildSimulatorProject() { - // TODO: Build the project for local simulation + BuildProblem problem = null; + + // Compile for local execution: + problem = compileLocalProject(); + if (problem != null) + return problem; + + return null; + } // end buildSimulatorProject() + + public BuildProblem buildSerialProject() + { + BuildProblem problem = null; + + // Compile for local execution: + problem = compileLocalProject(); + if (problem != null) + return problem; + + return null; + } // end buildSerialProject() + + /** + * Compiles this CreateProject for local execution. + * @return If there's a problem, returns a BuildProblem object. + */ + private BuildProblem compileLocalProject() + { // If we're running Linux, try compiling with GCC: if (MainLauncher.getRuntimePlatform() == Platform.LINUX) { @@ -77,7 +105,6 @@ public BuildProblem buildSimulatorProject() for (String module : modules) { main.printf("#include \"%s\"\n", module); - System.out.println(module); } CreateUtils.copyFile(FileNabber.FILE_MAIN, main); main.flush(); @@ -89,7 +116,6 @@ public BuildProblem buildSimulatorProject() // Try to compile the files String command = String.format("g++ -DMODE_LOCAL -o ..%s%s%s%s %s", File.separator, localBinFolder.getName(), File.separator, getProjectName(), FileNabber.FILE_MAIN); - System.out.println(command); Process compiler = runtime.exec(command, null, sourceFolder); InputStream stdIn = compiler.getInputStream(); @@ -128,14 +154,151 @@ public BuildProblem buildSimulatorProject() // Fail, we don't support other OSes just yet return new BuildProblem(this, "Operating System not supported.", "Operating System not supported.", -2); } - } + } // end compileLocalProblem() /** * Compiles the current code for execution on the Command Module. + * @return If there's a problem, returns a BuildProblem object. */ - public void buildEmbeddedProject() + public BuildProblem buildEmbeddedProject() + { + BuildProblem problem; + + problem = compileEmbeddedProject(); + if (problem != null) + return problem; + + return null; + } // end buildEmbeddedProject() + + private BuildProblem compileEmbeddedProject() + { + // If we're running Linux, try compiling with GCC: + if (MainLauncher.getRuntimePlatform() == Platform.LINUX) + { + File mainSource = null; + File header1 = null, header2 = null; + + // Try compiling the project under Linux using AVR-GCC: + try + { + // Copy the source library into the project folder + String[] modules = getModuleNames(); + + mainSource = new File(sourceFolder, FileNabber.FILE_MAIN); + header1 = new File(sourceFolder, FileNabber.FILE_HEADER_CM); + header2 = new File(sourceFolder, FileNabber.FILE_HEADER_OI); + + // Copy the main file, + PrintStream main = new PrintStream(new FileOutputStream(mainSource)); + main.print("#include \"cm.h\"\n"); + for (String module : modules) + { + main.printf("#include \"%s\"\n", module); + } + CreateUtils.copyFile(FileNabber.FILE_MAIN, main); + main.flush(); + main.close(); + + // Copy the headers. + CreateUtils.copyFile(FileNabber.FILE_HEADER_CM, header1); + CreateUtils.copyFile(FileNabber.FILE_HEADER_OI, header2); + + /* + * Compile: Done + * avr-g++ -c -mmcu=atmega168 -I. -gdwarf-2 -DF_CPU=18432000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns={$project}.lst -std=gnu99 -MD -MP -MF .dep/{$project}.o.d {$source} -o {$project}.o + * + * Link: + * avr-g++ -mmcu=atmega168 -I. -gdwarf-2 -DF_CPU=18432000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns={$project}.o -std=gnu99 -MD -MP -MF .dep/{$project}.elf.d {$project}.o --output {$project}.elf -Wl,-Map={$project}.map,--cref -lm + * + * Creating load file for Flash: + * avr-objcopy -O ihex -R .eeprom {$project}.elf {$project}.hex + * + * Creating load file for EEPROM: + * avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex {$project}.elf {$project}.eep + * + * Creating extended listing: + * avr-objdump -h -S {$project}.elf > {$project}.lss + * + * Creating symbol table: + * avr-nm -n {$project}.elf > {$project}.sym + */ + + // Compile the files + String command; + BuildProblem problem = null; + +// command = String.format("avr-g++ -DMODE_EMBEDDED -c -mmcu=atmega168 -I. -gdwarf-2 -DF_CPU=18432000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wa,-adhlns=%s.lst -MD -MP -MF .dep/%s.o.d %s -o %s.o", projectName, projectName, FileNabber.FILE_MAIN, projectName); + command = String.format("avr-g++ -DMODE_EMBEDDED -c -mmcu=atmega168 -I. -gdwarf-2 -DF_CPU=18432000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wa,-adhlns=../ebin/%s.lst -MD -MP %s -o ../ebin/%s.o", projectName, FileNabber.FILE_MAIN, projectName); + System.out.println(command); + problem = runProgram(command, sourceFolder); + if (problem != null) + return problem; + + // Link the files: + command = String.format("avr-g++ -mmcu=atmega168 -I. -gdwarf-2 -DF_CPU=18432000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=%s.o -std=gnu99 -MD -MP -MF .dep/%s.elf.d %s.o --output %s.elf -Wl,-Map=%s.map,--cref -lm", projectName, projectName, projectName, projectName, projectName); + System.out.println(command); + problem = runProgram(command, embeddedBinFolder); + if (problem != null) + return problem; + + // Create a load file for Flash ROMs: + command = String.format("avr-objcopy -O ihex -R .eeprom %s.elf %s.hex", projectName, projectName); + System.out.println(command); + problem = runProgram(command, embeddedBinFolder); + if (problem != null) + return problem; + + // Create a load file for EEPROMs: + command = String.format("avr-objcopy -O ihex -R .eeprom %s.elf %s.hex", projectName, projectName); + System.out.println(command); + problem = runProgram(command, embeddedBinFolder); + if (problem != null) + return problem; + + // If we got here, we succeeded + return null; + } + catch (IOException er) + { + return new BuildProblem(this, "File I/O error.", er.getMessage(), -2); + } + finally + { + // Delete the files we added: + if (mainSource != null) mainSource.delete(); + if (header1 != null) header1.delete(); + if (header2 != null) header2.delete(); + } +// return new BuildProblem(this, "Compiler failed.", "Unhandled error ocurred.", -1); + } + else + { + // Fail, we don't support other OSes just yet + return new BuildProblem(this, "Operating System not supported.", "Operating System not supported.", -2); + } + } // end compileEmbeddedProject() + + private BuildProblem runProgram(String command, File folder) throws IOException { - // TODO: Build the project for embedded operation. + Process compiler = Runtime.getRuntime().exec(command, null, folder); + InputStream stdIn = compiler.getInputStream(); + InputStream errIn = compiler.getErrorStream(); + Integer result = null; + try { + result = compiler.waitFor(); + } catch (InterruptedException er) { } + + if (result == 0) + { + // We succeeded + return null; + } + else + { + // Something failed + return new BuildProblem(this, new BufferedReader(new InputStreamReader(stdIn)), new BufferedReader(new InputStreamReader(errIn)), result); + } } /** @@ -177,7 +340,14 @@ public static CreateProject newProject(String name) srcFolder.mkdir(); // Add a bare minimum source file to the source folder - // TODO: Add a template source file + try + { + CreateUtils.copyFile(FileNabber.FILE_TEMPLATE, new File(srcFolder, "Main.cc")); + } + catch (IOException er) + { + + } // Return the new project return new CreateProject(projectFolder); @@ -222,5 +392,5 @@ public TextEditorPane loadModule(String moduleName) } } return null; - } + } // end loadModule(String) } diff --git a/create/simulator/window/EditorWindow.java b/create/simulator/window/EditorWindow.java index 33a1487..6d208ed 100644 --- a/create/simulator/window/EditorWindow.java +++ b/create/simulator/window/EditorWindow.java @@ -396,11 +396,20 @@ else if (command.equals(COMMAND_CHOOSE_PROJECT)) else if (command.equals(COMMAND_RUN_EMBEDDED)) { // Run -> Run on Command Module... + BuildProblem problem = project.buildEmbeddedProject(); + if (problem == null) + { + JOptionPane.showMessageDialog(window, "Compile completed successfully.", "Create Simulator", JOptionPane.INFORMATION_MESSAGE); + } + else + { + buildProblemDialog.showMessage(problem); + } } else if (command.equals(COMMAND_RUN_SERIAL)) { // Run -> Run serial control... - BuildProblem problem = project.buildSimulatorProject(); + BuildProblem problem = project.buildSerialProject(); if (problem == null) { JOptionPane.showMessageDialog(window, "Compile completed successfully.", "Create Simulator", JOptionPane.INFORMATION_MESSAGE); diff --git a/include/cm.h b/include/cm.h index 852b794..caca1c1 100644 --- a/include/cm.h +++ b/include/cm.h @@ -21,10 +21,13 @@ #ifdef MODE_LOCAL #include -typedef unsigned char uint8_t; typedef unsigned short uint16_t; +typedef signed short int16_t; #endif +typedef unsigned char uint8_t; +typedef signed char int8_t; + // Roomba IR codes: // Remote codes #define REMOTE_NO_BUTTON 255 diff --git a/include/main.cc b/include/main.cc index f9e2755..b369512 100644 --- a/include/main.cc +++ b/include/main.cc @@ -3,11 +3,18 @@ //#include "cm.h" // For waiting in milliseconds +#ifdef MODE_LOCAL #ifdef WINDOW #include #else #include #endif +#endif + +#ifdef MODE_EMBEDDED +volatile uint16_t __timer_count = 0; +volatile uint8_t __timer_running = 0; +#endif void padCommand(); void endCommand(); @@ -118,7 +125,7 @@ void cm_baud_rate(const uint8_t &baud) cli(); // Switch the baud rate register - switch (code) + switch (baud) { case Baud115200: UBRR0 = Ubrr115200; @@ -825,8 +832,8 @@ void cm_wait_ms(const uint16_t &time) { #ifdef MODE_EMBEDDED __timer_count = time; - __timer_on = 1; - while (__timer_on) ; + __timer_running = 1; + while (__timer_running) ; #endif #ifdef MODE_LOCAL // TODO: Figure out how to wait in a resolution of milliseconds @@ -845,7 +852,7 @@ void sendByte(const uint8_t &value) { #ifdef MODE_EMBEDDED while(!(UCSR0A & _BV(UDRE0))) ; - UDR0 = data; + UDR0 = value; #else // Typically, MODE_LOCAL is defined if MODE_EMBEDDED isn't printf("0x%02X", value); #endif @@ -931,9 +938,6 @@ int main(void) sendByte(CmdStart); } -volatile uint16_t __timer_count = 0; -volatile uint8_t __timer_running = 0; - // This is the timer callback // Timer 1 interrupt times delays in ms SIGNAL(SIG_OUTPUT_COMPARE1A) @@ -941,7 +945,7 @@ SIGNAL(SIG_OUTPUT_COMPARE1A) if (__timer_count) __timer_count --; else - __timer_on = 0; + __timer_running = 0; } #endif