Page 1 of 2

[Complete script] Using matlab to control Makergear

Posted: Tue Jul 18, 2017 8:57 pm
by hanming
Here is the matlab code to operate makergear.

Use at your own risk!!!

https://github.com/hanmingz/matlabmakergear

=====================================original questions=============================================

Hi guys,

I'm trying to control Makergear M2 using matlab for material science specific applications. In order for the fiber in our paste to align, we need to print in a specific path. I wonder if we can use matlab to send Gcode commands to makergear via serial communication and control the printing, then we can integrate other parts of our system better.

Matlab can control an arduino, sending strings via serial communication. This matlab code on an ordinary arduino would make the arduino receive string G28.

Code: Select all

clear all
clc

arduino=serial('COM3','BaudRate',9600); % create serial communication object on port COM4
 
fopen(arduino); % initiate arduino communication

fprintf(arduino,'%s','G28');
pause(5);

fclose(arduino); % end communication with arduino
Ideally, this should send the G28 string to home the printer, which is essentially an arduino, but the printer just blows its electronics and extruder fan and does nothing. Is there anything wrong with my understanding that the firmware is a Gcode interpreter that reads gcode via serial com? Or must connection be established in another way and firmware changes need to be made?

Thanks in advance!
Hanming

Re: Using matlab to control Makergear

Posted: Tue Jul 18, 2017 10:25 pm
by insta
Try at 115200 baud.

Re: Using matlab to control Makergear

Posted: Wed Jul 19, 2017 2:57 pm
by hanming
Yeah, it should be 115200, although that doesn't solve the problem. I use the following code and can receive all the setup echo from the makergear. However, it still does not home when G28

Code: Select all

clear all
clc

arduino=serial('COM3','BaudRate',115200); % create serial communication object on port COM4
 
fopen(arduino); % initiate arduino communication
pause(10);
count = 0;
while arduino.BytesAvailable > 0
    echo = fscanf(arduino);
    disp(echo);
    count = count + 1;
end
fprintf(arduino,'%s','G28');
disp('home');
echo = fscanf(arduino);
disp(echo);
pause(10);

fclose(arduino); % end communication with arduino
Here's what is returned from the matlab command window:

start

echo:Marlin1.0.2

echo: Last Updated: Oct 6 2016 15:42:53 | Author: (MG|Josh, M2E - (SnNRd) v100 02/18/2016)

Compiled: Oct 6 2016

echo: Free Memory: 3259 PlannerBufferBytes: 1232

echo:Stored settings retrieved

echo:Steps per unit:

echo: M92 X88.88 Y88.88 Z1007.70 E471.50

echo:Maximum feedrates (mm/s):

echo: M203 X200.00 Y200.00 Z25.00 E25.00

echo:Maximum Acceleration (mm/s2):

echo: M201 X900 Y1000 Z30 E2000

echo:Acceleration: S=acceleration, T=retract acceleration

echo: M204 S2000.00 T3000.00

echo:Advanced variables: S=Min feedrate (mm/s), T=Min travel feedrate (mm/s), B=minimum segment time (ms), X=maximum XY jerk (mm/s), Z=maximum Z jerk (mm/s), E=maximum E jerk (mm/s)

echo: M205 S0.00 T0.00 B20000 X4.00 Z0.40 E1.00

echo:Home offset (mm):

echo: M206 X0.00 Y0.00 Z10.16

echo:PID settings:

echo: M301 P25.89 I1.94 D86.53

echo:SD card ok

home
Warning: Unsuccessful read: A timeout occurred
before the Terminator was reached..

The last home is when the home command is sent, but the printer doesn't do anything, and doesn't return anything afterwards.

Thank you very much for your help.

Re: Using matlab to control Makergear

Posted: Wed Jul 19, 2017 4:13 pm
by insta
Does fprintf send a trailing newline? GCode needs it.

Re: Using matlab to control Makergear

Posted: Wed Jul 19, 2017 9:13 pm
by hanming
Yes, I added a carriage return and line feed (CR/LF) command, but the fscanf function said "The input buffer was filled before the Terminator was reached.. " Although it is able to read the rest of the echo in another read, and supposedly send the G28 command, the printer did not do anything. I wonder what is the echo terminator like? Is it a new line every time or no terminator at all. And I am also suspecting problem with line number? If it is proper gcode, the current line number is also sent to the printer?

Re: Using matlab to control Makergear

Posted: Wed Jul 19, 2017 9:29 pm
by hanming
Oh, the echo terminator is just a line feed, and I can receive echo correctly. But it still does not do the home command...

Re: Using matlab to control Makergear

Posted: Wed Jul 19, 2017 10:21 pm
by Tim
I hate serial I/O buffer problems, always a plague.

I can think of only one suggestion at the moment, which is to try "fflush(arduino)" after the fprintf statement to make sure that the output buffer has been flushed. stderr, for example, is always set to automatically flush, but stdout is not. Don't know what the default setup is for a COM serial channel.

---Tim

Re: Using matlab to control Makergear

Posted: Thu Jul 20, 2017 9:19 pm
by hanming
No, the flush doesn't solve the problem, but maybe this section from MarlinSerial.cpp can give insight

Code: Select all

int MarlinSerial::peek(void)
{
  if (rx_buffer.head == rx_buffer.tail) {
    return -1;
  } else {
    return rx_buffer.buffer[rx_buffer.tail];
  }
}

int MarlinSerial::read(void)
{
  // if the head isn't ahead of the tail, we don't have any characters
  if (rx_buffer.head == rx_buffer.tail) {
    return -1;
  } else {
    unsigned char c = rx_buffer.buffer[rx_buffer.tail];
    rx_buffer.tail = (unsigned int)(rx_buffer.tail + 1) % RX_BUFFER_SIZE;
    return c;
  }
}
In my original matlab code, I was sending unsigned char. Maybe I should send ascii int? But I've tried every format in matlab, and none worked :x
No, I will not mess around with the firmware, but want to communicate with the firmware via serial with matlab.

Re: Using matlab to control Makergear

Posted: Fri Jul 21, 2017 12:06 am
by kyeakel
Why don't you connect the output of Simplify3d or whatever you use to control the M2 to a serial port and see what it sends? Then replicate that traffic in matlab. There are USB analyzers for windows.

Kipp

Re: Using matlab to control Makergear

Posted: Fri Jul 21, 2017 2:37 pm
by hanming
Yes, I looked at the serial bus and the home command is N457 G28*21.. I understand the line number and G28. But what is the *[number]? The number seem to change every time as well. Can anyone explain? Then the line in ended in .. and Line Feed.

Hanming