{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic Test" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from Go2Py.sim.mujoco import Go2Sim\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "robot = Go2Sim()\n", "robot.standUpReset()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from Go2Py.control.walk_these_ways import *\n", "# checkpoint_path = \"/home/rstaion/projects/rooholla/locomotion/Go2Py/Go2Py/assets/checkpoints/walk_these_ways/\"\n", "checkpoint_path = \"/home/rstaion/projects/rooholla/walk-these-ways/runs/gait-conditioned-agility/pretrain-v0/train/025417.456545\"\n", "\n", "\n", "cfg = loadParameters(checkpoint_path)\n", "policy = Policy(checkpoint_path)\n", "command_profile = CommandInterface()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "p_gains: [20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20.]\n" ] } ], "source": [ "agent = WalkTheseWaysAgent(cfg, command_profile, robot)\n", "agent = HistoryWrapper(agent)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "control_dt = cfg[\"control\"][\"decimation\"] * cfg[\"sim\"][\"dt\"]\n", "simulation_dt = robot.dt\n", "obs = agent.reset()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "frq: 15.967047985229458 Hz\n", "frq: 42.47353444522081 Hz\n", "frq: 43.69977078558033 Hz\n", "frq: 40.20806211954177 Hz\n" ] } ], "source": [ "robot.reset()\n", "obs = agent.reset()\n", "for i in range(50000):\n", " policy_info = {}\n", " action = policy(obs, policy_info)\n", " if i % (control_dt // simulation_dt) == 0:\n", " obs, ret, done, info = agent.step(action)\n", " robot.step()\n", " command_profile.yaw_vel_cmd = 0.0\n", " command_profile.x_vel_cmd = 0.0\n", " command_profile.y_vel_cmd = 0.0\n", " time.sleep(robot.dt/4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Full System With FSM" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from Go2Py.robot.fsm import FSM\n", "from Go2Py.robot.remote import KeyboardRemote\n", "from Go2Py.robot.safety import SafetyHypervisor\n", "from Go2Py.sim.mujoco import Go2Sim\n", "from Go2Py.control.walk_these_ways import *\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "robot = Go2Sim()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "class walkTheseWaysController:\n", " def __init__(self, robot, remote, checkpoint):\n", " self.remote = remote\n", " self.robot = robot\n", " self.cfg = loadParameters(checkpoint)\n", " self.policy = Policy(checkpoint)\n", " self.command_profile = CommandInterface()\n", " self.agent = WalkTheseWaysAgent(self.cfg, self.command_profile, self.robot)\n", " self.agent = HistoryWrapper(self.agent)\n", " self.init()\n", "\n", " def init(self):\n", " self.obs = self.agent.reset()\n", " self.policy_info = {}\n", " self.command_profile.yaw_vel_cmd = 0.0\n", " self.command_profile.x_vel_cmd = 0.0\n", " self.command_profile.y_vel_cmd = 0.0\n", " self.command_profile.stance_width_cmd=0.25\n", " self.command_profile.footswing_height_cmd=0.08\n", " self.command_profile.step_frequency_cmd = 3.0\n", " self.command_profile.body_height_cmd = 0.5\n", "\n", " def update(self, robot, remote):\n", " action = self.policy(self.obs, self.policy_info)\n", " self.obs, self.ret, self.done, self.info = self.agent.step(action)\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "remote = KeyboardRemote()\n", "robot.standUpReset()\n", "safety_hypervisor = SafetyHypervisor(robot)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "p_gains: [20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20.]\n" ] } ], "source": [ "checkpoint = \"/home/rstaion/projects/rooholla/locomotion/Go2Py/Go2Py/assets/checkpoints/walk_these_ways/\"\n", "# checkpoint = \"/home/rstaion/projects/rooholla/walk-these-ways/runs/gait-conditioned-agility/pretrain-v0/train/025417.456545\"\n", "controller = walkTheseWaysController(robot, remote, checkpoint)\n", "fsm = FSM(robot, remote, safety_hypervisor, user_controller_callback=controller.update)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "controller.command_profile.pitch_cmd=0.0\n", "controller.command_profile.body_height_cmd=0.0\n", "controller.command_profile.footswing_height_cmd=0.08\n", "controller.command_profile.roll_cmd=0.0\n", "controller.command_profile.stance_width_cmd=0.2\n", "controller.command_profile.x_vel_cmd=-0.2\n", "controller.command_profile.y_vel_cmd=0.01\n", "controller.command_profile.setGaitType(\"trotting\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "controller.command_profile.pitch_cmd=0.0\n", "controller.command_profile.body_height_cmd=0.0\n", "controller.command_profile.footswing_height_cmd=0.08\n", "controller.command_profile.roll_cmd=0.0\n", "controller.command_profile.stance_width_cmd=0.2\n", "controller.command_profile.x_vel_cmd=-0.2\n", "controller.command_profile.y_vel_cmd=0.01\n", "controller.command_profile.setGaitType(\"trotting\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fsm.close()" ] } ], "metadata": { "kernelspec": { "display_name": "b1-env", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.18" } }, "nbformat": 4, "nbformat_minor": 2 }