WEEK 12 - Interface and Application Programming

TODOs for this week

☑ write an application that interfaces a user with an input &/or output device that you made


Concept development


I needed to interface with my 3 stepper motors for my CNC machine using similar concept as what the UGS is doing using the out put from this weeks assignment.

.

And for this to happen I need to understand How GRBL does it as that is how I want to finally controll my machine. I downloaded the grbl library from GitHub by gnea as a .ZIP folder, extracted it and added it to Arduino as a library. Then I tried open grblUpload file in Arduino and when I clicked on the Upload button, I received the error message. First of all I checked which Board type I chose, then Serial Port number...Then I spent some time to understand where is the problem was and found that when I extract the folder it has the "grbl" folder which has the subfolder "grbl" and the reason for the error message is that Arduino can't find this library path. What I need - just export files from subfolder to master folder. The Grlb was uploaded succesfully. I then installed Universal Gcode Platform software that sends out G - CODE which is the language in which we tell computerized machines (CNC) to do something. It's basically a file that contains X, Y and Z coordinates. After downloading, I tried to connect to the Arduino board that I just installed GRBL, but it doesn't work. eventhough the port used are the correct one - ofcaurse it didn't connect as the port was busy as at the same time I had opened the Arduino Serial Monitor. make sure you have only one App connected to the same Board

After that I started to understand what g_code commands are being sent to the arduino Grbl from UGS. The pocket guide I attached below was really helpful to undderstand what codes needs to be sent via serial to the Mother board. After getting this commands and understanding how GRBL interprates this, It was a huge help to the programming part that will come soon after.



Reference :
Slider example file
Arduino and Processing
SingleServoExample

I looked on Procesing reference library on declaring serial port and this is what I have learned: I have to declare my serial port according to what this println(Serial.list()); printed a list all the available serial ports:. I learned that my port is /dev/tty.usbmodemFA141

I wanted to use Processing to send a character that I type on my keyboard to the serial monitor and have the stepper motors respond accordingly, like by turning on/off or speeding up/slowing down when certain characters are sent.

one of my lobby on Processing discussion forums led me to find a Code in Processing below that would send certain characters to the Serial Monitor when a certain key was pressed. This version sent letter characters to the Serial Monitor, but I changed it just so that “a” would send over a 1. I planned on editing it later on so that I could press certain buttons within the Processing popup window that would turn on the stepper, turn off the stepper, and change the speeed of the stepper.

Here’s the altered code:

import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;        // Data received from the serial port

void setup()
{
  size(500, 500);
 String portName = "COM19";
 myPort = new Serial(this, "COM19", 4800);
}

void draw() {
  // keep draw() here to continue looping while waiting for keys
}

void keyPressed() {
  if (key == 'w') {
    myPort.write('w');
  }
  else if (key == 'a') {
    myPort.write('1');
    rect (250,250,50,50);
  }
  else if (key == 'd') {
    myPort.write('d');
  }
  else if (key == 's') {
    myPort.write('s');
  }
  else{
    myPort.write('x');
  }
}

Though there is code indicating what to do when w, d, s, and x is typed on the keyboard, I mainly ignored those and just paid attention to the “a” because I wanted to get one basic thing working before I added more layers. For the sake of spiral development.

Coding


import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import processing.serial.*;

Serial port = null;

String portname = "/dev/tty.usbmodem1412401"; // on my Mac OS X


boolean streaming = false;
float speed = 3200.0;
String[] gcode;
int i = 0;


void openSerialPort()
{
  if (portname == null) return;
  if (port != null) port.stop();

  port = new Serial(this, portname, 115200);

  port.bufferUntil('\n');
}


void setup()
{
  size(500, 300);
  openSerialPort();
}

void draw()
{
  background(10,255,0);
  fill(0);
  int y = 24, dy = 12;
  text("A! Cutter GRBL Controller", 22, y); y += dy;

  text("1: set speed to 3200 mms (1 mil) per jog", 12, y); y += dy;
  text("2: set speed to 3000 mms (10 mil) per jog", 12, y); y += dy;
  text("3: set speed to 2500 mms (100 mil) per jog", 12, y); y += dy;
  text("arrow keys: jog in x-y plane", 12, y); y += dy;
  text("page up & page down: jog in z axis", 12, y); y += dy;
  text("$: display grbl settings", 12, y); y+= dy;
  text("h: go home", 12, y); y += dy;
  text("0: zero machine (set home to the current location)", 12, y); y += dy;
  text("g: stream a g-code file", 12, y); y += dy;
  text("x: stop streaming g-code (this is NOT immediate)", 12, y); y += dy;
  y = height - dy;
  text("current jog speed: " + speed + " mms per step", 12, y); y -= dy;
  text("current serial port: " + portname, 12, y); y -= dy;
}

void keyPressed()
{
  if (key == '1') speed = 3200;
  if (key == '2') speed = 3000;
  if (key == '3') speed = 2500;

  if (!streaming) {
    if (keyCode == LEFT) port.write("$J=G21G91X3200F900"  + " Y0.000 Z0.000\n");

    if (keyCode == RIGHT)  port.write("$J=G21G91X-3200F900"  + " Y0.000 Z0.000\n");

    if (keyCode == UP) port.write("G91\nG20\nG00 X0.000 Y" + speed + " Z0.000\n");
    if (keyCode == DOWN) port.write("G91\nG20\nG00 X0.000 Y-" + speed + " Z0.000\n");
    if (keyCode == KeyEvent.VK_PAGE_UP) port.write("G91\nG20\nG00 X0.000 Y0.000 Z" + speed + "\n");
    if (keyCode == KeyEvent.VK_PAGE_DOWN) port.write("G91\nG20\nG00 X0.000 Y0.000 Z-" + speed + "\n");
    if (key == 'h') port.write("G90\nG20\nG00 X0.000 Y0.000 Z0.000\n");
    if (key == 'v') port.write("$0=75\n$1=74\n$2=75\n");
    //if (key == 'v') port.write("$0=100\n$1=74\n$2=75\n");
    if (key == 's') port.write("$3=10\n");
    if (key == 'e') port.write("$16=1\n");
    if (key == 'd') port.write("$16=0\n");
    if (key == '0') openSerialPort();
    if (key == '$') port.write("$$\n");
  }


  if (!streaming && key == 'g') {
    gcode = null; i = 0;
    File file = null;
    println("Loading file...");
    selectInput("Select a file to process:", "fileSelected", file);
  }

  if (key == 'x') streaming = false;
}

void fileSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    println("User selected " + selection.getAbsolutePath());
    gcode = loadStrings(selection.getAbsolutePath());
    if (gcode == null) return;
    streaming = true;
    stream();
  }
}

void stream()
{
  if (!streaming) return;

  while (true) {
    if (i == gcode.length) {
      streaming = false;
      return;
    }

    if (gcode[i].trim().length() == 0) i++;
    else break;
  }

  println(gcode[i]);
  port.write(gcode[i] + '\n');
  i++;
}

void serialEvent(Serial p)
{
  String s = p.readStringUntil('\n');
  println(s.trim());

  if (s.trim().startsWith("ok")) stream();
  if (s.trim().startsWith("error")) stream();
}

The functioning motors demonstration

This is the video showing how the motor responds with the code input from the Code and also able to change Direction.

Future improvements

FIRMATA + ControlLP5

For this week reesearch I come acrossProcessing a library, written by Andreas Schlegel, ControlLP5. It is a processing and java library for creating simple control GUIs, that is easy to use but I wasn't able to use that as the library is writen of an older version of Processing 2.0 version but clearly it would have added a huge usablity to this project from the UI point of view.

In the Next itereation it will be great to work more on interactive buttons in the interface itself rather than key presses. It will also be very handy to be able to have a slide controller both for the speed/ Jog and the fed rate inputs .



Group assignment

The aim of this week's group assignment was to explore different GUI tools. We have tried to compared Processing, Python, and Java script which were used by our group members to complete there individual assignments. We have summarised the main differences in terms of device and user interface, web framework...please read more at my collegues website Group assignment

Download

Arduino Code - Arduino Code-
Processing Code- Processing Code