PID Stabilizer

by TheFriends in Circuits > Robots

736 Views, 5 Favorites, 0 Comments

PID Stabilizer

IMG_3072.JPG
IMG_3067.JPG
1D PID Stabilizer
2D PID Stabilizer

A PID stabilizer is a device which uses input from the system conditions to balance something (like a ball) on a one- or two-dimensional platform. PID stands for Proportional, Integral, and Derivative, and is a continuous feedback loop which controls a complex output (i.e. the tilt of the platform) using a simple input (the position of the ball).

We used a specialized camera to detect the position of the ball, a Raspberry Pi to run the PID loop, and servos (one for the one dimensional model, three for two dimensional) to control the tilt of the platform. The one dimensional platform was made of balsa wood, and the two dimensional platform of acrylic.

Supplies

FOR ONE-DIMENSIONAL MODEL:

1x Raspberry Pi (and a monitor, keyboard, and mouse)

1x Pixy2 Camera

1x Servo

1x Servo HAT with power cord (allows Raspberry Pi to control servos)

12" x 4'' 1/8" Balsa Wood (or from a local hobby store)

1x 1" x 1" x 10' 80/20 Aluminum

10x Fixed corner brackets for 80/20

10x Single nut with button head for corner brackets

1x Ping pong ball (MUST be brightly colored)

Some super glue (this or similar)

FOR TWO-DIMENSIONAL MODEL:

1x Raspberry Pi (and a monitor, keyboard, and mouse)

1x Pixy2 Camera

3x Servo

1x Servo HAT with power cord (allows Raspberry Pi to control servos)

3x Servo horns (MUST be 64mm/2.5")

1x Servo extension cable pack

1x Circular platform (10" diameter)

4x Plastic Beams

6x Square nuts (get them at a hardware store - not worth it to buy online)

6x 6-32 Screws (get them at a hardware store - not worth it to buy online)

6x 6-32 Washers (get them at a hardware store - not worth it to buy online)

1x 1" x 1" x 10' 80/20 Aluminum

1x 1" x 1" x 3' 80/20 Aluminum

12x Fixed corner brackets for 80/20

3x Corner brackets for 80/20

15x Single nut with button head for corner brackets

1x Ping pong ball (MUST be brightly colored)

Some super glue (this or similar)

Set Up Raspberry Pi and Camera

2022-05-23-151259_1920x1080_scrot.png

Make sure that your Raspberry Pi is fully updated and running the newest operating system. Follow these instructions to allow the Pi to communicate with the camera; follow these instructions to allow the Pi to communicate with servos.

Next set up the camera: follow these instructions to install PixyMon, the program which shows a live feed from the camera. Follow these instructions to teach the camera to recognize your ping pong ball. You should use a brightly colored ball which will stand out against the background. We found that placing white poster paper down for a clean background helped stop the camera from detecting things other than the ball. You should also play around with the sensitivity and brightness to find a balance where the camera always detects the ping pong ball but never accidentally detects anything else.

Build 80/20 Frame

67217525058__AC4BA6B8-6AA2-4864-B2F8-79D85CCA9510.JPG

For One-Dimensional Model:

Cut your 10 feet of 80/20 into:

1x 20"

4x 17"

1x 15"

1x 10"

1x 5"

This comes out to 118", so you will have two inches left over.

Make a square out of the four 17" pieces. Before you use a corner bracket to secure each corner, you'll want to insert any sliding nuts you may need into the inner surfaces of the square; otherwise you would have to disassemble the frame to put them in later. Besides the four nuts used on the corners, you will need one each on any two opposite sides (you should now have used six out of the total ten). Next, attach the 15" piece to the middle of one of the sides that you put an extra nut in earlier; it should reach almost to the other side, but about an inch short. While it's open, slide in a nut each on the top and on one side for future use. That is because the 20" piece goes in that spot, vertically upward. Attach it both to the original square using the other extra nut, and to the 15" piece with another nut and corner bracket. Next, attach the 10" piece to the top of the 20" piece, so that it is hanging over the 15" piece. It doesn't really matter if it is attached on top or on the inner side of the 20" piece. Finally, the 5" piece sticks straight up from the middle of the 15" piece; attach it to the side with one corner bracket and to the top with another, using the nuts you put in earlier. The servo will be attached to the top of this piece (see step 3).

This completes the frame. All of the 80/20 (except for the extra 2") and brackets and nuts should have been used.

The camera can now be mounted to the side of the 10" piece, pointing down. Put it on the opposite side of the 5" piece; this will ensure that the platform is close to the middle of the camera's field of view. The camera comes with attachments, but they are not well positioned for mounting on 80/20. We contrived a rather complex mount using the materials that come with the camera, but you can also use duct tape or super glue - if you use glue, make sure the angle and field of view is perfect before securing it.

For Two-Dimensional Model:

Cut your 3' piece of 80/20 into:

1x 20"

1x 16"

Cut your 10' piece of 80/20 into:

4x 17"

1x 16"

1x 15"

1x 10"

This comes out to 109", so you will have 11" left over.

Make a square out of the four 17" pieces, as in the pictures above. Set aside the three smooth corner brackets for now, and only use the twelve fixed brackets (they don't rotate). Before you use a corner bracket to secure each corner, you'll want to insert any sliding nuts you may need into the inner surfaces of the square; otherwise you would have to disassemble the frame to put them in later. Besides the four nuts used on the corners, you will need three each on any two opposite sides (you should now have used ten out of the total twelve). Next, attach the 15" piece to the middle of one of the sides that you put the extra nuts in earlier (use the middle one); it should reach almost to the other side, but about an inch short. While it's open, slide in a nut on top for future use. Next, attach the two 16" pieces, one on each side of the 15" piece. They also each get a nut on top. Don't tighten them too much, because you'll need to adjust the position later. Next, attach the 20" piece vertically upward in the gap between the 15" piece and the edge of the square. Attach it both to the original square using the last extra nut, and to the 15" piece with another nut and corner bracket. Next, attach the 10" piece to the top of the 20" piece, so that it is hanging over the 15" piece. It doesn't really matter if it is attached on top or on the inner side of the 20" piece. One servo each will be attached to the 15" and the two 16" pieces, via the three remaining corner brackets, which are smooth and can rotate (see step 3).

The camera can now be mounted to the side of the 10" piece, pointing down. It comes with attachments, but they are not well positioned for mounting on 80/20. We contrived a rather complex mount using the materials that come with the camera, but you can also use duct tape or super glue - if you use glue, just make sure the angle and field of view is perfect before securing it.

This completes the frame. All of the brackets and nuts and all but 11" of the 80/20 should have been used.

Build Platform

IMG_3071.JPG
IMG_3065.JPG

For One Dimensional Model:

Cut the balsa wood so that you have three 2" x 12" rectangles. (We used two, and glued them at a 90º angle, but this required some extra effort to attach the servo, and it doesn't really matter if there is some extra room for the ball to move off axis). Super glue them together to make a trough shape, but don't attach the flat piece all the way at the bottom of the side pieces; leave about half an inch for the side pieces to hang down below the flat piece.

Next, super glue the circular servo horn to one side, in the center, so that the pivot point is exactly even with the flat piece of the trough. This is why we left extra room below the flat piece.

Attach the servo to its position on the 80/20 (duct tape is fine for this) and attach the platform to the servo via the servo horn (see step 4).

For Two-Dimensional Model:

Mark off the acrylic circle into thirds so you can make three equidistant dots around the circumference, for the position of the three legs. Next, cut one of your plastic beams in the following manner: you want to make three short pieces of plastic with one hole each. First, cut the ends off to get two pieces - it doesn't matter that one of their sides is rounded - and then cut off one more hole as well. IMPORTANT: Make sure you have as much room 'below' the hole as possible; these pieces will be glued to the underside of the acrylic, and if there is not enough room between the hole and the acrylic, the nut and/or washer may not fit. For this reason, it is probably best to sacrifice the second-from-the-end holes to make your first two pieces as long as possible, and then use a middle hole for the last piece so it can also be as long as possible.

At this point, you should have three small pieces of plastic, each with at least one flat side. Make sure that side is perfectly flat - you can sand it in a vice or use a hobby knife if your original cut was not perfect. Once that is done, rough up the bottom surface of each piece as well as the three spots on the acrylic (with coarse sandpaper or a hobby knife), to give the super glue a better bonding surface. Glue them on, and let them sit for 24 hours (preferably with some sort of clamp) to cure.

While the glue is curing, you can prepare the legs. You should have three remaining plastic beams, and each leg will consist of one beam and one of the three 2.5" servo horns. They will be joined via a screw, washer, and nut; you will need to drill into the last hole of each servo horn and make it larger so the screw will fit. The last hole on the other end of the plastic beams will each be connected in the same manner to the small pieces which are glued to the platform.

Once the glue has cured, you can attach the legs, so that they dangle down from the platform. IMPORTANT: Make sure they are all oriented the same way ours are in the picture above. If you're looking at a leg from outside the circle, the opening of the servo horn should be on the left. ALSO IMPORTANT: Keep the screws somewhat loose; the joints need to have a bit of play so they can bend off axis (since the platform can tilt in any direction). If the joints are too tight, the super glue bond may fail.

At this point, you will need to specify the position of the servos and hence the position of the two 16" pieces of 80/20 that they will be secured to. First, super glue each of your three remaining corner brackets (the smooth ones) to a servo. Make sure that the servo is right side up, and leave a little space so that the bracket can be secured to the 80/20 without the servo touching the 80/20. If the servo is flush with the bottom of the bracket, attaching the bracket to the 80/20 will stress and potentially break the super glue bond.

Next, attach one of the servo-bracket pieces to the middle piece of 80/20 (the 15" piece) facing sideways (see picture). The others can be measured off of this one. Aligning the other two servos can be tricky. You can just use geometry, but that would be putting a lot of faith into the precision of your gluing, and we recommend a more direct approach. Try attaching one leg to the servo that is already attached, and holding the platform level so you can mark where the other legs hang down (this works a lot better with two people). Slide the two 16" pieces to accommodate for the positions of the servos, and attach them using the brackets. Now attach all three legs to their respective servos, and make sure that none are experiencing any stress. Adjust the position and angle of the servos if needed. The platform should be able to balance by itself.

Make It Stabilize

For One-Dimensional Model:

Now that you’ve built the frame and platform, you’re ready to make it into a stabilizer! Use this link to visit our GitHub page, where you can find the code you’ll need. Download the files PID_1D.py and 1D_RESET.py.

Wait! Before you run PID_1D, make sure (using 1D_RESET) that your servo is in its 'zero' position before attaching the platform.

In order to run PID_1D, we had to move it into the ./pixy2/build/python_demos/ folder, so it can access the data from the camera. Run PID_1D using your IDE of choice. Our Raspberry Pi uses Thonny. Dig into the PID_1D file and play around with the variables labeled ‘cP’, ‘cI’, and ‘cD’. Changing these values will change the function of the stabilizer.

As you've probably guessed, these are the constants for each of the PID terms. cP controls the response to the current position of the ball. cI controls the response to the past position of the ball (basically the average position of the ball over the last several frames); this term isn't very necessary here, as your ball is unlikely to spend a lot of time on one side without crossing back to the other. pD controls the response to the velocity of the ball (basically the difference between the position a little while ago and the position now); this term is the most important for the stabilizer, and will be the one that you fiddle with most. Note: it must be negative for the stabilizer to function properly.

Can you make the platform more responsive to sudden motions of the ball? Can you make the platform move very smoothly? Use the 1DRESET file to return the platform to its equilibrium position.

For Two-Dimensional Model:

Now that you’ve built the frame and platform, you’re ready to make it into a stabilizer! Use this link to visit our GitHub page, where you can find the code you’ll need. Download the files PID_2D.py and 2D_RESET.py.

Wait! Before you run PID_2D, make sure (using 2D_RESET) that your servo is in its 'zero' position before attaching the legs. You MUST attach them at something close to a 45º angle INWARDS so that they are underneath the platform; this is how the code was designed to run, and it will not work correctly if you attach the legs upright or bent outward.

In order to run PID_2D, we had to move it into the ./pixy2/build/python_demos/ folder, so it can access the data from the camera. Run PID_2D using your IDE of choice. Our Raspberry Pi uses Thonny. Dig into the PID_2D file and play around with the variables labeled ‘cP’, ‘cI’, and ‘cD’. Changing these values will change the function of the stabilizer.

As you've probably guessed, these are the constants for each of the PID terms. cP controls the response to the current position of the ball. cI controls the response to the past position of the ball (basically the average position of the ball over the last several frames); this term isn't very necessary here, as your ball is unlikely to spend a lot of time in just one area of the platform. cD controls the response to the velocity of the ball (basically the difference between the position a little while ago and the position now); this term is the most important for the stabilizer, and will be the one that you fiddle with most. Note: it must be negative for the stabilizer to function properly. Be careful with cD, as it can be responsible for more sudden jerky motion which can break the super glue bonds between the legs and the platform.

Can you make the platform more responsive to sudden motions of the ball? Can you make the platform move very smoothly? Use the 2D_RESET file to return the platform to its equilibrium position.