Grasshopper

algorithmic modeling for Rhino

I have a question regarding strings and numbers... I'm reading in 2 lines of data from an Arduino board (these numbers are coming from an accelerometer...so that both numbers will change every time the Timer Component fires). This data is being printed in the out box of a VB.NET component... and in order for them to keep their formatting (meaning... each number will have their own index number in the Post-it Panel) the numbers are actually being read in as strings... If I change the print command in the VB.NET to something like: "Print(CInt(Val(StoredValue)))" then the strings are converted to doubles (so I can use them as numeric data in other components)... but it puts both numbers in one index item in the post it panel. So, instead of having two 3-digit number (each on their own line), I get one 6-digit number (in the example below, the post-it panel would just read 181130). In the image below, you can see it's reading both numbers in and keeping the formatting (each on their own index number)... I plan to just use a dispatch component to then split the list into two and isolate each number separately... but you can see the number parameter is red, because it thinks both numbers are strings. Is there any way to convert a string into a number?

Views: 5246

Replies to This Discussion

Andy,

I posted a reply but I didn't look at your image closely enough. How are you getting a value on the second line without an index? What does the data look like when you edit the notes?

If there is some kind of symbol that is causing a hard return (and converting the data to a string) perhaps you can develop a tokenizer routine to split the data back up again.

-taz
Andy,

Any particular reason why you're using the Out node, IOW the print function, rather than just passing the data to the variable A? The reason why I ask is that the Out node is simply meant for error messages, debugging info, and printing things. Its basically built to format every thing as a string. If you want things to actually retain their data types (even if they are strings or numbers), then actually passing them to one of the output variables (such as A) will allow you to do that.

Best,
Damien
Hey guys,
Thanks for the suggestions... Only, I'm still a little stuck. I tried the multi-line feature...but it still removes the formatting and puts both numbers on the same line... and Damien... I guess it was just an oversight that I was printing the stings/numbers... I tried passing the StoredValue to the A variable... but I still get the same error as the print line, in that it's combining both numbers onto 1 line. Here's an image where it is converting the string into a double by "A=(CInt(Val(StoredValue)))"...so the number parameters are happy... but it's still a 6 digit number and I need two 3-digit numbers. Actually... in the image below it's a 5 digit number... but that's because the first data reading from the accelerometer is a 2-digit number... the data read-out on the wii accelerometer roughly falls between 60-180... but the issue still remains. Any ideas?


Are you passing the values as a list...I'm not sure why its combining them, but if you want multiple values out, you're going to have to pass it an array or list or some sort of collection. A small code snippet might be useful here. I would try just passing the values without converting it an Integer before hand (ie remove CInt). That may actually be what's combining the numbers, and as long as they're numbers, it doesn't necessarily matter whether they are ints or doubles (GH treats them almost the same anyway).
I am not sure if I am reading your problem right, but can you assign your values to a List(Of Strings) or an Array?

Dim myOutputs(1) as String
and then assign myOutputs(0) & myOutputs(1) to the values?

Or is that your (StoredValue) is only one concatenated value that is not-splittable?
If it is coming from two different sources, I am guessing there is something along the way that is concatenating them?

Nevertheless, even if its a single number, a cheap-trick would be to divide it be say 100 or 1000 to get a decimal where you want to split the values, and then extract the Int part of the number in one variable and 'Double minus Int' to extract whats after the decimal. Hope it helps..
Suryansh,
I had thought about splitting the 6 digit number into 2 and then selecting the first three numbers... or the second three numbers... Only, my brain hadn't gotten around to making the connection on exactly how to do that... But your trick makes complete sense. I'll have to try it out once I get home tonight (I don't have the Arduino here at work). It's not an ideal solution, but it could be the quickest fix.
Cool. Let me know how it goes.

Good luck.
Taz,
The data is coming over the serial port... So, in my Arduino code I'm using "Serial.println(accx);" and "Serial.println(accy)". There are two ways to print the data over the serial port. You can use Serial.print or you can use Serial.println... To my knowledge, the only main difference in the two is that the Serial.println command causes a hard return, so that you numbers are printed in one long string, but instead causes a hard return after each number is printed. Here is some more information on the arudino site about the Serial.println command:
When the data is a string... I can put any kind of delimter in the string to separate the numbers... So, I could have my data output in Grasshopper look like "accx = 181, accy = 130"... but as a string... it doesn't really do me much good because I'm ultimately trying to use those values to control the rotation of something... So, I've been using the "(CInt(Val(StoredValue)))" which is what is converting the individual number strings into actual numbers that Grasshopper can use for different components... however, that same command is what is removing the formatting and collapsing it into 1 value.
Andy,

I hear ya.

Not to belabor the discussion or my point, but (assuming the 2 numbers are separated by a lot of spaces) you could still post-process the string output outside of the VB component. Obviously this is not the cleanest or best way, but expressions will work in a pinch using the spaces as a token.

P.S. How do I embed images that link to full size with a double-click? It's driving me nuts...

-taz

Well, at any rate, here's the image.
Attachments:
I think your version works well... and it's less components... which is always nice. Here is the way that I came up with using Suryansh's technique... It might be interesting to see if there is a time difference when I actually hook the wii remote up using these techniques... I wonder if there will be much difference.
BTW, if you want to link to full size.. when you click to load an image, just click on the Options and you should see something that says "Pop Up" for full size image.

Taz,
So, I tested out both methods last night... First, I should start out by saying... I got it to work... sort of. I mean, I was able to split up the number which is controlling a rotating arm...all being driven by the wii nunchuck... But, here's my issue. I tried it first by having the output from the listener be a 6-digit number... so, I'm using the (CInt(Val(StoredValue))) command and it's writing out 181130... and I can easily split it up selecting the Left(x,3) or Right(x,3)... I first rant that number through a Format("{0:000000}",x) so that even if one of the accx or accy numbers were a 2-digit number (so my overall number would only have 5-digits)... with this Format function... I'm always assured a 6-digit number. And this method works... except...
If the first group of numbers coming in only has 2-digits... So, lets say the accelerometer read out of the first one (accx) is 89. Let's say the accy read out is 119. So, when I run this through the Format function to make it have at least 6 digits, my number now reads 011989. So, if I were to take the first three numbers on the right, my read out would be 989... which is much higher than my expected (60-180 range that is really coming over the Serial Port)... So, I'm back to where I started... in that I need to figure out a better way to split up the data.
Which brings me to your method. I tried it as well... in fact, I added a comma in the serial readout, so the string coming out of the listener reads 89,119. So, I can use your trick to go look for a delimeter and then read to the left and right a certain number of digits... The problem I still have is that the data going into the function is a string, and thus even if I split the 3 digits to the right of the comma out (so, my output says 119)... it's still a string, and my number parameter is still red. In your picture above, was your original 181 130 a number or a string? My guess is that it was understood as a number, because your number parameters at the end are accepting the value. But, in my case... I'm still stuck with the inability to convert a string to a number... Does this make sense? And are their any other workarounds?

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service