Machsupport Forum

Third party software and hardware support forums. => Modbus => Topic started by: ejjenkins on September 11, 2016, 10:40:39 PM

Title: Modbus and Arduino Question
Post by: ejjenkins on September 11, 2016, 10:40:39 PM
I am making a pendant to use with my CNC machine.  The pendant is using an Arduino Due for the brains with a 5" touchscreen.  I have a 16 button keypad and 2 small joysticks (each with a center button) connected as inputs.  I am connecting to Mach3 with the modbus protocol.  

I have programmed the touchscreen to allow selecting an axis, changing Jog Mode, cycling through the Jog Increment steps, zeroing selected axis, and zeroing all.

I need to add the spindle speed on the screen as I plan to move to a spindle with vfd and of course make an enclosure.

I actually have more inputs then I know what to do with. I am open to suggestions  :D

My question is...how can I, with the pendant, have Mach3  go to a specific Axis' zero position (not machine zero).
Title: Re: Modbus and Arduino Question
Post by: ejjenkins on September 11, 2016, 10:48:32 PM
Some information to add...I have created brain files to get the DRO info from mach 3 and to pass the button presses and joystick movements.  Everything works great...I just need to have the machine go to a single axis zero location.

Any help would be appreciated.

Thank you.
Erik
Title: Re: Modbus and Arduino Question
Post by: Tweakie.CNC on September 12, 2016, 01:24:41 AM
Excellent work Erik, that looks like it is going to be a really nice pendant.

Tweakie.
Title: Re: Modbus and Arduino Question
Post by: ejjenkins on September 12, 2016, 08:36:29 PM
Thanks Tweakie.
Title: Re: Modbus and Arduino Question
Post by: ejjenkins on September 13, 2016, 04:52:38 PM
I had to write a macro and use OEM triggers to get the "Go To Selected Axis Zero" to work.

All is good now  :)
Title: Re: Modbus and Arduino Question
Post by: ejjenkins on September 18, 2016, 10:04:32 PM
I made an enclosure for my new pendant out of some thin (0.5" x 2") aluminum extrusion. The faceplate was a test in 1/8" hardboard, I plan to make the final one out of either aluminum or black ABS...I have both, but I am leaning towards the ABS. I think the black will look better with the aluminum framing.

Everything is working well now.  I switched to a different keypad with removable plastic covers for each button and a new label can be printed easily and the function of each button can also be changed quickly.  I have added the ability for user-defined macros as well.  I have three brain files setup to handle button presses as well as update the DROs on the screen. 
Title: Re: Modbus and Arduino Question
Post by: fbforos on September 18, 2016, 11:12:04 PM
Great job ejjenkins.

Will you share more info about your project.
Thanks
Title: Re: Modbus and Arduino Question
Post by: ejjenkins on September 19, 2016, 04:54:30 PM
Here are my notes at the top of the Arduino Sketch, it gives a bit of the info which makes this pendant function.  If you would like more info, please ask.
Code: [Select]
                                                                                                                     
// This is for a CNC Pendant to interact with my heavily modified 1000mm x 1000mm Inventables X-Carve using Mach 3 to control the machine.     
// I am using the Arduino Due for the brains of the pendant.  All Communication with Mach3 is accomplished             
// through the serial protocol modbus.  In Mach3 I have set up brains to handle the input and output to               
// the arduino.  One brain to handle output to the Pendant and two to handle button presses.  I have also configured   
// several OEMtriggers to handle the execution of macros in the M1000.m1s file.  The macro can execute the Probe Zero
// Z Axis, probe Zero All Axes, Got to Zero on X, Y, Z or A axis as well as user defined macros.                     
//                                                                                                                   
// I have the following hardware attached to the Arduino Due:                                                         
// 5" GLCD display and LCD Shield with a 800 x 480 resolution from SainSmart (using the UTFT libraries)               
// 16 button keypad attched as an analog keypad using only 1 analog pin on the Arduino as well as ground and 5v.       
// 2 joysticks, each attached to 2 analog pins and 1 digital pin for the center button.                               
//                                                                                                                     
//     Current keypad button functions are defined as:                                                                 
//                                                                                                                     
//        1: Reset             2: Cycle Start       3: Cycle Pause      4: Cycle Stop                                 
//        5: Home All          6: Rewind Program    7: Edit G-Code      8: Close G-Code                               
//        9: Probe Z Axis     10: Spindle On/Off   11: Jog On/Off      12: Toolpath On/Off                             
//       13: Select X Axis    14: Select Y Axis    15: Select Z Axis   16: Select A Axis                               
//                                                                                                                     
//                                                                                                                     
//  Here is a list of all the send and receive bits and what they are set for.  These are set within the Mach3         
//  brain files.                                                                                                       
//                                                                                                                     
// bitSet(rcd[0],0)  RefAll           bitSet(rcd[1],0)  Cycle Start      bitSet(rcd[2],0)  Jog Increment Cycle         
// bitSet(rcd[0],1)  Rewind File      bitSet(rcd[1],1)  Cycle Stop       bitSet(rcd[2],1)  Jog STEP                   
// bitSet(rcd[0],2)  Edit File        bitSet(rcd[1],2)  Reset            bitSet(rcd[2],2)  Jog CONT                   
// bitSet(rcd[0],3)  Jog X-Positive   bitSet(rcd[1],3)  Zero All         bitSet(rcd[2],3)  Probe Zero Z (OEMtrigger1) 
// bitSet(rcd[0],4)  Jog X-Negative   bitSet(rcd[1],4)  Zero X           bitSet(rcd[2],4)  Goto X Zero  (OEMtrigger2) 
// bitSet(rcd[0],5)  Jog Y-Positive   bitSet(rcd[1],5)  Zero Y           bitSet(rcd[2],5)  Goto Y Zero  (OEMtrigger3) 
// bitSet(rcd[0],6)  Jog Y-Negative   bitSet(rcd[1],6)  Zero Z           bitSet(rcd[2],6)  Goto Z Zero  (OEMtrigger4) 
// bitSet(rcd[0],7)  Jog Z-Positive   bitSet(rcd[1],7)  Zero A           bitSet(rcd[2],7)  Goto A Zero  (OEMtrigger5) 
// bitSet(rcd[0],8)  Jog Z-Negative   bitSet(rcd[1],8)  Jog Z-Right/Off  bitSet(rcd[2],8)  Zero X Axis                 
// bitSet(rcd[0],9)  Jog A-Positive   bitSet(rcd[1],9)  Jog Z-Left/Off   bitSet(rcd[2],9)  Zero y Axis                 
// bitSet(rcd[0],10) Jog A-Negative   bitSet(rcd[1],10) Jog A-Right/Off  bitSet(rcd[2],10) Zero Z Axis                 
// bitSet(rcd[0],11) Jog Y-Right/Off  bitSet(rcd[1],11) Jog A-Left/Off   bitSet(rcd[2],11) Zero A Axis                 
// bitSet(rcd[0],12) Jog Y-Left/Off   bitSet(rcd[1],12) Feedhold         bitSet(rcd[2],12)                             
// bitSet(rcd[0],13) Jog X-Right/Off  bitSet(rcd[1],13) Jog Off/On       bitSet(rcd[2],13)                             
// bitSet(rcd[0],14) Jog X-Left/Off   bitSet(rcd[1],14) Toolpath On/Off  bitSet(rcd[2],14)                             
// bitSet(rcd[0],15) Close File       bitSet(rcd[1],15) Spindle On/Off   bitSet(rcd[2],15)                             
//                                                                                                                     
// bits rcd[3-12], 0-15 are currently unused...there are a ton of unused bits still available.                         
//                                                                                                                     
//  Variables received from Mach3:                                                                                     
//                                                                                                                     
//  mcd[0] + mcd[1]  X-Axis location   - receiving one side of the decimal point in each data element.  Each one is   
//  mcd[2] + mcd[3]  Y-Axis location      converted to a signed float and displayed on the GLCD                       
//  mcd[4] + mcd[5]  Z-Axis location                                                                                   
//  mcd[6] + mcd[7]  A-Axis location                                                                                   
//  mcd[8] + mcd[9]  Jog Increment                                                                                     
//  mcd[10],0        Reset Status      - A simple toggle bit                                                           
//  mcd[10],1        Jog Mode          - A simple toggle bit                                                           
//  mcd[10],2-15 are currently unused                                                                                 
//  mcd[11]          Feedrate Overide  - An integer                                                                   
//  mcd[12]          Feedrate          - An integer                                                                   
//                                                                                                                     
// Currently there are 13 x 16 bit data elements to send and receive from Mach 3.  I am using all of the data         
// elements received from Mach 3 to update the DROs and Reset, Jog, Feedrate Overide and Feedrate.  If I want to       
// receive more information I will need to add more data elements.  to add Spindle Speed, I will need one more data   
// element as it will simply be an integer.
                                                                           
Title: Re: Modbus and Arduino Question
Post by: ejjenkins on September 21, 2016, 08:09:43 PM
I have had a few questions on how I am communicating with mach 3.  I am using the simple modbus slave library from here:  https://github.com/charlesbaynham/simple-modbus

In mach 3 I am using serial modbus non-plugin.

To pass the Axis coordinate DRO values to the LCD, I am using 2 Data elements...one for each side of the decimal point.

The brain to send the values are like this:

X-Position   ---   No Operation, Pass Signal   ---   MOD: 13            This is the Left side of the decimal point
X-Position   ---   Formula, (A*1000)-(int(A)*1000)   ---   MOD: 14  This is the Right side of the decimal point

Then I print to the LCD and format the results.

Make sense?
Title: Re: Modbus and Arduino Question
Post by: fbforos on October 10, 2016, 02:47:49 PM
Thanks ejjenkins
Title: Re: Modbus and Arduino Question
Post by: robertspark on October 12, 2016, 09:21:33 PM
Nice pendant
Title: Re: Modbus and Arduino Question
Post by: ejjenkins on October 16, 2016, 11:00:14 AM
Thank you!

Erik
Title: Re: Modbus and Arduino Question
Post by: hummeruli on January 29, 2017, 05:58:06 PM
@ejjenkins

I'm trying to build everything using a pendant with modbus based on an Arduino Mega 2560 or a smaller one, but I do not understand the brain programming or how the data come to the display in the Arduino.

I would be very happy if I would get the files from you.

Thank you
Title: Re: Modbus and Arduino Question
Post by: nokialkatel on January 31, 2017, 06:05:01 AM
I can help with brans but I need arduino code...
Title: Re: Modbus and Arduino Question
Post by: hummeruli on January 31, 2017, 03:41:33 PM
I use the corrected Arduino Code from Schilling, because there bugs in it. With the right one i test Buttons and Signal-output but no LCD on it. A second example code for a pedant gives me no connection to Mach3, but it is not for Modbus. I think it's for LinuxCNC. I can test it with a Terminal-program.
Title: Re: Modbus and Arduino Question
Post by: nokialkatel on January 31, 2017, 04:39:38 PM
How you is conected lcd to arduino.is it i2c or serial or parallel lcd
Title: Re: Modbus and Arduino Question
Post by: hummeruli on January 31, 2017, 04:43:10 PM
It is i2c with a 20x4 LCD and a 4x5 keyboard matrix also connected. Is it possible to wire it with Mach3 serial bus?
Title: Re: Modbus and Arduino Question
Post by: nokialkatel on January 31, 2017, 04:44:19 PM
SEND MY YOUR MAIL
Title: Re: Modbus and Arduino Question
Post by: nokialkatel on January 31, 2017, 04:47:58 PM
Code: [Select]
// Modbus:
#include <SimpleModbusSlave.h> //http://code.google.com/p/simple-modbus/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

const int HOLDING_REGS_SIZE = 22; // I use 22 registers, first 11 for sendidng info to Mach3
//and next 11 registers for receiving info from Mach3
unsigned int holdingRegs[HOLDING_REGS_SIZE]; // function 3 and 16 register array

int rcd[11]; //holds sending info
int mcd[11]; //holds received info

void setup(){

  pinMode(13, OUTPUT); //indicator LED

  modbus_configure(&Serial, 115200, SERIAL_8N2, 2, 13, HOLDING_REGS_SIZE, holdingRegs);
//2 is slave address, 13 is indicator led pin (useless)
 lcd.begin(20,4);         // initialize the lcd for 20 chars 4 lines, turn on backlight
 lcd.print("Hello, ARDUINO ");  
  lcd.setCursor ( 0, 1 );        // go to the next line
  lcd.print (" FORUM - fm   ");
  delay ( 1000 );
  
}

void loop(){

  //Reads buttons:
  if(digitalRead(4) == 1) bitSet(rcd[0],0);
  else bitClear(rcd[0],0);

  if(digitalRead(5) == 1) bitSet(rcd[0],1);
  else bitClear(rcd[0],1);

  if(digitalRead(6) == 1) bitSet(rcd[0],2);   //can be up to 15
  else bitClear(rcd[0],2);

  //Potens├é┬┤iometers:
  rcd[3] = analogRead(A0);
  rcd[4] = analogRead(A1);

  //Other variables:
  rcd[1] = 0x1111;
  rcd[2] = 0x2222;
  rcd[5] = 0x3333;
  rcd[6] = 32767;
  rcd[7] = 12345;
  rcd[8] = 54321;
  rcd[9] = 0;
  rcd[10] = 1;


  //Sending and receiving info:
  for (int i=0; i < 11; i++)
    holdingRegs[i] = rcd[i];

  modbus_update();

  for (int i=0; i < 11; i++)
    mcd[i] = holdingRegs[i+11];

  // Writes cordinates to LCD:
   float Xpos = mcd[0]+(mcd[1]/100.00);
   float Ypos = mcd[2]+(mcd[3]/100.00);
   float Zpos = mcd[4]+(mcd[5]/100.00);
  
   lcd.setCursor( 0,0 );
   lcd.print("                ");
   lcd.setCursor( 0, 0 );
   lcd.print("X");
   lcd.setCursor( 1, 0 );
   lcd.print(Xpos);
   lcd.setCursor( 0, 1 );
   lcd.print("                ");
   lcd.setCursor( 0, 1 );
   lcd.print("Y");
   lcd.setCursor( 1, 1 );
   lcd.print(Ypos);
   lcd.setCursor( 0, 2 );
   lcd.print("                ");
   lcd.setCursor( 0, 2 );
   lcd.print("Z");
   lcd.setCursor( 1, 2 );
   lcd.print(Zpos);
   lcd.setCursor(0, 3);
   lcd.print("                ");
   lcd.setCursor(0, 3);
   lcd.print("A");
  
  

}
  
Title: Re: Modbus and Arduino Question
Post by: hummeruli on January 31, 2017, 04:49:45 PM
I send you per IM
Title: Re: Modbus and Arduino Question
Post by: nokialkatel on January 31, 2017, 04:53:57 PM
POSTED..Remember to change your i2c address for lcd
Title: Re: Modbus and Arduino Question
Post by: hummeruli on February 05, 2017, 05:27:52 PM
Okay now it works. Correct display all four axis, positive and negative values shown on right position. I use a i2c-LCD and for this moment a Arduino Pro. The next step is a keyboard matrix.
Title: Re: Modbus and Arduino Question
Post by: nokialkatel on February 05, 2017, 05:41:18 PM
Keyboard matrix is best to one analog pin ..
Din lcd dro format ...is it 0.00 or 0.0000
Title: Re: Modbus and Arduino Question
Post by: hummeruli on February 05, 2017, 05:59:17 PM
It is correct 0.0000 and -0.0000
All on the right place.
I can send you the code and the right brain.
Title: Re: Modbus and Arduino Question
Post by: nokialkatel on February 05, 2017, 06:58:55 PM
Send code og brain to me..thanks