How to build the Marlin firmware simulator.


https://marlinfw.org/

Posted 19 august 2023
Using Visual Studio Code + PlatformIO, based on Marlin version 2.1.2.1 and MarlinSimulator 0.0.4.

The Marlin firmware comes with a simulator. Compile it as a simulated board and the then generated execuatble is a complete simulator. The simulator does what your printer does including display, buildplate, extruder, heating, SD card, EEPROM, printing and a command line to enter GCODE manually. It allows to monitor or analyse pins and serial interfaces. And it is the quietest 3d printer I know. So, how hard can it be to compile it ?
Compiling is a problem on Windows and Linux, several libraries are missing, I did not find any instructions...
that is until I discovered them in the Marlin ini directory file ini/native.ini . The instructions are in the comments...

The simulator is under development thus not all boards, drivers, displays are supported yet.
Plus it sometimes freezes or on Linux it crashed when creating a fs.img file (SD card).

The currently supported displays are stated in file Configurations/config/Simulator/Configuration.h .
Its not clear which drivers are supported but an error like "TMC220x Software Serial is not supported on LINUX." will indicate a TMC2208 is not supported.

In platform.ini set default_envs depended on Linux, MacOs or Windows (see below).
In Configuration.h I set the display to REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER and the drivers A4988.
The MOTHERBOARD is BOARD_SIMULATED.
See example Configuration.h and Configuration_adv.h

Close VSC when you start to install the required packages.

Linux (Ubuntu):

A number of additional packages is needed, they are described in ini/native.ini , as I discovered later.
Install each package with "sudo apt-get install ..." as described in ini/native.ini.
I installed (some may not be needed):
- sudo apt-get install libglm-dev
- sudo apt-get install libsdl2-image-dev
- sudo apt-get install libsdl2-dev
- sudo apt-get install libsdl2-mixer-dev
- sudo apt-get install libsdl2-ttf-dev
- sudo apt-get install libsdl2-net-dev

platform.ini: default_envs = simulator_linux_debug

Compiling did not go smoothly thus at some point I had to delete directory .cache located in .platformio in my home directory, and the .pio directory in Marlin. On Windows I even removed the complete .platformio directory.

Run simulator: in the Marlin\.pio\build\simulator_linux_debug double click MarlinSimulator

Simulator item SD Card, Generate Empty Image: crashes the simulator on Ubuntu.
The fs.img file created with WIN32 DISK IMAGER worked.

MacOs:

I don't have a Mac. See ini/native.ini .

Windows 11:

The MSYS2 building platform is needed for Windows. Install MSYS2 from https://www.msys2.org/ .
After installation it opens a MSYS2 terminal window. In the opened window execute the complete "pacman -S --needed ..." line as described in ini/native.ini.
In Windows extend the environment variable Path with the directories as described in ini/native.ini .
The Environment Variables... Path can be found in Advanced system settings (type this in the "Search" bar of Windows or in Settings "Find a setting").
Check: Open a Windows PowerShell and type "which gcc" which replies "/mingw64/bin/gcc" .

platform.ini: default_envs = simulator_windows

First attempt on windows gave this error: '"CC"' is not recognized as an internal or external command.
Solution: Close VSC, delete the .platformio directory in C:\Users\<you_as_user>\ then open VSC again. Now all required depencies are downloaded by VSC (platformio) again.

Run simulator: in the Marlin\.pio\build\simulator_windows double click MarlinSimulator.exe

How to create an .img file which can be read by the simulator (Windows)

Install WIN32 DISK IMAGER https://win32diskimager.org/
Slice your stl file and store it on a SD card (use a small size SD card, if possible).
Start Win32 Disk Imager and select the device letter of the SD card.
Input the Image File location and file name (example C:/fs.img ).
Push button Read. The SD card will be read and stored as C:/fs.img .

Then copy/move fs.img to the appropriate simulator directory (example simulator_windows Marlin\.pio\build\simulator_windows ). A fs.img is automatically loaded when the simulator is started. Other .img files can be selected when the simulator is running but that did not always work (Linux) or I was too impatient. But the fs.img created on Windows worked correctly on Ubuntu as well.

3D Benchy speedprint challenge

Using the "Printer Settings" extension in Cura I managed to get a by Cura estimated print time of 2 minuts. It was more a try and error method, by increasing "Print settings", until Cura estimated 2 minuts. With the extension you can set x y and z speed to extreme values. And then you can increase the print speeds to extreme values. File Configuration.h is tweaked and some M20* commands + M220 are used with also extreme high values. After changing the Marlin code for M220 setting the feedrate (in several files maximum feedrate increased from 999 to 9999) I even managed a print time of 02:08 .
Besides configurating a printer the code has many magic numbers and hard-coded values.

#define TFT_COLOR_UI

Download the Marlin and Configurations bugfix-2.1.x branch. Set the display background to blue (new) so the display distinguishes better within the black Simulator. In Configuration.h :

//
// Simulator currently supports these displays. Choose one!
//
//#define REPRAP_DISCOUNT_FULL_GRAPHIC_SMART_CONTROLLER
//#define REPRAP_DISCOUNT_SMART_CONTROLLER
//#define TFT_CLASSIC_UI
#define TFT_COLOR_UI

...

/**
 * TFT Theme for Color_UI. Choose one of the following or add a new one to 'Marlin/src/lcd/tft/themes' directory
 *
 * BLUE_MARLIN - Default theme with 'midnight blue' background
 * BLACK_MARLIN - Theme with 'black' background
 * ANET_BLACK - Theme used for Anet ET4/5
 */
#define TFT_THEME BLUE_MARLIN

Things you may (not) want

  1. Password settings
    Configuration.h: //#define PASSWORD_FEATURE

  2. Bed levelling, Add a menu item to move between bed corners for manual bed adjustment
    in menu "Motion" item "Bed Tramming".
    Configuration.h: #define LCD_BED_TRAMMING

  3. Include a page of printer information in the LCD Main Menu (menu About printer)
    Configuration_adv.h: #define LCD_INFO_MENU

Ad by Google.

Home