Creer un array avec msg.payload et msg.topic

Hello everyone,
I am looking to create an array with a cycle of messages.
the message contains msg.payload with a variable value and msg.topic with a cyclic value.
this is data coming from a lidar
msg.topic an angle from 0 to 356 and msg.payload the distance in mm
My problem is that the messages do not always start with 0 at startup but very randomly depending on the position where it is stopped.
I've tried all of them, but no luck.

Here is an example , i simulate the input with random start, hope i correctly simulated your data.
I used a join node to capture the 356 data messages, the join sorts the object for you, then the output of the array of values are in the correct order.
example

[{"id":"75776c05.18ada4","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":1040,"wires":[["430936ac.a529d"]]},{"id":"430936ac.a529d","type":"function","z":"c791cbc0.84f648","name":"simulate incoming data random start","func":"let random_start = Math.round(Math.random()*356);\nfor(let i = 0; i < 357; i+=1){\n    let topic = i+random_start;\n    node.send({payload:Math.round(Math.random()*10),\n    topic: (topic > 356 ? topic -357 : topic)\n    })\n}\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":430,"y":1040,"wires":[["18d8dc73.bbac9c"]]},{"id":"18d8dc73.bbac9c","type":"join","z":"c791cbc0.84f648","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"356","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":130,"y":1100,"wires":[["102bbc1d.cf6a1c","a4b5599.e8921a8"]]},{"id":"102bbc1d.cf6a1c","type":"debug","z":"c791cbc0.84f648","name":"Object of incoming data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":1180,"wires":[]},{"id":"a4b5599.e8921a8","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload.*","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":1100,"wires":[["1dfc18a4.b2d0ef"]]},{"id":"1dfc18a4.b2d0ef","type":"debug","z":"c791cbc0.84f648","name":"array topic 0 at position 0, topic 356 at position 355","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":680,"y":1100,"wires":[]}]

[edit] correct missing inject node.

Do you need the array to remain in existence or does it need to be built when the lidar starts?

How long do the measurements come from the lidar? i.e. does it make one rotation and stop or keep going. For example, it it starts at 75 degrees, does it run up and down (75, 76, 77,...355, 356, 355, 354,...) or reset back to 0 (75, 76, 77,...354, 355, 256, 0, 1, 2,...)

Here is a simulation where the angle start is random and it runs from that value to the limit and then from 0 to one less than th eoriginal angle.

[{"id":"c47e4246510dd633","type":"tab","label":"Simulated Lidar readings stored in array","disabled":false,"info":"","env":[]},{"id":"86a00315b3f92853","type":"group","z":"c47e4246510dd633","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["d1205ae551b67964","f8d13e6932591663","5216f33b3bc63bd0"],"x":54,"y":19,"w":352,"h":122},{"id":"91fd76adf2f94255","type":"group","z":"c47e4246510dd633","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["8786f21abb102227","b1e02f80b417c72e","e4d68688bf91fe30","3bcb0fe9321b4ea3"],"x":54,"y":159,"w":532,"h":162},{"id":"301fb6bc15d9aaf3","type":"group","z":"c47e4246510dd633","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["aeb7a645686e80b7","aba9675a81e1c38d","2bf581a2350928c3","69734ede3da30119","89e5e92f559bda2d","4ffb019b1d18fe89"],"x":54,"y":339,"w":512,"h":202},{"id":"e5711bb897243eed","type":"group","z":"c47e4246510dd633","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["4ddfa4d2f160f636","61a78b96a49fc689","9dd35e20a83c0942","af593b82ceb4f3df"],"x":54,"y":579,"w":372,"h":162},{"id":"d1205ae551b67964","type":"inject","z":"c47e4246510dd633","g":"86a00315b3f92853","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[]","payloadType":"json","x":150,"y":100,"wires":[["f8d13e6932591663"]]},{"id":"f8d13e6932591663","type":"function","z":"c47e4246510dd633","g":"86a00315b3f92853","name":"","func":"let arr = []\nfor (let i=0; i<10; i++) {\n    arr[i]= 0;\n}\nflow.set(\"lidar_array\", arr)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":100,"wires":[[]]},{"id":"8786f21abb102227","type":"inject","z":"c47e4246510dd633","g":"91fd76adf2f94255","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":240,"wires":[["b1e02f80b417c72e"]]},{"id":"b1e02f80b417c72e","type":"random","z":"c47e4246510dd633","g":"91fd76adf2f94255","name":"start at random place","low":"0","high":"9","inte":"true","property":"payload","x":420,"y":240,"wires":[["e4d68688bf91fe30"]]},{"id":"e4d68688bf91fe30","type":"change","z":"c47e4246510dd633","g":"91fd76adf2f94255","name":"","rules":[{"t":"set","p":"array_start","pt":"flow","to":"payload","tot":"msg"},{"t":"set","p":"reset","pt":"msg","to":"reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":280,"wires":[["aeb7a645686e80b7"]]},{"id":"aeb7a645686e80b7","type":"counter","z":"c47e4246510dd633","g":"301fb6bc15d9aaf3","name":"pass thru 10 readings","init":"0","step":"1","lower":"","upper":"10","mode":"increment","outputs":"1","x":180,"y":420,"wires":[["69734ede3da30119","89e5e92f559bda2d"]]},{"id":"4ddfa4d2f160f636","type":"switch","z":"c47e4246510dd633","g":"e5711bb897243eed","name":"","property":"countUpperLimitReached","propertyType":"msg","rules":[{"t":"null"},{"t":"true"}],"checkall":"false","repair":false,"outputs":2,"x":130,"y":660,"wires":[["61a78b96a49fc689"],["9dd35e20a83c0942"]]},{"id":"aba9675a81e1c38d","type":"debug","z":"c47e4246510dd633","g":"301fb6bc15d9aaf3","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":470,"y":500,"wires":[]},{"id":"2bf581a2350928c3","type":"random","z":"c47e4246510dd633","g":"301fb6bc15d9aaf3","name":"sensor value","low":"0","high":"200","inte":"true","property":"payload","x":150,"y":500,"wires":[["4ddfa4d2f160f636","aba9675a81e1c38d"]]},{"id":"61a78b96a49fc689","type":"function","z":"c47e4246510dd633","g":"e5711bb897243eed","name":"","func":"\nlet arr = flow.get(\"lidar_array\")\nnode.warn(\"payload=\"+msg.payload)\nnode.warn(\"count=\"+msg.count)\narr[msg.count-1] = msg.payload\nflow.set(\"lidar_array\", arr)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":660,"wires":[["aeb7a645686e80b7"]]},{"id":"9dd35e20a83c0942","type":"debug","z":"c47e4246510dd633","g":"e5711bb897243eed","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":290,"y":700,"wires":[]},{"id":"69734ede3da30119","type":"debug","z":"c47e4246510dd633","g":"301fb6bc15d9aaf3","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":470,"y":420,"wires":[]},{"id":"89e5e92f559bda2d","type":"change","z":"c47e4246510dd633","g":"301fb6bc15d9aaf3","name":"this removes msg.reset so flow will run","rules":[{"t":"delete","p":"reset","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":230,"y":460,"wires":[["2bf581a2350928c3"]]},{"id":"5216f33b3bc63bd0","type":"comment","z":"c47e4246510dd633","g":"86a00315b3f92853","name":"Initilize the array in the flow variable","info":"","x":220,"y":60,"wires":[]},{"id":"3bcb0fe9321b4ea3","type":"comment","z":"c47e4246510dd633","g":"91fd76adf2f94255","name":"This code simulates the initial angle is somewhere between 0 and 9","info":"","x":320,"y":200,"wires":[]},{"id":"4ffb019b1d18fe89","type":"comment","z":"c47e4246510dd633","g":"301fb6bc15d9aaf3","name":"this code simulates readings from the LIDAR","info":"","x":250,"y":380,"wires":[]},{"id":"af593b82ceb4f3df","type":"comment","z":"c47e4246510dd633","g":"e5711bb897243eed","name":"This code writes the readings into the array","info":"","x":240,"y":620,"wires":[]}]

Now you have@E1cid method and this one to play with it.

Either I expressed myself badly or I didn't understand your flows. But hey, I have the name that goes with it.
here is part of my flow.

var dist
var correct = msg.payload[6]+(msg.payload[7]*256)
var vitesse = msg.payload[2]+(msg.payload[3]*256)
if(21455<vitesse<21505 && correct !=0){
    dist = {payload:msg.payload[4]+(msg.payload[5]*256), topic:Math.min(360,Math.max(0,(msg.payload[1]-160)*4))}
}
return dist;

the topic output message cyclically gives me the angle 0, 4, 8, 12, ...356, 0,... but starts randomly in the session.
at the beginning I simply send in a radar chart but the values ​​do not line up in order. so my idea to put them in a well-ordered array to have an image of what my lidar scans

just reduce the count in the join node to match the total messages, which would be 90.
I have changed the simulated data to send topics angles 0, 4, 8, 12, ...356, 0,. I have also added a chart node so you can see the result.

I may be misunderstanding what you are trying to do, you may have to elaborate.

[{"id":"fa926a54.65081","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"5","topic":"","payload":"1","payloadType":"num","x":190,"y":1220,"wires":[["ad2df059.c5a2b"]]},{"id":"ad2df059.c5a2b","type":"function","z":"c791cbc0.84f648","name":"simulate incoming data random start","func":"let random_start = Math.floor(Math.random()*90);\n//random_start =0;\nfor(let h = 0; h < msg.payload; h++){\n    for(let i = 0; i < 90; i++){\n    let topic = (i*4)+(random_start*4);\n    node.send({payload:Math.round(Math.random()*10),\n    topic: (topic > 356 ? topic -360 : topic)\n    })\n}\n}\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":460,"y":1220,"wires":[["e80b8c96.087ae","c36594fd.341108"]]},{"id":"e80b8c96.087ae","type":"join","z":"c791cbc0.84f648","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"4","count":"90","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":200,"y":1280,"wires":[["6f180b75.202bd4","cdc6c741.1f051"]]},{"id":"c36594fd.341108","type":"debug","z":"c791cbc0.84f648","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":1180,"wires":[]},{"id":"6f180b75.202bd4","type":"debug","z":"c791cbc0.84f648","name":"Object of incoming data","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":580,"y":1360,"wires":[]},{"id":"cdc6c741.1f051","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[\t{\t\"series\":[\"X\"],\t\"data\":[[$$.payload.*]],\t\"labels\":[$keys($$.payload).($number($)%10 = 0 ? $ : \"\")]}\t]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":1280,"wires":[["16fe4aa2.7187d5","f77192b5.3eb968"]]},{"id":"16fe4aa2.7187d5","type":"debug","z":"c791cbc0.84f648","name":"array topic 0 at position 0, topic 356 at position 355","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":1280,"wires":[]},{"id":"f77192b5.3eb968","type":"ui_chart","z":"c791cbc0.84f648","name":"","group":"b9d366f18ff7ae74","order":15,"width":0,"height":0,"label":"chart","chartType":"radar","legend":"false","xformat":"HH:mm","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"","removeOlder":"24","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#d73920","#000000","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"className":"","x":550,"y":1320,"wires":[[]]},{"id":"b9d366f18ff7ae74","type":"ui_group","name":"test","tab":"ffba43d73860d25d","order":1,"disp":true,"width":12,"collapse":false},{"id":"ffba43d73860d25d","type":"ui_tab","name":"Level","icon":"dashboard","order":3,"disabled":false,"hidden":false}]

it was me who didn't understand.
After a night of sleep I used join and change. it works perfectly!

thanks again for the help

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.