From 806e2b039ef5f1d77e660d910cda16dfc60aaa3c Mon Sep 17 00:00:00 2001 From: ashelly Date: Wed, 20 May 2015 23:16:24 -0400 Subject: [PATCH] Rebased changes for validator --- HelloWorld.nc | 4 +- Makefile | 45 ++++++--- README.md | 47 ++++++--- eeprom.c => avr/eeprom.c | 25 +---- eeprom.h => avr/eeprom.h | 0 avr/pgmspace.c | 2 +- config.h | 2 +- grbl_eeprom_extensions.c | 45 +++++++++ platform_LINUX.c | 2 +- platform_linux.h | 2 + platform_windows.h | 2 + rename_report_status_message.h | 1 + sim.bat | 2 +- simport.sh | 1 + simulator.c | 7 +- validator.c | 177 +++++++++++++++++++++++++++++++++ 16 files changed, 304 insertions(+), 60 deletions(-) rename eeprom.c => avr/eeprom.c (66%) rename eeprom.h => avr/eeprom.h (100%) create mode 100644 grbl_eeprom_extensions.c create mode 100644 rename_report_status_message.h create mode 100755 simport.sh create mode 100644 validator.c diff --git a/HelloWorld.nc b/HelloWorld.nc index 637a4f3..70fb889 100644 --- a/HelloWorld.nc +++ b/HelloWorld.nc @@ -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 diff --git a/Makefile b/Makefile index 6919e74..749938e 100644 --- a/Makefile +++ b/Makefile @@ -16,29 +16,49 @@ # You should have received a copy of the GNU General Public License # along with Grbl. If not, see . +# 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 $@ @@ -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 $@ diff --git a/README.md b/README.md index 0847630..2666bf2 100644 --- a/README.md +++ b/README.md @@ -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 `/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. - diff --git a/eeprom.c b/avr/eeprom.c similarity index 66% rename from eeprom.c rename to avr/eeprom.c index 939336c..f6494df 100644 --- a/eeprom.c +++ b/avr/eeprom.c @@ -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 @@ -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 diff --git a/eeprom.h b/avr/eeprom.h similarity index 100% rename from eeprom.h rename to avr/eeprom.h diff --git a/avr/pgmspace.c b/avr/pgmspace.c index ebdfef8..4480f54 100644 --- a/avr/pgmspace.c +++ b/avr/pgmspace.c @@ -1,4 +1,4 @@ - /* +/* pgmspace.c - replacement for the avr library of the same name to provide dummy functions diff --git a/config.h b/config.h index e674561..8665f5a 100644 --- a/config.h +++ b/config.h @@ -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" diff --git a/grbl_eeprom_extensions.c b/grbl_eeprom_extensions.c new file mode 100644 index 0000000..7c24a75 --- /dev/null +++ b/grbl_eeprom_extensions.c @@ -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 . +*/ + +// 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 diff --git a/platform_LINUX.c b/platform_LINUX.c index a7fb851..426d6c3 100644 --- a/platform_LINUX.c +++ b/platform_LINUX.c @@ -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;; } diff --git a/platform_linux.h b/platform_linux.h index 641c6ff..239386a 100644 --- a/platform_linux.h +++ b/platform_linux.h @@ -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 diff --git a/platform_windows.h b/platform_windows.h index c5a291f..7dbf3e0 100644 --- a/platform_windows.h +++ b/platform_windows.h @@ -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 diff --git a/rename_report_status_message.h b/rename_report_status_message.h new file mode 100644 index 0000000..b4cea75 --- /dev/null +++ b/rename_report_status_message.h @@ -0,0 +1 @@ +#define report_status_message orig_report_status_message diff --git a/sim.bat b/sim.bat index d3e3396..ea3b05e 100755 --- a/sim.bat +++ b/sim.bat @@ -1 +1 @@ -./grbl_sim.exe -t 3 0.01 HelloWorld.dat 2> HelloWorldSteps.dat +./grbl_sim.exe -t 1 0.01 HelloWorld.dat 2> HelloWorldSteps.dat diff --git a/simport.sh b/simport.sh new file mode 100755 index 0000000..aa54810 --- /dev/null +++ b/simport.sh @@ -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" diff --git a/simulator.c b/simulator.c index 2b1e694..b863d13 100644 --- a/simulator.c +++ b/simulator.c @@ -25,7 +25,7 @@ #include #include "simulator.h" -#include "eeprom.h" +#include "avr/eeprom.h" #include "avr/io.h" #include "avr/interrupt.h" @@ -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(); @@ -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<. +*/ + +#include "platform.h" +#include +#include +#include +#include +#include "../nuts_bolts.h" +#include "../settings.h" +#include "../protocol.h" +#include "../report.h" +#include "../system.h" +#include "../gcode.h" +#include "../planner.h" +#include "../serial.h" +#include "avr/io.h" + + +// Declare system global variable structure +system_t sys; +volatile io_sim_t io; + + +typedef struct arg_vars { + // Output file handles + FILE *input_file; + FILE *output_file; + uint8_t echo; + uint8_t silent; +} arg_vars_t; +extern arg_vars_t args; + + +arg_vars_t args; +const char* progname; +uint8_t exit_code = 0; + + + +int usage(const char* badarg){ + if (badarg){ + printf("Unrecognized option %s\n",badarg); + } + printf("Usage: \n" + "%s [input_file]\n" + " Options:\n" + " -o : use output file instead of stdout\n" + " -e : echo input to output\n" + " -s : silent, no output only return code \n" + "\n Parses gcode from stdin or input line, prints grbl's expected response" + "\n Returns 0 on successs, or line number of error", + progname); + return -1; +} + +int main(int argc, char *argv[]) { + int positional_args=0; + + //defaults + args.input_file = stdin; + args.output_file = stdout; + args.echo = 0; + args.silent = 0; + + progname = argv[0]; + while (argc>1) { + argv++;argc--; + if (argv[0][0] == '-'){ + switch(argv[0][1]){ + case 'e': //echo mode + args.echo = 1; + break; + case 's': //silent + args.silent = 1; + break; + case 'o': //output file + argv++;argc--; + args.output_file = fopen(*argv,"w"); + if (!args.output_file) { + perror("fopen"); + printf("Error opening : %s\n",*argv); + return(usage(0)); + } + break; + case 'h': + return usage(NULL); + default: + return usage(*argv); + } + } + else { //handle positional arguments + positional_args++; + switch(positional_args){ + case 1: //input file + args.input_file = fopen(*argv,"r"); + if (!args.input_file) { + perror("fopen"); + printf("Error opening : %s\n",*argv); + return(usage(0)); + } + break; + default: + return usage(*argv); + } + } + } + + protocol_main_loop(); + return exit_code; +} + + +/**** Re-implemented Funcitons ****/ + +//read fom input; +uint8_t serial_read() { + int data = fgetc(args.input_file); + if (data == PLATFORM_EXTRA_CR) { return(0); } + if (args.echo ) { + fputc(data, args.output_file); + } + plan_reset(); + if (sys.abort || feof(args.input_file) || data == 0x06 || data == -1) { + sys.abort = 1; + return SERIAL_NO_DATA; + } + return data; +} +//write to output +void serial_write(uint8_t data) { + if (!args.silent){ + fputc(data, args.output_file); + + } +} + +uint8_t serial_get_rx_buffer_count() { + return 0; //not important for gcode validation +} + +extern void orig_report_status_message(uint8_t); +void report_status_message(uint8_t status_code) +{ + orig_report_status_message(status_code); + if (status_code && !exit_code) { + printf("EXITING %d\n",status_code); + exit_code = status_code; + sys.abort = 1; + } +} + + +/* don't delay at all in validator */ +void _delay_ms(int i) { +} + +void _delay_us(int i) { +}