Commit 23e7c661 authored by Guillaume Seguin's avatar Guillaume Seguin

Simplify nesting in gcode duration estimation code

parent 07cf32d6
...@@ -492,73 +492,72 @@ class GCode(object): ...@@ -492,73 +492,72 @@ class GCode(object):
ymax = max(ymax, line.current_y) ymax = max(ymax, line.current_y)
# Compute duration # Compute duration
if line.command in ["G1", "G0", "G4"]: if line.command == "G0" or line.command == "G1":
if line.command == "G4": x = line.x if line.x is not None else lastx
moveduration = P(line) y = line.y if line.y is not None else lasty
if moveduration: z = line.z if line.z is not None else lastz
moveduration /= 1000.0 e = line.e if line.e is not None else laste
totalduration += moveduration # mm/s vs mm/m => divide by 60
f = line.f / 60.0 if line.f is not None else lastf
# given last feedrate and current feedrate calculate the
# distance needed to achieve current feedrate.
# if travel is longer than req'd distance, then subtract
# distance to achieve full speed, and add the time it took
# to get there.
# then calculate the time taken to complete the remaining
# distance
# FIXME: this code has been proven to be super wrong when 2
# subsquent moves are in opposite directions, as requested
# speed is constant but printer has to fully decellerate
# and reaccelerate
# The following code tries to fix it by forcing a full
# reacceleration if this move is in the opposite direction
# of the previous one
dx = x - lastx
dy = y - lasty
if dx * lastdx + dy * lastdy <= 0:
lastf = 0
currenttravel = math.hypot(dx, dy)
if currenttravel == 0:
if line.z is not None:
currenttravel = abs(line.z) if line.relative else abs(line.z - lastz)
elif line.e is not None:
currenttravel = abs(line.e) if line.relative_e else abs(line.e - laste)
# Feedrate hasn't changed, no acceleration/decceleration planned
if f == lastf:
moveduration = currenttravel / f if f != 0 else 0.
else: else:
x = line.x if line.x is not None else lastx # FIXME: review this better
y = line.y if line.y is not None else lasty # this looks wrong : there's little chance that the feedrate we'll decelerate to is the previous feedrate
z = line.z if line.z is not None else lastz # shouldn't we instead look at three consecutive moves ?
e = line.e if line.e is not None else laste distance = 2 * abs(((lastf + f) * (f - lastf) * 0.5) / acceleration) # multiply by 2 because we have to accelerate and decelerate
# mm/s vs mm/m => divide by 60 if distance <= currenttravel and lastf + f != 0 and f != 0:
f = line.f / 60.0 if line.f is not None else lastf moveduration = 2 * distance / (lastf + f) # This is distance / mean(lastf, f)
moveduration += (currenttravel - distance) / f
# given last feedrate and current feedrate calculate the
# distance needed to achieve current feedrate.
# if travel is longer than req'd distance, then subtract
# distance to achieve full speed, and add the time it took
# to get there.
# then calculate the time taken to complete the remaining
# distance
# FIXME: this code has been proven to be super wrong when 2
# subsquent moves are in opposite directions, as requested
# speed is constant but printer has to fully decellerate
# and reaccelerate
# The following code tries to fix it by forcing a full
# reacceleration if this move is in the opposite direction
# of the previous one
dx = x - lastx
dy = y - lasty
if dx * lastdx + dy * lastdy <= 0:
lastf = 0
currenttravel = math.hypot(dx, dy)
if currenttravel == 0:
if line.z is not None:
currenttravel = abs(line.z) if line.relative else abs(line.z - lastz)
elif line.e is not None:
currenttravel = abs(line.e) if line.relative_e else abs(line.e - laste)
# Feedrate hasn't changed, no acceleration/decceleration planned
if f == lastf:
moveduration = currenttravel / f if f != 0 else 0.
else: else:
# FIXME: review this better moveduration = 2 * currenttravel / (lastf + f) # This is currenttravel / mean(lastf, f)
# this looks wrong : there's little chance that the feedrate we'll decelerate to is the previous feedrate # FIXME: probably a little bit optimistic, but probably a much better estimate than the previous one:
# shouldn't we instead look at three consecutive moves ? # moveduration = math.sqrt(2 * distance / acceleration) # probably buggy : not taking actual travel into account
distance = 2 * abs(((lastf + f) * (f - lastf) * 0.5) / acceleration) # multiply by 2 because we have to accelerate and decelerate
if distance <= currenttravel and lastf + f != 0 and f != 0: lastdx = dx
moveduration = 2 * distance / (lastf + f) # This is distance / mean(lastf, f) lastdy = dy
moveduration += (currenttravel - distance) / f
else: totalduration += moveduration
moveduration = 2 * currenttravel / (lastf + f) # This is currenttravel / mean(lastf, f)
# FIXME: probably a little bit optimistic, but probably a much better estimate than the previous one: lastx = x
# moveduration = math.sqrt(2 * distance / acceleration) # probably buggy : not taking actual travel into account lasty = y
lastz = z
lastdx = dx laste = e
lastdy = dy lastf = f
elif line.command == "G4":
moveduration = P(line)
if moveduration:
moveduration /= 1000.0
totalduration += moveduration totalduration += moveduration
lastx = x
lasty = y
lastz = z
laste = e
lastf = f
# FIXME : looks like this needs to be tested with "lift Z on move" # FIXME : looks like this needs to be tested with "lift Z on move"
if line.z is not None: if line.z is not None:
if line.command == "G92": if line.command == "G92":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment