New HiTechnic blocks for Sin, Cos, and ATan2

Announcing NXT-G blocks to calculate sine, cosine, as well as arctangent! These functions can be useful for advanced navigation or to process the data from sensors such as the Acceleration sensor. Both floating point (for LEGO Mindstorms 2.0 and above only) and integer versions (for all Mindstorms versions but intended for 1.0 users) are available.

Why HiTechnic Trig Functions?

A lot of HiTechnic sensors deal with angles in one form or another. For example, the HiTechnic Angle Sensor gives you the angle of the input axle and the Compass sensor gives you the heading in degrees. Less obvious is the Acceleration Sensor, which is also known as the tilt sensor.  This sensor doesn’t actually give you a tilt angle, but now, thanks to ATan2 block, that can easily be calculated.

For most robot projects the sensor values can be used directly and no tricky math is required.  But sometimes you need to process the angle data or perhaps convert between polar and Cartesian coordinates and then you need to be able some more advanced math operations.  That is when these new block will be handy.

One possible use is with the compass sensor.  For example, if you are driving around and you want to keep track of your current x, y location, then you need to know how your location changes for any possible heading.  One way to do this would be to have a loop where you get the current heading from the compass sensor, pass that to the Sin/Cos block, and then multiply both the sin and cos with the distance driven since the previous integration of the loop.  The sin(heading) times distance will be the East-West change and cos(heading) times distance will be the change in the North-South location.

Another handy use of the Sin/Cos block is with the NXT display. Now you can easily draw a line at an arbitrary angle. For example, you can make a compass needle on the NXT screen that always points north.

For the Acceleration Sensor, you can now use the ATan2 function to calculate the angle of tilt since the values of the sensor correspond to the component vectors of gravity.

Float and Integer versions

As you can see, there are four new blocks: a Sin/Cos and an ATan2 block that use floating point math and a Sin/Cos and an ATan2 blocks that use integer math. The floating point versions will only work with LEGO Mindstorms 2.0 and higher. The integer blocks will work with all versions of the LEGO Mindstorms software but are intended for 1.0 users since the 1.0 software does not support floating point numbers. If you have the 2.0 software you should use the floating point versions. These are both fast and accurate.

Sin/Cos Blocks

This block calculates the sine and cosine values of an angle. The easy way to think about sine and cosine is to imagine a circle with a radius of 1 like this:

The sine of the angle (aka sin) gives you the rise above the x axis and the cosine (aka cos) gives you the offset from the y axis. Note that these are signed values and that sin will go negative if the angle is between 180 and 360 and cos will be negative between 90 and 270.  Also note that sin and cos repeat infinity in each direction so that sin of 3610 will give you the same value as sin of 10 (3610 is 360 * 10 + 10).

For the floating point block the input angle can be in either degrees or radians.

The angle either be set in the configuration panel, as seen above, or passed in with a data wire.

With the data-hub drawer shown, the angle would be passed in to (1) and the sin and cos of the angle will come out at (2) and (3).  With this floating point block the sin and cos values will always be in the range of -1 and 1 and should have an accuracy of at least 6 digits (+/- 0.000001). 

The integer block (for LEGO Mindstorms 1.0 users) looks very similar to the floating point block except it only accepts degrees as the units for the angle.  The output sin and cos values are multiplied by 100 so the range will be -100 to 100.  For example, if you pass in an angle of 30 you will get 50 for sin and 87 for cos, which correspond to .5 and .87 respectively. 

The ATan2 Blocks

The ATan2 function is a powerful variation of the arctan function.  This function takes two values, x and y, then calculates the angle relative to the x axis. 

In simple terms, theta is equal to the arctan of y/x.  ATan2 also takes into account the sign of both x and y so it can correctly determine any angle along the circle.  For the floating point block, the angle output will be in the range -180 to 180.  For the integer block, the output angle is in the range -179 to 180.

On the configuration panel you can select the units for the angle as well as pass in values for x and y, though most likely these will be passed in through data wires.

x and y get input on (1) and (2) above and you will get the output angle on (3).

One use of ATan2 is to convert between Cartesian coordinates and polar coordinates.  ATan2 can also be used to calculate an angle based on component vectors, for example to convert the acceleration sensor values into a tilt angle.

Simple Demo Program of Sin/Cos Block

Here is a simple demo program of the Sin/Cos block in action. 

This program will display a sin and cos graph on the NXT screen:

Demo of ATan2 block together with the HiTechnic Acceleration Sensor

In this program, the X and Y acceleration values are passed to the ATan2 block to calculate the tilt angle.  Attach the Acceleration sensor to Port 3 and then hold the sensor in your hand so that the black cap faces up and the beam side of the sensor is away from you.  In this position the tilt angle will be 0.  If you tilt the sensor left the angle will go negative and if you tilt it right it will go positive.  If you tilt the sensor 90 degrees to the right the program will display a value of 90 on the screen and if you tilt it 90 degrees left if will say -90 on the screen. 

The program will also display a pointer on the screen that will follow the tilt of the sensor.  For example if you tilt the sensor about 30 degrees to the right, you may see something like this:

Downloads

HiTechnic Sin/Cos Block (floating point version for LEGO Mindstorms 2.0 and above)

HiTechnic ATan2 Block (floating point version for LEGO Mindstorms 2.0 and above)

HiTechnic Integer Sin/Cos Block (for LEGO Mindstorms 1.0 users)

HiTechnic Integer ATan2 Block (for LEGO Mindstorms 1.0 users)

Demo Program for LEGO Mindstorms 2.0 using floating point Sin/Cos Block

Demo Program for LEGO Mindstorms 1.0 using Integer Sin/Cos Block

Demo Program for LEGO Mindstorms 2.0 using floating point ATan2 Block

Demo Program for LEGO Mindstorms 1.0 using Integer ATan2 Block

Tags: , , , , , ,

5 Responses to “New HiTechnic blocks for Sin, Cos, and ATan2”

  1. Joey kelly says:

    Is there a sample program for the Accelerometer sensor that we could build upon?
    Also, could you have a sample program for the ATan2 block too?

    Awesome blocks!

    jojoguy10

  2. [...] — Xander @ 08:10 Tags: HiTechnic, NXT-G Over on the HiTechnic blog, Gus has written up a new article about his new NXT-G sin/cos and atan2 blocks. It’s chockfull of explanations and some [...]

  3. Gus says:

    Joey, I have updated the post to include a sample ATan2 program used together with the Acceleration Sensor. In fact, in this program I also use the Sin/Cos block to display a pointer on the screen that will match the tilt of the sensor.
    - Enjoy!

  4. Joey kelly says:

    YAY! Thanks a lot! I will see if I can make use of these awesome blocks!

    Thanks,
    jojoguy10

  5. [...] example programs for both blocks, plus the acceleration sensor!  Check out the HT Blog post here: http://www.hitechnic.com/blog/uncategorized/new-hitechnic-blocks-for-sin-cos-and-atan2/jojoguy10PDRTJS_settings_2609307_post_632 = { "id" : "2609307", "unique_id" : "wp-post-632", "title" [...]

Leave a Reply