tag:blogger.com,1999:blog-21402852.post115072709367786046..comments2015-11-22T09:37:35.311+10:00Comments on The NXT STEP is EV3 - LEGO® MINDSTORMS® Blog: Thoughts on Complex Programs...James Floyd Kelly (Jim)http://www.blogger.com/profile/13876934942928389738noreply@blogger.comBlogger12125tag:blogger.com,1999:blog-21402852.post-1151201096251600332006-06-25T12:04:00.000+10:002006-06-25T12:04:00.000+10:00Good solution! Indeed, this is just the type of so...Good solution! Indeed, this is just the type of solution I tried to implement for things like cos(). But there are a few problems...<BR/><BR/>Since NXT-G deals with only integers, all the intermediate calculation values are also truncated. To minimize the problem with this, it helps to keep the intermediate results as large as possible without overflowing. So for instance, instead of (x/23)^4, it works better to calculate x^4/279841. For the same reason it's nice to scale the result by the biggest number reasonable.<BR/><BR/>Another thing that came up when I was doing this was what "best" means. Minimizing the maximum error? Minimizing the sum of the square of the errors at all angles (a least squares fit)? Making sure that the function is continuous over the range of interest? The "right" answer depends critically on what you need the function to do. For my purposes, I used a 4th order polynomial fit that was continuous throughout the range as well at the endpoints, and scaled up by 100,000:<BR/><BR/>100,000 * cos() =<BR/>100,000 - 13 x^2 + x^4 / 12379<BR/><BR/>Don't think it's the "best"... it's not. But there's a complex tradeoff between what you need it to do, and how you go about doing it, as well as the limits of the language (integer), processor (bits of precision before overflow), and how much accuracy you want and how long you want to take (use a 6th order polynomial if you like, but it'll take more time).<BR/><BR/>That was a really good analysis, by the way!<BR/><BR/>-- <BR/>Brian DavisBrian Davishttp://www.blogger.com/profile/13348160352741782598noreply@blogger.comtag:blogger.com,1999:blog-21402852.post-1151192871875691542006-06-25T09:47:00.000+10:002006-06-25T09:47:00.000+10:00You can see a block diagram of the cosine algorith...You can see a block diagram of the cosine algorithm at<BR/><BR/>http://home.earthlink.net/~xaos69/cosine_approximation.html<BR/><BR/>The block diagram was done in LabView but should easily implemented in NXT-G.jeffhttp://home.earthlink.net/~xaos69/cosine_approximation.htmlnoreply@blogger.comtag:blogger.com,1999:blog-21402852.post-1151172024301371032006-06-25T04:00:00.000+10:002006-06-25T04:00:00.000+10:00Computation of Trig FunctionsIf a high degree of a...Computation of Trig Functions<BR/><BR/>If a high degree of accuracy is not required, and memory space is <BR/>limited, then computation provides an alternative to table look up <BR/>methods. The following example illustrates this approach for the cosine function.<BR/><BR/>Given a processor allowing only 16 bit signed integer arithmetic, a fair approximation for os(x), scaled by a multiplier of 1000 is<BR/><BR/> cos(x) = 1000-(x^2)/7+(x/23)^4<BR/><BR/>where x is angle in degrees and "^" means exponentiation. For example, <BR/>a^4 = aaaa. (This formula is derived from the Taylor series for the cosine function.)<BR/><BR/>The above formula may be implemented by standard NXT arithmetic blocks <BR/>using something like the following algorithm<BR/><BR/>1. xsqr = x * x<BR/>2. xsqr = xsqr / 7<BR/>3. x_23 = x / 23<BR/>4. result = x_23<BR/>5. result = result * x_23<BR/>6. repeat step 5 twice (hint: loop twice)<BR/>7. result = result - xsqr<BR/>8. result = result + 1000 <BR/><BR/>where "*" is multiplication,<BR/> "/" division<BR/> "-" subtraction<BR/> "+" addition<BR/><BR/>The table below shows the values calculated by the above formula versus the more accurate intrinsic software function.<BR/><BR/>angle formula software<BR/> percent error<BR/>0 1000 1000 0<BR/>5 997 996 0<BR/>10 986 985 0<BR/>15 968 966 0<BR/>20 943 940 0<BR/>25 912 906 1<BR/>30 873 866 1<BR/>35 826 819 1<BR/>40 773 766 1<BR/>45 712 707 0<BR/>50 659 643 2<BR/>55 584 574 1<BR/>60 502 500 0<BR/>65 413 423 1<BR/>70 381 342 4<BR/>75 278 259 2<BR/>80 167 174 1<BR/>85 49 87 4<BR/>90 -76 0 8<BR/><BR/>As you can see, the error tends to increase as we approach 90 degrees. <BR/>However if our application involves only mid-range angles, then the <BR/>above algorithm provides accuracy to about plus or minus 5%. Perhaps a <BR/>person with more mathematical background than I can find further <BR/>improvements of trig function computation?jeffnoreply@blogger.comtag:blogger.com,1999:blog-21402852.post-1151023795436775742006-06-23T10:49:00.000+10:002006-06-23T10:49:00.000+10:00No, it does not seem possible to make recursive My...No, it does not seem possible to make recursive MyBlocks. I'm not sure at this point if MyBlocks are the equivilent of in-line functions (duplicating code for eaqch use), limited subroutines (all calls share the same varaibles), or closer to "true" subroutines (each call generates its own varaible stack). I suspect they function as limited subroutines, but I've not tested it yet.<BR/><BR/>-- <BR/>Brian DavisBrian Davishttp://www.blogger.com/profile/13348160352741782598noreply@blogger.comtag:blogger.com,1999:blog-21402852.post-1151007452907812562006-06-23T06:17:00.000+10:002006-06-23T06:17:00.000+10:00Question: Are recursive functions posible in NXT-...Question: Are recursive functions posible in NXT-G? In other words, can a MyBlock be self-referential? That is, can a MyBlock contain itself?Jeffnoreply@blogger.comtag:blogger.com,1999:blog-21402852.post-1150807574713392062006-06-20T22:46:00.000+10:002006-06-20T22:46:00.000+10:00Bluetooth will allow a lot of interesting solution...Bluetooth will allow a lot of interesting solutions - like moving all the "intelligence" of a program off the NXT and into a desktop or laptop computer, using BT to drive the NXT to run motors, read sensors, etc. *BUT*, how does BT control the NXT? It's either got to be via direct commands (this is an availible option - it's how you use things like mobile phone to communicate with teh NXT), or from within NXT-G (at least until other environments come along).<BR/><BR/>The problem is there are some big drawbacks to this. First is not everybody has that option (a BT equiped PDA and the know-how to hack it), so solutions withing NXT-G are still really good to look for. Second, there's some real time issues with using BT this way. Queing up and sending a BT message, waiting while it's received, interepreted, and then waiting while a return message is qued up, send, and recieved, and then finally acted upon, takes *time*... and for a lot of tasks (line following being a cannonical example), you just don't have the time. By the time the PDA's response comes back, the robot has completely lost the line (I've seen RCX-based line-followers that run more than 1 m/s along a 3/4" black line (they could cross the line completely in about 20 ms). Third, you are still limited by the Bluetooth stack that is built into the NXT... and I'm not sure what limitations that presents as yet.<BR/><BR/>-- <BR/>Brian DavisBrian Davishttp://www.blogger.com/profile/13348160352741782598noreply@blogger.comtag:blogger.com,1999:blog-21402852.post-1150784388492733142006-06-20T16:19:00.000+10:002006-06-20T16:19:00.000+10:00I neverwor ked or atleast saw a Lego Mindstorm. Bu...I neverwor ked or atleast saw a Lego Mindstorm. But i believe, with the bluetooth feature in NXT, we can integrate a PDA with NXT and override whatever limits are present in NXT-G.Satishhttp://www.blogger.com/profile/12082846963267402545noreply@blogger.comtag:blogger.com,1999:blog-21402852.post-1150764726888227582006-06-20T10:52:00.000+10:002006-06-20T10:52:00.000+10:00Thanks byronczimmer and brian davis for pointing m...Thanks byronczimmer and brian davis for pointing me to the examples for doing math. I once worked on a project with a mathematician who developed some numerical algrothims for calculating trig functions. The algorithms converged very rapidly (four or five iterations) and would probably work using integer arithmitic. A factorial MyBlock would probably be necessary. Since the algorithms are based on power series (rather than table lookup), the need to check angle signs would be eliminated if radian angle measure can be used.<BR/><BR/>An interesting project would be to use triangulation to create an x-y map of the location of several objects. For example the sonar sensor could be used to determine the distance to an object. The robot could be programmed to turn ninty degrees, move a specified distance, and take another distance measurement. The angle to the object, relative to the new location, and the two distances would be sufficient to determine an x-y coordinate location, relative to the robot. The only limit to these sorts of programs would appear to be the program memory size of the NXT.<BR/><BR/>Incidentally, if you are willing to do a little navigating, a demo verison of LabView is avialable on the National Instruments web site. The LabView IDE can be used to develop such algorithms without the need of a NXT brick.<BR/><BR/>I'm really looking forward to seeing the software people develop with the NXT-G IDE. It should be pretty interesting!Jeffnoreply@blogger.comtag:blogger.com,1999:blog-21402852.post-1150747133675896812006-06-20T05:58:00.000+10:002006-06-20T05:58:00.000+10:00As byronczimmer mentioned, I covered some of this ...As byronczimmer mentioned, I covered some of this in a previous post. Since the NXT doesn't have floating point math, I just use integer math with the result multiplied by 100, or 1000, etc, to signify two or three places to the right of the decimal. As to how to calculate, I've implemented it using both tables as well as power series (somewhat ugly ones, due to the limitations of integer math).<BR/><BR/>I agree with Jim on the "Web Downloads" stuff as well - this may add a lot of interesting options for the future.<BR/><BR/>-- <BR/>Brian DavisBrian Davishttp://www.blogger.com/profile/13348160352741782598noreply@blogger.comtag:blogger.com,1999:blog-21402852.post-1150743906316231762006-06-20T05:05:00.000+10:002006-06-20T05:05:00.000+10:00May 8th post by Brian "A MyBlock primer (with a si...May 8th post by Brian "A MyBlock primer (with a side of math)" covered this in detail, sin() specifically.<BR/><BR/><BR/><A HREF="http://thenxtstep.blogspot.com/2006_05_01_thenxtstep_archive.html" REL="nofollow">Archive Link</A> (about 55/60% o the way down)<BR/><A HREF="http://www.blogger.com/comment.g?blogID=21402852&postID=114710014168232793" REL="nofollow">or use this direct link to the comments part here.</A> (but then you won't see the pictures)<BR/><BR/>WOW, I wish you could link to a specific post in the archives in this blog environment.<BR/><BR/>There were comments made a while ago about moving off of blogger -- are those sentiments still active?byronczimmernoreply@blogger.comtag:blogger.com,1999:blog-21402852.post-1150741551529358612006-06-20T04:25:00.000+10:002006-06-20T04:25:00.000+10:00Speaking of sine, cosine, etc. How do you create ...Speaking of sine, cosine, etc. How do you create NXT-G trig functions? Since trig functions produce decimal fractions, how do you express trig values as integers? (e.g. sine of pi over 4 equals 0.707106 to six decimal places. How is this expressed?) How are trig values calculated? By table lookup? power series? Can you post a picture of one of your trig programs in NXT-G?Jeffnoreply@blogger.comtag:blogger.com,1999:blog-21402852.post-1150737894533505772006-06-20T03:24:00.000+10:002006-06-20T03:24:00.000+10:00What I'm looking forward to is seeing how LEGO use...What I'm looking forward to is seeing how LEGO uses the "Web Downloads" category in the Custom Palette - what I think we'll start seeing is free downloads for functions that LEGO finds are useful - sine, cosine, modulo, etc... and the ability to share MyBlocks is going to be interesting, too - it may lead to some sort of voting or review for determining who has the best solution for a repeatable event that can be encapsulated inside a MyBlock.<BR/><BR/>So far, no word on how LEGO will use the "Web Downloads" category, but I keep hoping ... <BR/><BR/>JimJim Kellyhttp://www.blogger.com/profile/13876934942928389738noreply@blogger.com