Thursday, 30 July 2015

Junk CNC controller from flatbed scanner and dvd rom drive - schematics and pcb layout

Earlier last week we put together a CNC from some left over junk. It went together surprisingly easily - since both the flatbed scanner and DVD rom drive had 4-wire (bipolar) stepper motors, all we really needed was to interface with an L293D h-bridge chip to get them moving.

It's taken a little while to get the time to draw up a "proper" schematic and PCB for the controller board, but - after a few requests in recent days - here they are:





(this is a press-n-peel ready version of the controller board; although when transferred the lettering will appear the wrong way around, this tells you that you got it right!)

The PCB is not a perfect final version- as can be seen from the massive number of wire jumpers and zero ohm resistors. Both are essentially the same thing, but 0R resistors are simply "scars" from many, quick changes the design - implemented after the schematic and PCB we originally drawn up - there's nothing like iterative prototyping!

This is how the board looks when it's all made up and plugged in:


(we've created a little "daughter board" with some jog buttons on it, to avoid having to keep poking wires into holes to manually position the xy bed!)


We've already written a little bit about sourcing cheap stepper motors and how to investigate the type of motor and how to drive them, so there's nothing really new about that here. What is slightly different is the way we're handling incoming serial data.

Since we don't want to stop the machine from moving while data is being received, we've re-created the kind of thing you might find in an Arduino serial library (though minus the blocking functions for reading the serial buffer). It's basically an interrupt-driven circular buffer and it works like this;

  • We're using the hardware UART for receiving serial data.
  • We set up a high priority interrupt to fire every time a character is received in via the serial port.
  • The character is added to a buffer (a 34 character byte array)
  • If the byte received has the value zero, set a flag to say "we've had a new message".
  • In the main code, periodically check the data received flag and parse the new message as necessary


Keeping track of positions in the buffer is on the only really tricky thing here.
We need to use two "pointers" to track two position in the array - the first pointer is the place where we want to add the next character that arrives via serial. The second pointer is the place from where we should start reading data when we want to pull the message back from the buffer



  • Whenever a character is received over serial, the interrupt fires which does a few things;
  • Writes the received character/byte into the circular buffer
  • Moves the "write pointer" along one place
  • If the received byte is zero, sets the "message received" flag



When the "message received" flag is set, in our main program code (not in the interrupt - it's important to keep that code as tight as possible) we read the data back out from the buffer/byte array. This approach means that we're not "locking" the serial buffer while we're reading from it - in fact, it's quite possible that we can be reading the serial buffer while appending data to the end of it (via the interrupt routine) at the same time!


When we have read back a zero value byte from the buffer, we know we've reached the end of the message. If we reach the end of the buffer and both the "reading" and "writing" pointers are looking at the same space, we know that the serial buffer is empty so we clear the "message received" flag.

If the either pointer exceeds the size of our buffer, it is re-set to zero - effectively "wrapping round" to the start of the array again (and thus creating a "circular" buffer).


Our buffer size is 34 characters so, of course, if we stream in more than 34 bytes without a zero value (to trigger the "read back" routine in the main code and update the read pointer) there's a good chance that the write pointer will "overtake" the read pointer. When this happens, the data read back from the buffer will be truncated. So it's important to keep serial messages down to less than 34 characters.

We settled on 34 as this allows us to stream an entire message to the 16x2 character display (32 characters) if necessary, and still have a couple of bytes over, in the buffer. Most of our serial messages are much shorter than this (a couple of bytes at most).

Because we're using zero as an end of message marker, it means that we need to use a different method to send actual numerical data (since the numerical value zero might actually be a valid part of that data). We don't want to send the legitimate numerical value zero over serial and have our controller interpret it as an end of message marker! For this reason, we're converting our numerical values into hex, and sending them as ASCII characters.

So to send the value 255, for example, we send the character sequence "FF"
Similarly, to send the value 0, we send the character sequence "00".

So to send a byte value zero, we actually send two bytes, each of value 48 (the number zero is 48 in ascii). At the receiving end we convert the ascii characters into 0-9 or A-F, assign them their correct value (so the character "1" has the value 1, "A" has the value ten and so on) and apply to either the top or bottom "nibble" (4 bits) of the value transmitted, to create the actual numerical value originally sent.

This ensures that all numerical data has the value 48-59 or 65-70, and so we can still use our zero value to indicate the end of a serial message.

Define CONFIG1H = 0x0c
Define CONFIG2L = 0x18
Define CONFIG2H = 0x1e
Define CONFIG3L = 0x00
Define CONFIG3H = 0x01
Define CONFIG4L = 0x80
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40

Define CLOCK_FREQUENCY = 20
AllDigital

declarations:
     Dim state As Byte
     Dim serial_read_pos As Byte
     Dim serial_place_pos As Byte
     Dim serial_buffer(35) As Byte
     Dim serial_has_data As Bit
     Dim serial_byte As Byte
     Dim serial_state As Byte
     Dim get_more_data As Bit
     
     Dim lcd_char_count As Byte
     
     Dim target_x As Long
     Dim target_y As Long
     Dim current_x As Long
     Dim current_y As Long
     Dim step_difference_x As Long
     Dim step_difference_y As Long
     Dim step_delay_x As Byte
     Dim step_delay_y As Byte
     Dim tmp_long As Long
     Dim b_long As Long
     
     Dim x_motor_state As Byte
     Dim y_motor_state As Byte
     Dim home_at_start As Bit
     Dim is_moving As Bit
     Dim is_jogging As Bit
     Dim motor_dir_y As Bit
     Dim motor_dir_x As Bit

     Dim servo_dir As Bit
     Dim servo_counter As Byte
     
     Const state_default = 0
     Const state_home = 1
     Const serial_first_character = 0
     Const serial_lcd_msg = 1
     Const serial_get_position = 2
     Const serial_vac_pen = 3
     Const serial_servo = 4
     
     Define LCD_BITS = 4
     Define LCD_DREG = PORTD
     Define LCD_DBIT = 4
     Define LCD_RSREG = PORTD
     Define LCD_RSBIT = 2
     Define LCD_EREG = PORTD
     Define LCD_EBIT = 3
     Define LCD_RWREG = PORTD
     Define LCD_RWBIT = 1
          
     Dim i As Byte
     Dim j As Byte
     Dim k As Byte
     
symbols:
     Symbol x_motor_enable = PORTA.2
     Symbol x_coil_a_1 = PORTE.0
     Symbol x_coil_a_2 = PORTA.5
     Symbol x_coil_b_1 = PORTA.3
     Symbol x_coil_b_2 = PORTA.4
     
     Symbol y_motor_enable = RE.1
     Symbol y_coil_a_1 = PORTC.2
     Symbol y_coil_a_2 = PORTC.1
     Symbol y_coil_b_1 = PORTE.2
     Symbol y_coil_b_2 = PORTC.0
     
     Symbol tx = PORTC.6
     Symbol rx = PORTC.7
          
     Symbol limit_home_x = PORTB.6
     Symbol limit_home_y = PORTB.5
     Symbol limit_extent_x = PORTC.5
     Symbol limit_extent_y = PORTC.4
     
     Symbol jog_left = PORTB.0
     Symbol jog_right = PORTB.1
     Symbol jog_up = PORTB.2
     Symbol jog_down = PORTB.3
     Symbol jog_slow = PORTB.4
     
     Symbol servo_pin = PORTD.0
     Symbol led_pin = PORTA.0
     Symbol vac_pen_relay = PORTA.1
     
init:
     ConfigPin PORTA = Output
     ConfigPin PORTB = Input
     ConfigPin PORTC = Output
     ConfigPin PORTD = Output
     ConfigPin PORTE = Output

     ConfigPin tx = Output
     ConfigPin rx = Input
     ConfigPin servo_pin = Output
     ConfigPin led_pin = Output
     ConfigPin vac_pen_relay = Output
     ConfigPin limit_extent_x = Input
     ConfigPin limit_extent_y = Input
     
     '------------------------------
     'first make sure motors are off
     '------------------------------
     Gosub stop_x_motor
     Gosub stop_y_motor
     is_moving = 0
     is_jogging = 0
               
     '------------------------------------
     'lift the servo up (pen is retracted)
     '------------------------------------
     servo_dir = 0
     servo_counter = 0
     
     '-----------------------------
     'start with the vacuum pen off
     '-----------------------------
     Low vac_pen_relay
     
     '---------------------
     'start the UART/serial
     '---------------------
     Hseropen 9600 '115200
     'create an interrupt on serial input
     PIE1.RCIE = 1
     IPR1.RCIP = 1 'rx is high priority
     
     'set up the serial "cirular buffer"
     serial_read_pos = 0
     serial_place_pos = 0
     For i = 0 To 34
          serial_buffer(i) = 0
     Next i
     
     '-------------------------
     'set up the stepper motors
     '-------------------------
     x_motor_state = 0
     y_motor_state = 0
     home_at_start = 0
     
     target_x = 0
     target_y = 0
     current_x = 0
     current_y = 0
     
     'to use RC4 and RC5 we have to actively disable the USB
     UCON.3 = 0
     UCFG.3 = 1
     'for RA2 we need to disconnect the vref comparitor
     CVRCON.CVROE = 0
     CVRCON.CVREN = 0
     CVRCON.CVRSS = 0
          
     '--------------
     'set up the LCD
     '--------------
     High led_pin
     Lcdinit 0
     Lcdcmdout LcdClear
     Lcdout " El Cheapo "
     Lcdcmdout LcdLine2Home
     WaitMs 1000
     Low led_pin
     Lcdout " Pick and Place "
     Hserout "El Cheapo Ready", CrLf
          
     '------------------------
     'enable pull-ups on PORTB
     '------------------------
     INTCON2.RBPU = 0
     Low led_pin
     
     '---------------------------
     'set up the state machine(s)
     '---------------------------
     state = state_home
     If home_at_start = 0 Then state = state_default
     serial_state = serial_first_character
     Gosub set_servo
     
     '---------------------------------------
     'enable global and peripheral interrupts
     '---------------------------------------
     INTCON.GIE = 1
     INTCON.PEIE = 1
     
loop:
     Select Case state
     
          '--------------
          Case state_home
          '--------------
          target_x = 0
          target_y = 0
          current_x = 0
          current_y = 0
          
          If limit_home_x <> 0 Then current_x = 1
          If limit_home_y <> 0 Then current_y = 1
          
          If current_x = 0 And current_y = 0 Then
               'send a message back to the host to tell them we're at home
               Hserout "HOME", CrLf
               'we're at the home position, so go to default state
               state = state_default
          Else
               'move home as quickly as possible
               step_delay_x = 2
               step_delay_y = 2
          Endif
          
          '-----------------
          Case state_default
          '-----------------
          'here we're waiting for the serial buffer to be flushed
          '(when a zero byte is sent to indicate end of line)
          'and when it is, parse the data out of it (because it's sent
          'via ascii)
          
          If serial_has_data = 1 Then
               'parse the data from out of the buffer
               serial_state = serial_first_character
               get_more_data = 1
               
               While get_more_data = 1
                    serial_byte = serial_buffer(serial_read_pos)
                    serial_read_pos = serial_read_pos + 1
                    If serial_read_pos > 34 Then serial_read_pos = 0
                    
                    If serial_byte = 0 Then
                         'this is the message termination byte
                         If serial_read_pos = serial_place_pos Then
                              'the last character received was this termination character
                              'so we've emptied the serial buffer
                              serial_has_data = 0
                         Else
                              'we've received more data following the termination character
                              'so once we've finished with this message, leave the has_data
                              'flag set and we'll parse the next lot of data
                         Endif
                         get_more_data = 0
                    Else
                         'read the data from the buffer, one byte at a time
                         Select Case serial_state
                              '--------------------------
                              Case serial_first_character
                              '--------------------------
                              'we've just had our first serial character: this should be the
                              'message type marker
                              If serial_byte = "M" Then
                                   'this is a string message
                                   serial_state = serial_lcd_msg
                                   lcd_char_count = 0
                                   Lcdcmdout LcdClear
                              Endif
                              If serial_byte = "P" Then
                                   'this is positional data, sent as two lots of
                                   'four character hex values (16-bit)
                                   lcd_char_count = 0 'use this to count the serial bytes in
                                   tmp_long = 0
                                   is_moving = 1
                                   serial_state = serial_get_position
                              Endif
                              If serial_byte = "V" Then
                                   'this is the vacuum pen command, next byte
                                   'will tell us whether to turn it on or off
                                   serial_state = serial_vac_pen
                              Endif
                              If serial_byte = "S" Then
                                   'this is the servo up/down command
                                   serial_state = serial_servo
                              Endif
                              
                              '------------------
                              Case serial_lcd_msg
                              '------------------
                              'this is a character to display on the LCD
                              Lcdout serial_byte
                              lcd_char_count = lcd_char_count + 1
                              If lcd_char_count = 16 Then Lcdcmdout LcdLine2Home
                              If lcd_char_count = 32 Then
                                   Lcdcmdout LcdHome
                                   lcd_char_count = 0
                              Endif
                              
                              '-----------------------
                              Case serial_get_position
                              '-----------------------
                              If lcd_char_count < 4 Then
                                   'this is another hex-based character for the x position
                                   Gosub hex_to_value
                                   tmp_long = ShiftLeft(tmp_long, 4)
                                   tmp_long = tmp_long Or b_long
                                   If lcd_char_count = 3 Then
                                        target_x = tmp_long
                                        tmp_long = 0
                                   Endif
                              Endif
                              If lcd_char_count >= 4 And lcd_char_count < 8 Then
                                   Gosub hex_to_value
                                   tmp_long = ShiftLeft(tmp_long, 4)
                                   tmp_long = tmp_long Or b_long
                                   If lcd_char_count = 7 Then
                                        target_y = tmp_long
                                        'draw the received values to the LCD
                                        Gosub draw_coords_target
                                   Endif
                              Endif
                              lcd_char_count = lcd_char_count + 1
                              
                              '------------------
                              Case serial_vac_pen
                              '------------------
                              If serial_byte = 49 Or serial_byte = 1 Then
                                   'this is the "on" command (ascii value one)
                                   High vac_pen_relay
                                   Lcdcmdout LcdHome
                                   Lcdout "Vac pen on "
                              Else
                                   Low vac_pen_relay
                                   Lcdcmdout LcdHome
                                   Lcdout "Vac pen off "
                              Endif
                              
                              '----------------
                              Case serial_servo
                              '----------------
                              If serial_byte = 49 Or serial_byte = 1 Then
                                   'this is the "servo down" command (ascii value one)
                                   servo_dir = 1
                                   Lcdcmdout LcdHome
                                   Lcdout "Pen down "
                              Else
                                   'if you've not specifically put the pen down, lift it up
                                   servo_dir = 0
                                   Lcdcmdout LcdHome
                                   Lcdout "Pen up "
                              Endif
                              Gosub set_servo
                         EndSelect
                         
                    Endif
               Wend
          Endif
          
          'as well as waiting for serial data, we can always respond to button presses
          If jog_left = 0 And current_x > 0 Then
               is_jogging = 1
               motor_dir_x = 0
               target_x = current_x - 1
          Endif

          If jog_right = 0 Then
               is_jogging = 1
               motor_dir_x = 1
               target_x = current_x + 1
          Endif
          
          If jog_up = 0 And current_y > 0 Then
               is_jogging = 1
               motor_dir_y = 0
               target_y = current_y - 1
          Endif

          If jog_down = 0 Then
               is_jogging = 1
               motor_dir_y = 1
               target_y = current_y + 1
          Endif

     EndSelect
     
     '------------------------------------------------------------------------
     'if the motors need to be turned, move them in steps.
     'This takes a bit of thinking about: we set our target x and y values
     'as "number of HALF-steps" from current position. So if we want to target
     'a point 5 steps away, we set the target_x to 10.
     'Every half step, reduce the current_x so if we make a whole step, we
     'need to reduce current_x by 2 (not by one)
     '------------------------------------------------------------------------
          
     If target_x < current_x Then
          'we need to step closer to our target in the X axis
          'provided the limit switch hasn't been hit
          If limit_home_x = 0 Then
               'we've hit the home limit switch, so set current and target x to zero
               current_x = 0
               target_x = 0
               is_moving = 0
               Hserout "LIMIT X HOME", CrLf
               Lcdcmdout LcdHome
               Lcdout " "
               Lcdcmdout LcdHome
               Lcdout "Home X reached "
          Else
               motor_dir_x = 0
               step_difference_x = current_x - target_x
               Gosub set_x_stepper_speed
               Gosub move_x_stepper
               current_x = current_x - 1
          Endif
     Endif
     
     If target_x > current_x Then
          'we need to step closer to our target in the X axis
          
          If limit_extent_x = 0 Then
          'k = 1
          'If k = 2 Then
               target_x = current_x
               is_moving = 0
               Hserout "LIMIT X EXTENT", CrLf
               Lcdcmdout LcdHome
               Lcdout " "
               Lcdcmdout LcdHome
               Lcdout "Limit X reached "
          Else
               motor_dir_x = 1
               step_difference_x = target_x - current_x
               Gosub set_x_stepper_speed
               Gosub move_x_stepper
               current_x = current_x + 1
          Endif
     Endif
     
     If target_y < current_y Then
          'we need to step closer to our target in the Y axis
          'provided the limit switch hasn't been hit
          If limit_home_y = 0 Then
               'we've hit the home limit switch, so set current and target x to zero
               current_y = 0
               target_y = 0
               Hserout "LIMIT Y HOME", CrLf
               Lcdcmdout LcdHome
               Lcdout " "
               Lcdcmdout LcdHome
               Lcdout "Home Y reached "
          Else
               motor_dir_y = 0
               step_difference_y = current_y - target_y
               Gosub set_y_stepper_speed
               Gosub move_y_stepper
               current_y = current_y - 1
          Endif
     Endif
     
     If target_y > current_y Then
          'we need to step closer to our target in the Y axis
          
          If limit_extent_y = 0 Then
          'k = 1
          'If k = 2 Then
               target_y = current_y
               Hserout "LIMIT Y EXTENT", CrLf
               Lcdcmdout LcdHome
               Lcdout " "
               Lcdcmdout LcdHome
               Lcdout "Limit Y reached "
          Else
               motor_dir_y = 1
               step_difference_y = target_y - current_y
               Gosub set_y_stepper_speed
               Gosub move_y_stepper
               current_y = current_y + 1
          Endif
     Endif
     
     '--------------------------------------------------------------
     'this just helps stop the motors getting too hot so they're not
     'always powered, when they are stationary
     '--------------------------------------------------------------
     If target_x = current_x And jog_left = 1 And jog_right = 1 Then Gosub stop_x_motor
     If target_y = current_y And jog_up = 1 And jog_down = 1 Then Gosub stop_y_motor
          
     '----------------------------------------------------------------
     'if we've just stopped moving after reaching a target point, send
     'a message back via serial to the host, to say we've arrived
     '----------------------------------------------------------------
     If is_moving = 1 Then
          If target_x = current_x And target_y = current_y Then
               is_moving = 0
               Hserout "READY", CrLf
               Gosub draw_coords_current
          Endif
     Endif
     
     '-----------------------------------------------------------------------
     'if we've just released one of the jog buttons, write the current XY out
     '-----------------------------------------------------------------------
     If is_jogging = 1 Then
          If jog_left = 1 And jog_right = 1 And jog_up = 1 And jog_down = 1 Then
               is_jogging = 0
               Gosub draw_coords_current
          Endif
     Endif
     
Goto loop
End

On Low Interrupt

Resume

On High Interrupt
     If PIR1.RCIF = 1 Then
          'we've just received a byte from the serial port
          High led_pin
          serial_byte = RCREG
          serial_buffer(serial_place_pos) = serial_byte
          serial_place_pos = serial_place_pos + 1
          If serial_place_pos > 34 Then serial_place_pos = 0
          If serial_byte = 0 Then
               'set the flag to say we've a full message in the serial buffer
               serial_has_data = 1
               Low led_pin
          Endif
     Endif
Resume

move_x_stepper:

     High x_motor_enable
     If motor_dir_x = 1 Then
          x_motor_state = x_motor_state + 1
     Else
          If x_motor_state = 0 Then
               x_motor_state = 3
          Else
               x_motor_state = x_motor_state - 1
          Endif
     Endif

     If x_motor_state > 3 Then x_motor_state = 0
     
     'once each step, the polarity of one coil should be reversed
     Select Case x_motor_state

          Case 0
          Low x_coil_a_2
          Low x_coil_b_2
          High x_coil_b_1
          High x_coil_a_1
                              
          Case 1
          Low x_coil_a_1
          Low x_coil_b_2
          High x_coil_b_1
          High x_coil_a_2
                                   
          Case 2
          Low x_coil_a_1
          Low x_coil_b_1
          High x_coil_b_2
          High x_coil_a_2
          
          Case 3
          Low x_coil_a_2
          Low x_coil_b_1
          High x_coil_b_2
          High x_coil_a_1
                         
     EndSelect
     WaitMs step_delay_x
          
Return

stop_x_motor:
     Low x_coil_a_1
     Low x_coil_a_2
     Low x_coil_b_1
     Low x_coil_b_2
     Low x_motor_enable
Return

move_y_stepper:

     High y_motor_enable
     If motor_dir_y = 1 Then
          y_motor_state = y_motor_state + 1
     Else
          If y_motor_state = 0 Then
               y_motor_state = 3
          Else
               y_motor_state = y_motor_state - 1
          Endif
     Endif

     If y_motor_state > 3 Then y_motor_state = 0
     
     'once each step, the polarity of one coil should be reversed
     
     Select Case y_motor_state

          Case 0
          Low y_coil_a_2
          Low y_coil_b_2
          High y_coil_b_1
          High y_coil_a_1
                              
          Case 1
          Low y_coil_a_1
          Low y_coil_b_2
          High y_coil_b_1
          High y_coil_a_2
                                   
          Case 2
          Low y_coil_a_1
          Low y_coil_b_1
          High y_coil_b_2
          High y_coil_a_2
          
          Case 3
          Low y_coil_a_2
          Low y_coil_b_1
          High y_coil_b_2
          High y_coil_a_1
                         
     EndSelect
     WaitMs step_delay_y
          
Return

stop_y_motor:
     Low y_coil_a_1
     Low y_coil_a_2
     Low y_coil_b_1
     Low y_coil_b_2
     Low y_motor_enable
Return

hex_to_value:
     'ascii zero is 48, ascii A is 65
     If serial_byte >= 65 Then
          'if ascii A is 65, we want A=10, B=11 etc.
          b_long = serial_byte - 55
     Else
          b_long = serial_byte - 48
     Endif
Return

set_x_stepper_speed:
     step_delay_x = 2
     If step_difference_x < 20 Then step_delay_x = 8
     If step_difference_x < 10 Then step_delay_x = 14
     If step_difference_x < 4 Then step_delay_x = 25
     If jog_left = 0 Or jog_right = 0 Then step_delay_x = 2
     If jog_slow = 0 Then step_delay_x = 50
Return

set_y_stepper_speed:
     step_delay_y = 2
     If step_difference_y < 20 Then step_delay_y = 8
     If step_difference_y < 10 Then step_delay_y = 14
     If step_difference_y < 4 Then step_delay_y = 25
     If jog_up = 0 Or jog_down = 0 Then step_delay_y = 2
     If jog_slow = 0 Then step_delay_y = 50
Return

draw_coords_target:
     Lcdcmdout LcdClear
     Lcdout "Target position:"
     Lcdcmdout LcdLine2Home
     Lcdout "X:", #target_x, " "
     Lcdout "Y:", #target_y
Return
                                                                           
draw_coords_current:
     Lcdcmdout LcdClear
     Lcdout "Current position"
     Lcdcmdout LcdLine2Home
     Lcdout "X:", #current_x, " "
     Lcdout "Y:", #current_y
Return

set_servo:
     'this is a quick and dirty blocking function
     High led_pin
     For j = 0 To 50
          If servo_dir = 0 Then
               ServoOut servo_pin, 150
          Else
               ServoOut servo_pin, 200
          Endif
          WaitMs 20
     Next j
     Low led_pin
Return

Our junk CNC controller has a few ways of driving it:
  • Jog buttons to move the xy axis up/down/left/right
  • Send serial data to give new destination co-ordinates; the CNC will move to the new target position as soon as possible
  • Serial command to switch on a relay (which is connected to one side of the 240V mains supply of a vacuum pump operated pen, for picking up SMT components)
  • Serial command to move a servo (used to lift the pen up or put it down)

Whenever the machine has received a new positional instruction and has completed the move to that location, a "ready" message is sent back to the host over serial, so that it knows the CNC head has arrived at its destination - this is preferable to using nasty timing loops to determine when the next command should be sent, from a long list of commands for the machine.

[video]

Unfortunately, even though it is highly accurate (to within 0.05mm) the flatbed scanner bed is running a little slow, even when the motor is spinning as quickly as possible (we have to have a 2ms delay between steps, otherwise the motor locks up). This is because of the multi-cog gearing before the belt. We tried driving the belt pulley directly from the motor, but it doesn't have enough torque - the gears not only slow the motor down, to allow for precise positioning while scanning, but also give it more torque than the motor can provide, if driving the belt directly.

So while, in principle, we're calling this a success (it can populate a pcb automatically from a list of co-ordinates) in practice it's actually quicker to place a dozen or so components by hand, using tweezers!

Wednesday, 29 July 2015

Last Night In Zombieville - video production

It's of no surprise to regular readers that we're currently putting together an electronic board game, which includes playing piece tracking and an app for PC, tablets and smartphones. The idea is that we can use the same hardware for multiple games, covering multiple genres, and using lots of different rule sets.

On of our more ambitious ideas is the game "Last Night in Zombieville" - an interactive movie-based board game. Like interactive movies of the early nineties, we'll have a number of clips which are played in sequence, depending on the actions of the players.

So this weekend, a few of us hopped over to meet up with fellow nerds in Berlin for three days greenscreen video filming.



Andrea plays one of the hero characters who has the possibility of being turned into a zombie, so had twice as much work to do as the others! Here he is, playing the hero, waving around a big scary (replica) gun.



Sarah has been a make-up artist on quite a few independent horror/zombie movies (as well as being quite a proficient children's face-painter for festivals and parties) and made a great job of turning Andrea into one of the living dead.


The blood and gore were not only completely edible - being made from little more than summer fruits and food colouring - but, this being Berlin, we also completely organic and ethically sourced!

Right now, we've half-a-hard drive of video footage that needs some serious post-production to get it ready for adding to our game engine. There's enough to keep even a few of us busy for quite a while yet....

Monday, 13 July 2015

Getting jiggy with it (see what I did there?)

Using a jig and a stencil for applying solder paste has been quite a revelation for churning through our PCBs for the electronic board game sections - far more so than getting a rudimentary pick-n-place device working.

After just a few hours this evening, we had soldered and assembled a chain of eighteen boards for testing.


And with just one dry solder joint across all the boards, it was very encouraging indeed, to see each one responding the placement (and removal) of a magnet above each of the hall sensors



There are a few things come up with work today that may take a little time to resolve, so it's likely to be the weekend before we can get to the workshop and rout out some mdf boards to embed these in. But already things are looking very encouraging indeed. Can't wait to get a few sections daisy-chained together and actually see one of our board game apps responding to the  hardware for real!


Jig for SMT reflow soldering

After we got our PCBs and made a CNC pick-n-place machine from bits of junk, and created a solder-paste stencil from mylar, we were ready to start assembling our PCBs.

Except it's a multi-part operation. And one part was still a bit fiddly. Sure, getting the paste onto the boards was easy enough. And placing the components using our junk machine was quite painless (although quite a bit slower than we originally anticipated, thanks in part to the - relatively slow - speed of the geared scanner motor).

We even went crazy and bought a hot-air rework device to make soldering a bit easier (rather than have to hold each component exactly in place, we can blast it with hot air and let the paste pull the component into the correct position)


All this is fine for our surface mount components, that sit on top of the board. But we've also some hall sensors that not only sit on the board, but project out from the sides. And it's important that we place these so that their sensor parts are about 35mm apart (a few mm either side isn't critical).

Keeping the sensors raised while soldering the legs - particularly with a large hot air nozzle - while also ensuring they stayed the correct distance away from the PCB edge was proving tricky - so we decided to make a jig, to hold the board and the hall sensors during soldering.

In the fullness of time, we'd like to use a CNC mill and create the jig from some aluminium or similar material, but for now a few sheets of laser-cut mdf will suffice - at least to demonstrate whether the idea is worth pursuing or not.

The first - lower - layer contains slots for our PCBs to fit. This is cut from 2mm mdf (the boards are 1.6mm thick) and in the bottom of each slot we put some thick card so that the PCB is raised slightly. This means that the surface of the PCB is perfectly flush with the top of the mdf.



(the small rounded bits at the base of each pcb slot are to allow it to be easily lifted from the jig once soldered)



The top layer includes the pcb slots plus shapes that the hall sensors can be dropped into - ensuring that they are perfectly aligned with the connectors along the edges of each PCB, ready for hot-air soldering.




(a quick dry fit to make sure everything lines up)

Normally we'd just use some double-sided tape to stick our mdf pieces together, but since we're going to be subjecting this to some serious heat, it seemed sensible to use a generous amount of PVA to hold all the parts together. If the design proves successful, we'll probably invest in a slab of (soft) aluminium and have a go at routing the design on our CNC machine.

The final jig in use:


After trying the jig with a single board, we were very quickly soldering our PCBs in batches, up to ten at a time.


In practice, manually placing components with tweezers turned out to be quicker than using our junk-built cnc (and even worked out quicker/easier than manually placing using the vacuum pen). But the jig has proved invaluable for getting the hall sensors lined up and soldered in place quickly and easily.


Now all that remains is to embed them into the mdf panels that make up our electronic board game sections, and connect to one overall master controller. That's going to require a trip to the workshop, so for now, we'll just carry on soldering more of these PCBs up, until we can get down there!

Saturday, 11 July 2015

Creating a solder paste stencil with ExpressPCB and Inkscape


Having got a few hundred PCBs ordered from 3pcb.com, it was time to think about how  we were going to solder these up when they arrived! Immediately, the obvious solution was to create some kind of solder mask.

We've tried making thin metal solder masks in the past with mixed success (the drinks-can etching approach didn't really work, whereas etching thin sheets of modeller's hobby brass using the same techniques as etching a PCB turned out quite nicely).

This time, we thought we'd try laser-cut mylar.

Creating a solder paste stencil from an ExpressPCB file is relatively easy - although it does involve a few steps, so pay attention: the first thing is to makes sure you have CutePDF installed. This great little program creates a "virtual printer" which allows you to create a PDF of just about any document that supports printing.

So in ExpressPCB, print the pads and silkscreen layer


Wait for the PDF to be generated, then open this in Inkscape. The first thing to notice is that the entire object is grouped - selected everything and click "ungroup" (ctrl +  shift + G) until there are no more groups in the selection. Now individually select the things we don't need - like the board outline, silkscreen shapes and text etc.


When you have only the pads remaining, select everything and remove the fill, while setting the stroke to 0.1mm. This should now show you the shape of your stencil-to-be.


With all of the pads still selected, choose Object -> Transform from the menu and set the scale to around 90%. This is because when cutting the mylar, we're allowing for the material to shrink back a little around the "burnt" edges. It's also creating a slightly-too-small-for-the-pad opening, so that each pad doesn't get fully flooded with solder paste.


It's important when re-scaling to apply the scale function to each object individually, not to the selected group as a whole (otherwise some of the pads will become out of line)

There are two ways to go forward from here. At first, we saved the document out as a DXF file, with polyline options. This makes the file compatible with most laser cutters (or even routers, if you're crazy enough to want to route this stencil from very fine metal - although you may want to reduce the pads even further, to allow for the thickness of the router cutting bit). This means we were (vector) cutting the lines from the mylar with our laser cutter.

It didn't really work - as the laser tried to outline each of the pads, the heat build-up caused the bits of plastic between the pads to simply disintegrate.




Instead, we printed our design to CutePDF, opened in Inkscrape, then exported as a bitmap at 600dpi. The result was a raster image that we could engrave, rather than cut. This gave a much nicer finish to the cut edges.


But even then, the finish was slightly off. It looked like the mylar was warping as the holes were being burned into it. So one last try, this time with a small piece of mylar taped to a piece of scrap mdf



This time the stencil looked just fine with very little sign of warping. When it came out of the laser, the holes lined up perfectly with the pads on the PCB.


Next we created a jig to hold our PCB, and a frame for the stencil/mylar to be mounted onto. Some locating holes ensure that the two line up properly during use.



Getting the stencil and the pcb to  line up with each other as well as the the alignment holes was a bit fiddly, but we got there in the end!

For some reason, our image contained a set of pads which are not actually on the manufactured PCB - so we're either using an old design, or a super-new one, including changes we've made since having these boards made up! Any paste deposited through these two holes on the right can simply be wiped off

The mylar film was fixed to the frame using good old fashioned double-sided tape, and a small amount of solder paste applied in a line across one edge. Using a bit of an old plastic from some food packaging, we swiped the solder paste over the face of the stencil, pushing it onto the PCB underneath


Whereas before it could take three or four minutes to apply solder paste to a board - with a pin or fine-tipped implement - it now takes just seconds. The end result isn't perfect - yet - but apparently, even with the best made, jet-cut, steel stencils, learning to apply the paste properly is something of a fine art.


Although the paste is a bit blobby around the hall sensor connector, the rest of the pads look just fine. It's much more precise than our current application method (which is to just lay a slug-trail of solder paste across all the pads anyway) so in the morning we'll place some components and give them the hot-air treatment, to see how the excess is handled.

Mylar film stencils are not as hardwearing as metal ones, but are generally good for about a hundred passes (maybe more). So, until we're churning out our PCBs by the hundred or thousands, this seems like an ideal way of creating stencils for applying solder paste to our prototype boards!



Friday, 10 July 2015

858D hot air rework station

I just took receipt of a hot air rework station from eBay. It's one of the 858D hot gun things with a choice of nozzles.


Jason has one and when I used it a while back, it was great fun.
But there's something about the packaging that suggests the graphic designer was keen to get away, for an early dart on a Friday, when he knocked this one out...


Tuesday, 7 July 2015

Building a pick-n-place machine from junk (flatbed scanner, dvd drive)

We've got some PCBs to solder up. A few hundred of the buggers. And, at the minute, they take aaaages. That's mostly because we haven't yet got a stencil for the solder paste, and the solder paste is quite old and not as fluid as it should be, so putting it down is like trying to glob jelly down with a pin - sometimes it sticks, sometimes not, sometimes it lifts off with the pin and so on. It's a nightmare!

But other than the solder paste application problem (which is to be solved now we've a nice stainless steel stencil on order) there's also the small issue of placing parts. Anyone who's ever done this with tweezers is probably familiar with the age old problems....


If you keep your 1206 resistors in nice, separated, pill boxes (or jewellers box compartments, or whatever you call them) there's a good chance that they don't all fall onto your desk the same way up! Picking resistors not only the right way up, but also the right way around (yes, we like all our numbers to be the same way around on a soldered-up board) can be a pain.

Then there's the issue of getting a dab of solder paste on the tip of your tweezers. Which acts like some kind of crazy kinetic-mind-melting glue, attracting all kinds of SMT components (and bits of fluff) from all over the desktop.

Once you've put the component down on the board, you have to remove the tweezers cleanly, without sneezing (or, more likely, twitching or shaking) as you remove them, unless you want to spend the next few minutes nudging the component back into it's desired position. Nudge a bit, no, too much, back a bit, no, back where it was.....

We've been saying for some time we need to make a pick-n-place machine.
The only thing is, they're pretty time consuming. What we really need is a quick hacked-up pick-n-place machine, made from junk that's just lying around, and can be quickly thrown together and made to work. Since our PCBs are only very narrow (100mm x just 14mm wide) there's even a case for making a one-time pick-n-place, just for this one job. After all, if it can be done quickly, and cheaply, then there's no harm in it being a single-job machine, rather than a generic device capable of handling lots of different boards (which it would need to be if we were going to spend any great money on it!)

It just so happened that we had an old flatbed scanner lying around, and a knackered DVD rom drive. Perfect for making a simple xy carriage!

Unusually our flatbed scanner had just one rail, straight down the centre, rather than a rail either side as we'd expected. No matter, this will just have to do for now.

The flatbed scanner was easily dismantled. At first we were just going to take the stepper and the belt from it, but then it occurred to us that the actual plastic frame may just make a good enough y axis for our pick-n-place - after all, it was a good enough y-axis for a scanner, taking in images at 1200dpi. It has a footprint of just over A4 (so quite small) but even if we only used one "half" of the bed (splitting it long-ways and adding a second rail along one side) there's still plenty of travel in the y-direction and enough width for the x-carriage made from a DVD rom drive.


After putting back the belt and carriage system, we very quickly had a working y-axis. The x-carriage would mount on the black plastic piece that the current belt system drives up and down along the 6mm stainless steel rod. The y-axis is actually quite nicely assembled, for such a cheap scanner.

The original scanning head was pulled along by a single 48 steps/rev (7.5 degree) stepper motor.


The underside looked very interesting - it's been geared down quite a bit. So this will be great for super-precise positioning. Our only concern is that it doesn't slow things down too much!


The linear rail is simply a polished stainless steel rod with a piece of plastic clipped over it. The toothed belt sits in part of the plastic that has been moulded to fit the teeth - no need for clamping here! The rod feels like it may have had silicon grease applied at some point in the past. It's not the best quality for a carriage assembly, but it moves smoothly (no binding) and there's no wiggle in it as it travels up and down - everything you could want from a linear rail system!


The tension is maintained in the belt by a simple spring-loaded, free spinning pulley on the far end (opposite the motor). The pulley is not toothed, so the belt just passes around it. The spring ensures that the belt is under tension, without putting it under too much strain.


For the x-axis we butchered a old, broken, DVD rom drive.
It doesn't have particularly fine resolution, but should be just about good enough for what we're needing; after all, our PCBs can withstand placement up to 0.5mm or more out-of-place. If we're going to place all our components and use either hot air or an over to "bake" the boards, any slight mis-alignment will be corrected as the solder paste melts anyway.


To get at the carriage, simply keep removing "layers" from the DVD rom drive (retaining any parts that might look useful along the way) until the optical head positioning assembly is accessible.


This part had actually been glued (presumably with some kind of epoxy) to the cast aluminium frame, so required a bit of "persuasion" to release it. Much to the relief of the other nerds, the hammers stayed locked away for this (though a selection flat-ended screwdrivers made for great levers to prise apart the different layers)


We kept the ribbon cable from the little stepper motor intact, but it's quite trivial to solder a couple of wires to the connection points on the top, if it should get damaged. We took off as much of the optical reader hardware as possible from our carriage, until we were left with very little

The optical reader assembly had some interesting bits, including some tiny little mirrors and this tiny - but functional - magnifying lens!

With the two assemblies dismantled, it was now a matter of trying to get the stepper motors to work independently, before we put everything back together and try running it as an actual xy cnc.
This meant putting together a test rig, with a PIC microcontroller (what else?) some L293D H-bridge IC chips (thanks for those Jason) and - just for fun - a 16x2 character LCD to report what was happening (and when the thing is finished, to display which component is being placed, and where)



Sure, it just looks like a tangle of wires (though Steve should be impressed that there are plenty of different colours, to distinguish between each part of the circuit, instead of our usual trick of wiring everything in green wire, then not knowing where to start when it comes to debugging!). Sure, it's just a microcontroller driving a couple of steppers (no Arduino copy-n-paste library code here though!). But it does demonstrate that we've got the basic control over an x and a y axis, both using jog buttons, and by sending "destination co-ordinates" over serial.

Originally it was tempting to just throw an Arduino at the h-bridge chips and get the motors spinning, but in the end we went with the big beefy 18F4550 PIC for a couple of reasons:
  • Arduino libraries are of variable quality. Most are ok. Some are quite clever. A few are shockingly poor. If there are any problems trying to run multiple things together (like receiving serial while maintaining two steppers, and servicing a servo, as well as responding to manual jog controls) eliminating the libraries is the first place to start - so why use them in the first place?
  • We're going to throw quite a bit of stuff at this, and also quite liked the idea of a character LCD to report back which component the device is picking, what it's target destination/co-ordinates are etc. This means we need quite a few pins.
  • Manual jog buttons (up/down/left/right plus a "jog-slowly" option) means lots of I/O pins are needed. Probably more than we could get from a '328 AVR without resorting to shift registers or IO expanders.

Even with a massive 40-pin chip, we used up a lot of IO lines:


A full explanation of the code and ideas behind it (including interrupt-driven, circular buffered serial receive) will follow in a later post. For now, here's a video of some very crude testing:



Although you might blink and miss it, there's also a simple ramp-down function as the carriage starts to approach it's destination point - you can see it in the first motor, as it slows to a stop, a hear a definite change in tone of the motor as the DVD carriage reaches the destination point.