MPC-CBF

1.MPC-CBF.py

1.1 MPC类–实现代码1

import do_mpc
from casadi import *

import config


class MPC:
    """MPC-CBF Optimization problem:

    min Σ_{k=0}{N-1} 1/2*x'_k^T*Q*x'_k + 1/2*u_k^T*R*u_k   over u
    s.t.
        x_{k+1} = x_k + B*u_k*T_s
        x_min <= x_k <= x_max
        u_min <= u_k <= u_max
        x_0 = x(0)
        Δh(x_k, u_k) >= -γ*h(x_k)

    where x'_k = x_{des_k} - x_k
    """
    def __init__(self):
        self.sim_time = config.sim_time          # Total simulation time steps
        self.Ts = config.Ts                      # Sampling time
        self.T_horizon = config.T_horizon        # Prediction horizon
        self.x0 = config.x0                      # Initial pose
        self.v_limit = config.v_limit            # Linear velocity limit
        self.omega_limit = config.omega_limit    # Angular velocity limit
        self.R = config.R                        # Controls cost matrix
        self.Q = config.Q                        # State cost matrix
        self.static_obstacles_on = config.static_obstacles_on  # Whether to have static obstacles
        self.moving_obstacles_on = config.moving_obstacles_on  # Whether to have moving obstacles
        if self.static_obstacles_on:
            self.obs = config.obs                # Static Obstacles
        if self.moving_obstacles_on:             # Moving obstacles
            self.moving_obs = config.moving_obs
        self.r = config.r                        # Robot radius
        self.control_type = config.control_type  # "setpoint" or "traj_tracking"
        if self.control_type == "setpoint":      # Go-to-goal
            self.goal = config.goal              # Robot's goal pose
        self.gamma = config.gamma                # CBF parameter
        self.safety_dist = config.safety_dist    # Safety distance
        self.controller = config.controller      # Type of control

        self.model = self.define_model()
        self.mpc = self.define_mpc()
        self.simulator = self.define_simulator()
        self.estimator = do_mpc.estimator.StateFeedback(self.model)
        self.set_init_state()

    def define_model(self):
        """Configures the dynamical model of the system (and part of the objective function).

        x_{k+1} = x_k + B*u_k*T_s
        Returns:
          - model(do_mpc.model.Model): The system model
        """

        model_type = 'discrete'
        model = do_mpc.model.Model(model_type)

        # States
        n_states = 3
        _x = model.set_variable(var_type='_x', var_name='x', shape=(n_states, 1))

        # Inputs
        n_controls = 2
        _u = model.set_variable(var_type='_u', var_name='u', shape=(n_controls, 1))

        # State Space matrices
        B = self.get_sys_matrix_B(_x)

        # Set right-hand-side of ODE for all introduced states (_x).
        x_next = _x + B@_u*self.Ts
        model.set_rhs('x', x_next, process_noise=False)  # Set to True if adding noise

        # Optional: Define an expression, which represents the stage and terminal
        # cost of the control problem. This term will be later used as the cost in
        # the MPC formulation and can be used to directly plot the trajectory of
        # the cost of each state.
        model, cost_expr = self.get_cost_expression(model)
        model.set_expression(expr_name='cost', expr=cost_expr)

        # Moving obstacle (define time-varying parameter for its position)
        if self.moving_obstacles_on is True:
            for i in range(len(self.moving_obs)):
                model.set_variable('_tvp', 'x_moving_obs'+str(i))
                model.set_variable('_tvp', 'y_moving_obs'+str(i))

        # Setup model
        model.setup()
        return model

这段代码定义了一个名为 MPC

  • 23
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
carnd-mpc-project-master是一个自动驾驶汽车路径追踪控制器的项目。 该项目使用了模型预测控制(MPC)的方法来使自动驾驶汽车跟踪给定的路径。首先,通过收集车辆的传感器数据,如位置、速度和方向等,并结合车辆模型进行状态估计。接下来,根据给定的车辆模型和路径信息,使用MPC算法预测车辆未来的运动轨迹。MPC算法考虑了车辆的动力学限制以及路径跟踪误差,以寻找最佳的控制输入,例如转向角和加速度,来实现车辆的路径追踪。 在carnd-mpc-project-master中,主要包含了以下几个部分:模型、约束、误差目标以及目标函数。模型定义了车辆的动力学模型,通过车辆的状态与控制输入之间的关系来预测车辆的下一步行动。约束考虑了车辆的实际物理限制,如最大转向角、最大加速度等,以保证控制输入的可行性。误差目标是指车辆与目标路径之间的距离,目标函数通过考虑误差目标和约束,来寻找最佳控制输入,使得车辆能够最优地追踪给定的路径。 除了主要的控制器实现外,还提供了一些辅助功能,如路径生成、状态估计和传感器数据处理等。这些功能一起协同工作,实现了车辆的自动路径追踪。 Carnd-mpc-project-master通过实现高效的路径追踪控制器,可以使自动驾驶汽车更加安全、精确地跟踪所需的路径。该项目不仅提供了路径追踪的基本原理和算法,还提供了一个实际运行的系统,可以应用于真实道路环境中的自动驾驶汽车。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值