Shop OBEX P1 Docs P2 Docs Learn Events
Basic Stamp and a sine wave — Parallax Forums

Basic Stamp and a sine wave

GreenGiant83GreenGiant83 Posts: 43
edited 2006-09-14 19:22 in BASIC Stamp
I want to generate a sine wave in my basic stamp, and I want to be able to vary at the minimum is amplitude and preferably its frequency.· It does need to be a fast thing, thinking of wave lengths of about a second.· Can this be done purely progrmatically with the stamp, or will it require some sort additional circuit?

I need to able to get the values of the sine wave scaled to be able to generate PWM for a servo controller.

·

Comments

  • willthiswork89willthiswork89 Posts: 359
    edited 2006-09-14 17:30
    If you goto Parallax.com ,downloads, FAQ this exact question can be answered in detail

    http://www.parallax.com/dl/docs/prod/stamps/BasicStampFAQ.pdf

    there you are! hope this helps

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Learn somthing about everything, and Everthing about somthing-
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-09-14 17:49
    Green Giant -

    I know of no way to genenrate a "perfect" or near-perfect sine wave with a micro-controller. Your best bet, which will also take the work burden off of the micro-controller (Stamp or otherwise) is to use one of the waveform generator chips that are available. If you need a link, just shout back.

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • GreenGiant83GreenGiant83 Posts: 43
    edited 2006-09-14 18:01
    I am not certain that the Freqout command will do what I want, though I could be wrong.

    I want make a servo rotate it relation to a sine wave.· for instance I would need to be able to translate actual sine values to something worth while to send to a servo controller for instance.· These are sine wave values(+1 to avoid the negative number) the numbers on the right have been scaled up to a valid PWM for a servo(though for a servo controler it would be a different scale, I suppose).· Does that make my problem any more clear?



    attachment.php?attachmentid=73754
    134 x 210 - 3K
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-09-14 18:14
    Green Giant -

    Perhaps I misunderstood what you were trying to do. SIN is a perfectly legitimate PBASIC Command. Here is a preview from the PBASIC Help File

    " The Sine operator (SIN) returns the two’s complement, 16-bit Sine of an angle specified as an 8-bit (0 to 255) value. To understand the SIN operator more completely, let’s look at a typical Sine function. By definition: given a circle with a radius of 1 unit (known as a unit circle), the Sine is the y-coordinate distance from the center of the circle to its edge at a given angle. Angles are measured relative to the 3-o'clock position on the circle, increasing as you go around the circle counterclockwise.

    At the origin point (0 degrees) the Sine is 0, because that point has the same y (vertical) coordinate as the circle center. At 45 degrees the Sine is 0.707. At 90 degrees, Sine is 1. At 180 degrees, Sine is 0 again. At 270 degrees, Sine is -1.

    The BASIC Stamp SIN operator breaks the circle into 0 to 255 units instead of 0 to 359 degrees. Some textbooks call this unit a binary radian or brad. Each brad is equivalent to 1.406 degrees. And instead of a unit circle, which results in fractional Sine values between 0 and 1, BASIC Stamp SIN is based on a 127-unit circle. Results are given in two’s complement form in order to accommodate negative values. So, at the origin, SIN is 0. At 45 degrees (32 brads), Sine is 90. At 90 degrees (64 brads), Sine is 127. At 180 degrees (128 brads), Sine is 0 again. At 270 degrees (192 brads), Sine is -127. "

    More can be found in the PBASIC Help File or in the PBASIC Manual.

    Regards,

    Bruce Bates
  • Mike GreenMike Green Posts: 23,101
    edited 2006-09-14 18:16
    PBasic does have a SIN operator which takes a value of 0-255 representing the circle (0-359 degrees) and produces a two's complement 16 bit value (-32767 to 32768) for the sine (-1 to +1). You can scale this anyway you want. In particular, you could scale this for the PULSOUT statement to give a range from 1ms to 2ms (in units of 2us) which is the typical range for the 180 degree rotation of most servos.

    A servo requires a positioning pulse about every 20ms, that's 50 per second. Stepping through the circle one step per pulse gives a frequency of about 5 cycles per second so, to get a wave length of a second, you'd be putting out 5 pulses of the same width. Even though the SIN operator puts out a 16 bit value, the resolution of the PULSOUT statement is only about 9 bits in the servo pulse range (1ms to 2ms) and it's not clear that the servo itself has that fine a positioning capability.

    Anyway, you should be able to do it. It would be helpful to precompute the sine information, then step through the table of sine values (stored in EEPROM with DATA statements and accessed with READ statements) at a variable speed, doing a final scaling (for amplitude) at each step. You might be able to compute the sine information on the Stamp, but you'd want to experiment with the effect of this on timing accuracy.

    The PULSOUT statement has a resolution of 2us and you'd want to use a second complementary PULSOUT to an unused pin to ensure a fixed time for the servo pulse. In other words, if you're putting out a 1ms pulse for the servo, you'd follow this with a dummy 2ms pulse to give a total of 3ms. If you're putting out a 2ms pulse for the servo, you'd follow this with a dummy 1ms pulse to still give a total of 3ms. Follow this with some minimal statements to read the next pulse width, loop, etc. and a PAUSE for the remainder of the 20ms cycle and you'd have a reasonably stable cycle time since the PAUSE is quite accurate, but with a resolution only in ms.

    Post Edited (Mike Green) : 9/14/2006 6:22:49 PM GMT
  • GreenGiant83GreenGiant83 Posts: 43
    edited 2006-09-14 18:55
    Thank you for in depth reply Mike.· Its curious but the SIN function is not in the list of functions in my help file.· Knowing that it does exist is aweomse, and thanks for the explanation on its deviations.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-09-14 19:22
    Green Giant -

    All of the (so called) math function have a section unto themselves. Just put the funtion you're looking for in the DISPLAY box in the Help Display, hit the "Display" button, and you will be brought to that section.

    Regards,

    Bruce Bates

    Post Edited (Bruce Bates) : 9/14/2006 7:33:20 PM GMT
Sign In or Register to comment.