0
$\begingroup$

I simulated a spherical shaped robot (Snippet-a). I used Snippet-b to generate the Graphics using manipulate.

  1. How to save this as an animation to a video file?
  2. PlotRange command is not working in Snipped-b. No matter the changes I made it still plots x-axis and y-axis between -10 to 10. Is this a bug?

snippet-a

r = 1; kp = 100; kv = 10; \[Theta] = 0; xdes = 2; ydes = 1; pos = {1, 
  1, 1}; los = pos/Norm[pos];
Rdes = RotationMatrix[\[Theta], los];
Pdes = {xdes, ydes, 0};
R = {{r11[t], r12[t], r13[t]}, {r21[t], r22[t], r23[t]}, {r31[t], 
   r32[t], r33[t]}}; r11d = D[r11[t], t]; 
r12d = D[r12[t], t]; 
r13d = D[r13[t], t]; 
r21d = D[r21[t], t]; 
r22d = D[r22[t], t]; 
r23d = D[r23[t], t]; 
r31d = D[r31[t], t]; 
r32d = D[r32[t], t]; 
r33d = D[r33[t], t]; 
xd = D[x[t], t];
yd = D[y[t], t];
w = {w1[t], w2[t], w3[t]};
what = {{0, -w3 [t], w2[t]}, {w3[t], 0, -w1[t]}, {-w2[t], w1[t], 0}};
w1d = D[w1[t], t]; w2d = D[w2[t], t]; w3d = D[w3[t], t];
e1 = {1, 0, 0}; e2 = {0, 1, 0}; e3 = {0, 0, 1};
J = DiagonalMatrix[{.3, .4, .5}];
(*Fff=Cross[Transpose[R].e3,w]+0.5 \
(Cross[w,Transpose[R].e3]+Inverse[J].(Cross[w,J.Transpose[R].e3]-\
Cross[J.w,Transpose[R].e3]));*)
wdes = r*(R\[Transpose].e3) - los;(*Omega desired*)
P = {x[t], y[t], r};
Pbar = R\[Transpose].(P - Pdes) - r*los;
dpsi = wdes\[Cross]Pbar;
ew = w - wdes;
Fpd = -Inverse[J].(kp*dpsi + kv*ew);
Fff = (R\[Transpose].e3)\[Cross]w*r + wdes\[Cross]w + 
   1/2*(w\[Cross]wdes + 
      Inverse[J].(w\[Cross](J.wdes) - (J.w)\[Cross]wdes));
(*Fff=(R\[Transpose].e3)\[Cross]w+1/2 \
(w\[Cross](R\[Transpose].e3)+Inverse[J].(w\[Cross](J.R\[Transpose].e3)\
-(J.w)\[Cross](R\[Transpose].e3)));
Fpd=-Inverse[J].(kp r R\[Transpose].(x[t]*e2-y[t]*e1)+kv*(w-R\
\[Transpose].e3));*)
F = -Inverse[J].(w\[Cross](J.w)) + Fff + Fpd;
Rd = R.what;
dae2 = {r11d == Rd[[1, 1]], 
   r12d == Rd[[1, 2]], 
   r13d == Rd[[1, 3]], 
   r21d == Rd[[2, 1]], 
   r22d == Rd[[2, 2]], 
   r23d == Rd[[2, 3]], 
   r31d == Rd[[3, 1]], 
   r32d == Rd[[3, 2]], 
   r33d == Rd[[3, 3]], 
   xd == r*(r21[t]*w1[t] + r22[t]*w2[t] + r23[t]*w3[t]),
   yd == -r*(r11[t]*w1[t] + r12[t]*w2[t] + r13[t]*w3[t]),
   w1d == F[[1]], w2d == F[[2]], w3d == F[[3]],
   w1[0] == 0, w2[0] == 0, w3[0] == 0,
   r11[0] == 1, r12[0] == 0, r13[0] == 0,
   r21[0] == 0, r22[0] == (0.5)^0.5, r23[0] == -(0.5)^0.5,
   r31[0] == 0, r32[0] == (0.5)^0.5, r33[0] == (0.5)^0.5,
   x[0] == 9, y[0] == 9};
simtime = 4
sol = NDSolve[
  dae2, {r11, r12, r13, r21, r22, r23, r31, r32, r33, x, y, w1, w2, 
   w3}, {t, 0, simtime}, MaxSteps -> 10000000]; 
plotb = ParametricPlot3D[{Evaluate[x[t]] /. First[sol], 
    Evaluate[y[t]] /. First[sol], 0}, {t, 0, simtime}, 
   PlotRange -> {{-10, 10}, {-10, 10}, {0, 3*r}},
   AxesLabel -> {Style[x, FontWeight -> "Bold", FontSize -> 48], 
     Style[y, FontWeight -> "Bold", FontSize -> 48]},
   ImageSize -> 700, 
   BaseStyle -> {FontWeight -> "Bold", FontSize -> 24}, 
   PlotStyle -> {Thick, Hue[0.7]}];

snipped-b

Manipulate[Show[plotb, Graphics3D[
   {Sphere[{Evaluate[x[t]] /. First[sol], 
      Evaluate[y[t]] /. First[sol], r}, r],
    Thickness[0.01], Red, 
    Line[{{Evaluate[x[t]] /. First[sol], Evaluate[y[t]] /. First[sol],
        r}, {Evaluate[x[t]] /. First[sol], 
        Evaluate[y[t]] /. First[sol], r}
       + 2*
        r*{Evaluate[r11[t]] /. First[sol], 
         Evaluate[r21[t]] /. First[sol], 
         Evaluate[r31[t]] /. First[sol]}}],
    Thickness[0.01], Green, 
    Line[{{Evaluate[x[t]] /. First[sol], Evaluate[y[t]] /. First[sol],
        r}, {Evaluate[x[t]] /. First[sol], 
        Evaluate[y[t]] /. First[sol], r}
       + 2*
        r*{Evaluate[r12[t]] /. First[sol], 
         Evaluate[r22[t]] /. First[sol], 
         Evaluate[r32[t]] /. First[sol]}}],
    Thickness[0.01], Blue, 
    Line[{{Evaluate[x[t]] /. First[sol], Evaluate[y[t]] /. First[sol],
        r}, {Evaluate[x[t]] /. First[sol], 
        Evaluate[y[t]] /. First[sol], r}
       + 2*
        r*{Evaluate[r13[t]] /. First[sol], 
         Evaluate[r23[t]] /. First[sol], 
         Evaluate[r33[t]] /. First[sol]}}],
    Thick, Black, Line[{{0, -3, 0}, {0, 3, 0}}],
    Thick, Black, Line[{{-5, 0, 0}, {5, 0, 0}}],
    Thick, Blue, Line[{{xdes, ydes, r + 3}, {xdes, ydes, 0}}],
    Thick, Red, Line[{{xdes, ydes, r}, {xdes + 5, ydes, r}}],
    Thick, Green, Line[{{xdes, ydes, r}, {xdes, ydes + 5, r}}],
    Thick, 
    Red, {Arrowheads[.03], 
     Arrow[{{xdes, ydes, r}, {xdes, ydes, r} + 
        2.5*Evaluate[ Rdes.e1 /. First[sol]]}]},
    Thick, 
    Green, {Arrowheads[.03], 
     Arrow[{{xdes, ydes, r}, {xdes, ydes, r} + 
        2.5*Evaluate[ Rdes.e2 /. First[sol]]}]},
    Thick, 
    Blue, {Arrowheads[.03], 
     Arrow[{{xdes, ydes, r}, {xdes, ydes, r} + 
        2.5*Evaluate[ Rdes.e3 /. First[sol]]}]},
    Thick, 
    Black, {Arrowheads[.03], 
     Arrow[{{Evaluate[x[t]] /. First[sol], 
        Evaluate[y[t]] /. First[sol], 
        r}, {Evaluate[x[t]] /. First[sol], 
         Evaluate[y[t] /. First[sol]], r} + 
        1.2*Evaluate[ {{r11[t], r12[t], r13[t]}, {r21[t], r22[t], 
              r23[t]}, {r31[t], r32[t], r33[t]}}.pos /. 
           First[sol]]}]}}, Axes -> True, AxesLabel -> {x, y}, 
   BaseStyle -> {FontWeight -> "Bold", FontSize -> 18}, 
   PlotRange -> {{-1, 7}, {-1, 7}, {0, 10*r}}, ImageSize -> 900]], {t,
   0, simtime}] 
$\endgroup$
1
$\begingroup$

Question 1: "How to save this as an animation to a video file?"

This one is pretty easy. Simply Export your Manipulate to a gif or AVI or similar.

Export["question.gif", %, AnimationRepetitions->Infinity]

enter image description here

Instead of % here, you could place your entire Manipulate.

(You can also do "question.avi" and so on. The AnimationRepetitions is simply so the gif repeats more than once.)


Question 2: PlotRange command is not working in Snipped-b. No matter the changes I made it still plots x-axis and y-axis between -10 to 10. Is this a bug?

You have wrapped your Graphics3D in a Show, to show both the ball and your plots. However, you have not given the PlotRange to the Show - only to the Graphics3D. The Show then overrides this. You can solve this by passing the display options to the Show instead:

Manipulate[
 Show[plotb, 
  Graphics3D[{Sphere[{Evaluate[x[t]] /. First[sol], 
      Evaluate[y[t]] /. First[sol], r}, r], Thickness[0.01], Red, 
    Line[{{Evaluate[x[t]] /. First[sol], Evaluate[y[t]] /. First[sol],
        r}, {Evaluate[x[t]] /. First[sol], 
        Evaluate[y[t]] /. First[sol], r} + 
       2*r*{Evaluate[r11[t]] /. First[sol], 
         Evaluate[r21[t]] /. First[sol], 
         Evaluate[r31[t]] /. First[sol]}}], Thickness[0.01], Green, 
    Line[{{Evaluate[x[t]] /. First[sol], Evaluate[y[t]] /. First[sol],
        r}, {Evaluate[x[t]] /. First[sol], 
        Evaluate[y[t]] /. First[sol], r} + 
       2*r*{Evaluate[r12[t]] /. First[sol], 
         Evaluate[r22[t]] /. First[sol], 
         Evaluate[r32[t]] /. First[sol]}}], Thickness[0.01], Blue, 
    Line[{{Evaluate[x[t]] /. First[sol], Evaluate[y[t]] /. First[sol],
        r}, {Evaluate[x[t]] /. First[sol], 
        Evaluate[y[t]] /. First[sol], r} + 
       2*r*{Evaluate[r13[t]] /. First[sol], 
         Evaluate[r23[t]] /. First[sol], 
         Evaluate[r33[t]] /. First[sol]}}], Thick, Black, 
    Line[{{0, -3, 0}, {0, 3, 0}}], Thick, Black, 
    Line[{{-5, 0, 0}, {5, 0, 0}}], Thick, Blue, 
    Line[{{xdes, ydes, r + 3}, {xdes, ydes, 0}}], Thick, Red, 
    Line[{{xdes, ydes, r}, {xdes + 5, ydes, r}}], Thick, Green, 
    Line[{{xdes, ydes, r}, {xdes, ydes + 5, r}}], Thick, 
    Red, {Arrowheads[.03], 
     Arrow[{{xdes, ydes, r}, {xdes, ydes, r} + 
        2.5*Evaluate[Rdes.e1 /. First[sol]]}]}, Thick, 
    Green, {Arrowheads[.03], 
     Arrow[{{xdes, ydes, r}, {xdes, ydes, r} + 
        2.5*Evaluate[Rdes.e2 /. First[sol]]}]}, Thick, 
    Blue, {Arrowheads[.03], 
     Arrow[{{xdes, ydes, r}, {xdes, ydes, r} + 
        2.5*Evaluate[Rdes.e3 /. First[sol]]}]}, Thick, 
    Black, {Arrowheads[.03], 
     Arrow[{{Evaluate[x[t]] /. First[sol], 
        Evaluate[y[t]] /. First[sol], 
        r}, {Evaluate[x[t]] /. First[sol], 
         Evaluate[y[t] /. First[sol]], r} + 
        1.2*Evaluate[{{r11[t], r12[t], r13[t]}, {r21[t], r22[t], 
              r23[t]}, {r31[t], r32[t], r33[t]}}.pos /. 
           First[sol]]}]}}], Axes -> True, AxesLabel -> {x, y}, 
  BaseStyle -> {FontWeight -> "Bold", FontSize -> 18}, 
  PlotRange -> {{-1, 7}, {-1, 7}, {0, 10*r}}, ImageSize -> 900], {t, 
  0, simtime}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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