Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup of simulation code #12

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -130,24 +130,3 @@ config.h: config.h.dist
%.sym: %.elf
@echo " SYM $@"
@$(OBJDUMP) -t $< | perl -ne 'BEGIN { printf " ADDR NAME SIZE\n"; } /([0-9a-f]+)\s+(\w+)\s+O\s+\.(bss|data)\s+([0-9a-f]+)\s+(\w+)/ && printf "0x%04x %-20s +%d\n", eval("0x$$1") & 0xFFFF, $$5, eval("0x$$4")' | sort -k1 > $@


##############################################################################
# #
# Simulation #
# #
##############################################################################

SIM_SOURCES = $(PROGRAM).c serial_sim.c dda.c gcode_parse.c gcode_process.c timer_sim.c clock_sim.c temp.c sermsg.c dda_queue.c debug.c sersendf.c heater.c analog_sim.c delay_sim.c simulation.c
SIM_HEADERS = config.h serial.h dda.h gcode_parse.h gcode_process.h timer.h clock.h temp.h sermsg.h dda_queue.h debug.h sersendf.h heater.h analog.h delay.h simulation.h

SIM_OBJ = $(patsubst %.c,%.sim.o,${SIM_SOURCES})
SIM_CFLAGS = -g -Wall -Wstrict-prototypes -Os $(DEFS) -std=gnu99 -funsigned-char -funsigned-bitfields -fshort-enums

%.sim.o: %.c $(SIM_HEADERS)
@echo " CC $@"
@cc -DDEBUG -DSIMULATION -Wa,-adhlns=$(<:.c=.al) -c $(SIM_CFLAGS) -o $@ $<

sim: $(SIM_OBJ)
@echo " LINK $@"
@cc $(SIM_CFLAGS) -o $@ $^
19 changes: 2 additions & 17 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,7 @@ The PID code in heater.c is probably quite generalisable, and seems to work well
# #
##############################################################################

To compile the simulation code, use

$ make sim

The simulation program will open a serial port for communication. If you don't want to connect a null modem cable, you can use 'socat' to connect a virtual serial port to your terminal:

$ socat -d -d PTY READLINE
2010/10/18 20:24:33 socat[3700] N PTY is /dev/pts/2
2010/10/18 20:24:33 socat[3700] N using readline on stdin for reading and stdio for writing
2010/10/18 20:24:33 socat[3700] N starting data transfer loop with FDs [3,3] and [0,0]

On the first line of output, socat will tell you the name of the virtual serial port. Pass this as an argument to the simulation program (in another terminal):

$ ./sim /dev/pts/2

Now you can send G-codes from the socat terminal. The simulation code will print any data sent via the firmware's serial interface. Stepper positions will be shown in green, counting a rising slope on the pin as one step.
The directory simulation/ contains helper code to run the FiveD firmware on any PC, simulation the low-level hardware. This can be useful for debugging. See the README file in the simulation directory.


##############################################################################
Expand Down Expand Up @@ -177,7 +162,7 @@ This file associates various functions with particular pins on your avr
*** README
this file

*** simulation.[sh]
*** simulation/*.[ch]
helper code to run the code on any Unix PC

*** sender.sh
Expand Down
4 changes: 4 additions & 0 deletions arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
#include "arduino_1280.h"
#endif /* __AVR_ATmega1280__) */

#if defined (SIMULATION)
#include "simulation/simulation.h"
#endif

#ifndef DIO0_PIN
#error pins for this chip not defined in arduino.h! If you write an appropriate pin definition and have this firmware work on your chip, please tell us via the forum thread
#endif
Expand Down
56 changes: 0 additions & 56 deletions clock_sim.c

This file was deleted.

6 changes: 2 additions & 4 deletions config.h.dist
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@
- comment out pins not in use, as this drops the corresponding code and makes operations faster
*/

#ifndef SIMULATION
#include "arduino.h"
#endif

/*
RESERVED pins
we NEED these for communication
Expand Down Expand Up @@ -262,4 +258,6 @@
#define power_off() if (0) {}
#endif

#include "arduino.h"

#endif /* _CONFIG_H */
5 changes: 1 addition & 4 deletions dda.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#include "dda.h"

#include <string.h>

#ifndef SIMULATION
#include <avr/interrupt.h>
#endif
#include <avr/interrupt.h>

#include "timer.h"
#include "serial.h"
Expand Down
8 changes: 2 additions & 6 deletions dda_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

#include <string.h>

#ifdef SIMULATION
#include "simulation.h"
#else
#include <avr/interrupt.h>
#include <util/atomic.h>
#endif
#include <avr/interrupt.h>
#include <util/atomic.h>

#include "config.h"
#include "timer.h"
Expand Down
6 changes: 2 additions & 4 deletions heater.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

#ifdef HEATER_PIN

#ifndef SIMULATION
#include <avr/eeprom.h>
#endif
#include <avr/eeprom.h>

#include "sersendf.h"
#include "debug.h"
Expand Down Expand Up @@ -106,4 +104,4 @@ void heater_tick(int16_t current_temp, int16_t target_temp) {
#endif
}

#endif /* HEATER_PIN */
#endif /* HEATER_PIN */
2 changes: 1 addition & 1 deletion heater.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define _HEATER_H

#include "config.h"
#include "simulation.h"
#include "simulation/simulation.h"

#ifdef HEATER_PIN

Expand Down
7 changes: 2 additions & 5 deletions mendel.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#ifndef SIMULATION
#include <avr/io.h>
#include <avr/interrupt.h>
#endif
#include <avr/io.h>
#include <avr/interrupt.h>

#include "config.h"

Expand All @@ -18,7 +16,6 @@
#include "sersendf.h"
#include "heater.h"
#include "analog.h"
#include "simulation.h"

void io_init(void) {
// disable modules we don't use
Expand Down
7 changes: 2 additions & 5 deletions serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@

#include <stdint.h>

#ifndef SIMULATION
#include <avr/io.h>
#include <avr/pgmspace.h>
#endif
#include "simulation.h"
#include <avr/io.h>
#include <avr/pgmspace.h>

// initialise serial subsystem
void serial_init(void);
Expand Down
4 changes: 1 addition & 3 deletions sersendf.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#include "sersendf.h"

#include <stdarg.h>
#ifndef SIMULATION
#include <avr/pgmspace.h>
#endif
#include <avr/pgmspace.h>

#include "serial.h"
#include "sermsg.h"
Expand Down
5 changes: 1 addition & 4 deletions sersendf.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#ifndef _SERSENDF_H
#define _SERSENDF_H

#ifndef SIMULATION
#include <avr/pgmspace.h>
#endif
#include "simulation.h"
#include <avr/pgmspace.h>

void sersendf(char *format, ...) __attribute__ ((format (printf, 1, 2)));
void sersendf_P(PGM_P format, ...) __attribute__ ((format (printf, 1, 2)));
Expand Down
24 changes: 24 additions & 0 deletions simulation/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
##############################################################################
# #
# Simulation #
# #
##############################################################################

include ../Makefile

vpath %.c ..
vpath %.h ..
vpath config.h.dist ..

ARCH =
DEFS += -DSIMULATION -DGLOBAL_CLOCK
SOURCES += simulation.c
OBJ = $(patsubst %.c,%.o,${SOURCES})
CFLAGS = -g -Wall -Wstrict-prototypes -Os $(DEFS) -std=gnu99 -funsigned-char -funsigned-bitfields -fshort-enums -I.
LDFLAGS =
LIBS = -lrt -lm

sim: $(OBJ)
@echo " LINK $@"
@$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)

33 changes: 33 additions & 0 deletions simulation/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
##############################################################################
# #
# Simulation #
# #
##############################################################################

This directory contains helper code to run the FiveD firmware on any PC, simulating the low-level hardware. This can be useful for debugging. To compile the simulation code, use

$ make sim

The simulation program will open a serial port for communication. If you don't want to connect a null modem cable, you can use 'socat' to connect a virtual serial port to your terminal:

$ socat -d -d PTY READLINE
2010/10/18 20:24:33 socat[3700] N PTY is /dev/pts/2
2010/10/18 20:24:33 socat[3700] N using readline on stdin for reading and stdio for writing
2010/10/18 20:24:33 socat[3700] N starting data transfer loop with FDs [3,3] and [0,0]

On the first line of output, socat will tell you the name of the virtual serial port. Pass this as an argument to the simulation program (in another terminal):

$ ./sim /dev/pts/2

Now you can send G-codes from the socat terminal. The simulation code will print the time in seconds and x, y, z stepper positions, counting a rising slope on the pin as one step.

The shell script plot.sh can be used to plot positions and speeds. Simply direct the output (stdout) of the simulation program to file

$ ./sim /dev/pts/2 > example.txt

Then, call plot.sh with the file as argument:

$ ./plot.sh example.txt

plot.sh will, using gnuplot, generate two files, example-xy.png and example-speed.png, showing position in the z-plane and speeds, respectively.

2 changes: 1 addition & 1 deletion analog_sim.c → simulation/analog.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "analog.h"
#include "../analog.h"
#include "simulation.h"

static bool analog_initialised = false;
Expand Down
Empty file added simulation/avr/eeprom.h
Empty file.
7 changes: 7 additions & 0 deletions simulation/avr/interrupt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef _AVR_INTERRUPT_H
#define _AVR_INTERRUPT_H

void sei(void);

#endif /* _AVR_INTERRUPT_H */

13 changes: 13 additions & 0 deletions simulation/avr/io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _AVR_IO_H
#define _AVR_IO_H

#include <stdint.h>

#define ACD 7
#define OCIE1A 1

extern uint8_t ACSR;
extern uint8_t TIMSK1;

#endif /* _AVR_IO_H */

11 changes: 11 additions & 0 deletions simulation/avr/pgmspace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef _AVR_PGMSPACE_H
#define _AVR_PGMSPACE_H

#define PGM_P const char *
#define PROGMEM
#define PSTR(x) (x)
#define pgm_read_byte(x) (*((uint8_t *)(x)))
#define pgm_read_word(x) (*((uint16_t *)(x)))

#endif /* _AVR_PGMSPACE_H */

Loading