-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for the wheeled inverted pendulum
- Loading branch information
1 parent
c029f21
commit 6bce7db
Showing
2 changed files
with
47 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright 2022 Stéphane Caron | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import unittest | ||
|
||
import numpy as np | ||
|
||
from qpmpc import solve_mpc | ||
from qpmpc.systems import WheeledInvertedPendulum | ||
|
||
|
||
class TestWheeledInvertedPendulum(unittest.TestCase): | ||
def setUp(self): | ||
self.pendulum = WheeledInvertedPendulum() | ||
|
||
def test_properties(self): | ||
self.assertGreater(self.pendulum.horizon_duration, 0.1) | ||
self.assertGreater(self.pendulum.omega, 0.1) | ||
|
||
def test_mpc_problem(self): | ||
mpc_problem = self.pendulum.build_mpc_problem( | ||
terminal_cost_weight=10.0, | ||
stage_state_cost_weight=1.0, | ||
stage_input_cost_weight=1e-3, | ||
) | ||
initial_state = np.zeros(self.pendulum.STATE_DIM) | ||
goal_state = initial_state.copy() | ||
nx = self.pendulum.STATE_DIM | ||
target_states = np.zeros(self.pendulum.nb_timesteps * nx) | ||
mpc_problem.update_initial_state(initial_state) | ||
mpc_problem.update_goal_state(goal_state) | ||
mpc_problem.update_target_states(target_states) | ||
plan = solve_mpc(mpc_problem, solver="proxqp") | ||
self.assertIsNotNone(plan) | ||
self.assertIsNotNone(plan.first_input) | ||
dt = self.pendulum.sampling_period | ||
print(f"{plan.first_input=}") | ||
state = self.pendulum.integrate(initial_state, plan.first_input, dt) | ||
self.assertTrue(np.allclose(state, initial_state)) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |