Qsin/Qcos examples?
DiverBob
Posts: 1,113
in Propeller 2
I wanted to try out some of the new Spin2 commands and was getting nowhere with qsin and qcos. Couldn't find any examples where someone has used them yet. I tried various versions of the following code but output returns 0. I think the problem is I'm not understanding the twopi value in the command.
PUB main() | x, y, length, angle, val, twopi angle := 45 length := 15 val := pi/180 twopi := 2*pi x := qsin(length, angle, twopi) debug(udec(x))
This is the entry in the Spin2 manual:
QSIN(length, angle, twopi) : y
Rotate (length,0) by (angle / twopi) * 2Pi and return y. Use 0 for twopi = $1_0000_0000. Twopi is unsigned.
QCOS(length, angle, twopi) : x
Rotate (length,0) by (angle / twopi) * 2Pi and return x. Use 0 for twopi = $1_0000_0000. Twopi is unsigned.
Comments
This is from @Wuerfel_21's VJET demo...
The parameters for QCOS and QSIN are a bit confusing:
For "length" you want the value to be returned for cos(0). Basically this is a scaling factor -- all the "real" sin and cos values will be multiplied by this
For "twopi" you want the angle that represents a full rotation. So for example if your angle is in degrees you would use "360" for twopi
For "angle" use the angle as measured relative to twopi
So for example to calculate when the input is a simple angle in degrees (no fractional parts) and with the result as a decimal with 3 decimal places use:
length = 1000
twopi = 360
So qsin(1000, 45, 360) will return 707 (the actual sin value multiplied by 1000).
If your angles are measured in tenths of a degree, use 3600 for twopi; if they're in radians multiplied by 100 use (314*2); and so on.
Is the result signed? I'd assume so...
I've attached an example Spin2 file of using qsin() and qcos() and the DEBUG graphical Scope.
It is a short program, I tried posting the code directly into this message, but unfortunately the tic-mark that Chip used to define the graphical DEBUG commands/routines is the same symbol that the forum's "code" format option uses, so the code won't display correctly. :-(
Here is an example of the program's output:
Hopefully this example will help you, I have some others too...
Thanks for that Eric. I had briefly looked at Chip's description and not got it either.
Works totally fine
That ping didn't work BTW, always put a space after the username.
Thanks for all the replies. No wonder it didn’t work for me. Where is this information documented?
So what were the steps you used to post the code? I selected the 'code' entry in the pulldown and all it did was mangle the code when I put it in. I am using the latest version of Firefox when working with this forum.
I will improve the description. Sorry about this.
Yes, the result is signed.
I had to figure it out by trial and error in order to implement QSIN and QCOS in flexspin, although it didn't take too long once I saw what was going on.
It'd probably be a good idea to update the shared Spin document with more examples. Is that something you would be able to take on?
EDIT: whoops, looks like Chip is on it already . Thanks Chip.
You need to triple-backtick it. The code block button defaults to single backticks, which are for
inline code
.Needs to look like this:
```
PUB doStuff()
send("Code goes here")
send("Also why is the Spin highlighter still not in here?")
```
which turns into
OK, Thank you. Live and learn...
That is a great explanation, ran your example so now time to play! May be time to get back to trying rotxy and polxy again....
... Hanging on the edge of tomoooorrow!
No?
Ok I'll see myself out then.