-
Notifications
You must be signed in to change notification settings - Fork 191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to create a customized joint? #232
Comments
Im not sure exactly what you are looking for with moveable, but the things you can control are the properties on them. Maybe you could use max_bias, error_bias and max_force to restrict how quickly the joint controls its bodies? As for creating your own joint its possible, but not very convenient right now (no one has asked about it before). A joint basically has 4 functions, preStep, applyCachedImpulse, applyImpulse and getImpulse. These would need implementation. But since they are not in the Pymunk API, nor even private Pymunk API you would need to wrap the underlying c code, and then implement it. As a consequence the python joints would also be slower than their C counterparts, because of python and the cffi wrapping between c and python.. but maybe that would be ok for such specialized use cases? |
I thought customized joints were an interesting thing to try, so I put together a quick example in this branch: https://github.com/viblo/pymunk/tree/custom-joints The very very basics works, but I didnt have time to make a actual joint with it. Quickly looking at the existing joints at least I think a SimpleMotor joint would be possible to re-implement with this new CustomJoint. But overall, how useful it is im not sure of.. I have some memory that people tried to do this with the Chipmunk 2D library (which Pymunk is built on), but in the end I think it never turned out useful or was too much work to implement whatever effect they were after.. Given Im not convinced its something anyone will actually use to something useful I cant promise I will finalize and merge this into Pymunk master, but obviously the chance is higher if you (or someone else) find some use for it. |
Many thanks for your generous help! The example code is just what I need to continue. It looks like I need to write some C code if I want to create my ideal joints. As to why I want customized joints, the major reason is that the preset joints are too free for me. For instance, SimpleMotor and GearJoint allows the two bodies to have any relative positions. This freedom is very problematic in my code. Another reason comes from this tutorial. The rag doll example is really interesting, but the doll can't even stand without external forces applied to its joints connecting its arms and legs. It's quite natural to me to want to directly control the joints because that's what's been done in ISAAC SIM, a 3D simulator. My use case is indeed special. I'm trying to simulate robots in 2D simulator. Although the common practice is to simulate them in 3D simulators directly, 2D simulator such as pymunk is a lot simpler and faster. That's why I choose pymunk as the environment to test the correctness of my RL algorithms. But before that, I need a set of adjustable joints in order to build the action space. |
Just to double check, are you aware that you can have multiple joints between the same bodies? For example use both a SimpleMotor and a PinJoint to rotate a wheel but do not allow it to move , like its done in the video on pymunk.org homepage (code here: def car(space):) |
Well, I didn't know pymunk supports multiple joints between bodies until now. Sorry for that. Thanks a lot for the tip. This feature can solve most of my issues, but there are still some joints in my mind that can't be considered as combinations of the preset joints in pymunk. Namely GrooveJoint, SlideJoint and FixedJoint.
|
FixedJoint is easy to answer, instead of a joint you can add several shapes to the same body. This is one of the reasons its possible to set an offset when you create a Circle shape, or set the transform when you create a Poly shape. |
Thanks, it's a good solution but this method will break the premise that a constraint is a relationship between two bodies. I just figure out an alternative. A fixedJoint could be a combination of RotaryLinitJoint (set the min and max angles to 0) and a PivotJoint (set the pivot coordinate for the second body as (0, 0)). It will also work but leave the premise intact. |
Yeah, that could work. Note that something that might be important is
stability. Two shapes to the same body is as stable as a single shape and
the shapes will never move in relation to each other. However, constraints
work by adjusting the relative velocity of the two bodies, and are by
default less stable. You can see that in some of the pymunk examples if you
look carefully.
…On Wed, Sep 6, 2023, 12:31 xuanyaoming ***@***.***> wrote:
Thanks, it's a good solution but this method will break the premise that a
constraint is a relationship between two bodies.
I just figure out an alternative. A fixedJoint could be a combination of
*RotaryLinitJoint* (set the min and max angles to 0) and a *PivotJoint*
(set the pivot coordinate for the second body as (0, 0)). It will also work
but leave the premise intact.
—
Reply to this email directly, view it on GitHub
<#232 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAJD2CA3QNB5VTGKGBLI3K3XZBGJ5ANCNFSM6AAAAAA4J6QEPU>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
The instability comes from bias. I found setting error_bias to 0 and max_force to math.inf can generate stable enough results in my tests, although this setting will break the collision system sometimes. I guess I need to look into the collision mechanism next, but that's another topic. As to the topic of joints, I think what are missing is a velocity constraint and a position constraint. Most constraints are designed for angular properties, such as limiting the relative angles and keeping the relative angular speed fixed to a number. But no preset joints in pymunk provides tools for limiting the relative position or maintaining a relative speed between two bodies, to the best of my knowledge. I don't think PinJoint and PivotJoint are enough to do the job. Anyway, I guess my best option is to dive into C code and customize the engine to my needs. Thanks for your help, guidance and patience nevertheless. |
Hi. I browsed the page because I have similar issues with customized joints. I find Xuan's problem surprisingly similar to mine: My real-world model has two arms moving in reverse directions at the same speed, which are driven by a rotating gear in the center, but such mechanical interactions seem hard to be achieved using Pymunk. Thus I also need customized joints to control the relative speed between two robot arms. The methods I've tried include applying forces or impulses manually according to arm locations. It proved effective in simple cases but errors emerge when more running individuals are connected. I guessed that's because a fixed function of setting the driving forces or impulses only applies for a certain situation. For example, if a robot is connected to other two while running, the force needed to drive it correctly must be different from when it runs alone. If customized joints that control the relative velocity can be achieved(like how Simplemotor controls the relative angular velocity), I guess it would provide a good solution. I don't know if it's possible to write one based on Simplemotor but that seems worth trying. |
Im not sure I understand your simulation. Could you provide one or a couple of pictures to illustrate? Anyway, on top of my head I cant see a reason why there couldn't be a SimpleLinearMotor (or something).. I dont think it would be too difficult to write such a constraint. |
Hi, I'm working on 2D robot sim too, mostly focusing on walking robot. 1. Position servo joint.This approach based on simple motor by limiting max_force then control the rate using proportional control for example 2. Damped Rotary spring as motorIf the orecision is not critial, spring-damper is equivalent to PD control so you can set the stiffness and damp to reasonable value and then set rest_angle as desired angle, However max_force is not implement in the damped Rotary spring but you can work around as mention in https://github.com/viblo/pymunk/issues/241. However we can't control the max_rate directly is result in more extream locomotion like jumping. For now I successfully implement both method. this is show case for Position servo joint. After that I just want to know how robot learn to walk with just one leg and this result implement with damped rotary spring method I working on making github repo on this. If you interested you can join this project. This is example code for position servo.
|
Hi, thanks for your good work!
I'm trying to verify a reinforcement learning algorithm in a pymunk environment. But I found that almost all joints in pymunk are not moveable, which means I can't assign actions to them. Is there any way to modify the kinematic properties of joints during the simulation? For example. how to control the sliding speed for "Slide Joint" ?
If it's not possible to control the preset joints, how do I create my own customized joint in Python?
The text was updated successfully, but these errors were encountered: