← all projects
Robotics · December 2025 · 7 min read

MPC for UR7e Robotic Arm: Warehouse Sorting

Constrained joint-space Model Predictive Control for a UR7e manipulator. An RGB-D perception pipeline localizes objects and obstacles; a receding-horizon CasADi/IPOPT solver generates collision-free joint trajectories at 12 Hz, validated in MuJoCo and deployed on real hardware.

PythonMuJoCoCasADiROS2Optimal Control

Demo

Simulation (MuJoCo)
MPC simulation: arm picking and placing colored cubes around obstacle
On Real Hardware

The same MPC pipeline running in simulation and on the physical UR7e. The receding-horizon solver replans every control tick: no hand-coded waypoints, no pre-scripted trajectories. The arm detects cube colors via an RGB-D camera, plans a collision-free joint-space path around the obstacle bin, grasps each cube, and drops it in the correct zone.

System Architecture

Two-phase system architecture: Reaching Phase and Moving Phase

The system operates in two alternating phases that share the same perception stack but use different controllers.

Reaching Phase: the arm moves to a grasp pose. The RGB-D point cloud feeds into an object classifier (identifying cube colors c=(c1,,cn)\mathbf{c} = (c_1, \ldots, c_n)) and a point cloud processor (localizing object positions xobj,i=(xobj,i,yobj,i,zobj,i)\mathbf{x}_{\text{obj},i} = (x_{\text{obj},i},\, y_{\text{obj},i},\, z_{\text{obj},i}) and the obstacle bounding box xobs,wobs,hobs\mathbf{x}_{\text{obs}},\, w_{\text{obs}},\, h_{\text{obs}}). A pick-order planner selects the next target cube and passes its 3D position to an inverse kinematics solver, which computes a target joint configuration qR6\mathbf{q}^* \in \mathbb{R}^6. Joint velocities u\mathbf{u} drive the UR7e to that pose.

Moving Phase: MPC takes over for the transport. The camera continuously updates the obstacle AABB xobs,i\mathbf{x}_{\text{obs},i} and the predefined goal positions xgoal,c\mathbf{x}_{\text{goal},c} for each color class. The MPC solves a receding-horizon optimization each tick and outputs joint velocity commands u\mathbf{u} to carry the held cube to the drop zone while maintaining obstacle clearance throughout the trajectory.

Physical Setup

Top-down view: red and black cubes, blue obstacle bin, and two drop-off zones Lab setup: Intel RealSense camera, Robotiq gripper, blue obstacle bin, red and black cubes, and two drop-off zones

The workspace has multiple red and black cubes, a rigid blue plastic bin acting as the obstacle, and two rectangular drop zones marked on the table surface. An Intel RealSense RGB-D435i camera is mounted overhead to stream point-cloud and color data. The UR7e carries a Robotiq 2F-85 parallel-jaw gripper. The obstacle is deliberately positioned between the pick region and the drop zones, so the arm must always plan a non-trivial detour, which makes straight-line Cartesian planning insufficient and validates the MPC’s obstacle avoidance behavior.

Perception: RViz Visualization

Published cube positions and obstacle bounding box in RViz point cloud

The perception node processes the RealSense depth stream in real time. DBSCAN clustering segments the point cloud into individual objects, and axis-aligned bounding boxes (AABBs) are fit to the obstacle cluster. Color classification assigns each cube to a class. The RViz visualization shows published cube positions and the inflated obstacle AABB before y-axis extension; the colored markers correspond to detected red and black cubes, and the transparent box is the inflated obstacle region passed directly into the MPC collision constraint.

MPC Formulation

The MPC operates in joint space. The state at discrete time kk is the 6-DoF joint configuration qkR6\mathbf{q}_k \in \mathbb{R}^6, and the control input is a bounded joint position increment ΔqkR6\Delta\mathbf{q}_k \in \mathbb{R}^6. The prediction model is a simple integrator:

qk+1=qk+Δqk\mathbf{q}_{k+1} = \mathbf{q}_k + \Delta\mathbf{q}_k

with sample period Δt=0.08s\Delta t = 0.08\,\text{s} and prediction horizon N=30N = 30 steps. Choosing joint position increments (rather than joint velocities or Cartesian targets) keeps the prediction model linear and lets joint limits be enforced as hard box constraints directly on the optimization variables.

Cost Function

At each control cycle the MPC minimizes:

J=k=0N1(p(qk)pQp2+ΔqkR2)+p(qN)pQp,N2+wfacesface2+wtabkstab,k2J = \sum_{k=0}^{N-1} \left( \|\,p(\mathbf{q}_k) - p^*\|^2_{Q_p} + \|\Delta\mathbf{q}_k\|^2_R \right) + \|\,p(\mathbf{q}_N) - p^*\|^2_{Q_{p,N}} + w_{\text{face}}\, s_{\text{face}}^2 + w_{\text{tab}} \sum_k s_{\text{tab},k}^2

where p(qk)p(\mathbf{q}_k) is the end-effector position from forward kinematics, pp^* is the desired drop position (computed via IK), Qp=IQ_p = I, RQpR \ll Q_p, and Qp,N=10QpQ_{p,N} = 10\,Q_p (a heavier terminal weight to drive convergence to goal). The slack terms sfaces_{\text{face}} and stab,ks_{\text{tab},k} soften the orientation and table-clearance constraints to maintain solver feasibility under perception noise.

Constraints

(i) Joint limits and step bounds:

qk,i2π,Δqk,i0.15radi,k|\mathbf{q}_{k,i}| \le 2\pi, \qquad |\Delta\mathbf{q}_{k,i}| \le 0.15\,\text{rad} \quad \forall\, i,\, k

The per-step bound of ±0.15rad\pm 0.15\,\text{rad} directly reflects the position-increment interface of the UR7e’s low-level controller, approximating actuator velocity limits.

(ii) Obstacle avoidance. The gripper and grasped object are approximated by three proxy spheres centered at [0,0,0][0,0,0], [0,0,0.02][0,0,0.02], [0,0,0.03]m[0,0,0.03]\,\text{m} in the tool frame, each with inflated radius ri[0.03,0.05]mr_i \in [0.03, 0.05]\,\text{m}. At every horizon step the Euclidean distance from each sphere center ci(qk)c_i(\mathbf{q}_k) (computed via forward kinematics) to the obstacle AABB B\mathcal{B} must satisfy:

d(ci(qk),B)    rii{1,2,3},  kd\bigl(c_i(\mathbf{q}_k),\, \mathcal{B}\bigr) \;\ge\; r_i \qquad \forall\, i \in \{1,2,3\},\; k

The instantaneous obstacle clearance margin is mobs(q)mini(d(ci(q),B)ri)m_{\text{obs}}(\mathbf{q}) \triangleq \min_i \bigl( d(c_i(\mathbf{q}), \mathcal{B}) - r_i \bigr). Collision-free motion corresponds to mobs0m_{\text{obs}} \ge 0.

(iii) Table clearance. A soft vertical floor constraint prevents the arm from colliding with the tabletop during transport, relaxed via slack stab,k0s_{\text{tab},k} \ge 0:

ztable+ϵtab    ciz(qk)+stab,kz_{\text{table}} + \epsilon_{\text{tab}} \;\le\; c_i^z(\mathbf{q}_k) + s_{\text{tab},k}

The effective floor height is phase-dependent: stricter on descent to the grasp pose, relaxed during transport to allow the arm to skim over the obstacle.

(iv) Terminal orientation. A down-facing tool-frame orientation is enforced softly only at the terminal step NN via slack sface0s_{\text{face}} \ge 0. Intermediate orientation is left unconstrained, which gives the optimizer freedom to find non-obvious collision-avoiding paths without locking the gripper angle along the entire trajectory.

Receding Horizon Implementation

Rather than applying only the first computed increment (standard MPC), the first three increments (0.24s\approx 0.24\,\text{s} of motion) are executed before replanning. This reduces solver invocation frequency while keeping responsiveness adequate for the task. The loop terminates when p(q)p3cm\|p(\mathbf{q}) - p^*\| \le 3\,\text{cm} or after 40 solver iterations. Warm-starting with the previous horizon solution is the primary driver of the ~3× speedup from cold to steady-state solve time.

MuJoCo Simulation

MuJoCo sim: arm approaching cubes MuJoCo sim: arm grasping cube
MuJoCo sim: arm carrying cube over obstacle MuJoCo sim: cube placed in drop zone

The MuJoCo environment mirrors the real lab setup: colored drop zones, the static obstacle bin, and a Robotiq gripper. The cyan markers visualize the MPC’s predicted end-effector path over the N=30N = 30 horizon steps in real time. The four frames show the full task sequence: approach, grasp, obstacle transit, and placement.

Results on Real Hardware

Hardware setup: UR7e with cubes and sorting zones Red cubes successfully sorted into the red zone

Red cubes in the red zone, black cubes in the black zone: fully autonomous end-to-end. The arm reclassifies each cube’s color from the live camera feed, routes around the obstacle bin, and places each cube in the correct zone with no human intervention between picks.

Trajectory Analysis

End-effector Cartesian trajectory around the obstacle AABB

The executed end-effector Cartesian trajectory (blue) for a representative transport phase. Starting from the initial grasp pose (green dot), the MPC generates a smooth path that intentionally arcs around the obstacle AABB (red box) before converging to the target drop position (red ×). The detour is not hand-coded; it emerges from the obstacle clearance constraint at each horizon step. At no point does the optimizer force a fixed height; the arm finds its own clearance strategy, trading off goal progress against constraint satisfaction at every tick.

Convergence & Safety

End-effector position error converges to within 3 cm tolerance Obstacle clearance margin stays strictly positive throughout

The end-effector position error p(q)p\|p(\mathbf{q}) - p^*\| decreases steadily from an initial offset of 0.7m\approx 0.7\,\text{m} to within the 3cm3\,\text{cm} stopping tolerance. Small non-monotonic fluctuations near convergence are expected in closed-loop MPC; each replan trades off goal progress against the obstacle and table constraints, so the path is not strictly greedy. The obstacle clearance margin mobs(q)m_{\text{obs}}(\mathbf{q}) remains strictly positive throughout the entire trajectory, confirming collision-free behavior even during the obstacle detour. The soft slack variables prevent constraint infeasibility under perception noise without relaxing the hard obstacle constraint.

Joint Profiles & Solve Time

All 6 joint increment commands smooth and within ±0.15 rad bounds Solve time: cold start ~38ms, steady state mean ~11.7ms

Joint increment commands Δqk,i\Delta\mathbf{q}_{k,i} remain within the enforced ±0.15rad\pm 0.15\,\text{rad} per-step bounds across all six joints throughout the trajectory, with no aggressive joint motion, consistent with the position-increment interface assumption. Solve time drops from 38ms\approx 38\,\text{ms} on the cold start (no prior solution to warm-start from) to a steady-state mean of 11.7ms\approx 11.7\,\text{ms}, well within the 80ms80\,\text{ms} control timestep (Δt=0.08s\Delta t = 0.08\,\text{s}). Warm-starting with the previous horizon’s solution is the dominant factor in the speedup; the optimizer starts already near the previous optimum and converges in far fewer IPOPT iterations.

#mpc#robotics#trajectory-optimization#mujoco#kinematics

Related projects