Skip to content

Instantly share code, notes, and snippets.

@EmerickH
Last active February 3, 2019 14:09
Show Gist options
  • Save EmerickH/ab89db3e47ae0d338a8938eda91f4a0a to your computer and use it in GitHub Desktop.
Save EmerickH/ab89db3e47ae0d338a8938eda91f4a0a to your computer and use it in GitHub Desktop.
[
{
"id": "74c755b6.fccf74",
"type": "tab",
"label": "test machine protocol",
"disabled": false,
"info": ""
},
{
"id": "e9ca7f96.d33a1",
"type": "serial out",
"z": "74c755b6.fccf74",
"name": "",
"serial": "72a677ea.82816",
"x": 570,
"y": 240,
"wires": []
},
{
"id": "e6449f19.2bfeb8",
"type": "serial in",
"z": "74c755b6.fccf74",
"name": "",
"serial": "72a677ea.82816",
"x": 100,
"y": 240,
"wires": [
[
"3db978ea.28532",
"b99871e5.0eda98"
]
]
},
{
"id": "3db978ea.28532",
"type": "function",
"z": "74c755b6.fccf74",
"name": "checkmsg",
"func": "//node.warn(msg);\n\nvar s = flow.get('s') || {\n state: 0,\n count: 0,\n CS: 0,\n curr_msg:{\n SOM:2,\n CI:0,\n len:0,\n bytes: new Buffer(256)\n },\n curr_msg_bytes: null,\n ascii: '',\n CI: 0,\n\n};\n\nflow.set('s', s);\n\nvar states = {\n PROTOCOL_STATE_IDLE:0,\n PROTOCOL_STATE_WAIT_CI:1,\n PROTOCOL_STATE_WAIT_LEN:2,\n PROTOCOL_STATE_WAIT_END:3,\n};\n\n\nvar PROTOCOL_SOM = 2;\n\nvar send_ack = function(CI) {\n var b = new Buffer(5);\n b[0] = 2;\n b[1] = CI;\n b[2] = 1;\n b[3] = 'A'.charCodeAt(0);\n b[4] = (0 - b[1] - b[2] - b[3]) & 0xff;\n \n node.send([null, {payload: b}]);\n}\n\n\nvar protocol_byte = function( byte ){\n //node.warn(byte + \" \"+ s.state);\n switch(s.state){\n case states.PROTOCOL_STATE_IDLE:\n if (byte == PROTOCOL_SOM){\n s.curr_msg.SOM = byte;\n s.state = states.PROTOCOL_STATE_WAIT_CI;\n s.CS = 0;\n } else {\n //////////////////////////////////////////////////////\n // if the byte was NOT SOM (02), then treat it as an \n // ascii protocol byte. BOTH protocol can co-exist\n //ascii_byte( byte );\n //////////////////////////////////////////////////////\n if (byte >= 0x20) {\n s.ascii += String.fromCharCode(byte);\n }\n if (byte == 10) {\n if (s.ascii){\n node.send([null, null, {payload:s.ascii}]);\n }\n s.ascii = '';\n }\n\n if (byte == 13) {\n if (s.ascii){\n node.send([null, null, {payload:s.ascii}]);\n }\n s.ascii = '';\n }\n \n }\n break;\n case states.PROTOCOL_STATE_WAIT_CI:\n s.curr_msg.CI = byte;\n s.CS += byte;\n s.state = states.PROTOCOL_STATE_WAIT_LEN;\n break;\n case states.PROTOCOL_STATE_WAIT_LEN:\n s.curr_msg.len = byte;\n s.curr_msg_bytes = new Buffer(s.curr_msg.len+4);\n s.curr_msg_bytes[0] = 2;\n s.curr_msg_bytes[1] = s.curr_msg.CI;\n s.curr_msg_bytes[2] = s.curr_msg.len;\n \n s.count = 0;\n s.CS += byte;\n s.state = states.PROTOCOL_STATE_WAIT_END;\n break;\n case states.PROTOCOL_STATE_WAIT_END:\n if (!s.curr_msg_bytes){\n s.state = states.PROTOCOL_STATE_IDLE;\n break;\n }\n s.curr_msg_bytes[s.count+3] = byte;\n s.curr_msg.bytes[s.count++] = byte;\n s.CS += byte;\n s.CS &= 0xff;\n if (s.count >= s.curr_msg.len+1){\n if (s.CS !== 0){\n node.warn('BAD CS');\n var m = [];\n m.push(s.curr_msg.SOM);\n m.push(s.curr_msg.CI);\n m.push(s.curr_msg.len);\n for (var x = 0; x < s.curr_msg.len; x++){\n m.push(s.curr_msg.bytes[x]);\n }\n m.push(s.CS);\n node.warn('bad cs'+ util.inspect(m)+\"xxx\"+ util.inspect(s.curr_msg_bytes));\n var msgs = flow.get(\"msgs\");\n //node.warn(msgs);\n \n if (msgs && msgs[s.curr_msg.bytes[1]]){\n node.warn(\"last msg\", msgs[s.curr_msg.bytes[1]]);\n }\n } else {\n switch (s.curr_msg.bytes[0]){\n case 'N'.charCodeAt(0):\n node.warn('NACK');\n break;\n case 'A'.charCodeAt(0):\n //node.warn('ACK');\n break;\n default:\n node.send({payload: s.curr_msg.bytes.slice(0, s.curr_msg.len)});\n send_ack(s.curr_msg.CI);\n break;\n }\n //node.warn(s.curr_msg);\n }\n //node.warn(\"set idle\");\n s.state = states.PROTOCOL_STATE_IDLE;\n }\n break;\n }\n};\n\nfor (var i = 0; i < msg.payload.length; i++){\n protocol_byte(msg.payload[i]);\n}\n\n//return msg;",
"outputs": 3,
"noerr": 0,
"x": 290,
"y": 240,
"wires": [
[
"2e663089.fb8f7"
],
[
"e9ca7f96.d33a1"
],
[
"f0fcca76.036828"
]
]
},
{
"id": "cb8231dd.9a57b",
"type": "debug",
"z": "74c755b6.fccf74",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 790,
"y": 420,
"wires": []
},
{
"id": "1ee491d.9f33f6e",
"type": "inject",
"z": "74c755b6.fccf74",
"name": "",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 130,
"y": 80,
"wires": [
[
"43e5b0cd.455e78"
]
]
},
{
"id": "43e5b0cd.455e78",
"type": "function",
"z": "74c755b6.fccf74",
"name": "stop debug and stop poweroff",
"func": "var out = new Buffer(4);\nout[0] = \"E\".charCodeAt(0);\nout[1] = \"\\n\".charCodeAt(0);\nout[2] = \"P\".charCodeAt(0);\nout[3] = \"\\n\".charCodeAt(0);\n\nmsg.payload = out;\n\nflow.set('s', null);\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 360,
"y": 80,
"wires": [
[
"e9ca7f96.d33a1"
]
]
},
{
"id": "b0c292f8.3109b",
"type": "comment",
"z": "74c755b6.fccf74",
"name": "newprotocol",
"info": "",
"x": 140,
"y": 40,
"wires": []
},
{
"id": "b64755ec.27b0f",
"type": "function",
"z": "74c755b6.fccf74",
"name": "request hall data",
"func": "\nvar len = 2;\nvar test = new Buffer(len);\n\ntest[0] = \"R\".charCodeAt(0);\ntest[1] = 4;\n\nmsg.payload = test;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 340,
"y": 460,
"wires": [
[
"e138c948.232f58"
]
]
},
{
"id": "f81b22b9.7599d8",
"type": "inject",
"z": "74c755b6.fccf74",
"name": "",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 130,
"y": 460,
"wires": [
[
"b64755ec.27b0f"
]
]
},
{
"id": "2e663089.fb8f7",
"type": "function",
"z": "74c755b6.fccf74",
"name": "parsecommands",
"func": "\nvar out = '';\nswitch (msg.payload[0]){\n case 't'.charCodeAt(0):\n out = {test:'test return len'+msg.payload.length};\n break;\n case 'R'.charCodeAt(0):\n switch(msg.payload[1]) {\n case 1:\n out = {\n sensordata:{\n Angle:[msg.payload.readInt16LE(2+0),\n msg.payload.readInt16LE(2+28+0)],\n Roll:[msg.payload.readInt16LE(2+7+0),\n msg.payload.readInt16LE(2+28+7+0)],\n raw:msg.payload.slice(2),\n \n }\n };\n break;\n case 4:\n out = {\n hallmm:{\n L:msg.payload.readInt32LE(2),\n R:msg.payload.readInt32LE(6),\n LO:msg.payload.readInt32LE(10),\n RO:msg.payload.readInt32LE(14),\n }\n };\n // reset the offsets\n msg.payload[0] = 'W'.charCodeAt(0);\n node.send([null, msg]);\n break;\n }\n break;\n \n}\n\nif (out) {\n msg.payload = out;\n return msg;\n}",
"outputs": 2,
"noerr": 0,
"x": 720,
"y": 200,
"wires": [
[
"ac4b1240.59435"
],
[
"e138c948.232f58"
]
]
},
{
"id": "e138c948.232f58",
"type": "function",
"z": "74c755b6.fccf74",
"name": "sendmsg",
"func": "\n\n\nvar len = msg.payload.length;\nvar test = new Buffer(len+4);\n\nvar CI = flow.get(\"CI\") || 0;\nCI = ((CI+1)%256);\nflow.set('CI', CI);\n\ntest[0] = 0x02;\ntest[1] = CI;\ntest[2] = len;\n\n\n// one less, because cmd is included\nvar cs = 0;\n\nvar i;\nfor (i = 1; i < test.length-1; i++){\n if (i > 2) {\n test[i] = msg.payload[i-3];\n }\n cs -= test[i];\n}\ntest[i] = cs & 0xff;\n\nvar msgs = flow.get(\"msgs\") || {};\nmsgs[CI] = test;\nflow.set(\"msgs\", msgs);\n\nmsg.payload = test;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 390,
"y": 360,
"wires": [
[
"e9ca7f96.d33a1"
]
]
},
{
"id": "f0fcca76.036828",
"type": "debug",
"z": "74c755b6.fccf74",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"x": 160,
"y": 380,
"wires": []
},
{
"id": "ac4b1240.59435",
"type": "function",
"z": "74c755b6.fccf74",
"name": "combinetovars",
"func": "\nvar vars = flow.get('vars') || {};\n\nObject.assign(vars, msg.payload);\n\nflow.set('vars', vars);\n\nreturn { payload: vars };",
"outputs": 1,
"noerr": 0,
"x": 710,
"y": 340,
"wires": [
[
"b9e0facd.be13d"
]
]
},
{
"id": "8623be89.2b55f8",
"type": "template",
"z": "74c755b6.fccf74",
"name": "",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "This is the payload: {{payload}} !",
"output": "str",
"x": 670,
"y": 520,
"wires": [
[]
]
},
{
"id": "b9e0facd.be13d",
"type": "json",
"z": "74c755b6.fccf74",
"name": "",
"property": "payload",
"action": "",
"pretty": false,
"x": 640,
"y": 420,
"wires": [
[
"cb8231dd.9a57b"
]
]
},
{
"id": "767a7760.23b9f",
"type": "inject",
"z": "74c755b6.fccf74",
"name": "",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 130,
"y": 500,
"wires": [
[
"645a1f43.ec271"
]
]
},
{
"id": "645a1f43.ec271",
"type": "function",
"z": "74c755b6.fccf74",
"name": "request sensor data",
"func": "\nvar len = 2;\nvar test = new Buffer(len);\n\ntest[0] = \"R\".charCodeAt(0);\ntest[1] = 1;\n\nmsg.payload = test;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 350,
"y": 500,
"wires": [
[
"e138c948.232f58"
]
]
},
{
"id": "7bc3b365.f59c7c",
"type": "function",
"z": "74c755b6.fccf74",
"name": "make test buffer",
"func": "\nvar len = ((Math.random()*30)>>0)+1;\nvar test = new Buffer(len);\n\ntest[0] = \"T\".charCodeAt(0);\n\nvar start = (Math.random()*256)>>0;\n\nfor (i = 1; i < len; i++){\n test[i] = (start+i) & 0xff;\n}\n\nmsg.payload = test;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 330,
"y": 540,
"wires": [
[
"e138c948.232f58"
]
]
},
{
"id": "780857fa.68ea98",
"type": "inject",
"z": "74c755b6.fccf74",
"name": "",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 130,
"y": 540,
"wires": [
[
"7bc3b365.f59c7c"
]
]
},
{
"id": "4b0b1e4d.f0b788",
"type": "inject",
"z": "74c755b6.fccf74",
"name": "",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 130,
"y": 600,
"wires": [
[
"d278b3a9.ae6f28"
]
]
},
{
"id": "d278b3a9.ae6f28",
"type": "function",
"z": "74c755b6.fccf74",
"name": "clear vars",
"func": "flow.set('vars', {});\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 320,
"y": 600,
"wires": [
[]
]
},
{
"id": "accd827.d38b1",
"type": "function",
"z": "74c755b6.fccf74",
"name": "move",
"func": "\nvar len = 4;\nvar test = new Buffer(len);\n\ntest[0] = \"W\".charCodeAt(0);\ntest[1] = 5; // Pos control\ntest[2] = 1; // ~6cm\ntest[3] = 1; // ~6cm\n\nmsg.payload = test;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 290,
"y": 720,
"wires": [
[
"e138c948.232f58"
]
]
},
{
"id": "dfb41223.398d98",
"type": "inject",
"z": "74c755b6.fccf74",
"name": "",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 130,
"y": 720,
"wires": [
[
"accd827.d38b1"
]
]
},
{
"id": "85624085.b1434",
"type": "inject",
"z": "74c755b6.fccf74",
"name": "",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 130,
"y": 680,
"wires": [
[
"6edc54f3.fa849c"
]
]
},
{
"id": "6edc54f3.fa849c",
"type": "function",
"z": "74c755b6.fccf74",
"name": "set pos control",
"func": "\nvar len = 1;\nvar test = new Buffer(len);\n\ntest[0] = \"P\".charCodeAt(0);\n//test[1] = \"p\".charCodeAt(0);\n\nmsg.payload = test;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 320,
"y": 680,
"wires": [
[
"e138c948.232f58"
]
]
},
{
"id": "b99871e5.0eda98",
"type": "debug",
"z": "74c755b6.fccf74",
"name": "",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"x": 270,
"y": 180,
"wires": []
},
{
"id": "1e9ec6ff.d1e901",
"type": "inject",
"z": "74c755b6.fccf74",
"name": "",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 130,
"y": 860,
"wires": [
[
"fefd8463.446ee8"
]
]
},
{
"id": "fefd8463.446ee8",
"type": "function",
"z": "74c755b6.fccf74",
"name": "move standart",
"func": "\nvar len = 4;\nvar test = new Buffer(len);\n\ntest[0] = \"W\".charCodeAt(0);\ntest[1] = 3; // Pos control\ntest[2] = 10; // ~6cm\ntest[3] = 10;\n\nmsg.payload = test;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 330,
"y": 860,
"wires": [
[
"e138c948.232f58"
]
]
},
{
"id": "b6b7c058.f7bc4",
"type": "function",
"z": "74c755b6.fccf74",
"name": "Get speed",
"func": "\nvar len = 2;\nvar test = new Buffer(len);\n\ntest[0] = \"R\".charCodeAt(0);\ntest[1] = 3; // Pos control\n\nmsg.payload = test;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 310,
"y": 940,
"wires": [
[
"e138c948.232f58"
]
]
},
{
"id": "2e523074.7de69",
"type": "inject",
"z": "74c755b6.fccf74",
"name": "",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 130,
"y": 940,
"wires": [
[
"b6b7c058.f7bc4"
]
]
},
{
"id": "f5b9ec66.c56168",
"type": "function",
"z": "74c755b6.fccf74",
"name": "move v2",
"func": "\nvar len = 10;\nvar test = new Buffer(len);\n\ntest[0] = \"W\".charCodeAt(0);\ntest[1] = 5; // Pos control\ntest[2] = 1; // ~6cm\ntest[3] = 0; // why?\ntest[4] = 0; // why?\ntest[5] = 0; // why?\ntest[6] = 1; // ~6cm\ntest[7] = 0; // why?\ntest[8] = 0; // why?\ntest[9] = 0; // why?\n\nmsg.payload = test;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 300,
"y": 760,
"wires": [
[
"e138c948.232f58"
]
]
},
{
"id": "8b6c83.9967cb8",
"type": "inject",
"z": "74c755b6.fccf74",
"name": "",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 130,
"y": 760,
"wires": [
[
"f5b9ec66.c56168"
]
]
},
{
"id": "84727e3d.87d84",
"type": "function",
"z": "74c755b6.fccf74",
"name": "move standart v2",
"func": "\nvar len = 10;\nvar test = new Buffer(len);\n\ntest[0] = \"W\".charCodeAt(0);\ntest[1] = 3; // Speed\ntest[3] = 0; // why?\ntest[4] = 0; // why?\ntest[5] = 0; // why?\ntest[6] = 5;\ntest[7] = 0; // why?\ntest[8] = 0; // why?\ntest[9] = 0; // why?\n\nmsg.payload = test;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 330,
"y": 900,
"wires": [
[]
]
},
{
"id": "40cb3dbf.ce2fd4",
"type": "inject",
"z": "74c755b6.fccf74",
"name": "",
"topic": "",
"payload": "",
"payloadType": "date",
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"x": 130,
"y": 900,
"wires": [
[
"84727e3d.87d84"
]
]
},
{
"id": "b8c634da.95c128",
"type": "comment",
"z": "74c755b6.fccf74",
"name": "Working for 1 motor",
"info": "The other one is not moving",
"x": 560,
"y": 860,
"wires": []
},
{
"id": "b711b90e.d101c",
"type": "comment",
"z": "74c755b6.fccf74",
"name": "Working for 1 motor (see comment)",
"info": "The other one is not moving\nNeed to set speed before",
"x": 570,
"y": 720,
"wires": []
},
{
"id": "7bb8878e.24251",
"type": "comment",
"z": "74c755b6.fccf74",
"name": "Not working",
"info": "",
"x": 490,
"y": 760,
"wires": []
},
{
"id": "93de2805.c274d",
"type": "comment",
"z": "74c755b6.fccf74",
"name": "Not working",
"info": "",
"x": 530,
"y": 900,
"wires": []
},
{
"id": "2397dbb3.229de4",
"type": "comment",
"z": "74c755b6.fccf74",
"name": "Working",
"info": "",
"x": 480,
"y": 680,
"wires": []
},
{
"id": "72a677ea.82816",
"type": "serial-port",
"z": "",
"serialport": "/dev/ttyS0",
"serialbaud": "9600",
"databits": "8",
"parity": "none",
"stopbits": "1",
"newline": "50",
"bin": "bin",
"out": "interbyte",
"addchar": false,
"responsetimeout": ""
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment