gnea / grbl

An open source, embedded, high performance g-code-parser and CNC milling controller written in optimized C that will run on a straight Arduino
https://github.com/gnea/grbl/wiki
Other
4.04k stars 1.61k forks source link

stream.py example confusion #1109

Open RyanFenn opened 2 years ago

RyanFenn commented 2 years ago

I have been looking at the stream.py example for streaming G-code to Grbl with a more aggressive strategy in order to prevent planner buffer starvation. The example can be found in this repository at: https://github.com/gnea/grbl/blob/master/doc/script/stream.py. On line 164 it says: while sum(c_line) >= RX_BUFFER_SIZE-1 | s.inWaiting() : This is the line I am confused about, and before I explain why I am confused, I'll show a snippet of the code surrounding it to give anyone who is reading this a gernal idea of what this section of code is supposed to do:

Send g-code program via a more agressive streaming protocol that forces characters into

Grbl's serial read buffer to ensure Grbl has immediate access to the next g-code command

rather than wait for the call-response serial protocol to finish. This is done by careful

counting of the number of characters sent by the streamer to Grbl and tracking Grbl's

responses, such that we never overflow Grbl's serial read buffer.

g_count = 0 c_line = [] for line in f: l_count += 1 # Iterate line counter l_block = re.sub('\s|(.*?)','',line).upper() # Strip comments/spaces/new line and capitalize

l_block = line.strip()

c_line.append(len(l_block)+1) # Track number of characters in grbl serial read buffer grbl_out = '' while sum(c_line) >= RX_BUFFER_SIZE-1 | s.inWaiting() : out_temp = s.readline().strip() # Wait for grbl response if out_temp.find('ok') < 0 and out_temp.find('error') < 0 : print " MSG: \""+out_temp+"\"" # Debug response else : if out_temp.find('error') >= 0 : error_count += 1 g_count += 1 # Iterate g-code counter if verbose: print " REC<"+str(g_count)+": \""+out_temp+"\"" del c_line[0] # Delete the block character count corresponding to the last 'ok' s.write(l_block + '\n') # Send g-code block to grbl

There are two things with line 164 that I don't get. Firstly, why is "1" subtracted from RX_BUFFER_SIZE? Secondly, what is the purpose of using a bitwise OR operation with RX_BUFFER_SIZE-1 and s.inWaiting()? I am not understanding why I can't just do this instead for line 164: while sum(c_line) >= RX_BUFFER_SIZE: To me, this would mean that if Grbl's RX buffer is full, we are not going to send more commands, and instead we are going to read acknowledgements coming from the Grbl controller until it is determined that we can send the next block.

My mind turns to mush when I look at this line of code, so forgive me if I am asking stupid questions.