## Balancing ball PID

Typically: "How do I... ", "How can I... " questions
mariobros231
Posts: 3
Joined: 25 Nov 2019, 21:30

### Balancing ball PID

Hello! I have a problem with my project. How properly adjust a revolute joint which is attach to a frame/plate and ball is running on it? I want to control the ball with PID, so e.g. ball a lilttle bit on the right sight and revolute joint(as a motor) rotate -> ball returns in the middle of plate. Shall I use joint callback function for speed up simulation a lil bit?

coppelia
Posts: 7527
Joined: 14 Dec 2012, 00:25

### Re: Balancing ball PID

Hello Michal,

so you basically want to control the inclination of a plate in two directions, so as to control the position in X/Y of a ball on the plate?
For that, use something like:

Code: Select all

baseObject(static) --> revoluteJoint1 --> auxiliaryShape(dynamic) --> revoluteJoint2 --> plate(dynamic)
Make sure your joints are in force/torque mode, motor enabled, and controller enabled. A simple default PID controller should be enough. Then, use for instance a vision sensor above the plate, to identify the location of the ball on the plate (e.g. use a blob detection filter, that will return the x/y position of the blobs). You can also simply query its position via sim.getObjectPosition instead. Then use that position to adjust the target angular positions of your joints, via sim.setJointTargetPosition.

Cheers

mariobros231
Posts: 3
Joined: 25 Nov 2019, 21:30

### Re: Balancing ball PID

Thanks a lot for the answer!

According to your solution I have couple questions.

1. How to properly adjust the angular postion of revolute joint? Recieve data from sim.getObjectPosition(
position: table of 3 values (x, y and z)) and how pass it into sim.setJointTargetPosition?

2. And what is the best way to provide possibility to change position by user? E.g. move ball a lil bit to one of directions and then PID moves ball back into the center?

coppelia
Posts: 7527
Joined: 14 Dec 2012, 00:25

### Re: Balancing ball PID

Well, there you have plenty of possibilities. That's basically what your controller will have to do. There are many types of controllers you could implement for that task. The simplest is always a PID- (or even a simple P-) controller, maybe with an upper threshold.

To change the position of the ball by the user (i.e. instead of always keeping the ball centered on the plate), then you can simply introduce an offset to the ball position.

Cheers

mariobros231
Posts: 3
Joined: 25 Nov 2019, 21:30

### Re: Balancing ball PID

I have last question... want to control revolute joint using method sim.setJointTargetPosition and to use that method, I have to turn control loop in this revolute joint, but I wrote my custom PID in non threded child script. And I don't what to set hardcoded PID_P, PID_I. What I suppose to do?