Basic Stamp and a sine wave
GreenGiant83
Posts: 43
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.
·
I need to able to get the values of the sine wave scaled to be able to generate PWM for a servo controller.
·
Comments
http://www.parallax.com/dl/docs/prod/stamps/BasicStampFAQ.pdf
there you are! hope this helps
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
-Learn somthing about everything, and Everthing about somthing-
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 -->
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?
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
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
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