So I'm trying to learn some control theory for a little project I'm working on and decided I'd try the examples of pendulum and cart one can find all over the internets, and I'm following this post and this website and this post on the wolfram blog

I know the mechanics "relatively" well and I decided to do slightly differently than on those pages and use the Lagrange 2nd form to build my ode like so:

xx = x[t];
xy = 0;
px = l Cos[\[Phi][t]] + xx;
py = l Sin[\[Phi][t]] + xy;
params = {m1 -> 1, m2 -> 0.5, l -> 0.85, g -> -9.8};

T1 = 1/2 (m1 (D[xx, t]^2 + D[xy, t]^2)) (*cart*);
T2 = 1/2 (m2 (D[px, t]^2 + D[py, t]^2))(*pend*);
V1 = -m1 g xy (*cart*);
V2 = -m2 g py (*pendulum*);


T = FullSimplify[T1 + T2];
V = FullSimplify[V1 + V2];
L = FullSimplify[T - V] ;

eqn1 = FullSimplify[D[D[L, \[Phi]'[t]], t] - D[L, \[Phi][t]]];
eqn2 = FullSimplify[D[D[L, x'[t]], t] - D[L, x[t]]];

deqns = {eqn1 == 0, eqn2 == f[t]} ;
ics = {\[Phi][0] == (85 \[Pi])/180, \[Phi]'[0] == 0, x[0] == 0, x'[0] == 0};
sol = NDSolve[{{deqns /. params, ics} /. {f[t] -> 0}}, {\[Phi][t], x[t]}, {t, 0, 30}];
pen1 = {l Cos[\[Phi][t]], l Sin[\[Phi][t]]} /. sol /. params

With this plot as a result for the pendulum and cart respectively:

ParametricPlot[pen1, {t, 0, 2}, PlotRange -> All, ImageSize -> Medium, PlotLegends -> "Expressions", PlotStyle -> Dashed] Plot[x[t] /. sol, {t, 0, 3}]

newplot2

So far everything is fine, Though is is the beginning of my problem, please note I have used g -> -9.8 and potential energies also negative to get this to plot correctly.

Afterwards, I have started make the SSM and the TFM like so:

model = StateSpaceModel[deqns /. params, {{\[Phi][t], \[Pi]/2}, {\[Phi]'[t], 0}, {x[t], 0}, {x'[t], 0}}, f[t], \[Phi][t], t]

tfmodel = FullSimplify[TransferFunctionModel[model]] /. params

tfmodel

And now things I don't understand anymore:

As you can see in the picture the first component in the denominator is negative. Which, surely breaks causality...

Attempting to Plot the OutputResponse on the homogenous side gives me:

Plot[{OutputResponse[tfmodel /. params , UnitStep[1 t], {t, 0, 60}]}, {t, 0, 30}, Evaluated -> True, PlotRange -> All, PlotTheme -> "Scientific", GridLines -> Automatic, ImageSize -> Medium, PlotPoints -> 200] 

wrongplot

Which to me seems completely wrong. But, I carry on to try and create a control scheme regardless:

nmodel = model /. params
q = DiagonalMatrix[{1, 1000, 10, 1000}];
r = {{1}};
gains = LQRegulatorGains[nmodel, {q, r}];
controlmodel = SystemsModelStateFeedbackConnect[nmodel, gains];
Plot[OutputResponse[{controlmodel, {0, 0, 1, 0}}, {0}, {t, 40}] // Evaluate, {t, 0, 20}, PlotRange -> All, PlotStyle -> {Blue, Red}, Epilog -> Inset[Column[{Style["x(t)", 10, Blue], Style["\[Theta](t)", 10, Red]}], {3, 0.004}], AxesLabel -> {"t"}]

And this as a result:

poor

Which besides some obvious poor drift, seems like a plausible result...

Until I change in the params, g -> 9.8 Here all parts of the TFM are now positive and the final plot looks more like one that is actually under a control scheme, However the parametric plot is now broken, as if gravity is now forcing the pendulum upwards

control

I must be missing something here, because there are two problems I don't understand,

One: What gives with g? When plotting the curve, One must absolutely have a negative gravity for the pendulum to fall down with, This reflects well in the parametric plot

Two: If this is simply a definition problem...why are my TFM, SSM, and in the end LQR regulator all requiring a different for lack of better word, direction of g?

What exactly am I misunderstanding here? Are these plots even correct? Are they only showing the deviation at pi/2 that I defined in the SSM?

A bonus question would be: How do I plot the x-cart like in the linked examples...although I nearly one for one copied the code. I can't get x[t] to plot parallel with phi[t] which would greatly help in determining of the control scheme is doing anything correctly.

xplot

Thanks in advance for the help!

edit I have updated the initial plot with labels like suggested, the inner red arrow is the initial angle displacement of the simple undampened pendulum, 85pi/180 rads, in mathematical positive direction, the second outer red arrow is the direction I expect the system to fall with a negative gravity...Which it does like expected.

  • The first thing you need to do is to create a diagram showing what you mean by x, y, and $\theta$. That will probably help you answer your own question. And your ParametricPlot is essentially that of a simple pendulum, not an inverted one. – Suba Thomas Oct 15 at 2:49
up vote 2 down vote accepted

Make the following changes:

  • $g\to 9.8$ in params
  • $V1= m1 g \ xy$
  • $V2= m2 g \ py$

And then finally do

Plot[StateResponse[{controlmodel, {0, 0, 1, 0}}, {0}, {t, 40}]//Evaluate, 
{t, 0, 20}, PlotRange -> All, PlotLabels -> {\[Phi], \[Phi]', x, x'}]

to get

enter image description here

The above (and the result of OutputResponse[tfmodel /. params...]) are the deviations from the equilibrium values. So $\phi$ is now zero at the positive y axis and positive in the counter clockwise direction.

  • Hi! thank you for the answer!, however I can't agree with your changes. You're right paraplot IS a simple pendulum, The sys is displaced in math. pos. direction changing that to math. neg. in ics and g to positive will model the inverse system. One that hangs at 3pi/2 that falls upwards and a negative tfm...meaning that a pos. right x movement will result in a pos. right fall from the pendulum..which physically can't happen...lizard-truth.com/wp-content/uploads/2018/10/newp-2.png See here. Atleast thats how I understand my system and your answer, did I misunderstand? My bad if so! – morbo Oct 15 at 13:33
  • I don't fully understand your comment above. But in ParametricPlot the y axis in positive upwards, in your convention the y axis is positive downwards. You would be better off plotting $\{l \cos (\phi (t)),-l \sin (\phi (t))\}$. – Suba Thomas Oct 15 at 13:46
  • Mmm. Plotting like your suggested gives me the inverse system of the plot...I'm confident my y is defined postive...Unless I'm drastically misundestanding how I mechanically defined my system. (could very well be!) lizard-truth.com/wp-content/uploads/2018/10/updown.png the change you suggested plots y in the negative direction. (the orange)..Which is not how my system is defined. The y component in the L vector most certainly should be l*sin[phi[t]]. My apologies If I'm being dumb here or something here....but I can't see how -l is a possibility at all. – morbo Oct 15 at 14:06
  • We are probably talking past each other. As per my first suggestion, add a diagram showing the mass and pendulum, and your positive x and y axis, and $\theta$. – Suba Thomas Oct 15 at 14:12
  • Ah, that could very well be! I updated the post, and here is a direct link of my system. i.stack.imgur.com/OpQI9.png – morbo Oct 15 at 14:20

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.