Arduino I/O with Linuxcnc

More
14 Aug 2012 14:40 - 14 Aug 2012 15:19 #23166 by ArcEye
Arduino I/O with Linuxcnc was created by ArcEye
Apologies JR1050

I tried to split your last post into a new thread and succeeded but when I tried to remove it from the original thread it deleted both!

You asked if an Arduino Mega 2560 was able to be used as an I/O board for Linuxcnc

OK I have written you an arduino sketch and a hal component that will send and receive respectively, the values at 32 pins on the arduino mega.
The arduino pins used are 14-49, which I think are all digital and I set them to input

You will need to download AVR and Arduino 1.0.1, possibly install the -dev package for linuxcnc and learn how to use comp to create the component ( comp --install compname).
I could send you a compiled component, but that is the easy bit. I can't program your arduino board for you, so there is no shortcut to going to the arduino site and reading.

I have just tested it printing to stderr, you will need to connect the arduino pins to something to test them and connect the component pins bit-0 to 31 to something in hal to do anything.
Comment out the 3 test lines when using for real.
You just get mostly 0 but some 1's in the test print, but the pins are in an unknown floating state unconnected.
//arduinosend.ino
#include <SoftwareSerial.h>
#include "Wire.h"

void setup() 
{
    Serial.begin(9600);
    for(int x = 14; x < 50; x++)pinMode(x, INPUT);
}

void loop() 
{
int x, y, z;  
uint8_t  buff[5];

    buff[4] = '\0';
    z = 14;
    for(x = 0; x < 4; x++) // 4 packets 8 bits
        for(y = 0; y < 8 ; y++) bitWrite(buff[x], y, digitalRead(z++));
    Serial.write(buff, 4);
    delay(1000);    
}
//serialreceive.comp
component serialreceive              "Receiving 32 bits data from arduino";

pin out bit bit-##[32] = false       "Output bits";

option singleton yes;               
option userspace yes;

author "ArcEye arceye@mgware.co.uk";
license "GPL";
;;

#include <stdio.h>    /* Standard input/output definitions */
#include <stdlib.h> 
#include <stdint.h>   /* Standard types */
#include <string.h>   /* String function definitions */
#include <unistd.h>   /* UNIX standard function definitions */
#include <fcntl.h>    /* File control definitions */
#include <errno.h>    /* Error number definitions */
#include <termios.h>  /* POSIX terminal control definitions */
#include <sys/ioctl.h>

#define BAUDRATE B9600
#define DEVICE "/dev/ttyACM0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))

int serialport_init();
                             
struct termios toptions;;       // port setup

void user_mainloop(void)
{
int c, x, y, z;
char ch;
int fd = serialport_init();
  
    while (fd != -1) 
        {
        usleep(1000000);
        FOR_ALL_INSTS() 
            {
            z = 0;
            for(x = 0; x < 4; x++) // 4 packets 8 bits
                {
                read(fd,&ch,1); 
                for(y = 0; y < 8 ; y++) bit(z++) = bitRead(ch, y);
                }
            // test - to show that bits were written
            // comment out next 3 lines when using for real
            for(x = 0; x < 32; x++) 
                fprintf(stderr,"%d",bit(x));
            fprintf(stderr,"\n");
            }
        }
    close(fd);
    exit(0);
}


//######################################################################

int serialport_init()
{
int fd;
 
    fd = open(DEVICE, O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1)  
        {
        perror("init_serialport: Unable to open port ");
        return -1;
        }
    if (tcgetattr(fd, &toptions) < 0) 
        {
        perror("init_serialport: Couldn't get term attributes");
        return -1;
        }
    speed_t brate = BAUDRATE; 
    cfsetispeed(&toptions, brate);
    cfsetospeed(&toptions, brate);
    // 8N1
    toptions.c_cflag &= ~PARENB;
    toptions.c_cflag &= ~CSTOPB;
    toptions.c_cflag &= ~CSIZE;
    toptions.c_cflag |= CS8;
    // no flow control
    toptions.c_cflag &= ~CRTSCTS;
    toptions.c_cflag |= CREAD | CLOCAL;  // turn on READ & ignore ctrl lines
    toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
    toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
    toptions.c_oflag &= ~OPOST; // make raw
    toptions.c_cc[VMIN]  = 0;
    toptions.c_cc[VTIME] = 20;
    if( tcsetattr(fd, TCSANOW, &toptions) < 0) 
        {
        perror("init_serialport: Couldn't set term attributes");
        return -1;
        }
    return fd;
}

regards
Last edit: 14 Aug 2012 15:19 by ArcEye.
The following user(s) said Thank You: tuzki

Please Log in or Create an account to join the conversation.

More
14 Aug 2012 17:11 #23175 by JR1050
Replied by JR1050 on topic Re:Arduino I/O with Linuxcnc
Arceye,

Thank you!! This is exactly what I need to get started.Ive programmed stuff before using comp(all my plc's).Thanks again.

JR

Please Log in or Create an account to join the conversation.

More
15 Aug 2012 08:32 - 15 Aug 2012 10:07 #23229 by ArcEye
Replied by ArcEye on topic Re:Arduino I/O with Linuxcnc
Just so you can see how to address the pins, a screen shot of hal window



regards

NB
The pin states will oscillate when you test things.

Arduino pins configured as inputs are said to be in a high-impedance state and take little to trigger them.
This means however, that input pins with nothing connected to them, or with wires connected to them that are not connected to other circuits,
will report seemingly random changes in pin state, picking up electrical noise from the environment, or capacitively coupling the state of a nearby pin.

You may want to ground the unused pins with a pulldown resistor (10K resistor to ground) to prevent this

Depending upon the mounting point, you may want to mount inside a screened enclosure or shroud and use screened cables to prevent false signals
Attachments:
Last edit: 15 Aug 2012 10:07 by ArcEye.

Please Log in or Create an account to join the conversation.

More
15 Aug 2012 23:31 #23276 by JR1050
Replied by JR1050 on topic Re:Arduino I/O with Linuxcnc
Thanks!! I cant wait to get the board and start working on this.

I have three other machines slated for an Emc conversion.Another Hardinge,a Cincinnatti Lathe that had an Acra 850 that died and a Matsurra 760v2.The matsurra runs but has an aggrivating tool changer problem that no one seems to be able to figure out.It is somehow tied to the original Yasnac control.These are future projects...

The Cinci lathe was setup with a Pmac control that works.I just dont care for the windows system,and its fussy.I do however want to reuse their operators panael,hence the need for the i/o board.


Thanks again!!

JR

Please Log in or Create an account to join the conversation.

More
20 Aug 2012 19:52 #23433 by Lucariel
Replied by Lucariel on topic Re:Arduino I/O with Linuxcnc
Very interesting ..
I tried to install your module "serialreceive" without success.

1. I installed the-dev package linuxcnc

2. I wanted to copy your "serialreceive.comp" in the directory "linuxcnc / src / hal / components" but I could not find the specified directory

could you explain step by step how to do?

thanks in advance
Luca

sorry for my bad english

Please Log in or Create an account to join the conversation.

More
21 Aug 2012 09:05 - 21 Aug 2012 09:07 #23464 by ArcEye
Replied by ArcEye on topic Re:Arduino I/O with Linuxcnc
Hi

Its not a module, it is the code for one

linuxcnc.org/docs/2.5/html/hal/comp.html
shows how to compile and install it

I wanted to copy your "serialreceive.comp" in the directory "linuxcnc / src / hal / components" but I could not find the specified directory

This refers to the linuxcnc source file tree, if you are not building your own Linuxcnc from sources it is not relevant

regards
Last edit: 21 Aug 2012 09:07 by ArcEye.

Please Log in or Create an account to join the conversation.

More
21 Aug 2012 19:03 #23511 by Lucariel
Replied by Lucariel on topic Re:Arduino I/O with Linuxcnc
Thank you for having responded to me quickly .....

I had already read the document on the use of comp
unfortunately I can not perform the procedure for compile
I try to explain how I proceeded.

. I created a new text file on my desktop.
. I copied and pasted the code that you wrote (serialreceive.comp) in my text file
. I renamed the file to "serialreceive.comp."
. at this point the document that you linked to the point 11 writes:
"Place the. Comp file in the source directory " linuxcnc/src/hal/components " and re-run make. Comp files are automatically detected by the build system."
or
"If a. Comp file is a driver for hardware, it may be placed in " linuxcnc/src/hal/components " and will be built Unless LinuxCNC is configured as a userspace simulator."

in my system the path " /linuxcnc/src/hal/components " does not exist

I tried some additional information but I could not understand how.

all sorry for my English is not perfect and thanks in advance
Luke.

Please Log in or Create an account to join the conversation.

More
21 Aug 2012 19:18 #23512 by BigJohnT
Replied by BigJohnT on topic Re:Arduino I/O with Linuxcnc
I have LinuxCNC installed with the LiveCD you have to get the dev by opening up a terminal and

sudo apt-get install linuxcnc-dev

Once that is installed then change directory to where your comp file is and

sudo comp --install serialreceive.comp

Once that is done you can use your comp by loading it in your hal file.

John

Please Log in or Create an account to join the conversation.

More
21 Aug 2012 19:23 #23513 by Lucariel
Replied by Lucariel on topic Re:Arduino I/O with Linuxcnc
Thank you once again for having responded to me quickly .....

I forgot to write that the package linuxcnc-dev is already properly installed

Please Log in or Create an account to join the conversation.

More
21 Aug 2012 21:58 #23517 by andypugh
Replied by andypugh on topic Re:Arduino I/O with Linuxcnc
Lucariel wrote:

Thank you once again for having responded to me quickly .....

I forgot to write that the package linuxcnc-dev is already properly installed


The first instructions you described were for compiling in the source tree, ie with a full set of LinuxCNC source files.
You shouldn't need that, though you might do if the .comp file #includes some headers that are not available.

One thing you will need to check is that the component name in the file matches the file name. If you change only the file name, it will not work.

You ideally want to be in the same directory as the .comp file when you issue the sudo comp --install instruction. You will need "sudo" with an installed system.

If that doesn't work, post the last 100 lines or so of the output of the dmesg command.

Please Log in or Create an account to join the conversation.

Time to create page: 0.074 seconds
Powered by Kunena Forum