# UndefVarError: orientation not defined

**URL:** <https://discourse.julialang.org/t/undefvarerror-orientation-not-defined/121364>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 16, 2024, 7:32am UTC](https://discourse.julialang.org/t/undefvarerror-orientation-not-defined/121364 "2024-10-16T07:32:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jay\_Gao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jay_gao/32/212251_2.png) [@jay\_Gao](https://discourse.julialang.org/u/jay_Gao)\
**Post date:** [October 16, 2024, 7:32am UTC](https://discourse.julialang.org/t/undefvarerror-orientation-not-defined/121364/1 "2024-10-16T07:32:06Z")

</div>

I am getting this error:

```julia
UndefVarError: orientation not defined

Stacktrace:
  [1] overdub
    @ .\In[31]:4 [inlined]
  [2] overdub(::Cassette.Context{MeshCat.var"##AnimationCtx#Name", Tuple{MeshCat.Animation, Int64}, Nothing, Cassette.var"##PassType#285", Nothing, Nothing}, ::typeof(visualize!), ::Visualizer, ::RBState{Float64})
    @ Cassette C:\Users\xxx\.julia\packages\Cassette\4UsSX\src\overdub.jl:0
  [3] overdub
    @ .\In[31]:18 [inlined]
  [4] overdub(::Cassette.Context{MeshCat.var"##AnimationCtx#Name", Tuple{MeshCat.Animation, Int64}, Nothing, Cassette.var"##PassType#285", Nothing, Nothing}, ::typeof(visualize!), ::Visualizer, ::Quadrotor{QuatRotation{Float64}, 9}, ::SVector{13, Float64})
    @ Cassette C:\Users\xxx\.julia\packages\Cassette\4UsSX\src\overdub.jl:0
 ...

```

Here is my code:

```julia
function visualize!(vis, x::RBState{<:Real})
    p = position(x)
    q = orientation(x)
    settransform!(robot, compose(Translation(p), LinearMap(UnitQuaternion(q))))
end

function visualize!(vis, model::Quadrotor, x::StaticVector)
    xbar = RBState(model, x)
    if model.ned
        r = position(xbar)
        v = linear_velocity(xbar)
        r = SA[r[1],-r[2],-r[3]]
        v = SA[v[1],-v[2],-v[3]]
        xbar = RBState(r, RotX(pi)*orientation(xbar), v, angular_velocity(xbar)) 
    end
    visualize!(vis, xbar)
end

```

How to solve it?  
Looking forward to your reply very much.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [October 16, 2024, 7:49am UTC](https://discourse.julialang.org/t/undefvarerror-orientation-not-defined/121364/2 "2024-10-16T07:49:03Z")

</div>

Your post lacks a lot of detail required to help you.

See [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)

---

<div class="post-metadata">

**Author:** ![jay\_Gao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jay_gao/32/212251_2.png) [@jay\_Gao](https://discourse.julialang.org/u/jay_Gao)\
**Post date:** [October 16, 2024, 8:18am UTC](https://discourse.julialang.org/t/undefvarerror-orientation-not-defined/121364/4 "2024-10-16T08:18:03Z")

</div>

I recently encountered the same error while running several examples.An error occurs when the orientation() function is called in some functions.Maybe I need to add a package.But which package does the orientation() function belong to?  
Thank you very much for your reply.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [October 16, 2024, 8:23am UTC](https://discourse.julialang.org/t/undefvarerror-orientation-not-defined/121364/5 "2024-10-16T08:23:18Z")

</div>

> [@jay\_Gao](#):
>
> I recently encountered the same error while running several examples

What examples are you talking about?

> [@jay\_Gao](#):
>
> But which package does the orientation() function belong to?

It’s impossible to know since you haven’t indicated at all what code you are trying to run and where it comes from.

---

<div class="post-metadata">

**Author:** ![jay\_Gao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jay_gao/32/212251_2.png) [@jay\_Gao](https://discourse.julialang.org/u/jay_Gao)\
**Post date:** [October 16, 2024, 9:14am UTC](https://discourse.julialang.org/t/undefvarerror-orientation-not-defined/121364/6 "2024-10-16T09:14:11Z")

</div>

Such as the Quadrotor. Ipynb example in TrajectoryOptimization.jl:  
[TrajectoryOptimization-Quadrotor.ipynb](https://github.com/RoboticExplorationLab/TrajectoryOptimization.jl/blob/main/examples/Quadrotor.ipynb)  
When I build the trajectory optimization problem and provide a dynamically-feasible initialization:

```julia
prob = Problem(model, obj, x0, tf, xf=xf, constraints=conSet)
initial_controls!(prob, U_hover)
rollout!(prob);

```

I get the error:

```julia
UndefVarError: orientation not defined

Stacktrace:

  [1] forces(model::Quadrotor{QuatRotation{Float64}}, x::SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true}, u::SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true})

    @ Main d:\Document\Julia\Vscode\TrajectoryOptimization.jl-main\examples\jl_notebook_cell_df34fa98e69747e1a8f8a730347b8e2f_X11sZmlsZQ==.jl:2

  [2] forces

    @ C:\Users\xxx\.julia\packages\RobotDynamics\OMNjA\src\rigidbody.jl:250 [inlined]

  [3] wrenches(model::Quadrotor{QuatRotation{Float64}}, x::SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true}, u::SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true}, t::Float64)

    @ RobotDynamics C:\Users\xxx\.julia\packages\RobotDynamics\OMNjA\src\rigidbody.jl:245

  [4] dynamics(model::Quadrotor{QuatRotation{Float64}}, x::SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true}, u::SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true}, t::Float64)

    @ RobotDynamics C:\Users\xxx\.julia\packages\RobotDynamics\OMNjA\src\rigidbody.jl:217

  [5] integrate(#unused#::RobotDynamics.RK4, model::Quadrotor{QuatRotation{Float64}}, x::SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true}, u::SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true}, t::Float64, h::Float64)

    @ RobotDynamics C:\Users\xxx\.julia\packages\RobotDynamics\OMNjA\src\integration.jl:281
...
    @ TrajectoryOptimization C:\Users\xxx\.julia\packages\TrajectoryOptimization\9vKEc\src\problem.jl:330

 [12] top-level scope

    @ d:\Document\Julia\Vscode\TrajectoryOptimization.jl-main\examples\jl_notebook_cell_df34fa98e69747e1a8f8a730347b8e2f_X26sZmlsZQ==.jl:6
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

```

Based on the error message, I looked at the force function.In this example, the force function is defined as follows:

```julia
function RobotDynamics.forces(model::Quadrotor, x, u)
    q = orientation(model, x)
    kf = model.kf
    g = model.gravity
    m = model.mass

    # Extract motor speeds
    w1 = u[1]
    w2 = u[2]
    w3 = u[3]
    w4 = u[4]

    # Calculate motor forces
    F1 = max(0,kf*w1);
    F2 = max(0,kf*w2);
    F3 = max(0,kf*w3);
    F4 = max(0,kf*w4);
    F = @SVector [0., 0., F1+F2+F3+F4] #total rotor force in body frame

    m*g + q*F # forces in world frame
end

```

I guess the error may come from q = orientation(model, x). But how to fix it?  
Here is the package I use:

```julia
using RobotDynamics, Rotations
using TrajectoryOptimization
using StaticArrays, LinearAlgebra

```

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [October 16, 2024, 9:28am UTC](https://discourse.julialang.org/t/undefvarerror-orientation-not-defined/121364/7 "2024-10-16T09:28:34Z")

</div>

Try `RobotDynamics.orientation`

---

<div class="post-metadata">

**Author:** ![jay\_Gao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jay_gao/32/212251_2.png) [@jay\_Gao](https://discourse.julialang.org/u/jay_Gao)\
**Post date:** [October 16, 2024, 9:43am UTC](https://discourse.julialang.org/t/undefvarerror-orientation-not-defined/121364/8 "2024-10-16T09:43:43Z")

</div>

This successfully solved my problem！Thank you very much.
