Skip to content

Quick Start Tutorials

These tutorials will guide you through common control scenarios using OpenSMC.

Tutorial 1: Classical SMC on Double Integrator

This is the simplest case, demonstrating the modularity of OpenSMC.

import numpy as np
import matplotlib.pyplot as plt
from opensmc.surfaces import LinearSurface
from opensmc.reaching import ConstantRate
from opensmc.controllers import ClassicalSMC
from opensmc.plants import DoubleIntegrator
from opensmc.simulator import Simulator

# Define a linear sliding surface s = e_dot + c*e
surface = LinearSurface(c=2.0)

# Define a constant reaching law s_dot = -k * sign(s)
reaching = ConstantRate(k=5.0)

# Create a Classical SMC controller using the surface and reaching law
controller = ClassicalSMC(surface, reaching)

# Define the plant model
plant = DoubleIntegrator()

# Simulate the closed-loop system
sim = Simulator(dt=0.001, T=5.0, method='rk4')
result = sim.run(controller, plant, x0=np.array([1.0, 0.0]))

# Visualize results
fig, axes = plt.subplots(3, 1, figsize=(8, 8), sharex=True)
axes[0].plot(result['t'], result['x'][:, 0], label='position')
axes[0].plot(result['t'], result['xref'][:, 0], '--', label='reference')
axes[0].set_ylabel('Position')
axes[0].legend()

axes[1].plot(result['t'], result['u'][:, 0])
axes[1].set_ylabel('Control u')

axes[2].plot(result['t'], result['s'][:, 0])
axes[2].set_ylabel('Sliding variable s')
axes[2].set_xlabel('Time (s)')

plt.tight_layout()
plt.show()

Tutorial 2: NFTSMC on Inverted Pendulum

Non-singular Fast Terminal Sliding Mode Control (NFTSMC) provides finite-time convergence.

import numpy as np
from opensmc.surfaces import NonsingularTerminalSurface
from opensmc.reaching import ExponentialRate
from opensmc.controllers import NFTSMC
from opensmc.plants import InvertedPendulum
from opensmc.simulator import Simulator
from opensmc import metrics

# Define a nonsingular terminal sliding surface
surface = NonsingularTerminalSurface(alpha=2.0, beta=1.5, p=5, q=3)

# Use an exponential reaching law for smoother response
reaching = ExponentialRate(k=10.0, q=0.1)

# Create an NFTSMC controller
controller = NFTSMC(surface, reaching)

# Define the inverted pendulum plant
plant = InvertedPendulum()

# Simulate and evaluate performance metrics
sim = Simulator(dt=0.001, T=10.0, method='rk4')
result = sim.run(controller, plant, x0=np.array([0.5, 0.0]))

# Calculate performance metrics
m = metrics.compute_all(result)
print(f"Settling Time: {m['settling_time']:.3f} s")
print(f"Chattering Index: {m['chattering_index']:.3f}")

Tutorial 3: RL-SMC Integration with Gymnasium

OpenSMC provides Gymnasium-compatible environments for training Reinforcement Learning agents to discover or optimize sliding surfaces.

import gymnasium as gym
import opensmc  # Registers OpenSMC environments

# Create an OpenSMC Gymnasium environment
env = gym.make("OpenSMC/DoubleIntegrator-v0")

# Reset the environment
obs, info = env.reset()

# Example training loop (using random actions)
for _ in range(100):
    action = env.action_space.sample()  # Your RL agent would pick actions here
    obs, reward, terminated, truncated, info = env.step(action)

    if terminated or truncated:
        obs, info = env.reset()

env.close()

Tip

Check the Examples section for more advanced use cases and pre-trained RL models.