Skip to content

Commit

Permalink
Rebased changes for validator
Browse files Browse the repository at this point in the history
  • Loading branch information
ashelly committed May 21, 2015
1 parent da651e3 commit 806e2b0
Show file tree
Hide file tree
Showing 16 changed files with 304 additions and 60 deletions.
4 changes: 2 additions & 2 deletions HelloWorld.nc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
( Made using CamBam - http://www.cambam.co.uk )
( Untitled 2/13/2012 2:54:50 PM )
( T0 : 0.0394 )
G20 G90 G64 G40
G20 G90 G40
G0 Z0.125
( T0 : 0.0394 )
T0 M6
(T0 M6 )
( Engrave1 )
G17
M3 S1000
Expand Down
45 changes: 33 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,49 @@
# You should have received a copy of the GNU General Public License
# along with Grbl. If not, see <http://www.gnu.org/licenses/>.

# PLATFORM = WINDOWS
PLATFORM = LINUX

SIM_OBJECTS = main.o simulator.o serial.o eeprom.o avr/pgmspace.o avr/interrupt.o avr/io.o util/delay.o util/floatunsisf.o platform_$(PLATFORM).o
APP_OBJECTS = grbl_interface.o ../main.o ../protocol.o ../planner.o ../settings.o ../print.o ../nuts_bolts.o ../serial.o ../stepper.o ../gcode.o ../spindle_control.o ../motion_control.o ../limits.o ../report.o ../coolant_control.o ../probe.o ../system.o
#The original grbl code, except those files overriden by sim
GRBL_BASE_OBJECTS = ../protocol.o ../planner.o ../settings.o ../print.o ../nuts_bolts.o ../stepper.o ../gcode.o ../spindle_control.o ../motion_control.o ../limits.o ../coolant_control.o ../probe.o ../system.o
# grbl files that have simulator overrrides
GRBL_OVERRIDE_OBJECTS = ../main.o ../serial.o ../report.o

#AVR interface simulation
AVR_OBJECTS = avr/interrupt.o avr/pgmspace.o avr/io.o avr/eeprom.o grbl_eeprom_extensions.o

# Simulator Only Objects
SIM_OBJECTS = main.o simulator.o serial.o util/delay.o util/floatunsisf.o platform_$(PLATFORM).o

GRBL_SIM_OBJECTS = grbl_interface.o $(GRBL_BASE_OBJECTS) $(GRBL_OVERRIDE_OBJECTS) $(SIM_OBJECTS) $(AVR_OBJECTS)
GRBL_VAL_OBJECTS = validator.o overridden_report.o $(GRBL_BASE_OBJECTS) $(AVR_OBJECTS)

CLOCK = 16000000
EXE_NAME = grbl_sim.exe
COMPILE = $(CC) -Wall -g -DF_CPU=$(CLOCK) -O3 -include config.h -I. -DPLAT_$(PLATFORM)
SIM_EXE_NAME = grbl_sim.exe
VALIDATOR_NAME = gvalidate.exe
COMPILE = $(CC) -Wall -g -DF_CPU=$(CLOCK) -include config.h -I. -DPLAT_$(PLATFORM)
LINUX_LIBRARIES = -lrt -pthread
WINDOWS_LIBRARIES =
WINDOWS_LIBRARIES =

# symbolic targets:
all: main
all: main gvalidate

new: clean main
new: clean main gvalidate

clean:
rm -f $(EXE_NAME) $(SIM_OBJECTS) $(APP_OBJECTS)
rm -f $(SIM_EXE_NAME) $(GRBL_SIM_OBJECTS) $(VALIDATOR_NAME) $(GRBL_VAL_OBJECTS)

# file targets:
main: $(SIM_OBJECTS) $(APP_OBJECTS)
$(COMPILE) -o $(EXE_NAME) $(SIM_OBJECTS) $(APP_OBJECTS) -lm $($(PLATFORM)_LIBRARIES)
main: $(GRBL_SIM_OBJECTS)
$(COMPILE) -o $(SIM_EXE_NAME) $(GRBL_SIM_OBJECTS) -lm $($(PLATFORM)_LIBRARIES)


gvalidate: $(GRBL_VAL_OBJECTS)
$(COMPILE) -o $(VALIDATOR_NAME) $(GRBL_VAL_OBJECTS) -lm $($(PLATFORM)_LIBRARIES)


%.o: %.c
$(COMPILE) -c $< -o $@
$(COMPILE) -c $< -o $@

../planner.o: ../planner.c
$(COMPILE) -include planner_inject_accessors.c -c $< -o $@
Expand All @@ -49,4 +69,5 @@ main: $(SIM_OBJECTS) $(APP_OBJECTS)
../main.o: ../main.c
$(COMPILE) -include rename_main.h -c $< -o $@


overridden_report.o: ../report.c
$(COMPILE) -include rename_report_status_message.h -c $< -o $@
47 changes: 33 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
GRBL SIM : by Jens Geisler
# GRBL SIM

: by Jens Geisler, Adam Shelly


This repository contains an experimental Grbl simulator that compiles the main Grbl source code into a wrapped executable for use on a computer. No Arduino required. When the executable is run, the user should be able to interact with the Grbl simulator as if connected to an Arduino with Grbl.

WARNING: Grbl Sim is under heavy development. So many things may not work, or respond in ways unexpected. At the moment, this code is a proof-of-concept.
*WARNING: Grbl Sim is under heavy development.* So many things may not work, or respond in ways unexpected. At the moment, this code is a proof-of-concept.

## What can you do with Grbl Sim?

- Simply check out how Grbl works without needing an Arduino.
- Visualize a g-code program by having the simulator parse and execute to a GUI. Fluctuations in feed rates by the acceleration planner can be viewed as well.
- A powerful debugging tool for development.
- Each of the AVR functions are replaced with dummy functions, like the stepper ISR. These could be written to do whatever you need. For example, output simulated step pulses over time and examine its performance.
- On Linux, hook it to a fake serial port (/dev/ttyFAKE) and use it to test your Grbl interface software:

- `> socat PTY,raw,link=/dev/ttyFAKE,echo=0 "EXEC:'./grbl_sim.exe -n -s step.out -b block.out',pty,raw,echo=0" `


### Realtime modifications:

Now simulates Atmel hardware in separate thread. Runs in *aproximate* realtime. Emphasis on * **Approximate** *. Work is underway to speed it up.

## How do you compile Grbl Sim?

- Clone this repository into the directory containing the Grbl source code. (should be `<repo>/grbl`).

- Edit the Grbl-Sim Makefile to select the correct `PLATFORM =` line. LINUX and WINDOWS are currently supported.

- *(You may need to make other modifications to the Makefile and some environment variables for your particular machine. Please share any modifications you find)*

- Run `> make new` to compile Grbl Sim!

What can you do with Grbl Sim?
- Simply checking out how Grbl works without needing an Arduino.
- Visualize a g-code program by having the simulator parse and execute to a GUI. Fluctuations in feed rates by the acceleration planner can be viewed as well.
- A powerful debugging tool for development.
- Each of the AVR functions are replaced with dummy functions, like the stepper ISR. These could be written to whatever you need. For example, output simulated step pulses over time and examine its performance.

Realtime modifications by Adam Shelly:

Simulates Atmel hardware in separate thread. Runs in aproximate realtime.
## Validator
**NEW**

How do you compile Grbl Sim?
Run `gvalidate.exe GCODE_FILE` to validate that grbl will parse your GCODE with no errors.

Simply place the sim folder into the directory containing the Grbl source code. Within the sim folder, use the Makefile there to compile Grbl Sim! Edit the `PLATFORM =` line in the Makefile to "LINUX" or "WINDOWS" as needed. (You may need to make other modifications to the Makefile and some environment variables for your particular machine.)

On Linux, use `socat PTY,raw,link=/dev/ttyFAKE,echo=0 "EXEC:'./grbl_sim.exe -n -s step.out -b block.out',pty,raw,echo=0"` to create a fake serial port connected to the simulator. This is useful for testing grbl interface software.

25 changes: 1 addition & 24 deletions eeprom.c → avr/eeprom.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
eeprom.c - replacement for the avr library of the same name to provide
dummy functions
replacement functionality - write to "EEPROM.dat" in working directory
Part of Grbl Simulator
Expand Down Expand Up @@ -69,28 +69,5 @@ void eeprom_put_char( unsigned int addr, unsigned char new_value ) {
}


// Extensions added as part of Grbl
// KEEP IN SYNC WITH ../eeprom.c

void memcpy_to_eeprom_with_checksum(unsigned int destination, char *source, unsigned int size) {
unsigned char checksum = 0;
for(; size > 0; size--) {
checksum = (checksum << 1) || (checksum >> 7);
checksum += *source;
eeprom_put_char(destination++, *(source++));
}
eeprom_put_char(destination, checksum);
}

int memcpy_from_eeprom_with_checksum(char *destination, unsigned int source, unsigned int size) {
unsigned char data, checksum = 0;
for(; size > 0; size--) {
data = eeprom_get_char(source++);
checksum = (checksum << 1) || (checksum >> 7);
checksum += data;
*(destination++) = data;
}
return(checksum == eeprom_get_char(source));
}

// end of file
File renamed without changes.
2 changes: 1 addition & 1 deletion avr/pgmspace.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
pgmspace.c - replacement for the avr library of the same name to provide
dummy functions
Expand Down
2 changes: 1 addition & 1 deletion config.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#ifndef config_h

// Include parent's system.h, not config.h,
// Include grbl's system.h, not its config.h,
// because of circular dependency (config.h includes system.h which includes config.h).
// This way ensures that the CPU Map and other config flags are set before they are needed
#include "../system.h"
Expand Down
45 changes: 45 additions & 0 deletions grbl_eeprom_extensions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
grbl_eeprom_extensions.c -
Grbl adds 2 functions to the orignal avr eeprom library.
They need to be reproduced here because we need to completely override the
original eeprom interface for simulation
Grbl is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Grbl is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grbl. If not, see <http://www.gnu.org/licenses/>.
*/

// Extensions added as part of Grbl
// KEEP IN SYNC WITH ../eeprom.c

void memcpy_to_eeprom_with_checksum(unsigned int destination, char *source, unsigned int size) {
unsigned char checksum = 0;
for(; size > 0; size--) {
checksum = (checksum << 1) || (checksum >> 7);
checksum += *source;
eeprom_put_char(destination++, *(source++));
}
eeprom_put_char(destination, checksum);
}

int memcpy_from_eeprom_with_checksum(char *destination, unsigned int source, unsigned int size) {
unsigned char data, checksum = 0;
for(; size > 0; size--) {
data = eeprom_get_char(source++);
checksum = (checksum << 1) || (checksum >> 7);
checksum += data;
*(destination++) = data;
}
return(checksum == eeprom_get_char(source));
}

// end of file
2 changes: 1 addition & 1 deletion platform_LINUX.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ uint32_t platform_ns()
static uint32_t timestamp;
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC,&ts);
timestamp = ts.tv_sec*1e9+ts.tv_nsec/1;
timestamp = ts.tv_sec*1e9+ts.tv_nsec;
if (gTimeBase== 0){gTimeBase=timestamp;}
return timestamp - gTimeBase;;
}
Expand Down
2 changes: 2 additions & 0 deletions platform_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ typedef struct {
typedef void*(*plat_threadfunc_t)(void*);
#define PLAT_THREAD_FUNC(name,arg) void* name(void* arg)

#define PLATFORM_EXTRA_CR '\r'

#endif
2 changes: 2 additions & 0 deletions platform_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ typedef struct {
typedef DWORD WINAPI(*plat_threadfunc_t)(LPVOID);
#define PLAT_THREAD_FUNC(name,arg) DWORD WINAPI name(LPVOID arg)

#define PLATFORM_EXTRA_CR 0

#endif
1 change: 1 addition & 0 deletions rename_report_status_message.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#define report_status_message orig_report_status_message
2 changes: 1 addition & 1 deletion sim.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
./grbl_sim.exe -t 3 0.01 <HelloWorld.nc >HelloWorld.dat 2> HelloWorldSteps.dat
./grbl_sim.exe -t 1 0.01 <HelloWorld.nc >HelloWorld.dat 2> HelloWorldSteps.dat
1 change: 1 addition & 0 deletions simport.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
socat -d-d PTY,raw,link=/tmp/ttyFAKE,echo=0 "EXEC:'./grbl_sim.exe 1 -n -s step.out -b block.out',pty,raw,echo=0"
7 changes: 3 additions & 4 deletions simulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <stdbool.h>

#include "simulator.h"
#include "eeprom.h"
#include "avr/eeprom.h"
#include "avr/io.h"
#include "avr/interrupt.h"

Expand All @@ -49,14 +49,13 @@ void sim_add_hooks(sim_hook_fp initialize,
//setup
void init_simulator(float time_multiplier) {
sim.speedup = time_multiplier;
// sim.baud_ticks = (int)((double)F_CPU*8/BAUD_RATE); //ticks per byte

sim_hook[sh_INIT]();
}



//shutdown simulator - save eeprom
//shutdown simulator - call exit hooks, save eeprom
void shutdown_simulator() {
sim_hook[sh_EXIT]();
eeprom_close();
Expand Down Expand Up @@ -111,7 +110,7 @@ void sim_loop(){
sim_hook[sh_TICK]();

if (read_serial){
//decode baud rate.
//decode baud rate. //TODO: maybe do this only once, baud shouldn't change on the fly
sim.baud_ticks = 2*((UBRR0H<<8)+UBRR0L)+1;
if (UCSR0A & (1<<U2X0)) { sim.baud_ticks*=2; }

Expand Down
Loading

0 comments on commit 806e2b0

Please sign in to comment.