Package: sk8o-sim
This folder contains code that to simulates SK8O in Python in two ways:as a linear approximation of a Segway ~ SK8O with fixed legs insk8o_sim/segway,as a non-linear simulation of SK8O usingMuJoCo("full model") insk8o_sim/full.You can install this package from PyPI by runningpip install sk8o_simFor more information,check my diploma thesis.Making changesThis project targets Python 3.11, though it was originally written for Python 3.9, which may affect some parts of the codebase (particularly the typing annotations). The necessary packages can be installed using the standardrequirements.txtfile e.g. bypip install -r sk8o_sim/requirements.txtIf you make any changes you wish to make available via pip, make changes topyproject.tomlas necessary (at least increment version number!) and GitLab's CI should detect this change and do the dirty work for you. :-)SimulationSegwayFiles insk8o_sim/segwaycontain the linear approximation of a Segway that was used to train RL models and to tune the LQR. It is highly configurable, can generate randomized models, simulate motor quantization and can be rendered viapygame. Note that by default, the measurement and process noise can be quite severe (in addition to initial conditions not necessarily too close to the equilibrium), so even the LQR that works just fine on SK8O may sometimes fail to stabilize the robot.What it does not support is any way to end the simulation due to the robot falling - it is up to you to decide when the approximation is no longer valid!Main componentsThe building block of the Segway simulation is theSegwaySimDatadataclass found insk8o_sim/segway/data.py. It contains all the simulation data and some helper functions. Insk8o_sim/segway/simulation.py, you can find how the simulation is performed andview.pyis in charge of the rendering.DemoA very crude simulation of an LQR stabilization with "slow-mo" rendering can be run using the following code:importtimeimportnumpyasnpfromsk8o_simimport(SegwayLQRController,SegwayLQRControllerCfg,SegwaySimulation,SegwaySimulationCfg,)# initialize everythingsim=SegwaySimulation(SegwaySimulationCfg())data=sim.reset()controller=SegwayLQRController(SegwayLQRControllerCfg())# run the simulationsim_time=10# [s]forkinrange(sim_time*controller.control_frequency):action=controller(data)data=sim.run(action,1/controller.control_frequency)sim.render()time.sleep(0.5)Note that the FPS will likely be unstable in this example. For a more advanced interaction, check out theexamples/interactive_segway.pyfile.Full modelThe full model is implemented inMuJoCoand can be found insk8o_sim/full. Themujoco-python-viewerpackage is used for rendering.If you've familiarized yourself with the Segway simulation, this one should hopefully feel similar. Again, you will mostly work with what's contained in thesimulation.py-- this time the class isFullSimulationand will generateFullSimulationDataobjects found indata.py. This class is essentially a wrapper around MuJoCo that enables rendering and additionally computes the equivalent Segway states for easier control. Note that these may not make much sense whenever the robot is not in a "close-to-upright" position.One (admittedly experimental) trick of this simulation is the option to "lock" SK8O's hips/legs (see the(un)lock_hipsmethods) for testing.You may also wish to use thehas_fallenmethod to gauge whether your control system is successful. This method will returnTruewhenever anything else but the wheels touch the ground.ControllersThe controllers can be found insk8o_sim/controllers.pyand includeSegwayLQRControllerto control the wheels as per Adam's thesis and a simple PID hip controller,SK8OHipController. Both implement theSK8OControllerinterface and are combined in theSK8OFullControllerclass which controls both. You can use this class if you wish to implement only partial control.  For example, if your controller only takes care of wheel torques, you can implement the interface and pass your controller as thewheel_controllertoSK8OFullController.ConfigurationThe project is ready forOmegaConf/hydra: thesk8o_sim/configs.pyfile contains all the configuration classes that are available for the simulation, rendering and controllers. This allows you to control everything via custom YAML configs and/or command-line arguments with a only little bit of work. This should also make logging the configuration easy. If you experiment with many versions of a new controller, you can leverage this to easily compare the changes you've made using e.g. Weights & Biases or similar software. To see a sample, see theexamples/cli_configuration.pydemo file.