Skip to content

Commit

Permalink
add quadrotor example
Browse files Browse the repository at this point in the history
  • Loading branch information
lyg1597 committed Mar 5, 2024
1 parent 479e9ce commit ce6129f
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
68 changes: 68 additions & 0 deletions demo/dryvr_demo/dryvr_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Example agent.
from typing import Tuple, List

import numpy as np
from scipy.integrate import odeint

from verse.agents import BaseAgent
from verse.map import LaneMap
from verse.parser import ControllerIR

class QuadrotorAgent(BaseAgent):
def __init__(self, id, code = None, file_name = None):
self.decision_logic: ControllerIR = ControllerIR.empty()
self.id = id
self.init_cont = None
self.init_disc = None
self.static_parameters = None
self.uncertain_parameters = None

def dynamics(self, x, t):
x1, x2, x3,\
x4, x5, x6,\
x7, x8, x9,\
x10, x11, x12, u1 = x

u2 = 0
u3 = 0

g = 9.81
R = 0.1
l = 0.5
Mrotor = 0.1
M = 1
m = M+4*Mrotor

Jx = 2/5*M*R**2+2*l**2*Mrotor
Jy = Jx
Jz = 2/5*M*R**2+4*l**2*Mrotor

F = m*g-10*(x3-u1)+3*x6
tau_phi = -(x7-u2)-x10
tau_theta = -(x8-u3)-x11
tau_psi = 0

dx1 = np.cos(x8)*np.cos(x9)*x4+(np.sin(x7)*np.sin(x8)*np.cos(x9)-np.cos(x7)*np.sin(x9))*x5+(np.cos(x7)*np.sin(x8)*np.cos(x9)+np.sin(x7)*np.sin(x9))*x6
dx2 = np.cos(x8)*np.sin(x9)*x4+(np.sin(x7)*np.sin(x8)*np.sin(x9)+np.cos(x7)*np.cos(x9))*x5+(np.cos(x7)*np.sin(x8)*np.sin(x9)-np.sin(x7)*np.cos(x9))*x6
dx3 = np.sin(x8)*x4-np.sin(x7)*np.cos(x8)*x5-np.cos(x7)*np.cos(x8)*x6
dx4 = x12*x5-x11*x6-g*np.sin(x8)
dx5 = x10*x6-x12*x4+g*np.cos(x8)*np.sin(x7)
dx6 = x11*x4-x10*x5+g*np.cos(x8)*np.cos(x7)-F/m
dx7 = x10+np.sin(x7)*np.tan(x8)*x11+np.cos(x7)*np.tan(x8)*x12
dx8 = np.cos(x7)*x11-np.sin(x7)*x12
dx9 = np.sin(x7)/np.cos(x8)*x11+np.cos(x7)/np.cos(x8)*x12
dx10 = (Jy-Jz)/Jx*x11*x12+1/Jx*tau_phi
dx11 = (Jz-Jx)/Jy*x10*x12+1/Jy*tau_theta
dx12 = (Jx-Jy)/Jz*x10*x11+1/Jz*tau_psi
du1 = 0

return [dx1,dx2,dx3,dx4,dx5,dx6,dx7,dx8,dx9,dx10,dx11,dx12,du1]

def TC_simulate(self, mode, initialSet, time_horizon, time_step, map=None):
y0 = initialSet
t = np.round(np.arange(0.0, time_horizon+time_step/2, time_step), 8)
trace = odeint(func = self.dynamics, y0 = y0, t = t)
t = t.reshape((-1,1))
trace = np.hstack((t, trace))
return trace

1 change: 0 additions & 1 deletion demo/dryvr_demo/origin_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from verse.agents import BaseAgent
from verse.map import LaneMap


class vanderpol_agent(BaseAgent):
def __init__(self, id, code=None, file_name=None):
# Calling the constructor of tha base class
Expand Down
39 changes: 39 additions & 0 deletions demo/dryvr_demo/quadrotor_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from dryvr_agent import QuadrotorAgent
from verse import Scenario, ScenarioConfig
from verse.plotter.plotter2D import *

import plotly.graph_objects as go
from enum import Enum, auto

class AgentMode(Enum):
Default = auto()

if __name__ == "__main__":
scenario = Scenario(ScenarioConfig(parallel=False))

quad = QuadrotorAgent('quad')
scenario.add_agent(quad)
# The initial position of the quadrotor is uncertain in
# all directions within [−0.4, 0.4] [m] and also the velocity
# is uncertain within [−0.4, 0.4] [m/s] for all directions

# The inertial (north) position x1, the inertial (east) position x2,
# the altitude x3, the longitudinal velocity x4,
# the lateral velocity x5, the vertical velocity x6,
# the roll angle x7, the pitch angle x8,
# the yaw angle x9, the roll rate x10,
# the pitch rate x11, and the yaw rate x12.
scenario.set_init(
[
[[-0.4,-0.4,-0.4,-0.4,-0.4,-0.4, 0, 0, 0, 0, 0, 0, 0.98],
[ 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0, 0, 0, 0, 0, 0, 1.02]]
],
[
(AgentMode.Default, )
]
)
traces = scenario.verify(5, 0.01)

fig = go.Figure()
fig = reachtube_tree(traces, None, fig, 0, 3, [1,3], "lines", "trace")
fig.show()

0 comments on commit ce6129f

Please sign in to comment.