Nth degree pendulum

I made an nth degree pendulum, but it moves very fast. This means there is something wrong with the equations of motion:

for (int i = 1; i <circles.size(); i++)
 {     
  circles.get(i).acceleration.set(0, (-g / lenghts.get(i)) * sin(angles.get(i)));
  circles.get(i).velocity.add(circles.get(i).acceleration);
  angles.add(i, circles.get(i).velocity.mag());
  circles.get(i).point.set(lenghts.get(i)*sin(angles.get(i)), lenghts.get(i)*cos(angles.get(i)));
  circles.get(i).point.add(circles.get(i - 1).point);   
  circles.get(i).velocity.mult(damping);
}

But I don’t know what. Here’s all the code: https://pastebin.com/8vzTued1 Can anyone help me with this?

have you angles as radians?

sin and cos need radians

see reference

They should be in the range of two pi.

1 Like

this start with 0 normally

Yes, but I don’t want to move first ball.

1 Like

that actually doesn’t solve the issue[quote=“Rafsing, post:1, topic:26411”]
circles.get(i).point.add(circles.get(i - 1).point);
[/quote]
if starting i == 0, then i - 1 used in the .get(i-1 or -1) returns out of bounds

Hello,

I set frameRate(1) in setup() for testing and fiddled with some variables…

That was in the middle of the night… I was heading in the right direction at one point.
Busy days so won’t be looking at this again for a while.

Use println() as required to help debugging; it helps to see the state of the variables.

Keep at it!

:)

Hi @Rafsing,

Simulating a nth degree pendulum is not trivial. Is a chain of masses connected that depend on each other to move. I tried your code with one simple pendulum and the problem is also happening on that simple pendulum as well.
So, my first advice would be to break the problem into smaller steps and build from there. Try first to implement a simple pendulum. Then, move to a double pendulum. The double pendulum equations can be found here:

Daniel Shiffman has implemented a double pendulum, which you can find here if it helps you :slight_smile:

Once you have understood the equations of motion you can generalize to nth degrees.

I am not sure if I have the proper time to check your code or implement what I have said above, but I believe that it would be a good exercise for you as well :slight_smile:
This kind of relations are also followed in kinematic equations for robotic arms

Hope it helps!
Best regards

@MiguelSanches I implemented single and double pendulum, but I don’t now how to generalize it further. This is my code to do that:
a)Single pendulum:

void update()
{
  penAcc = (-gravity / lengh) * sin(angle);

  penVel += penAcc;
  angle += penVel;

  position.set(lengh*sin(angle), lengh*cos(angle));
  position.add(origin);   
  penVel *= damping;
}

b)Double pendulum:

 void double_pen()
{
//calculation of acceleration, velocity and position of double pendulum
counter[0] = -(g) * (2 * mas1 + mas2) * sin(a1);
counter[1] = -mas2 * (g)*sin(a1 - 2 * a2);
counter[2] = -2 * sin(a1 - a2) * mas2;
counter[3] = a2_vel * a2_vel * length2 + a1_vel * a1_vel * length1 * cos(a1 - a2);
den = length1 * (2 * mas1 + mas2 - mas2 * cos(2 * a1 - 2 * a2));
a1_acc = (counter[0] +  counter[1] + counter[2] * counter[3]) / den;

counter[4] = 2 * sin(a1 - a2);
counter[5] = (a1_vel * a1_vel * length1 * (mas1 + mas2));
counter[6] = (g) * (mas1 + mas2) * cos(a1);
counter[7] = a2_vel * a2_vel * length2 * mas2 * cos(a1 - a2);
den2 = length2 * (2 * mas1 + mas2 - mas2 * cos(2 * a1 - 2 * a2));
a2_acc = (counter[4] * (counter[5] + counter[6] + counter[7])) / den2;

position[0].x = length1 * sin(a1);
position[0].y = length1 * cos(a1);
position[0].add(origin); 
position[1].x = position[0].x + length2 * sin(a2);
position[1].y = position[0].y + length2 * cos(a2);

a1_vel += a1_acc;
a2_vel += a2_acc;
a1 += a1_vel;
a2 += a2_vel;

a1_vel *= 0.999;
a2_vel *= 0.999;
px2 = position[1].x;
py2 = position[1].y;
}

Hi @Rafsing,

I will have to take a look into it and check equations. It might take a couple of days but I will let you know if I get something. I know it is not trivial and I am not sure if possible to nth degrees because of the increasing complexity. Meanwhile, maybe try a third one

I found a formula for an nth degree pendulum, but firstly I don’t know if it’s good and secondly, I don’t know how to use it.

Hi @Rafsing,

After some research, I found that as you increase the number of pendulums on the system, the complexity of the equations also increases, and is non-trivial to deduct all the equations. I you have to calculate the position of each arm and derive to get the velocity and acceleration. The system becomes chaotic and trying to brute force the equations isn’t the proper way to go.
However, I found the following blog (the problem is the code being in python):

There is a method called Kane’s method for dynamic motion

Here is an interesting article on the topic:
https://www.researchgate.net/publication/267490975_Constrained_Multibody_Dynamics_With_Python_From_Symbolic_Equation_Generation_to_Publication

Which allows doing all calculations in the back. I haven’t found this same method for java but I don’t have much time either right now to look into this.
I just wanted to update you and point you in some direction that might be hopeful to you.

Best regards

Hi, if you want more precision, you could slow down the time. To slow down by, let’s say 10, just lower all the forces by factor 10. To lower all the forces by factor 10, just divide gravity by 10. It is the same effect as if you make 10 times smaller timesteps. You now have scaled time. If you want to try out, do this with one of your working simpler pendulums. This method works in all simulation systems.