Close

Drawing a line: Lua code

A project log for RigTig's Big 3D Printer

A DIY 3D printer (big volume, inexpensive, lightweight and portable).

rigtigRigTig 01/17/2017 at 07:100 Comments

So, we now have motors connected by WiFi rather then copper cables. This also means that the controller gives up using individual steps for each motor as well as giving up the tight co-ordination of the movements. So we need to rewrite the line drawing code to allow for a higher level command set to each motor. Since we really need the controller to be an access point, the least expensive controller I have found is an ESP12 development board. I started doing a GCODE interpreter using a Forth language, but then decided to learn Lua mainly due to the availability of the excellent NodeMCU platform.

So, here is the routine for drawing a line in 4D (being the 3 cartesian dimensions of x, y and z, plus the extruder), using the Lua language.

function line(x, y, z, e)
  -- starting at {posx, posy, posz, pose}
  --  go to {x, y, z, e} at speeds stored in globals
  local tmp = (x-posx)^2 + (y-posy)^2 + (z-posz)^2
  if(tmp < .1 and math.abs(pose - e) < 1) then return end -- already there (within 10 um)
 
  -- calculate string lengths at end posn
  local l1,l2,l3,l4;  
  l1, l2, l3 = IK(x,y,z);
  l4 = e * eRate; --2do: define interface and conversion for extruder
 
  -- calculate time for movement: distance to move / rate of movement
  -- 2do check maths for length
  local dist = math.sqrt(tmp) * 1000 -- um
  local duration = 1000000 * dist / xyzSpeed -- microseconds
 
  -- send target length and duration to get there to each motor
  msgMotor(0, string.format('%d', duration) .. ' ' .. (l1) .. ' move')
  msgMotor(1, string.format('%d', duration) .. ' ' .. (l2) .. ' move')
  msgMotor(2, string.format('%d', duration) .. ' ' .. (l3) .. ' move')
  msgMotor(3, string.format('%d', duration) .. ' ' .. (l4) .. ' move')
 
  -- in object space
  posx=x;
  posy=y;
  posz=z;
  pose=e;
end
A couple of interesting points in this code that I'll mention are the interface to the motors (msgMotor()) and the conversion from the cartesian coordinates to the string lengths (IK()).

The good part is that this version of line drawing routine is much less tedious than the code needed for directly attached motors.

Discussions