|
| 1 | +# ## RNE Visualization |
| 2 | + |
| 3 | +using |
| 4 | + GLMakie, |
| 5 | + LinearAlgebra, |
| 6 | + StaticArrays, |
| 7 | + VMRobotControl |
| 8 | + |
| 9 | +X = SVector{3, Float64}(1.0, 0.0, 0.0) |
| 10 | +Y = SVector{3, Float64}(0.0, 1.0, 0.0) |
| 11 | +Z = SVector{3, Float64}(0.0, 0.0, 1.0) |
| 12 | + |
| 13 | +T1 = zero(Transform{Float64}) |
| 14 | +J1 = Revolute(Y, T1) |
| 15 | + |
| 16 | +T2 = Transform(Z) |
| 17 | +J2 = Revolute(Y, T2) |
| 18 | + |
| 19 | +T3 = Transform(Z) |
| 20 | +J3 = Rigid(T3) |
| 21 | + |
| 22 | +mech = Mechanism{Float64}("2Link") |
| 23 | +cart_frame = add_frame!(mech, "Cart") |
| 24 | +L2_frame = add_frame!(mech, "L2") |
| 25 | +EE_frame = add_frame!(mech, "EE") |
| 26 | + |
| 27 | +add_joint!(mech, J1; parent="root_frame", child=cart_frame, id="J1") |
| 28 | +add_joint!(mech, J2; parent=cart_frame, child=L2_frame, id="J2") |
| 29 | +add_joint!(mech, J3, parent=L2_frame, child=EE_frame; id="J3") |
| 30 | + |
| 31 | +# Then, we add a coordinate to the mechanism to represent the tip of the pendulum, and a |
| 32 | +# coordinate to represent the position of the cart. These are used to add point masses to |
| 33 | +# the mechanism. |
| 34 | +add_coordinate!(mech, FrameOrigin(EE_frame); id="tip_pos") |
| 35 | +add_coordinate!(mech, FrameOrigin(cart_frame); id="cart_pos") |
| 36 | +add_component!(mech, PointMass(1.0, "cart_pos"); id="cart_mass") |
| 37 | +add_component!(mech, PointMass(1.0, "tip_pos"); id="pendulum_mass") |
| 38 | + |
| 39 | +# We compile the mechanism, and setup an ODE problem to simulate the dynamics of the rail robot. |
| 40 | +m = compile(mech) |
| 41 | +cache = Observable(new_inverse_dynamics_cache(m)) |
| 42 | + |
| 43 | + |
| 44 | +begin |
| 45 | +t = 0.0 |
| 46 | +q = [0.0, π/2] |
| 47 | +q̇ = zero_q̇(m) |
| 48 | +q̈ = zero_q̈(m) |
| 49 | +g = SVector{3, Float64}(0.0, 0.0, -9.81) |
| 50 | +inverse_dynamics!(cache[], t, q, q̇, q̈, g) |
| 51 | + |
| 52 | +fig = Figure() |
| 53 | +display(fig) |
| 54 | +ls = LScene(fig[1, 1]; show_axis=false) |
| 55 | +robotsketch!(ls, cache; linewidth=3) |
| 56 | + |
| 57 | +frameIDs = get_compiled_frameID.((cache[],), frames(cache[])) |
| 58 | +tfs = map(cache) do cache |
| 59 | + tfs = map(id -> get_transform(cache, id), frameIDs) |
| 60 | +end |
| 61 | +positions = map(tfs) do tfs |
| 62 | + map(tf -> Point3f(origin(tf)...), tfs) |
| 63 | +end |
| 64 | +fs = map(cache) do cache |
| 65 | + fs = map(id -> VMRobotControl.get_frame_force(cache, id), frameIDs) |
| 66 | +end |
| 67 | +f_scale = map(fs) do fs |
| 68 | + 0.1/norm(maximum(fs)) |
| 69 | +end |
| 70 | +τs = map(cache) do cache |
| 71 | + map(id -> VMRobotControl.get_frame_torque(cache, id), frameIDs) |
| 72 | +end |
| 73 | + |
| 74 | +arrows!(ls, positions, fs; lengthscale=f_scale, arrowsize=0.1, color=:red, ) |
| 75 | +arrows!(ls, positions, τs; lengthscale=f_scale, arrowsize=0.1, color=:red, ) |
| 76 | + |
| 77 | + |
| 78 | +end |
| 79 | + |
| 80 | +using Test, Random |
| 81 | +Revise.includet("../test/inverse_dynamics_test.jl") |
| 82 | +@testset test_inverse_dynamics(m) |
| 83 | + |
| 84 | +# function create_rotated_cylinder_mesh(N1, N2, r1, r2, ϕ) |
| 85 | +# for θ2 = LinRange(0, ϕ, N2) |
| 86 | +# pᶜ = Point3f(r2*cos(θ2), r2*sin(θ2), 0) |
| 87 | +# for θ1 = LinRange(0, 2π, N1) |
| 88 | +# points = Point3f() |
| 89 | +# end |
| 90 | + |
| 91 | +# end |
| 92 | +# end |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +# module_path = joinpath(splitpath(splitdir(pathof(VMRobotControl))[1])[1:end-1]) |
| 97 | +# savepath = joinpath(module_path, "docs/src/assets/rail_robot.mp4") |
| 98 | +# animate_robot_odesolution(fig, sol, cache, savepath; f_setup=animate_f_setup, f_control=animate_f_control); |
| 99 | + |
| 100 | +# ```@raw html |
| 101 | +# <video controls width="100%" height="auto" autoplay loop> |
| 102 | +# <source src="../../assets/rail_robot.mp4" type="video/mp4"> |
| 103 | +# </video> |
| 104 | +# ``` |
0 commit comments