# Can anyone help with some maths, need cosine from rotary encoder data

Hello,

Trying to make a sketch that includes formula:

E(J) = WgR (cos b - cos a) - L

E - energy in Jules
W - Weight of pendulum
g - gravitational acceleration(9.80665 m/s2)
R - Lenth of the pendulum from pivot pont
Cos a - Starting angle
Cos B - ending angle
L - energy lost

This is a pendulum with a weight and I will be using starting angle of the pendulum (cos a) and the first point of reversal (cos b) to calculate the energy in Jules lost (E).

Maths is really not my strong suit, and I could really do with some help on how I calculate the cos to put into the formula based on the degrees of rotation.

`cos()` works, so long as you put the angle in radians.

MorganS:
`cos()` works, so long as you put the angle in radians.

So, if I take my degrees from converting the 400 points from the encoder im using by dividing 400/360 I can then use that value × pi/180 and use `cos()`?

Yes, in principle, how does the encoder "know" the position for angle = zero?

jremington:
Yes, in principle, but how does the encoder "know" the position for angle = zero?

Was planning on having gravity help here, as its a pendulum, well a hammer on a rod, at rest it will always be 0 zero degrees

Correction:

by dividing 400/360

Other way around.

jremington:
Correction: Other way around.

well spotted!

Simpler: cos(encoder_position*PI/200), assuming encoder steps counted from zero angle.

jremington:
Simpler: cos(encoder_position*PI/200), assuming encoder steps counted from zero angle.

Thank you, code and maths are very much weaknesses I'm trying to work on, but I can solder like a champ!

Make sure the result of the intermediate calculation is a float. Just dividing integer by integer will give you an integer and you need the decimals.

The PI macro is a float, so that will force a type conversion in jremington's code.

MorganS:
Make sure the result of the intermediate calculation is a float. Just dividing integer by integer will give you an integer and you need the decimals.

The PI macro is a float, so that will force a type conversion in jremington's code.

To be totally honest, this project is probably so far above my coding skill level I will end up paying someone on Fiverr, and try and learn from their code.

Mechanical and electrically sound, but I just can't code, just can't get it to click, and have so many projects that never got out the notebook dude to my inability to grasp the basics.