Hi everybody!
I am trying to capture a digital signal (hall-signal) with an maximum Freqency of 160Hz (tik every 6ms) over a gpio of an android C4 . It is a speed signal from a classic tachometer of a car.
I am gathering the basis data with an exec node running:
gpiomon -b -r -l gpiochip0 70
delivering that
|event:|RISING|EDGE|offset:|70|timestamp:|[|4874.916729293]|
|event:|RISING|EDGE|offset:|70|timestamp:|[|4874.922985595]|
|event:|RISING|EDGE|offset:|70|timestamp:|[|4874.929242813]|
|event:|RISING|EDGE|offset:|70|timestamp:|[|4874.935498823]|
|event:|RISING|EDGE|offset:|70|timestamp:|[|4874.941755500]|
|event:|RISING|EDGE|offset:|70|timestamp:|[|4874.948014135]|
|event:|RISING|EDGE|offset:|70|timestamp:|[|4874.954268519]|
|event:|RISING|EDGE|offset:|70|timestamp:|[|4874.960525237]|
|event:|RISING|EDGE|offset:|70|timestamp:|[|4874.966789456]|
|event:|RISING|EDGE|offset:|70|timestamp:|[|4874.973042299]|
|event:|RISING|EDGE|offset:|70|timestamp:|[|4874.979301184]|
|event:|RISING|EDGE|offset:|70|timestamp:|[|4874.985550360]|
|event:|RISING|EDGE|offset:|70|timestamp:|[|4874.991809037]|
Following out logging tests (writing out to a file with a file node, there is nothing of data lost even up to 300Hz.
However, although I know that I should not deal that as string but a I just cannot avoid a .substring statement to get that timestamp isolated here! I consider that as the first big performance trap. If so how to avoid that?
My first asset was then to declare a counter in a function node which counts the ticks / timespan.
Turned out that the counter could not follow the ticks fast enough in order to calculate speed higher than 50km/h (~40Hz, tick every 25 ms).
In terms of qualitiy of the signal: mean while I run a frequency generator to fake the tachometer signal in door - both the tachometer of the car itself and the signal generator result in the same outcome @ nodered:
My second asset (flow attached) was to use an array, fill it up with the payloads and do the same like above but with array.length, so avoid counting.
But also at ~40Hz I cannot write fast enough in the array obviouly, which led me finally to the same crappy result.
Please see my latest flow (below).
[{"id":"4788c8134ed09947","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"402a5383db5006b2","type":"exec","z":"4788c8134ed09947","command":"gpiomon -r -b gpiochip0 70","addpay":"","append":"","useSpawn":"true","timer":"","winHide":false,"oldrc":false,"name":"","x":400,"y":520,"wires":[["f30db6a458de8e99","4acdc22d90eae667"],["d3158db9d08fa6de"],["5b53c3878b668d37"]]},{"id":"ce6fee17b2401b8f","type":"inject","z":"4788c8134ed09947","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"timestamp","v":"","vt":"date"},{"p":"arr","v":"","vt":"num"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"rising","payload":"0","payloadType":"str","x":140,"y":520,"wires":[["402a5383db5006b2"]]},{"id":"f30db6a458de8e99","type":"debug","z":"4788c8134ed09947","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":450,"y":460,"wires":[]},{"id":"4acdc22d90eae667","type":"function","z":"4788c8134ed09947","name":"parse","func":"\nvar array_length = \"0\";\nvar Newmsg = {array:0};\nvar array_timespan = 0; \nvar previous_arr_last_object = flow.get(\"previous_arr_last_object\").toString().substring(1,19);\nvar OpenSwitch = 1;\nvar CloseSwitch = 0; \n\n// beachte \"START\" von diesem Funktion Node!\n\nmsg.Switchnode = CloseSwitch; \n\nmsg.payload = msg.payload.substring(42,62);\n\nNewmsg.array= flow.get(\"arr\");\n\n\n//only add payloads to an non-empty array, which more than < \"TachoLimitFreq\" (ms) apart\nif (msg.topic == \"rising\" && Newmsg.array.length > 0 && (Number(msg.payload.substring(1,19)) - Number((Newmsg.array[Newmsg.array.length-1]).substring(1,19)))*1000 > global.get(\"TachoLimitFreq\"))\n{\n flow.get(\"arr\").push(msg.payload);\n\n flow.set(\"arr\", flow.get(\"arr\"));\n \n Newmsg.array= flow.get(\"arr\");\n}\n\n\n//only add payloads to an empty array, which more than < 6ms apart from the previous_arr_last_object\nif (msg.topic == \"rising\" && Newmsg.array.length == 0 && (Number(msg.payload.substring(1,19)) - Number(previous_arr_last_object))*1000 > global.get(\"TachoLimitFreq\"))\n{\n flow.get(\"arr\").push(msg.payload);\n\n flow.set(\"arr\", flow.get(\"arr\"));\n \n Newmsg.array= flow.get(\"arr\");\n}\n\n//Reset nach \"TachoSampleRate\" und speed berechnen\nif( msg.topic == \"reset\" )\n{ \n Newmsg.array = flow.get(\"arr\");\n \n //setze previous_arr_last_object auf den Wert von actual_arr_last_object\n \n if (Newmsg.array.length > 0)\n {\n flow.set(\"previous_arr_last_object\", Newmsg.array[Newmsg.array.length-1]);\n }\n else\n {\n flow.set(\"previous_arr_last_object\", 0); \n }\n \n //bestimme Anzahl der Ticks durch Array_length\n array_length = flow.get(\"arr\").length.toString();\n \n //bestimme array-timespan in s\n array_timespan = 1, //(Number((Newmsg.array[Newmsg.array.length-1]).substring(1,19)) - Number((Newmsg.array[0]).substring(1,19)));\n \n msg.SwitchNode = OpenSwitch;\n speed = ((20 / 17) * array_length / array_timespan) + (60 / 17);\n if (speed <= 60 / 17)\n {\n msg.speed = 0;\n //msg.speed = NewMsg.speed;\n }\n else\n {\n msg.speed = Math.round(speed);\n //msg.speed = NewMsg.speed;\n }\n \n Newmsg.array.length = 0;\n flow.get(\"arr\").length = 0;\n flow.set(\"arr\", flow.get(\"arr\"));\n \n}\n\nmsg.arr = flow.get(\"arr\"); \nreturn msg;","outputs":1,"noerr":0,"initialize":"// Der Code hier wird ausgeführt,\n// wenn der Node gestartet wird\n\nflow.set(\"arr\", []);\nflow.set(\"previous_arr_last_object\", 0);\n","finalize":"","libs":[],"x":790,"y":500,"wires":[["1591ac925c5f74ec","cb393bac6d87b676"]]},{"id":"1591ac925c5f74ec","type":"debug","z":"4788c8134ed09947","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":930,"y":600,"wires":[]},{"id":"d3158db9d08fa6de","type":"debug","z":"4788c8134ed09947","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":630,"y":540,"wires":[]},{"id":"5b53c3878b668d37","type":"debug","z":"4788c8134ed09947","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":430,"y":580,"wires":[]},{"id":"c1da860091466f03","type":"inject","z":"4788c8134ed09947","name":"","props":[{"p":"topic","vt":"str"},{"p":"timestamp","v":"","vt":"date"},{"p":"arr","v":"","vt":"num"},{"p":"arrlength","v":"","vt":"num"},{"p":"payload"},{"p":"SwitchNode","v":"0","vt":"str"},{"p":"speed","v":"","vt":"num"}],"repeat":"${TachoSampleRate}","crontab":"","once":false,"onceDelay":0.1,"topic":"reset","payload":"0","payloadType":"str","x":640,"y":380,"wires":[["4acdc22d90eae667"]]},{"id":"73bc008fe77dbdcf","type":"debug","z":"4788c8134ed09947","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1190,"y":420,"wires":[]},{"id":"cb393bac6d87b676","type":"switch","z":"4788c8134ed09947","name":"Moving","property":"SwitchNode","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":1000,"y":500,"wires":[["73bc008fe77dbdcf","5df098d4be0d6d10"],["d4a6eb72fbb9e91b"]]},{"id":"d4a6eb72fbb9e91b","type":"debug","z":"4788c8134ed09947","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1190,"y":560,"wires":[]},{"id":"5df098d4be0d6d10","type":"link out","z":"4788c8134ed09947","name":"Tachosignal -->Balckbox_to_MySQL_insert_Velocity","mode":"link","links":["616a5d227d0d8ef5","5cda5648b888f4c2"],"x":1155,"y":460,"wires":[]}]
So can please somebody give me any tips how to improve performance here, as I do not know enough about that, but that my hardware is capeable of doing obviously up to 300 Hz.
How to improve Java code of function node?
kind regards HABAKUK