# Building a balancing robot with differential drive

I've already built a two wheeled balancing robot using some continuous rotation servos and an accelerometer/gyroscope. I upgraded the servos to some geared DC motors with 8-bit encoders with the goal having the robot drive around while balancing.

I'm kind of stuck on how to program it to drive around while still balancing. I think one way would be to just have the control input to the motors act sort of like pushing it. So the robot would be momentarily unbalanced in the direction I want it to travel. That seems kind of clumsy to me though. There must be a better way of doing? I think I need to combine the dynamic model for the balancer with the differential drive but this is a bit beyond the control theory that I know.

Update From Anorton's answer I have a good looking state matrix now.

Now about pole placement: The A matrix will will have to be 4x4 based on the new state vector. And B will then have to be a 4x2 matrix since I can only control the left/right wheel torque (u = 2x1 vector).

I may need to read more about this but is there a systematic way to determine the A matrix by pole placement? It seems to me for this example and even more complicated examples, determining A by guess and check would be very difficult.

Update #2 After a bit of reading I think I understand it now. I still need the dynamics of the robot to determine the A matrix. Once I have that I can do the pole placement using matlab or octave.

• What type of approach are you using/what control theory do you know? I can write up one approach, but it could be super basic or super advanced, depending on your background... :) – apnorton Mar 10 '13 at 21:46
• Thanks! for the old design with the servos I was calculating the inclination angle using data from the gyro/acceleromter. Sending that to a Kalman filter and then a PID for the servo position. I'm good with differential equations and okay with linear algebra. I'm still pretty new to controls so maybe the basic one? Or both if you're willing. I'm always interested in learning more :) – JDD Mar 11 '13 at 2:39
• I only have one approach, so my prior comment was really saying it could be taken both ways (either super advanced if you were just starting off with robots, or super basic if you have some knowledge of controls)... Based on your prior comment, I have a feeling I'm outranked. :) My controls knowledge is very slim, but I'll throw my $0.02 worth in there... – apnorton Mar 11 '13 at 3:34 • Welcome to Robotics JDD, it's better to edit additional information into your question rather than adding it as a comment, that way comments can be tidied up (deleted) later and they will stop distracting readers from the question itself. – Mark Booth Mar 12 '13 at 13:00 ## 1 Answer Disclaimer: I have never done this myself, but only have seen a description of it being done through Georgia Tech's "Control of Mobile Robotics" on Coursera. My knowledge of controls is spotty, too. Thus... take this with a grain of salt. :) To keep the robot upright (and still), you're trying to stabilize (send to$0$) the state$x$, where: $$x=\left[\begin{array}{c}\text{leftVelocity} \\ \text{rightVelocity} \\ \text{angle from vertical}\end{array}\right]$$ However, when this system is stable, the left and right wheel velocities will be$0$. So, we want an offset for the target speed: $$x_{new}=\left[\begin{array}{c}\text{leftVelocity}-\delta_L \\ \text{rightVelocity}-\delta_R \\ \text{angle from vertical}\end{array}\right]$$ Where$\delta$is the target speed for either side. When this system is stabilized, the robot will be upright, and will have each wheel rotating at a desired speed. This is the basic approach/outline. I'll edit this with some more details tomorrow (and actual math, etc.), but I wanted to at least post the general idea now. (It's late in my time zone, and I have an early class to get to.) EDITED: Oh goodness. So, I just looked back at the slides relating to this in the Coursera course (Section 4, slide 29). You may want to go and enroll in that class just to download that slide set... :) The hard part is computing the$A$and$B$matrices (it's a big linearization mess). Anyway, you want to make your state matrix as follows (not as above--my memory wasn't exactly right): $$x=\left[\begin{array}{c}v \\ \omega \\ \phi \\ \dot \phi\end{array}\right]$$ Where$v$is the velocity of the segway,$\omega$is the rotational velocity (how fast the robot is pivoting) and$\phiis the angle from vertical. We want to have a desired velocity, so let's define a new state vector: \begin{align}\tilde x &= x - \left[\begin{array}{c}v_d \\ \omega_d \\ 0 \\ 0 \end{array}\right]\\ &= x - \delta \end{align} Where\delta$is the desired velocity and rotational quantity (as seen above). Differentiating: $$\dot{\tilde x} = \dot x - \dot \delta \underset{\text{\delta is constant}}{=} \dot x$$ Thus, we have $$\dot{\tilde x} = Ax + Bu$$ Where$A$is the standard coeff matrix$B$is the input matrix, and$u$is the control vector. We can then say: $$\dot{\tilde x} = A(x - \delta) + Bu + A\delta$$ But, based on the solution for$A$(from Coursera),$A\delta = 0\$. Thus: $$\dot{\tilde x} = A\tilde x+ Bu$$

Now you can use pole placement to determine the correct eigenvalues, etc.

• Awesome thanks! I did that same Coursera class too we're probably not too far off in controls knowledge. This is turning out to be a much harder project than I though it was going to be. I like your state matrix so far though. – JDD Mar 11 '13 at 11:05
• Eek! It appears that I'm not answering this tonight! I was on spring break last week, and forgot about something that was due, so that kept me busy today. I'll edit it tomorrow... – apnorton Mar 12 '13 at 3:04
• No problem. I blew up my last h-bridge yesterday trying to get it put together so I'll have to wait for a replacement. – JDD Mar 13 '13 at 1:30
• Thanks a lot Anorton for the help. I think I'm understanding how to do this a lot more now. I updated my original question. How to determine the A and B matrices is still confusing me though. – JDD Mar 13 '13 at 11:21