You are here

linuxCNC and arduino

To extend the possibilities i did find it interessting to conect an arduino with linuxCNC.

I will use the parall port to take care of steppers and limitswitch but for all the " just for fun stuff" a arduino give you a world of opputunities.

Here in this link you can find some inspiration  https://emc2arduino.wordpress.com/ 

But if you want to have a look how i did it then just take a look here :

first you have to install pyserial

sudo apt-get install python-pip

sudo pip install pyserial

then you have to make a new file in you linuxCNC folder where your machine configuration are

i did call it 3axis_hjh_1

when created remember to

sudo chmod +x 3axis_hjh_1

######################################################

#!/usr/bin/python
import serial
import hal
import sys
import time

PORT = "/dev/ttyACM0"
ser = serial.Serial(PORT, 115200, timeout=15)

#Now we create the HAL component and its pins
## HAL_IN arduino can read the from linuxCNC
## HAL_OUT arduino can write to linuxCNC

c = hal.component("arduino")
c.newpin("spindleEnable",hal.HAL_BIT,hal.HAL_IN)
c.newpin("stop",hal.HAL_BIT,hal.HAL_OUT)
c.newpin("axis0-cmd",hal.HAL_FLOAT,hal.HAL_IN)
c.newpin("axis1-cmd",hal.HAL_FLOAT,hal.HAL_IN)
c.newpin("axis2-cmd",hal.HAL_FLOAT,hal.HAL_IN)
c.newpin("x-minus",hal.HAL_BIT,hal.HAL_OUT)
c.newpin("x-plus",hal.HAL_BIT,hal.HAL_OUT)
c.newpin("speed",hal.HAL_FLOAT,hal.HAL_OUT)
c.newpin("y-minus",hal.HAL_BIT,hal.HAL_OUT)
c.newpin("y-plus",hal.HAL_BIT,hal.HAL_OUT)

time.sleep(1)
c.ready()

#We save the machine state (i.e. whether it's off or on) so that we only
#send a message to the Arduino when it changes
axis0cmd = c['axis0-cmd']
axis1cmd = c['axis1-cmd']
axis2cmd = c['axis2-cmd']

axis0cmdOld = 0;
axis1cmdOld = 0;
axis2cmdOld = 0;
spindleEnableOld = 0;

try:
  while 1:
    time.sleep(.01)

    axis0cmd = c['axis0-cmd'];
    if axis0cmd != axis0cmdOld:
      axis0cmdOld = axis0cmd;
      axis0icmd = round(c['axis0-cmd'],2);
      ser.write("X");
      ser.write(repr(axis0icmd))
      ser.write(";");

    axis1cmd = c['axis1-cmd'];
    if axis1cmd != axis1cmdOld:
      axis1cmdOld = axis1cmd;
      axis1icmd = round(c['axis1-cmd'],2);
      ser.write("Y");
      ser.write(repr(axis1icmd))
      ser.write(";");

    axis2cmd = c['axis2-cmd'];
    if axis2cmd != axis2cmdOld:
      axis2cmdOld = axis2cmd;
      axis2icmd = round(c['axis2-cmd'],2)
      ser.write("Z");
      ser.write(repr(axis2icmd))
      ser.write(";");

    spindleEnable = c['spindleEnable'];
    if spindleEnable != spindleEnableOld:
      spindleEnableOld = spindleEnable;
      if spindleEnable == 1:
        ser.write("+S;");
      else:
        ser.write("-S;");

    #Check to see if we have a message waiting from the Arduino
    while ser.inWaiting():
      #This should be set to the length of whatever fixed-length message
      #you're sending from the arduino. It does not have to be the same length
      #as the outbound messages.
      key = ser.read(2)
      #The Arduino generates two different key events
      #One when the key is pressed down (+S) and another when it is released (-S)
      #In this case we are going to ignore the release

#eStop
      if(key == "e0"):
        c['stop'] = 0

      if(key == "e1"):
        c['stop'] = 1

#jog x minus
      if(key == "qq"):
        c['x-plus'] = 0
        c['speed'] = 0
        c['x-minus'] = 0
        c['y-minus'] = 0
        c['y-plus'] = 0

               
#jog x minus
      if(key == "x-"):
        c['speed'] = 500
        c['x-minus'] = 1
     

#jog x plus
      if(key == "x+"):
        c['speed'] = 500
        c['x-plus'] = 1

#jog y minus
      if(key == "y-"):
        c['speed'] = 500
        c['y-minus'] = 1

#jog y plus
      if(key == "y+"):
        c['speed'] = 500
        c['y-plus'] = 1

except KeyboardInterrupt:
    ser.write("-P;");
    raise SystemExit

######################################################

then you have to edit you custom.hal file

######################################################

# Include your customized HAL commands here
# This file will not be overwritten when you run stepconf again
# Include your customized HAL commands here
# This file will not be overwritten when you run stepconf again

#First load the Python user module
loadusr -Wn arduino /home/hjh/Skrivebord/my-mill/3axis_hjh_1

#Second 'unlinkp' our pins to make them available for use.
# Then use 'net' to recreate/hook into them.

net my-jogspeed halui.jog-speed <= arduino.speed
net my-jogxminus halui.jog.0.minus <= arduino.x-minus
net my-jogxplus halui.jog.0.plus <= arduino.x-plus
net my-jogyminus halui.jog.1.minus <= arduino.y-minus
net my-jogyplus halui.jog.1.plus <= arduino.y-plus

# Spindle power
unlinkp motion.spindle-on
net spindle-On arduino.spindleEnable <= motion.spindle-on

# Stop
unlinkp iocontrol.0.user-enable-out
net estop-out arduino.stop

unlinkp axis.0.motor-pos-cmd
net xpos-cmd axis.0.motor-pos-cmd => arduino.axis0-cmd

unlinkp axis.1.motor-pos-cmd
net ypos-cmd axis.1.motor-pos-cmd => arduino.axis1-cmd

unlinkp axis.2.motor-pos-cmd
net zpos-cmd axis.2.motor-pos-cmd => arduino.axis2-cmd

#####################################################

remember to make a line in your .ini file

HALUI = halui

and now it is time for the arduino file

###################################################

#include <LiquidCrystal.h>
 
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int lcd_key     = 0;
int adc_key_in  = 0;
 
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

#define BAUD    (115200)
char buffer[128];
int sofar;

void setup()
{
  lcd.begin(16, 2);               // start the library
   lcd.setCursor(0,0);             // set the LCD cursor   position
   lcd.print("on");
  Serial.begin(BAUD);
  sofar=0;

}

void loop()
{

  // listen for serial commands
  while(Serial.available() > 0) {
    buffer[sofar++]=Serial.read();
    if(buffer[sofar-1]==';') break;  // in case there are multiple instructions
  }
 
  // if we hit a semi-colon, assume end of instruction.
    if(sofar>0 && buffer[sofar-1]==';') {
     lcd.setCursor(0,0);  
    for(int i =0; i<16;i++){
      lcd.print(buffer[i]);
    }
    
    buffer[sofar]=0;
    sofar=0;
  }
  lcd.setCursor(0,0);  
 lcd_key = read_LCD_buttons();   // read the buttons
 
   switch (lcd_key){               // depending on which button was pushed, we perform an action
 
       case btnRIGHT:{             //  push button "RIGHT" and show the word on the screen
            lcd.print("RIGHT ");
            Serial.print("x+");
            delay(20);
             Serial.print("qq");
            break;
       }
       case btnLEFT:{
             lcd.print("LEFT   "); //  push button "LEFT" and show the word on the screen
             Serial.print("x-");
             delay(20);
             Serial.print("qq");
             break;
       }    
       case btnUP:{
             lcd.print("UP    ");  //  push button "UP" and show the word on the screen
              Serial.print("y+");
              delay(20);
             Serial.print("qq");
             break;
       }
       case btnDOWN:{
             lcd.print("DOWN  ");  //  push button "DOWN" and show the word on the screen
              Serial.print("y-");
              delay(20);
             Serial.print("qq");
             break;
       }
       case btnSELECT:{
             lcd.print("SELECT");
             Serial.print("e1");
             break;
       }
       case btnNONE:{
           //  lcd.print("NONE  ");  //  No action  will show "None" on the screen
             //Serial.print("qq");
             break;
       }
   }
 
 
 
}

int read_LCD_buttons(){            
     adc_key_in = analogRead(0);  
     if (adc_key_in > 1000) return btnNONE;
     if (adc_key_in < 50)   return btnRIGHT;  
     if (adc_key_in < 195)  return btnUP;
     if (adc_key_in < 380)  return btnDOWN;
     if (adc_key_in < 555)  return btnLEFT;
     if (adc_key_in < 790)  return btnSELECT;   
     return btnNONE;                // when all others fail, return this.
}

##############################################################

now i do hope that you do have it up and running ;o)

 

 

Recent comments

No comments available.