2024-03-21 05:40:33 +08:00
|
|
|
{
|
|
|
|
"cells": [
|
2024-05-21 05:35:10 +08:00
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"# Test In Simulation"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"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 *"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"robot = Go2Sim()"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"remote = KeyboardRemote()\n",
|
|
|
|
"robot.standUpReset()\n",
|
|
|
|
"safety_hypervisor = SafetyHypervisor(robot)"
|
|
|
|
]
|
|
|
|
},
|
2024-03-21 05:40:33 +08:00
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2024-05-05 09:14:41 +08:00
|
|
|
"execution_count": null,
|
2024-03-21 05:40:33 +08:00
|
|
|
"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",
|
2024-03-21 12:06:35 +08:00
|
|
|
" 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.bodyHeight = 0.00\n",
|
2024-03-21 05:40:33 +08:00
|
|
|
"\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",
|
2024-03-21 12:06:35 +08:00
|
|
|
" vy = -robot.getRemoteState().lx\n",
|
|
|
|
" vx = robot.getRemoteState().ly\n",
|
|
|
|
" omega = -robot.getRemoteState().rx*2.2\n",
|
|
|
|
" self.command_profile.x_vel_cmd = vx*1.5\n",
|
|
|
|
" self.command_profile.y_vel_cmd = vy*1.5\n",
|
2024-05-05 08:09:01 +08:00
|
|
|
" self.command_profile.yaw_vel_cmd = omega"
|
2024-03-21 05:40:33 +08:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2024-05-21 05:35:10 +08:00
|
|
|
"checkpoint = \"../Go2Py/assets/checkpoints/walk_these_ways/\"\n",
|
|
|
|
"controller = walkTheseWaysController(robot, remote, checkpoint)\n",
|
|
|
|
"fsm = FSM(robot, remote, safety_hypervisor, user_controller_callback=controller.update)"
|
2024-05-05 08:09:01 +08:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"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()"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"# Test on Real Robot"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"from Go2Py.robot.fsm import FSM\n",
|
2024-05-21 05:35:10 +08:00
|
|
|
"from Go2Py.robot.remote import UnitreeRemote\n",
|
2024-05-05 08:09:01 +08:00
|
|
|
"from Go2Py.robot.safety import SafetyHypervisor\n",
|
|
|
|
"from Go2Py.control.walk_these_ways import *"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2024-05-21 05:35:10 +08:00
|
|
|
"from Go2Py.robot.interface import GO2Real\n",
|
2024-05-05 08:09:01 +08:00
|
|
|
"import numpy as np\n",
|
|
|
|
"robot = GO2Real(mode='lowlevel')"
|
|
|
|
]
|
|
|
|
},
|
2024-03-21 05:40:33 +08:00
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"remote = UnitreeRemote(robot)\n",
|
|
|
|
"safety_hypervisor = SafetyHypervisor(robot)"
|
|
|
|
]
|
|
|
|
},
|
2024-05-21 05:35:10 +08:00
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"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.bodyHeight = 0.00\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",
|
|
|
|
" vy = -robot.getRemoteState().lx\n",
|
|
|
|
" vx = robot.getRemoteState().ly\n",
|
|
|
|
" omega = -robot.getRemoteState().rx*2.2\n",
|
|
|
|
" self.command_profile.x_vel_cmd = vx*1.5\n",
|
|
|
|
" self.command_profile.y_vel_cmd = vy*1.5\n",
|
|
|
|
" self.command_profile.yaw_vel_cmd = omega"
|
|
|
|
]
|
|
|
|
},
|
2024-03-21 05:40:33 +08:00
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
2024-05-05 08:09:01 +08:00
|
|
|
"checkpoint = \"../Go2Py/assets/checkpoints/walk_these_ways/\"\n",
|
2024-03-21 05:40:33 +08:00
|
|
|
"controller = walkTheseWaysController(robot, remote, checkpoint)\n",
|
2024-05-05 08:09:01 +08:00
|
|
|
"safety_hypervisor = SafetyHypervisor(robot)"
|
2024-03-21 05:40:33 +08:00
|
|
|
]
|
2024-03-21 12:06:35 +08:00
|
|
|
},
|
2024-05-05 09:14:41 +08:00
|
|
|
{
|
|
|
|
"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.04\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\")"
|
|
|
|
]
|
|
|
|
},
|
2024-03-21 12:06:35 +08:00
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
2024-05-05 08:09:01 +08:00
|
|
|
"source": [
|
|
|
|
"fsm = FSM(robot, remote, safety_hypervisor, user_controller_callback=controller.update)"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"fsm.close()"
|
|
|
|
]
|
2024-03-21 05:40:33 +08:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"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
|
|
|
|
}
|