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

Grbl Simulator #112

Merged
merged 1 commit into from Jan 17, 2013
Merged

Grbl Simulator #112

merged 1 commit into from Jan 17, 2013

Conversation

jgeisler0303
Copy link
Contributor

Hi fellow maintainers,
I finally managed to finish the grbl simulator with comments and tested it. The changes may seem immense but grbl really only changes in one place: the execute_runtime is moved to a separate module, so that I can overwrite it for the simulator. All the other grbl files are only changed to use float declaration instead of double (I didn't find a noninvasive way of telling gcc to compile the double like avrgcc does).
The rest of the simulator is in the new directory 'sim'. The simulator uses as much code from grbl as possible and should work with many changes to grbl without touching it.
It is built by calling 'make all' in the 'sim' directory (only MinGW on Windows supported at the time but all other platforms should be straigth forward since its all pure c).
grbl_sim takes commands from stdin until an EOF is encountered. It resonds just like grbl does plus giving info about newly created blocks on stdout. On stderr it gives info on the stepper position. This can be configured by a commandline argument (just a float number) that tells the simulator the time step for printing the info.
The stepper is simulated so that the buffer is always maximally full except when plan_synchronize is called.

What do you say?

@chamnit
Copy link
Member

chamnit commented Jul 30, 2012

Well you've been busy. :) There's a lot of changes to breakdown and analyze, but I do like the idea of moving the runtime commands into a new module. I'd like to really look into everything before we integrate it in.

One comment though, you've changed all of the double's to float's. As far as I understand, the AVR compiler automatically changes these to float upon compilation. But, as far as portability to more capable processors, I think it might be a good idea to keep the more critical computations that need more precision as doubles, but we may have to figure out what variable need these more accurate values first, like the arc computations. (that is if you haven't done this already) :)

@jgeisler0303
Copy link
Contributor Author

Yeah, sure take your time. With so many changes I'm almost sure you will find something :)
As for the floats: it was my intention to let the simulator run exactly as grbl, low presision floating points and all, so that one can predict what will happen down to every step. Originally I wanted something to verify my alternate planner. But now I can imagine a GUI grbl feeder that calls grbl_sim to give a 3D preview - I'd like to call it TrueSim(TM) :)

BTW: As for the GUI grbl feeder, have you seen https://github.com/winder/Universal-G-Code-Sender? It works like a charm. But apperently there is an issue with rxtx on Macs where you have to create a special directory /var/something... its written on the Universal-G-Code-Sender site

@chamnit
Copy link
Member

chamnit commented Jul 30, 2012

So I gave it a go on my Mac, and I had to do a couple of things to get it working. In the Makefile, changed the compiler to 'COMPILER=gcc.exe' and the removed the '--gc-sections' compiler flag from the build statement. (For some reason, my Mac didn't recognize this.) I ran the executable and got the Grbl prompt. It seems to work well, but the only issue seems to be that it does not respond to any run-time commands.

So, I've been contemplating why this would be useful, given that this adds another (major) thing to maintain and that all you need to do is connect your Arduino to your USB port to see how Grbl responds. But now I see where you're going with this, creating a previewer for interfaces could definitely be a handy tool. I also like how it uses all of Grbl's actual code and wraps it into an emulator.

I guess my alarmist side wants to ensure that the emulator truly outputs exactly the same output as an Arduino would. If not, then a user could simulate their code to verify it all works, and then find that their parts are screwed up after a physical run on their setup.

For me, I think this would be great if we can get data from the stepper pulses from this emulator to be able to process and view it without an oscilloscope (which is limited in a few ways). Or track how much processor usage there is... Then again, why wouldn't a hardware emulator be just as effective? After doing a quick search, there seems to be a couple out there, but I haven't seen what they can do.

@jgeisler0303
Copy link
Contributor Author

Ah, now I think I know what you mean with "more capable processors": other micro-controllers?
In this case, maybe, we should introduce special processor dependent floating point typedefs.

Just noticed, that my pullrequest cannot be automatically merged, although I tried to work off your latest commit. I could rebase it if that helps.

@jgeisler0303
Copy link
Contributor Author

Yes, I've looked into the avr emulators myself a bit and figured that they are way to slow (much below realtime) and cumbersome to setup and operate, especially with serial communication.
So, my objective with the simulator was to create something that simulates grbl behavior down to the stepper pulse train very fast and easy to setup. One condition on equality of grbl behavior and simulator output: the block buffer is always kept reasonably full.
Indeed, what cannot be done is an analysis of processor usage. An avr emulator would be required for this. No fast track available here.
A verification of the simulator results would be nice but then again, in real life steps might be lost, the tool can jam into a clamp etc. So, blindly relying on the simulator will not be an option anyway.
In conclusion: grbl_sim is an easy way to generate a toolpath preview that is a) as close to what grbl does as it can get and b) comes at almost no extra coding cost, no recoding of a gcode interpreter and accel/decel planning in another programming language/environment.
I believe these are two very good reasons.

@chamnit
Copy link
Member

chamnit commented Jul 30, 2012

Interesting. Maybe emulators will get better in time, but what you have here would definitely work for the ideas you state. Plus, I have to say your approach is pretty ingenious.

So a few points and issues.

The main thing here is that I can't see this being useful for user, at least at the moment, but is clearly useful as a developmental tool. So, maybe we should place a README in the sim folder to state its developmental and experimental use, or make another official Grbl group repo for GrblSim (Simen, any comments?).

Secondly, I think that the simulator needs to be completely hands-off to the main Grbl codebase. The float changes are fine by me and typedefs would fix this too, but you added a function in stepper.c to get stepper time. Instead we can use the stepper's timer calls themselves with a dummy function to compute them independently for the simulator. Perhaps, this is how we can extract all the data we need, with these dummy functions; i.e. stepper pulse data.

So, how do you envision this interfacing with external UI programs? Would the simulator output (step, location, state) data via stdout or some text file? Would it be in some kind of pre-defined data structure?

@jgeisler0303
Copy link
Contributor Author

Ok, the README is a good idea.
Totally forgot about the extra function in the stepper.c. I will add code to calculate the position from outbits after every call to the stepper interrupt.
I already have a rough java interface for pipelining commands to grbl_sim and reading them back via stdout and stderr. This is how I envisioned the usage of grbl_sim, Of course, in- and outfile command line parameters would be an option too, but the redirection of stdin, stdout and stderr are just as useful (on Windows I do 2grbl_sim 0.1 < some.gcode > outfile.txt 2> stepper.txt"). The "0.1" tells grbl_sim to print stepper position only every 0.1sec (stepper sim time that is, use something like 0.000001 to get every step).

I'll fix a commit with all the changes discussed and try to rebase for an automatic merge.

@langwadt
Copy link

I just tried running it and it never prints any stepper positions, seems it never gets to calling the stepper interrupt, what I'm I missing?

@chamnit
Copy link
Member

chamnit commented Jul 31, 2012

It's beta. There's much to be done and worked out, but this is more of a proof-of-concept than anything else. If you have any suggestions on what it should do, it's most welcome.

@langwadt
Copy link

I'll definately have a better look at it later, it seems the way it has been implemented could also be used as a simple way to port to other mcu's with out changing the avr main line

@jgeisler0303
Copy link
Contributor Author

langwadt: the stepper position is printed to stderr. I don't know where that goes per default on Mac or linux, on Windows it just goes to the console. You also have to give the time step for printing of stepper position as the first (and only) argument (e.g. 0.01). Omitting this argument means time_step= 0.0 which currently is interpreted as "don't print any info". But there is a bug that causes the position at the end of every block to be printed anyway like step: 0, 0, 0, block_end. So, if you didn't see this you should check where stderr goes. This behaviour can be changed in main.c

@simen
Copy link
Member

simen commented Aug 2, 2012

This may prove incredibly useful! I'm in the middle of my family summer vacation, so this is all the feedback I have time to provide at this time :-)

@chamnit
Copy link
Member

chamnit commented Sep 21, 2012

@jgeisler0303: Jens. Just following up. Where are you with this? I'm very curious as it's pretty cool.

@jgeisler0303
Copy link
Contributor Author

Sorry I didn't comment lately. I keep reading the issues though. Really busy with my house before the cold season and less light in the evenings force me to stop for this year. Didn't implement any of the discussed changes yet but didn't forget either. Is it urgent? I will certainly come back to it as I still hope that it could also power the visualization extension of the java Universial-GCode-Sender.

@chamnit
Copy link
Member

chamnit commented Oct 1, 2012

Nope! Not urgent. Just wondering. :)

@chamnit
Copy link
Member

chamnit commented Nov 28, 2012

@jgeisler0303 : I'm working on improving the planner and stepper drivers and I think this would be extremely handy. Do you have any time to update some of this code so I can get started on it? It doesn't have to be perfect. I just need to get pointed in the right direction. Thanks!

@chamnit
Copy link
Member

chamnit commented Nov 28, 2012

Nevermind! I got it working. It's pretty straightforward (and brilliant). :)

@jgeisler0303
Copy link
Contributor Author

Thats good to hear. I still plan on making it work with your latest edge soon. Let me know if I can help you in any other way. I think I have a MATLAB script somewhere that reads the grbl-sim output and plots it.

I try to keep up with the issues. ITS BUZZING. How do you manage this? Have you still got a regular work or do you just skip sleeping? You should really get a medal or something!

@jgeisler0303
Copy link
Contributor Author

PS: you've probably already figured it out, so just in case:

  1. The only command line parameter tells grbl-sim the time interval between stepper info lines. So, if you want to get every stepper tick with its own time stamp you have to set this parameter to 1e-6 or so.
  2. To get the right settings into grbl-sim you can put the $x= yyy.yy commands before the actual test code. I plan to add a second command line parameter and/or default file for the settings.

@chamnit
Copy link
Member

chamnit commented Nov 30, 2012

Lol. Sleep? Who needs sleep? :) I've been lucky to have a good amount of freetime recently with my fiancee in graduate school and working as well. When she's studying, I work on Grbl. Who knows when the freetime will stop, so hence the hard push to get this stuff done.

Anyhow, been concentrating on getting the stepper algorithm fixed/upgrades, so I haven't been able to play with it these last days. Mainly, because it can't simulate the actual process time of the AVR during the step interrupts, but I imagine I'll use it when the planner gets the same treatment.

@csdexter
Copy link

csdexter commented Dec 1, 2012

@jgeisler0303 why not actually implement NVS (EEPROM) with a file, instead of having the user always add $* to the beginning of their test G-code? I mean, you already hooked/replaced the eeprom.c functions, why not go all the way and make it useful :-)
That's what I did in my fork and, to the rest of the code, it behaves just like the real EEPROM: you only have to configure the settings once.

@jgeisler0303
Copy link
Contributor Author

@csdexter That sounds like a logical next step. I really made it run bare bones and then stopped development.
To get the settings into the EEPROM the first time you have to type in the $x=y commands?

Nice to hear that you cloned it. I have to take a look at your fork. Did you adapt it to the latest grbl edge?

@csdexter
Copy link

csdexter commented Dec 2, 2012

@jgeisler0303 I did not clone your simulator, but developed a full HAL to make porting to other architectures easier. The first HAL implementation was for the AVR and the second for the host (i.e. POSIX machine on which you compile), which I extensively used for debugging and observing the way the code works. All credit goes to you for sparking that idea -- it's many many times easier than making simulavr work and then debugging the code the traditional way (gdb & Co.).

My EEPROM code (in the host HAL) is here and the "native" (AVR) version is here.

My fork is somewhat behind the official code here for strategic reasons: I intend to implement a few things differently than the way they're implemented here.

code very messy but tested
@jgeisler0303
Copy link
Contributor Author

Just pushed a relaunch of the simulator that runs with the latest edge of grbl. It needs no modifications of the original files but does hack into planner.c and protocol.c via injected includes at compile time (see makefile).
The code is very messy but I wanted to get this out after all this time. A first test produced this beautiful picture of a ShapeOko 'HelloWorld.nc' file: https://www.dropbox.com/s/m3j3nsubb050qzu/HelloWorld.nc.jpg. It shows the exact stepper positions in 3D with color coded stepper speed. The test file and the gnuplot script to produce the graph are included in the commit.
Input and output of grbl_sim still go via stdin, stdout and stderr. But the output format changed so that it can be used with gnuplot without modifications.
If you want to compile grbl_sim make sure you change the path to your gcc compiler in makefile.

@chamnit
Copy link
Member

chamnit commented Jan 17, 2013

Awesome, Jens! This is really cool stuff. I'll go ahead and merge it and add a read me to the sim directory to let people know what it is. I'm a little out of commission for a few weeks, as I'm working on getting my workshop finally setup, but I do have an idea with making the planner more efficient. It's based on your idea that you had about a year ago. I've fleshed it out some and would like to get your input. It's in a Matlab based simulator now. Can I send it to you?

chamnit added a commit that referenced this pull request Jan 17, 2013
@chamnit chamnit merged commit dd06268 into grbl:edge Jan 17, 2013
@jgeisler0303
Copy link
Contributor Author

Wow, thanks for merging. Now I really have a reason to tidy up the code :-)
Indeed, my plan was to use the simulator for testing my planner. But if
you have time to do that, it's even better since I'm constantly busy
with my home and children. (The simulator was moonlighted at work). Of
course I would love to assist and look at your code!

Another lane I have in mind is to write a java GUI for viewing the
simulator output along with the with gcode and with animation and the
whole shebang. This could eventually become a part of UGS.
Good luck and lots of fun with you workshop.

Awesome, Jens! This is really cool stuff. I'll go ahead and merge it
and add a read me to the sim directory to let people know what it is.
I'm a little out of commission for a few weeks, as I'm working on
getting my workshop finally setup, but I do have an idea with making
the planner more efficient. It's based on your idea that you had about
a year ago. I've fleshed it out some and would like to get your input.
It's in a Matlab based simulator now. Can I send it to you?


Reply to this email directly or view it on GitHub
#112 (comment).

@JKSoft
Copy link

JKSoft commented Jan 28, 2013

The simulator works fine for preview in my (very fresh) frontend:
http://www.j-k-s.com/guckma/grblfront.png

@derFrickler
Copy link

Nice!
What is it written in?

@JKSoft
Copy link

JKSoft commented Jan 28, 2013

C++, VS2010

@younew
Copy link

younew commented Jan 28, 2013

open source?

@JKSoft
Copy link

JKSoft commented Jan 28, 2013

not yet, too many features missing (even for my own use) like jogging, length-sensor...

@derFrickler
Copy link

mmmh Windows unfortunately...

@alpharesearch
Copy link
Contributor

Hello JKSoft,

if you are thinking going open source you could use QT for example to
make is cross platform so we Linux users may be able to use it too.

Thanks,
Markus

On 2013-01-28 10:29, JKSoft wrote:

not yet, too many features missing (even for my own use) like jogging,
length-sensor...


Reply to this email directly or view it on GitHub
#112 (comment).

@younew
Copy link

younew commented Jan 28, 2013

Nice!
Come on!

@JKSoft
Copy link

JKSoft commented Jan 28, 2013

if you are thinking going open source you could use QT for example to
make is cross platform so we Linux users may be able to use it too

I don't know anything about Linux..

@alpharesearch
Copy link
Contributor

On 2013-01-28 10:51, JKSoft wrote:

if you are thinking going open source you could use QT for example to
make is cross platform so we Linux users may be able to use it too

I don't know anything about Linux..

I think you don't need to know Linux, you would just need to develop
under Windows for now.
But if you do make it open source and if you use QT or any other cross
platform framework it will not take much time to have it running on Mac
OS and Linux... And someone (me for example for Linux) can send you a
patch for the other operating system if necessary. I don't see many
changes for this application, so most likely it would just be a
recompile on the other platform.

Regards,
Markus

PS: I used QT only one time and someone told me he was able to compile
the code under Windows, so I guess this should be true for the opposite
as well:
https://gitorious.org/~alpharesearch/robocut/alpharesearchs-robocut
https://gitorious.org/%7Ealpharesearch/robocut/alpharesearchs-robocut

@chamnit
Copy link
Member

chamnit commented Jan 28, 2013

@JKSoft : Very sweet! I'm glad you got the simulator integrated without too much difficulty (I hope). We were hoping that someone would take up the new code and apply in a new cool way, as most of us are quite busy recently to keep working at this.

As for QT, I think if you can share your C++ source on Github, that in my mind would be good enough to get people who do know QT or Java to see how you installed it into your GUI frontend. Good commenting goes a long way. This would definitely help everyone I think.

@JKSoft
Copy link

JKSoft commented Jan 28, 2013

I'm glad you got the simulator integrated without too much difficulty (I hope).

No Problem, just had to install MinGW because VS could not compile it (as documented).

As for QT, I think if you can share your C++ source on Github, that in my mind would be good enough to get people who do know QT or Java to see how you installed it into your GUI frontend.

That's the way i would prefer, Github itself is completely new for me...
The serial communication has to be changed for other platforms, for the preview i'm using OpenGL.

@JKSoft
Copy link

JKSoft commented Jan 29, 2013

Realtime preview is possible too.

@JKSoft
Copy link

JKSoft commented Feb 22, 2013

I've played with QT for the first time and put my frontend here on Github.

@nielsnl68
Copy link

@JKSoft, do you have also a runtime file to download? i like to try it?

gnonnenmacher pushed a commit to gnonnenmacher/grbl that referenced this pull request Sep 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants