Box2D learning: Balancing cars

Box2D is an open source physics engine developed by Erin Catto at Blizzard. There are many famous games using it, such as Angry Birds, Limbo, Tiny Wings, etc. Game engines like Unity and Cocos2d integrate it. Why did you want to learn Box2D? Because I want to create some intelligent agents in the virtual physical world. Although I have learned some basic implementations of force and motion in The Nature of Code before, collision and connection are not involved. We can certainly build our own wheels to do this, but by taking advantage of the existing Box2D engine and standing on the shoulders of giants, we can focus more on what we need to do to build intelligent Agents.

Segway

The best way to learn Box2D is to do a project to practice. The project was to build a balancing car in Box2D and use a control algorithm to keep it balanced and move to a given position on command. In this project, we abstracted the balance car into a simple model, consisting of a handle, a rod and a wheel. The wheel is controlled by an electric motor and can turn forward or backward. (Ideas and methods for the project come from Hardmaru’s blog)

create

The original Box2D was implemented in C++, using a version of Javascript in NOC, which made it easy to publish projects on the web, but also lost significant performance benefits. And since Box2D is a pure physics engine with no rendering capabilities, we use p5.js for rendering. The general idea is to create some basic elements, such as rectangles and circles, and add rendering methods and physics engine elements to them. Then assemble them together using connectors in Box2D, and don’t forget to set up a floor. The resulting balance car had no control over the wheels, so it just fell over.

I don’t want to go into too much code implementation detail here, but I recommend this site for Box2D usage and check the source code for details on balancing vehicle creation.

PID

How do I balance the balance cart? This requires the use of PID control algorithms that are widely used in various systems, such as balancing vehicles and unmanned aerial vehicles. Its main function is to adjust the system so that the value of a parameter in the system reaches your set value. PID represents three adjusting parts, proportion, integral and differential respectively, and the output value of the controller is the sum of these three.

In the discrete case, these three terms are expressed more simply:

And the final output is u t = P t + I t + D t “role=”presentation”>D t” role=”presentation”>For 1/30 of a second. For the balance vehicle, we set the target value as the Angle between the balance vehicle and the ground, and the PID control quantity is the increase or decrease of the wheel speed.

In the firstProportional controllerIts function is very straightforward, is how much error to reply to it. Performance in the flat very car is if the car to the left to control the wheel to run to the left, to the right to run to the right. Add K p “role=”presentation”>The value of can speed up the error reduction rate, but will produce oscillation.

Integral controllerAnd as you can see from the way it’s computed, what it does is it eliminates the total error of all the errors. K I “role=”presentation”> K I” role=”presentation”>The coefficients are set to 0.

Differential controllerEquivalent to adding damping, can make the oscillation fast decay, become stable. After we added differential control to the balance car, its stability was significantly increased.

There is no in-depth research on PID parameter tuning. Interested students can find relevant information by themselves. The next goal is how to move the balance car to the position we specify.

mobile

If you think about driving a balanced car, to reach your target, you need to first lean forward and accelerate. When you reach the maximum speed limit, keep your body basically upright and lean back to slow down as you approach your destination. So we want the velocity curve of the balancing car to move to its target position to look like this:



For initial acceleration, the TARGET PID value can be set to a certain Angle, and this Angle can be appropriately increased the further away from the target location. And then when you reach a speed limit, you set the target back to zero so that the equilibrium car is moving at a constant speed. Finally, according to the current speed, a position that the balancing vehicle will reach in the future is assumed. Since this position will pass the target position before the balancing vehicle reaches the target position, if the virtual position is used to calculate the target Angle, the effect equivalent to braking will be obtained.

The top curve in the GIF is the horizontal speed curve of the balancing vehicle. The red vertical line is where we want to go, and the green vertical line is where we think the balancer will go in the future.

So, this is the whole small project, interested students can go to the Demo page to play.


hertzcat

2018-09-01