Help requested with a C-Beam Robot recording position and measurement data in lab experiments

For the XYZ location, did you try just subscribing to this pubsub?

It’s in the pubsub for the TinyG widget

Yes thats exactly what I did. Got an error in my macro, I tried to do too much at one time.
Is there a way to comment a section of a macro like this?
*/
some stuff
More stuff
*/

Yes, but it’s /* on the first line and */ on last line of multiline comment. Just search the internet for “multiline javascript comment”

Aha, now i get it, the macros are JavaScript. I’m new to JavaScript but am learning thanks to your help.
I hope to have some code to show today that does display the position. Modified one of the routines that compaired the position to 0,0 and instead had it output the contents of the x and y position. Now going to include this in the onExecute and Pause routine.

This is where I’m at now. I can read the position two ways.

onRecvPosition: function(pos) {
// we get a nice object of XYZ values

if ( pos.x != null | pos.y != null )
{
macro.status("Position X = " + (pos.x != null ? pos.x : 0)+ " , Y = " + (pos.y != null ? pos.y : 0));
}

This function gets multiple values and the first and last values are null which I suppress.

Position X = 0 , Y = 0.01
Position X = 0 , Y = 0.04
Position X = 0 , Y = 0.05

or I can parse the command in the pause that has the value of the position that was requested

macro.status(“Got onExecuted:\n” + JSON.stringify(data));

{“line”:9,“gcode”:“N9 G1 Y0.05 (chilipeppr_pause) ;Go to x=0,y=.05,z=0”}

I’m thinking that parsing the paused command is the best way to get the position.
I just need to write the code to pick out the position.
The macro that fires the laser on the arduino has a good example that I will use.

I’ll post the macro after I finish the parsing routine

Below is the log of my macro.

This macro was run before. Cleaning up…
Uninitting chilipeppr_pause macro.
Subscribing to chilipeppr_pause pubsub event
Ran macro.
Got onExecuted:
{“line”:8,“gcode”:“N8 G1 X0 Y0 Z0 (chilipeppr_pause) ;msg Y Forward Direction start at 0,0,0”}
Measurement Number 1
Moving onto next Measurement
Got onExecuted:
{“line”:9,“gcode”:“N9 G1 Y0.05 (chilipeppr_pause) ;Go to x=0,y=.05,z=0”}
Measurement Number 2
Position X = 0 , Y = 0.01
Position X = 0 , Y = 0.04
Position X = 0 , Y = 0.05
Moving onto next Measurement
Got onExecuted:
{“line”:11,“gcode”:“N11 G1 Y0.10 (chilipeppr_pause)”}
Measurement Number 3
Position X = 0 , Y = 0.059
Position X = 0 , Y = 0.089
Position X = 0 , Y = 0.1
Moving onto next Measurement
Got onExecuted:
{“line”:13,“gcode”:“N13 G1 Y0.15 (chilipeppr_pause)”}
Measurement Number 4
Position X = 0 , Y = 0.109
Position X = 0 , Y = 0.139
Position X = 0 , Y = 0.15
Moving onto next Measurement
Got onExecuted:
{“line”:15,“gcode”:“N15 G1 Y0.20 (chilipeppr_pause)”}
Measurement Number 5
Position X = 0 , Y = 0.16
Position X = 0 , Y = 0.19
Position X = 0 , Y = 0.2
Moving onto next Measurement

That’s looking awesome!

Now trying to understand how to receive data on the serial port other than the default.
In the Widget / Serial Port JSON Server I have selected a non default serial port.
I just want 9600 Bits Per Second, 8 Data Bits, 1 Stop Bit, No Parity, No Flow Control.
I selected default and 9600 in the widget. Is the correct choice for my setup?

NonDefaultPortSPJS

I chose the “Send Serial to CNC and Arduino Simultaneously” Macro.
What I want to do is just capture the serial port output to the log but I must be missing something.
My device is sending continuous strings to the serial port and I have subscribed to ALL serial ports.
I need to modify the following code from the macro to log the ascii data being received on COM7.
How is the data from the SPJS formatted? I can see the code looks for lines that begin with “{” I’m assuming what we get is { “key” : " value" }
Does the code below just log the data being received?
I’m sending data on the serial port but nothing shows up in the log.
Thanks for helping me figure this out

msg: “”, // stores data received
onRecvData: function(data) {
// data comes in as it arrives, so not as lines
// wait until we see a newline

	// see if json
	if (data.match(/^{/)) {
		var json = $.parseJSON(data);
		if ('P' in json && json.P == this.portArduino) {
			console.log("got onRecvData for this port. json:", json);
			this.msg += json.D;
			if (this.msg.match(/\n/)) {
				macro.status(this.msg);
				this.msg = ""; // clear buffer
			}
		}
	}
},

I also tried setting the port with the device that sends the data as the default device.
Nothing was displayed in the console of the TingG workspace.
There must be something I’m not doing correctly

The default serial port device needs to be TinyG or the workspace won’t work correctly because the Gcode would get sent to your other USB device.

Just print the data as it comes in, so like the first line of your onRecvData method, just so you can debug better. I don’t see any other issues.

If you want to know how SPJS is formatted, just show the console for the SPJS widget.

Showing that console will also let you see what you should be getting in your macro.

SPJS
Thanks for the tip for the SPJS Console. I can watch all communications on TinyG port COM3
But …
When I issue the list command the checkbox for com1 is cleared and is open is set false.
Obviously since the port is not open I cant see anything on the SPJS console.

I tried open COM1 9600 but nothing changed. How do I open the COM1 Port?

{“Version” : “1.95”}
{“Commands” : [“list”, “open [portName] [baud] [bufferAlgorithm (optional)]”, “send [portName] [cmd]”, “sendnobuf [portName] [cmd]”, “sendjson {P:portName, Data:[{D:cmdStr, Id:idStr}]}”, “close [portName]”, “bufferalgorithms”, “baudrates”, “restart”, “exit”, “broadcast [anythingToRegurgitate]”, “hostname”, “version”, “program [portName] [core:architecture:name] [path/to/binOrHexFile]”, “programfromurl [portName] [core:architecture:name] [urlToBinOrHexFile]”, “execruntime”, “exec [command] [arg1] [arg2] […]”]}
{“Hostname” : “ARkival-Robot”}

“SerialPorts”: [
{
“Name”: “COM1”,
“Friendly”: “Communications Port (COM1)”,
“SerialNumber”: “ACPI\PNP0501\1”,
“DeviceClass”: “”,
“IsOpen”: false,
“IsPrimary”: false,
“RelatedNames”: null,
“Baud”: 0,
“BufferAlgorithm”: “”,
“AvailableBufferAlgorithms”: [
“default”,
“timed”,
“nodemcu”,
“tinyg”,
“tinyg_old”,
“tinyg_linemode”,
“tinyg_tidmode”,
“tinygg2”,
“grbl”,
“marlin”
],
“Ver”: 1.95,
“UsbVid”: “”,
“UsbPid”: “”,
“FeedRateOverride”: 0
},
{
“Name”: “COM3”,
“Friendly”: “USB Serial Port (COM3)”,
“SerialNumber”: “FTDIBUS\VID_0403+PID_6015+DN02HWASA\0000”,
“DeviceClass”: “”,
“IsOpen”: true,
“IsPrimary”: true,
“RelatedNames”: null,
“Baud”: 115200,
“BufferAlgorithm”: “tinyg”,
“AvailableBufferAlgorithms”: [
“default”,
“timed”,
“nodemcu”,
“tinyg”,
“tinyg_old”,
“tinyg_linemode”,
“tinyg_tidmode”,
“tinygg2”,
“grbl”,
“marlin”
],
“Ver”: 1.95,
“UsbVid”: “0403”,
“UsbPid”: “6015”,
“FeedRateOverride”: 0
}
]
}

I thought your Prolific device was on COM7? Not COM1

It was originally but I had to return the borrowed usb to serial adapter so I’m now on the COM1.

Must be something wrong with COM1 on PC. I got COM7 working on the SPJS Console

Making progress now using COM7, something was wrong with my hardware COM1

Here is what I got so far:

{“P”:“COM7”,“D”:“f = 118180 Hz “}
{“P”:“COM7”,“D”:”\n\rx = 138.7 uT”}
{“P”:“COM7”,“D”:"\n\r"}
{“P”:“COM7”,“D”:“f = 118180”}
{“P”:“COM7”,“D”:" Hz \n\rx = 141"}
{“P”:“COM7”,“D”:".8 uT\n\r"}
{“P”:“COM7”,“D”:“f = 114”}
{“P”:“COM7”,“D”:"172 Hz \n\rx = "}
{“P”:“COM7”,“D”:“141.8 uT\n\r”}

And this is what I’d like to end up with after I combine the position with the measurements.

X = 0, Y = 0.5, Z=0, f = 118180 Hz, x = 138.7 uT
X = 0, Y = 1.0, Z=0, f = 118180 Hz, x = 141.8 uT
X = 0, Y = 1.5, Z=0, f = 114172 Hz, x = 141.8 uT

Just need to parse the serial port data some.

Why don’t you just output JSON formatted data from your device and then it’s super easy to parse in Javascript? Or, if you can’t change the format of the output data on your device, time for you to write lots of regular expressions in Javascript.

1 Like

I can’t change the output from the instruments so I’ll have to parse them into something I can use.

From the SPJS Console

send COM7 meas x
send COM7 meas f
{“Cmd”:“Queued”,“QCnt”:1,“Ids”:[""],“D”:[“meas f\r”],“Port”:“COM7”}
{“Cmd”:“Write”,“QCnt”:0,“Id”:"",“P”:“COM7”}
{“Cmd”:“Queued”,“QCnt”:1,“Ids”:[""],“D”:[“meas x\r”],“Port”:“COM7”}
{“Cmd”:“CompleteFake”,“Id”:"",“P”:“COM7”}
{“Cmd”:“Write”,“QCnt”:0,“Id”:"",“P”:“COM7”}
{“Cmd”:“CompleteFake”,“Id”:"",“P”:“COM7”}
{“P”:“COM7”,“D”:"\r\u003e m"}
{“P”:“COM7”,“D”:"\r\u003e me"}
{“P”:“COM7”,“D”:"\r\u003e mea"}
{“P”:“COM7”,“D”:"\r\u003e meas"}
{“P”:“COM7”,“D”:"\r\u003e me"}
{“P”:“COM7”,“D”:“as “}
{“P”:“COM7”,“D”:”\r\u003e meas f”}
{“P”:“COM7”,“D”:"\r\u003e “}
{“P”:“COM7”,“D”:“meas f\r\n\rf = 14”}
{“P”:“COM7”,“D”:“691 Hz \n\r\n\r\u003e “}
{“P”:“COM7”,“D”:”\r\u003e”}
{“P”:“COM7”,“D”:” m"}
{“P”:“COM7”,“D”:"\r\u003e me"}
{“P”:“COM7”,“D”:"\r\u003e mea"}
send COM7 meas x
send COM7 meas f
{“Cmd”:“Queued”,“QCnt”:1,“Ids”:[""],“D”:[“meas f\r”],“Port”:“COM7”}
{“Cmd”:“Write”,“QCnt”:0,“Id”:"",“P”:“COM7”}
{“Cmd”:“Queued”,“QCnt”:1,“Ids”:[""],“D”:[“meas x\r”],“Port”:“COM7”}
{“Cmd”:“CompleteFake”,“Id”:"",“P”:“COM7”}
{“Cmd”:“Write”,“QCnt”:0,“Id”:"",“P”:“COM7”}
{“Cmd”:“CompleteFake”,“Id”:"",“P”:“COM7”}
{“P”:“COM7”,“D”:"\r\u003e mea"}
{“P”:“COM7”,“D”:“s”}
{“P”:“COM7”,“D”:"\r\u003e meas “}
{“P”:“COM7”,“D”:”\r\u003e meas x"}
{“P”:“COM7”,“D”:"\r\u003e meas x"}
{“P”:“COM7”,“D”:"\r\n\rx = 47.2 u"}
{“P”:“COM7”,“D”:"T\n\r\n\r\u003e "}

The commands I send are :

meas x
meas f

I don’t know what “CompleteFake” means?

I’m confused as to how to access the strings from the SPJS to apply the regular expression.

Where can I get some info on using the “match”? I understand regular expressions.

I only need the “x = 47.2 uT” and the “f = 14691 Hz”.

so my two regexp are “x = +\d+.*\d+ uT” and "f = +\d+ Hz"

chilipeppr.subscribe("/com-chilipeppr-widget-serialport/ws/recv", this, this.onRecvData);
msg: "", // stores data received
	onRecvData: function(data) {
		// data comes in as it arrives, so not as lines
		// wait until we see the pattern we want to match
		// see if json
		if (data.match(/^{/)) {
			var json = $.parseJSON(data);
			if ('P' in json && json.P == this.serialPort) {
				console.log("got onRecvData for this port. json:", json);
				this.msg += json.D;
				if (this.msg.match((x = +\d+\.*\d+ uT)) {
					macro.status(this.msg);
					this.msg = ""; // clear buffer
				}
			}
		}
	},

Am I looking at this correctly?
Thanks

If you included line numbers in your Gcode, then the ID’s come back as Complete or CompleteFake. Line numbers are specified with the N value in Gcode. CompleteFake means we got back a receipt from TinyG for say line 5 and then next as line 10, so CompleteFake fills in the gaps with Line 6, 7, 8, 9 so you get reliable events per line. I doubt you need to worry about the Complete or CompleteFake JSON that comes back.

With respect to your regular expressions, that’s just general Javascript programming techniques which I would say just search Google for how to do Javascript regular expressions. I tend to prefer using groupings in regex’s so I can refer to them as RegExp.$1 and RegExpt.$2, etc in my code after the match is found.

1 Like