|
| 1 | +using DyadBotComponents |
| 2 | +using WGLMakie |
| 3 | +using ModelingToolkit |
| 4 | +using OrdinaryDiffEq |
| 5 | + |
| 6 | +t_end = 100 |
| 7 | +@mtkcompile bot = DyadBotComponents.CascadeControlledFlatDyadBot() |
| 8 | +prob = ODEProblem(bot, [bot.x_ref => 0.75], (0,t_end)) |
| 9 | +sol = solve(prob) |
| 10 | + |
| 11 | +plot(sol; idxs=[bot.ref.output.u, bot.plant.x]) |
| 12 | +plot(sol; idxs=bot.plant.theta) |
| 13 | + |
| 14 | + |
| 15 | +function get_figure() |
| 16 | + |
| 17 | + defs = ModelingToolkit.defaults(bot) |
| 18 | + |
| 19 | + r = defs[bot.plant.R] #wheel radius |
| 20 | + y1 = r |
| 21 | + |
| 22 | + L = defs[bot.plant.L] # body length |
| 23 | + y2 = y1 + L |
| 24 | + |
| 25 | + w1 = r*0.5 |
| 26 | + w2 = r*2 |
| 27 | + |
| 28 | + |
| 29 | + f = Figure(size = (600, 600); aspect=DataAspect()) |
| 30 | + ax = Axis(f[1, 1]) |
| 31 | + |
| 32 | + |
| 33 | + # wheel |
| 34 | + base_transform = Transformation(origin = Vec3d(0.0, y1, 0)) |
| 35 | + spoke_transform = Transformation(base_transform; origin = Vec3d(0.0, y1, 0)) |
| 36 | + linesegments!(ax, [-r,+r], [y1,y1]; transformation=spoke_transform, color=:blue) |
| 37 | + linesegments!(ax, [0.0,0.0], [y1-r,y1+r]; transformation=spoke_transform, color=:blue) |
| 38 | + poly!(ax, Circle(Point2f(0.0,y1), r); color=:transparent, strokecolor=:blue, strokewidth=2, transformation=spoke_transform) |
| 39 | + |
| 40 | + # body |
| 41 | + body_transform = Transformation(base_transform; origin = Vec3d(0.0, y1, 0)) |
| 42 | + poly!(ax, Rect(0.0-w1/2, y1, w1, y2-y1); color=:transparent, strokecolor=:red, strokewidth=2, transformation=body_transform) |
| 43 | + poly!(ax, Rect(0.0-w2/2, y2, w2, w2); color=:transparent, strokecolor=:green, strokewidth=2, transformation=body_transform) |
| 44 | + |
| 45 | + # frame |
| 46 | + poly!(ax, Rect(-3/2, 0 , 3, 3); color=:transparent) |
| 47 | + |
| 48 | + return f, base_transform, spoke_transform, body_transform |
| 49 | +end |
| 50 | + |
| 51 | +f, base_transform, spoke_transform, body_transform = get_figure() |
| 52 | + |
| 53 | +function animate(;framerate = 30, filename="dyadbot.mp4") |
| 54 | + |
| 55 | + nframes = 500 |
| 56 | + times = range(0, t_end, length=nframes) |
| 57 | + |
| 58 | + translate!(base_transform, 0.0) |
| 59 | + rotate!(body_transform, 0.0) |
| 60 | + rotate!(spoke_transform, 0.0) |
| 61 | + |
| 62 | + record(f, filename, times; |
| 63 | + framerate) do t |
| 64 | + x = sol(t; idxs=bot.plant.x) |
| 65 | + theta = sol(t; idxs=bot.plant.theta) |
| 66 | + |
| 67 | + # x = wheel*r |
| 68 | + |
| 69 | + translate!(base_transform, x) |
| 70 | + rotate!(body_transform, -theta + π) |
| 71 | + rotate!(spoke_transform, -x/r) |
| 72 | + end |
| 73 | + |
| 74 | +end |
| 75 | + |
| 76 | +animate(; framerate=5, filename = "dyadbot_slow.mp4") |
| 77 | +animate(; framerate=30, filename = "dyadbot_fast.mp4") |
| 78 | + |
0 commit comments