Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

How to code servo with a gyro?!?


#1

Greetings to everyone. I have a question. I have a gyro and a servo motor that I’m needing to control based upon the angular output from the gyro. To put it simply, if you can imagine a bucket on a bobcat with an auto leveling feature. the bucket will maintain a parallelism with the earth based upon the upward and downward movement of the bucket. That way the dirt doesn’t dump out of the bucket when you move it. I’m meeting to do that but I’m needing to do it in a 360 degree rotational application where the bucket maintains a parallelism with the Earth through 360 degrees of movement. Right now I have the arm, the DC servo motor, and the bucket all hooked up, I just need to know how to program the gyro angles to maintain is parallelism with the Earth and loop it continuously. Any help would be greatly appreciated give me the way to go, but I’m very new to this so I’m not even sure exactly what that means. Please advise. Thank you.


#2

Hello.

We spoke on the phone earlier today. I recommend getting started by reading this Wikipedia page on PID controllers and this section of our 3pi robot user’s guide, which also describes how PID control works. Then, to see an example of what that kind of controller looks like in an Arduino sketch, I recommend reviewing the code in the main loop of our LineFollowing example sketch for our Zumo 32U4 robot. The method and structure in these examples should be similar, even though the actual application is different. If you have any questions after looking through those things, or have any trouble writing your control loop, feel free to post your code here (the simplest version that you think should work, but does not), and I would be happy to take a look.

-Jon


#3

Hey Jon. Great. I will do that. Thanks in advance.


#4

Will I even need the maestro for this application? Or just the 32u4 and the mpu6050?


#5

Since the A-Star can generate RC servo pulses, you could directly control your servo from that instead of adding a Maestro to your system. For a single servo, the Arduino servo library might be good enough for some applications as long as you don’t mind a little jitter. However, if you want better resolution and stability, especially if you want to control multiple servos, it might then be worth it to add the Maestro into your system since it is very well optimized for that task.

-Jon


#6

How would that be connected in terms of output and input wires?

Can I still use the gyro data fed to the astar and then send the TX out to the maestro RX?


#7

A gyro alone is useless for this application, because it measures the rate of rotation, not angles. An accelerometer can be used to measure tilt angles, as the acceleration due to gravity defines “down”. That won’t work well if the accelerometer is in motion, so in addition, gyros are used to correct for motion induced errors.

You should work to understand these concepts completely, and implement a simple tiltmeter, before doing anything else in the project. Alternatively, consider using an absolute orientation sensor, like the BNO055, which combines an accelerometer, a gyro and a magnetometer.


#8

hello Jim. Thank you for the information as it is greatly appreciated. However, let me back up a little bit (as I was a little vague in my description of what I was trying to accomplish). I know that the gyro does not measure angles, and, subsequently, needs a accelerometer in order to do so. I am utilizing the mpu6050 (which is an accelerometer / gyro / temp sensor) and I written a sketch that post out all six axis in any way shape and form you can possibly imagine. Angles included. So I know what my XYZ angles are, and I just need to know how to process and digest them in order to maintain this parallelism. I guess the best way to describe it would be a gimbal single-axis along the x axis. That being said, do you have any suggestions? Maybe I can use a PID in conjunction with the MPU6050 data that’s being posted out? I’m just trying to figure out what that code looks like. I just downloaded the PID library so maybe I’ll be able to find something in there that will help. I’m all ears


#9

If you can reliably measure the two tilt angles roll and pitch, then in principle you need two PID loops to correct them.

You probably don’t need the PID library to start understanding the basic principles. If both roll and pitch are defined as zero when the platform is perfectly level, then the simplest correction is just the proportional term of the full PID calculation:

servo_angle = Kp*tilt_angle; //for each of roll and pitch

Adjust Kp (and its sign) by experiment. If the platform oscillates, add in the other K terms or use the full PID library. Google “PID tuning” for advice on choosing the K constants.


#10

Hello Mr. Remington. The roll is a constant measurement as it is mechanically secured in that axis and cannot deviate from it’s orientation. The only axis (I think) that I should be calculating is the x axis and maintaining that parallelism with the Earth. Question: the term “tilt angle” in your code-qgerw does that come from?


#11

And one other question. How do you code or define a continuously rotating servo speed versus a 180° servo?


#12

Also it might be worth nothing that the servos are continuously rotating servos, and I don’t believe that they can accept angle input data. How does this change the aforementioned code that you provided?


#13

You have not described the electromechanical part of your project.

Without that and without a clear explanation for how the servo mechanism should control the orientation of the bucket, I doubt anyone can offer advice.


#14

Sure. Please see the attached sketch and advise. Thanks Jim. VID_20181216_181902.3gp (1015.8 KB)


#15


#16

The drawing suggests that no motor, sensor or electronics are needed.

If the bucket hangs on bearings from a support axle directly above its center of gravity, it will automatically level itself.


#17

if you look closer, you can see that I put 5 volt input to a single servo motor at the top of the page. I also denoted the servo location and direction of servo rotation as well. Let me send you another picture of the actual unit.


#18

See pics. The bucket is not shown in this pic.


#19

VID_20181216_190111.3gp (370.5 KB)


#20

I just sent you some pics and a video. Right now I’m using a pololu a * 3 to you four and a mpu6050 to try and accomplish what I need to do. I also have a maestro mini 6 channel servo controller I can use as well.