Help with implementing sin

•  Today 06:42 Forum: Proton Plus Compiler v3 Starter: shantanu@india Views: 0 Replies: 10 Hi, The code generated by the earlier version(3.6.2.6) used to fit but this one is not. Maybe I should revert back..... FL has a USED button which finds unused variables a bit faster than doing a search of each one. By: moonshadow Yesterday 20:20 Forum: Absolute Beginners Section Starter: moonshadow Views: 0 Replies: 0 Where or how can I access them please ? Where or how can I access them please ?

# Thread: Help with implementing sin – 4 days old

1. 0

## Help with implementing sin

Greetings all, I have some confusion over the P24 implementation of sine.

The example in the manual uses the figure of 123 but doesn't give what the result would be. Is sin (123) calculated as (sine 123 = 0.8386) x pi = 2.634?

Aim of the project...
I have a camera mounted on the Z axis of a gimble and this axis is mounted inside the Y axis. Using a gyro on the Y axis, I detect rotation in the Z axis (0 +/-32767).
When the Y axis is at 90 degrees (horizontal) the Z gyro gain will be 1. When the Y axis is at 0 or 180 deg (vertical) the Z gyro gain will be 0 because roll inputs would translate to corrupt the horizon.

What I wish to do is reduce the magnitude of Z gyro at a sinusoidal rate according to the Y angle (0-90) from the horizon.

Would this work?

GYRO_GAIN = (sin(Y_ANGLE)) / 3.1415
GYRO_Z = GYRO_Z * GYRO_GAIN

Thanks,

2. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

3. 0

## Re: Help with implementing sin

The Sin function works in Radians, so the value placed into it needs to be converted from degrees to radians. For example, the procedures below will do the trick with triginometry:

Code:
```'-----------------------------------------------
' Output    : Returns the equivalent in degrees
' Notes     : The equations is: pRadians * 180 / PI
'
EndProc
'-----------------------------------------------
' Input     : pDegrees holds the value in degrees
' Output    : Returns the equivalent in radians
' Notes     : The equations is: pDegees × PI / 180
'
Result = pDegrees * 0.0174533
EndProc

'-----------------------------------------------
Dim Floatin As Float            ' Holds the value to Sin
Dim Floatout As Float           ' Holds the result of the Sin

Floatin = DegToRad(123)         ' Convert the degree value of 123 into radians
Floatout = Sin(Floatin)         ' Extract the Sine of the value
HRSOutLn "Sine of 123 = ", Dec Floatout ' Display the result on a serial terminal```
The resulting value from the demo of a Sine of 123 degrees is 0.838, which is correct.

Or, using procedures, you can create a Sine function that will operate in degrees:

Code:
```'-----------------------------------------------
' Perform a Sine calculation using degrees
' Input     : pDegrees holds the value in degrees
' Output    : Returns the sine of pDegrees
' Notes     : None
'
Proc SineDeg(pDegrees As Float), Float
Result = Sin(pDegrees * 0.0174533)
EndProc

Dim Floatin As Float    ' Holds the value to Sin
Dim Floatout As Float   ' Holds the result of the Sin

For Floatin = -10 To 10
Floatout = SineDeg(Floatin)
HRSOutLn "Sine of ", Dec Floatin, " = ", Dec Floatout  ' Display the result
Next```
The above demo produces these values on the serial terminal:

Sine of -10.000 = -0.173
Sine of -9.000 = -0.156
Sine of -8.000 = -0.139
Sine of -7.000 = -0.121
Sine of -6.000 = -0.104
Sine of -5.000 = -0.087
Sine of -4.000 = -0.069
Sine of -3.000 = -0.052
Sine of -2.000 = -0.034
Sine of -1.000 = -0.017
Sine of 0.000 = 0.000
Sine of 1.000 = 0.017
Sine of 2.000 = 0.034
Sine of 3.000 = 0.052
Sine of 4.000 = 0.069
Sine of 5.000 = 0.087
Sine of 6.000 = 0.104
Sine of 7.000 = 0.121
Sine of 8.000 = 0.139
Sine of 9.000 = 0.156
Sine of 10.000 = 0.173

If you want more accuracy, use Double variables instead of Float and the dSin function. This will use 64-bit floating point.

4. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

5. 0

## Re: Help with implementing sin

Thankyou Les.
Might be worth copying this answer into the P24 manual.

6. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues