Skip to content

Commit

Permalink
Did some more work on the RobotRepeater class, fixed a bunch of error…
Browse files Browse the repository at this point in the history
…s in the embedded version of the internal library, and started work on the embedded compilation code.
  • Loading branch information
raceimaztion committed May 28, 2011
1 parent 7f91f2b commit 69e4480
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 19 deletions.
13 changes: 12 additions & 1 deletion create/simulator/RobotRepeater.java
Expand Up @@ -27,7 +27,7 @@ public class RobotRepeater
protected BufferedInputStream realIn;
protected OutputStream realOut;

public RobotRepeater(Process master)
private RobotRepeater(Process master)
{
masterProcess = master;

Expand All @@ -36,13 +36,24 @@ 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);

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);
Expand Down
186 changes: 178 additions & 8 deletions create/simulator/window/CreateProject.java
Expand Up @@ -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)
{
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -222,5 +392,5 @@ public TextEditorPane loadModule(String moduleName)
}
}
return null;
}
} // end loadModule(String)
}
11 changes: 10 additions & 1 deletion create/simulator/window/EditorWindow.java
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion include/cm.h
Expand Up @@ -21,10 +21,13 @@
#ifdef MODE_LOCAL
#include <stdio.h>

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
Expand Down
20 changes: 12 additions & 8 deletions include/main.cc
Expand Up @@ -3,11 +3,18 @@
//#include "cm.h"

// For waiting in milliseconds
#ifdef MODE_LOCAL
#ifdef WINDOW
#include <Winbase.h>
#else
#include <unistd.h>
#endif
#endif

#ifdef MODE_EMBEDDED
volatile uint16_t __timer_count = 0;
volatile uint8_t __timer_running = 0;
#endif

void padCommand();
void endCommand();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -931,17 +938,14 @@ 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)
{
if (__timer_count)
__timer_count --;
else
__timer_on = 0;
__timer_running = 0;
}

#endif
Expand Down

0 comments on commit 69e4480

Please sign in to comment.