Custom Hinge Applying Torque to achieve specific angle

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Custom Hinge Applying Torque to achieve specific angle

Postby SBlade » Thu Jan 19, 2017 3:41 am

Dear All,

I use Newton 2.35 version and I need to apply the torque which will rotate the object only as much as specified/demanded angle ,how am I supposed to do that? If apply the raw torque then the object continuosly rotate until it hits the max angle, so I can not achieve the desired effect.

Regards,
SBlade
 
Posts: 20
Joined: Thu Jan 12, 2017 5:42 am

Re: Custom Hinge Applying Torque to achieve specific angle

Postby JoeJ » Thu Jan 19, 2017 10:10 am

You can control the orientation of a single oject easily with torque:
1. Calculate the angular velocity needed to reach a given target in a given amount of time.
(This involves also things like initial accelerating and final breaking towards the target.)
2. Caluculate the torque that results in that angular velocity.

I could help out with the math, but you are the robotic arm guy so i guess it won't work.
The problem is a robotic arm means multiple bodies connected by multiple joints, thus step 2 becomes hard and you need some kind of solver to get the torques and forces to drive the whole system.

You should look if you can get motorized joints to work, they will calculate those things internally for you.
Not sure if 2.35 already had motorized joints (actuators) in the joint lib, but the interface was there and it was possible to build motorized joints with little work, just by setting joint acceleration.
For instance, to make a morozied hinge you would take the code from the joint lib and add some things in the SubmitConstraints function:
1. Calculate acceleration to get your target velocity.
2. Use NewtonUserJointSetRowAcceleration() so Newtons solver cares for it.

If you don't have any access to Newton source code inside your framework, the only way is to make your own solver (e.g. using Featherstone method), or look for a existing implementation.
User avatar
JoeJ
 
Posts: 1453
Joined: Tue Dec 21, 2010 6:18 pm

Re: Custom Hinge Applying Torque to achieve specific angle

Postby SBlade » Mon Jan 30, 2017 8:37 am

Probably I sholdn't be diving directly into how to do this how to do that, instead I should be familiarizing myself with code structure of Newton dynamics, so this what I've done so far. But there are some things that I couldn't understand and I'll appreciate the community help.

Regarding the Tutorial - 103 http://newtondynamics.com/wiki/index.php5?title=Tutorial_103:_-_Adding_Joints I think that it's a good example on how to construct the motorized hinges(with double pendulum, excluding the gravity and forcing teh bodies with acceleration). I set my scene exactly as is been instructed in the tutorial -103 but in my case the 2nd rigid body has it's own pin point in spatial space instead of connected to the 1st rigid body to form the 2 pendulum.

The first hinge(Hng1) is Parentless object which is bind to Kinematic Controller, the parent of 2nd hinge is 1st hinge rigid body and Parent is it's own rigid body, Parent of Kinematic Controller is the first object where I controll the movement ( I presume that this is the pascals wrapper way of implementing it) I studied the code in pascal wrapper but couldn't find what could be a miss for such a discrepancy ?

As a result 2 nd body is never getting connected to the first object, their movement is somehow interconnected but not the way that I want, I'll appreciate your help.

Regards,
SBlade
 
Posts: 20
Joined: Thu Jan 12, 2017 5:42 am

Re: Custom Hinge Applying Torque to achieve specific angle

Postby JoeJ » Tue Jan 31, 2017 3:31 am

You may want to look at StandartJoints.cpp in the sandbox project.
There are many setups with joints using the joint lib (no motors enabled but setup is the same), uncomment them at the bottom of the file.

There is also my implementation of a motorized ragdoll joint, enable by uncommenting:
//AddJoesPoweredRagDoll(scene, dVector(0.0f, 0.0f, -25.0f), 0.0f, 20);
AddJoesPoweredRagDoll(scene, dVector(0.0f, 0.0f, 5.0f), 1.5f, 4);
//AddJoesPoweredRagDoll(scene, dVector(0.0f, 0.0f, 15.0f), 0.0f, 4);
This is an example of implementing a joint on your own, without using the joint lib.

SBlade wrote:As a result 2 nd body is never getting connected to the first object, their movement is somehow interconnected but not the way that I want, I'll appreciate your help.


Sounds like the matrices are not right, all joints use a pair of local offset matrices to get from each bodies world transform to the point and orientation where the joint is defined.
Start with connecting two boxes with a Ball and Socket joint (without any limits), that's the easiest example.
User avatar
JoeJ
 
Posts: 1453
Joined: Tue Dec 21, 2010 6:18 pm

Re: Custom Hinge Applying Torque to achieve specific angle

Postby SBlade » Tue Jan 31, 2017 7:16 am

JoeJ wrote:Sounds like the matrices are not right, all joints use a pair of local offset matrices to get from each bodies world transform to the point and orientation where the joint is defined.
Start with connecting two boxes with a Ball and Socket joint (without any limits), that's the easiest example.

In order to coduct controlled test, I tested them individually each box / sphere / capsule/etc.. there is not a problem they work exactly as expected, furthermore to make sure that there is not any wrong implementation in pascal wrapper code ( code shown below), I directly checked the code which calls the external Newton DLL codes.

I also noticed that at the tutorial first hinge is created with the following code
Code: Select all
frownyHinge = CreateCustomJoint6DOF (&matrix[0][0], &matrix[0][0], frownyBody, NULL);
But in my code I use CreateCustomHinge for both of hinges, could that be the source of discrepancy?


Can you confirm that setting the parent / child arguments of hinges is enough to make the interrelated (double) pendulum? So I'll not waste my effort on already proven things and concentrated on other parts.

Code: Select all
procedure BuildCustomHinge(itemIndex: Integer);
  var
    pinAndPivot: TMatrix;
    bso: TGLBaseSceneObject;
  begin
    { Newton wait from FPinAndPivotMatrix a structure like that:
      First row: the pin direction
      Second and third rows are set to create an orthogonal matrix
      Fourth: The pivot position

      In glscene, the GLBaseSceneObjects direction is the third row,
      because the first row is the right vector (second row is up vector). }
    with NewtonJoint.Items[itemIndex] as TNGDJoint do
      if Assigned(FParentObject) and Assigned(FChildObject) then
      begin
        bso := TGLBaseSceneObject.Create(FManager);
        bso.AbsolutePosition := FCustomHingeOptions.FPivotPoint.AsVector;
        bso.AbsoluteDirection := FCustomHingeOptions.FPinDirection.AsVector;
        pinAndPivot := bso.AbsoluteMatrix;
        pinAndPivot[0] := bso.AbsoluteMatrix[2];
        pinAndPivot[2] := bso.AbsoluteMatrix[0];
        bso.Free;

        FNewtonUserJoint := CreateCustomHinge(@pinAndPivot,
          GetBodyFromGLSceneObject(FChildObject),
          GetBodyFromGLSceneObject(FParentObject));

        HingeEnableLimits(FNewtonUserJoint, 1);
        HingeSetLimits(FNewtonUserJoint,
          VectorGeometry.DegToRad(FCustomHingeOptions.FMinAngle),
          VectorGeometry.DegToRad(FCustomHingeOptions.FMaxAngle));
        CustomSetBodiesCollisionState(FNewtonUserJoint, Ord(FCollisionState));
        NewtonJointSetStiffness(CustomGetNewtonJoint(FNewtonUserJoint), FStiffness);
      end;
  end;     
SBlade
 
Posts: 20
Joined: Thu Jan 12, 2017 5:42 am

Re: Custom Hinge Applying Torque to achieve specific angle

Postby Julio Jerez » Tue Jan 31, 2017 10:16 am

I also noticed that at the tutorial first hinge is created with the following code
But in my code I use CreateCustomHinge for both of hinges, could that be the source of discrepancy

CreateCustomHinge is the best choice. you should use it whenever you can, the other is for case when people ty to make joints that can change on the fly, although this is not important either because on those cases you can make your own joint like joe said.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Custom Hinge Applying Torque to achieve specific angle

Postby SBlade » Tue Jan 31, 2017 10:33 am

Thanks for clarification, eliminated one step

Can you confirm that setting the parent / child arguments of hinges is enough to make the interrelated (double) pendulum?

??
SBlade
 
Posts: 20
Joined: Thu Jan 12, 2017 5:42 am

Re: Custom Hinge Applying Torque to achieve specific angle

Postby SBlade » Sat Feb 04, 2017 9:25 am

Probably it is better to describe what I'm trying to achieve with figure, is it possible to create such hinges with Newton standart hinges v2.29. If I attempt to create that with standart hinges, the m1 and m2 objects have their own individual pivot point which is point of swing, this doesn't conform with what I'm trying to achieve, I want to hook second hinge to the m1 object. Is it possible to do that ?

Image

It seems that I should manipulate the modelview matrix of M2 object to be referenced as relative to M1 object and compose/modify the modelview matrix.

Pseudo code is as follow, IMHO setting the relative position must be enough to attach to 1st object, but that needs to be called for each timestep of Newton, your gudance will be apprecated.

Code: Select all
var
  relative_pos = M2_pos - M1_pos
FNewtonJoint := NewtonConstraintCreateHinge(FNewtonWorld,
          @(relative_pos),     @(FHingeOptions.FPinDirection.AsVector),        GetBodyFromGLSceneObject(FChildObject),   GetBodyFromGLSceneObject(FParentObject));
SBlade
 
Posts: 20
Joined: Thu Jan 12, 2017 5:42 am

Re: Custom Hinge Applying Torque to achieve specific angle

Postby JoeJ » Sat Feb 04, 2017 10:50 am

SBlade wrote:I want to hook second hinge to the m1 object. Is it possible to do that ?


Yes, you can do what is shown in the figure.

SBlade wrote:Pseudo code is as follow, IMHO setting the relative position must be enough to attach to 1st object, but that needs to be called for each timestep of Newton, your gudance will be apprecated.


This may be the origin of your confusion: It is NOT necessary to do anything per timestep.

At setup you do this:
Create the bodies at their inital positions in worldspace.
Create the joints given the pinAndPivot matrix also in worldspace, the Joint constructor will calculate the local offset matrix for each body and store it.

At runtime you do nothing, but Newton calls the SubmitConstraints member function of each joint.
Here it reads the actual bodies worldspace matrices, multiplies them with the stored local matrices to get the actual joint positions in worldspace.
User avatar
JoeJ
 
Posts: 1453
Joined: Tue Dec 21, 2010 6:18 pm

Re: Custom Hinge Applying Torque to achieve specific angle

Postby SBlade » Sun Feb 05, 2017 4:22 am

With respect to what you say, there doesn't seem to be anything to be abnormal, I've done it exactly as you said, I have created a short video demo what I succeeded up to now and what I want to achieve, on screen description.

http://gph.is/2kG5QIG
SBlade
 
Posts: 20
Joined: Thu Jan 12, 2017 5:42 am

Re: Custom Hinge Applying Torque to achieve specific angle

Postby JoeJ » Sun Feb 05, 2017 6:30 am

Make sure the last column af your matrices is (0,0,0,1).
That kind of buggy behaviour reminds me of that.

But if so, what happens if you create a BallAndSocket instead Hinge with the same parameters,
does this work as expected?
User avatar
JoeJ
 
Posts: 1453
Joined: Tue Dec 21, 2010 6:18 pm

Re: Custom Hinge Applying Torque to achieve specific angle

Postby JoeJ » Sun Feb 05, 2017 6:39 am

But i'm also confused about the hirarchy, i guess there is a (buggy) hinge between blue and red body, but there seems something more holding at least one of them somehow (one additional hinge with null parent?).

Maybe it helps if you post setup code.
User avatar
JoeJ
 
Posts: 1453
Joined: Tue Dec 21, 2010 6:18 pm

Re: Custom Hinge Applying Torque to achieve specific angle

Postby SBlade » Sun Feb 05, 2017 12:37 pm

Make sure the last column af your matrices is (0,0,0,1).
That kind of buggy behaviour reminds me of that.

Good spot, I also have experienced the same erratical attitude in vector and matrix math, I 'll try to remedy it.

Situation for ball and socket is worser because it shakes frenziedly, please see the video at the following link
http://i.giphy.com/l3q2NhPFz1IU1ovIc.gif

But i'm also confused about the hirarchy, i guess there is a (buggy) hinge between blue and red body, but there seems something more holding at least one of them somehow (one additional hinge with null parent?).

There is nothing unusal about it, I created them , as I depicted in the ball and socket video, those are the pivot points of blue sphere and red box, respectively.

And this was exactly what confuses me, if there is parent / child relation then what is pivot point for? Normally and intuitively If there is parent for the joint I would expect the child to be attached to the parent and omit the pivot point, if there is not parent, then pivot point should come into play and child object should be attached to it. Am I wrong? That leads to fact that Pascal implementation is wrong of at least need a little bit refctoring.

Here is the code reponsible for hinge creation, it is in pascal but I hope its tangible since naming convention is quite definitive.

Code: Select all
  procedure BuildHinge(itemIndex: Integer);
  begin
    with NewtonJoint.Items[itemIndex] as TNGDJoint do
      if Assigned(FParentObject) and Assigned(FChildObject) then
      begin
        FNewtonJoint := NewtonConstraintCreateHinge(FNewtonWorld,
          @(FHingeOptions.FPivotPoint.AsVector),
          @(FHingeOptions.FPinDirection.AsVector),
          GetBodyFromGLSceneObject(FChildObject),
          GetBodyFromGLSceneObject(FParentObject));
        NewtonJointSetCollisionState(FNewtonJoint, Ord(FCollisionState));
        NewtonJointSetStiffness(FNewtonJoint, FStiffness);
      end;
  end;   


I'll also will testify whether the Pascal implentation assigns the correct values in matrices or not, but it will take time,
SBlade
 
Posts: 20
Joined: Thu Jan 12, 2017 5:42 am

Re: Custom Hinge Applying Torque to achieve specific angle

Postby JoeJ » Sun Feb 05, 2017 1:12 pm

hinge.png
hinge.png (6.09 KiB) Viewed 7903 times


SBlade wrote:And this was exactly what confuses me, if there is parent / child relation then what is pivot point for?


The pivot is the point where the joint connects the two bodies.
The image shows two bodies (Parent and Child) and the pivot with positions (plus 1 to indicate a point as 4th. number).
Also there you see the Pin direction (plus 0 to indicate a vector).

Hope this eliminates confusion about terminology.
Note that all bodies must have been created already at their initial position when you create the joint.

I see your wrapper takes pivot and direction, which differs from matrices which i'm used to from C++,
but this does not matter.

It may matter that you have the 4th number right, similar to the 4th column for matrices.
The gifs you show look like it is an issue about that, but i can't be sure.
Your AsVector() may be the root of all evil, e.g. if it returns a pointer but only 3 numbers make sense and the 4th is random memory.

One more thing: If you want to render the pivot correctly for moving bodies, you have to store the local offset for both or at least one bodies.
Or, if you have access to the joint member variables, the local matrices are axactly that.

Local offsets would be (3,0,0) for Parent, and (-3,0,0) for Child.
So to render the pivot you would need to transform this vector by the body matrix to get it's actual world space position.
Making this work should show what really happens here.

Also note, if you have two bodies like in my example it does not matter which one is parent or child.
This only matters if you want a joint between only one body and the static world (only parent can be zero).
User avatar
JoeJ
 
Posts: 1453
Joined: Tue Dec 21, 2010 6:18 pm

Re: Custom Hinge Applying Torque to achieve specific angle

Postby SBlade » Sun Feb 05, 2017 3:35 pm

OK thanks, but all this almost proves that its not doable with hinges/custom hinges because I can not have variable pivot point, am I wrong ?

Please take a look at the following, which is my goal, press run button
http://www.tapdancinggoats.com/double-pendulum
SBlade
 
Posts: 20
Joined: Thu Jan 12, 2017 5:42 am

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 17 guests

cron