Go2Py_SIM/examples/05-walk-these-ways-RL-contr...

448 lines
144 KiB
Plaintext
Raw Permalink Normal View History

{
"cells": [
2024-05-21 05:35:10 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Test In Simulation"
]
},
{
"cell_type": "code",
"execution_count": 1,
2024-05-21 05:35:10 +08:00
"metadata": {},
2024-10-10 06:23:15 +08:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"pygame 2.6.1 (SDL 2.28.4, Python 3.10.12)\n",
"Hello from the pygame community. https://www.pygame.org/contribute.html\n"
]
}
],
2024-05-21 05:35:10 +08:00
"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",
2024-11-05 06:27:32 +08:00
"execution_count": 20,
2024-05-21 05:35:10 +08:00
"metadata": {},
"outputs": [],
"source": [
"robot = Go2Sim()"
]
},
{
"cell_type": "code",
2024-11-05 06:27:32 +08:00
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"map = np.zeros((1200, 1200))\n",
"map[:200, :200] = 255\n",
"robot.updateHeightMap(map)"
]
},
2024-05-21 05:35:10 +08:00
{
"cell_type": "code",
2024-11-05 06:27:32 +08:00
"execution_count": 22,
2024-05-21 05:35:10 +08:00
"metadata": {},
"outputs": [],
"source": [
"remote = KeyboardRemote()\n",
"robot.sitDownReset()\n",
2024-05-21 05:35:10 +08:00
"safety_hypervisor = SafetyHypervisor(robot)"
]
},
{
"cell_type": "code",
2024-11-05 06:27:32 +08:00
"execution_count": 23,
"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",
2024-10-10 06:23:15 +08:00
" self.hist_data = {}\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",
2024-11-05 06:27:32 +08:00
" vy = 0.8 # Update these based on your implementation of the remote controller\n",
" vx = 0.\n",
2024-11-05 06:27:32 +08:00
" omega = 0.8\n",
" self.command_profile.x_vel_cmd = vx*1.5\n",
" self.command_profile.y_vel_cmd = vy*1.5\n",
2024-10-10 06:23:15 +08:00
" self.command_profile.yaw_vel_cmd = omega\n",
" for key, value in self.info.items():\n",
" if key in self.hist_data:\n",
" self.hist_data[key].append(value)\n",
" else:\n",
" self.hist_data[key] = [value]"
]
},
{
"cell_type": "code",
2024-11-05 06:27:32 +08:00
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"p_gains: [20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20.]\n"
]
2024-11-05 06:27:32 +08:00
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"controller timeout\n"
]
}
],
"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
]
},
2024-11-05 06:27:32 +08:00
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"contacts = []\n",
"feet_vels = []\n",
"\n",
"for i in range(2000):\n",
" contact_state = robot.getFootContact()>15\n",
" sites = ['FR_foot', 'FL_foot', 'RR_foot', 'RL_foot']\n",
" feet_vel = [np.linalg.norm(robot.getFootVelInWorld(s)) for s in sites]\n",
" contacts.append(contact_state)\n",
" feet_vels.append(feet_vel)\n",
" time.sleep(0.01)\n",
"\n",
"feet_vels = np.stack(feet_vels)\n",
"contacts = np.stack(contacts)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAnYAAAHWCAYAAAD6oMSKAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAADDU0lEQVR4nO2debgcVZ3+3+rt7ku2m9zsK4QtYYfIFoGwBB0QnWGUHwYXUAeYcVBEHEFxRnEXFEWHQREVUVRgBsISkUX2ENYsBBISst7sd7+31/r9cepUne6u7q6qXuqc6u/nefJ0596+devWcuo973c5mq7rOgiCIAiCIAjlCfm9AwRBEARBEERlIGFHEARBEAQREEjYEQRBEARBBAQSdgRBEARBEAGBhB1BEARBEERAIGFHEARBEAQREEjYEQRBEARBBAQSdgRBEARBEAEhUutfmMlksGPHDrS1tUHTtFr/eoIgCIIgCKXQdR0DAwOYPHkyQqHinlzNhd2OHTswbdq0Wv9agiAIgiAIpdm6dSumTp1a9DM1F3ZtbW0A2M61t7dX7fckk0k89thjOOussxCNRqv2e4jKQOdLLeh8qQWdL7Wg86UWtThf/f39mDZtmqmhilFzYcfDr+3t7VUXds3NzWhvb6cbQwHofKkFnS+1oPOlFnS+1KKW58tJChsVTxAEQRAEQQQEEnYEQRAEQRABgYQdQRAEQRBEQKh5jh1BEARB1Av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HAHzta19zevpcQ8KOIAiCICpNcgQY3A00TAM0DSPJNA694VFfdmXtN85Gc6z0435gYAC33HILbr31VixbtgwAMGfOHJx88skAgLvvvhujo6O466670NLSAgC49dZb8cEPfhDf+c53MHHiRAAs3+zWW29FOBzG/Pnzcd555+Hxxx/HZZddhrFjxyIcDqOtrQ2TJk0yf/eUKVPwxS9+0fz/VVddhUcffRR//OMfcfzxx5fctwkTJgAAxo0bl7XdXLZs2YKjjjoKxx57LABkuWuFtnH66adnbeO///u/0dnZiaeeegof+MAHzJ/jS4JxbrzxRnz5y18293f27Nn4z//8T3zpS18iYUcQBEEQSjHSCwwDaB8HxFr83htHrFu3DvF4HGeccUbB7y9cuNAUdQBw0kknIZPJYP369aawO+yww7JWTuju7sabb75Z9Hen02l861vfwh//+Eds374diUQC8XjcbNpbat+c8rnPfQ4f/vCH8corr+Css87CBRdcgPe9731Ff2bXrl346le/iieffBK7d+9GOp3G8PAwtmzZUvTnXn/9dTz77LNZoex0Oo3R0VEMDw9XrYE1CTuCIAiCqCS6DmSMPCo9A4CFQ9d+42xHP57JZDDQP4C29raKhWIdfc5Y1qpcotFo1v81TUMmkyn6M9/73vdwyy234Oabb8YRRxyBlpYWfP7zn0cikajovp177rl47733sHz5cqxYsQJnnHEGrrjiCnz/+98v+DPLli3Dvn37cMstt2DGjBloaGjAokWLzH0rxODgIG688UZceOGFed9rbGws+28pBAk7giAIgqgkI70AdPZeZ6+apjkKhwJM2KViYTTHIjVdK3bevHloamrC448/jk9/+tN53z/kkENw5513YmhoyHTtnn32WYRCIRx88MGOf08sFkM6nZ1v+Oyzz+L888/H//t//w8AOwZvv/02Dj30UEf7xvPhcrdrx4QJE7Bs2TIsW7YMp5xyCq655hp8//vfL7iNZ599Fj/72c+wdOlSAMDWrVuxd+/erM9Eo9G8nzv66KOxfv16zJ07t+Q+VRKqiiUIgiCISjK4S/iP7ttuuKWxsRHXXnstvvSlL+Guu+7Cxo0b8cILL+COO+4AAFx88cVobGzEsmXLsHr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480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"start = 250\n",
"end = 500\n",
"plt.plot(contacts[start:end,0])\n",
"plt.plot(feet_vels[start:end,0])\n",
"plt.legend(['contact state', 'foot velocity'])\n",
"plt.grid(True)\n",
"plt.tight_layout()\n",
"plt.savefig('walk.png')"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAnYAAAHWCAYAAAD6oMSKAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAADE+ElEQVR4nOydebgcVZn/v9Xb3ZdsNwvZWUNIwhoMuyxBQEZUFIVxoqPgAv6GwRUVGNQRx2UUlBFFJaMjMC4oIyIQlrCvgUAgC9kTsm83d7+91e+PU6equru6u6q6q/o9fd/P8+SpTt++1XVP1TnnPd93OZqu6zoYhmEYhmEY5YnU+gIYhmEYhmGY6sCGHcMwDMMwTJ3Ahh3DMAzDMEydwIYdwzAMwzBMncCGHcMwDMMwTJ3Ahh3DMAzDMEydwIYdwzAMwzBMncCGHcMwDMMwTJ0QC/sLs9kstm/fjra2NmiaFvbXMwzDMAzDKIWu6+jt7cWkSZMQiZTW5EI37LZv344pU6aE/bUMwzAMwzBKs3XrVkyePLnkZ0I37Nra2gCIi2tvbw/se1KpFB555BEsXLgQ8Xg8sO9hqgPfL7Xg+6UWfL/Ugu+XWoRxv3p6ejBlyhTThipF6IaddL+2t7cHbtg1Nzejvb2dO4YC8P1SC75fasH3Sy34fqlFmPfLTQgbJ08wDMMwDMPUCWzYMQzDMAzD1Als2DEMwzAMw9QJbNgxDMMwDMPUCWzYMQzDMAzD1Als2DEMwzAMw9QJbNgxDMMwDMPUCWzYMQzDMAzD1Als2DEMwzAMw9QJbNgx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6rDi5x8mwY6W4PHIxkWgTNSdln+U
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import matplotlib.pyplot as plt\n",
"start = 250\n",
"end = 500\n",
"plt.plot(contacts[start:end,0])\n",
"plt.plot(feet_vels[start:end,0])\n",
"plt.legend(['contact state', 'foot velocity'])\n",
"plt.grid(True)\n",
"plt.tight_layout()\n",
"plt.savefig('walk_these_ways_random_adaptation.png')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"controller.policy.adaptation_module"
]
},
2024-05-05 08:09:01 +08:00
{
"cell_type": "code",
2024-10-10 06:23:15 +08:00
"execution_count": 7,
2024-05-05 08:09:01 +08:00
"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": "markdown",
"metadata": {},
"source": [
"Pressing `u` on the keyboard will make the robot stand up. This is equivalent to the `L2+A` combo of the Go2 builtin state machine. After the the robot is on its feet, pressing `s` will hand over the control the RL policy. This action is equivalent to the `start` key of the builtin controller. When you want to stop, pressing `u` again will act similarly to the real robot and locks it in standing mode. Finally, pressing `u` again will command the robot to sit down."
]
},
2024-05-05 08:09:01 +08:00
{
"cell_type": "code",
2024-11-05 06:27:32 +08:00
"execution_count": 18,
2024-05-05 08:09:01 +08:00
"metadata": {},
"outputs": [],
"source": [
2024-11-05 06:27:32 +08:00
"fsm.close()\n",
"robot.close()"
2024-05-05 08:09:01 +08:00
]
},
2024-10-10 06:23:15 +08:00
{
"cell_type": "code",
2024-11-05 06:27:32 +08:00
"execution_count": null,
2024-10-10 06:23:15 +08:00
"metadata": {},
2024-11-05 06:27:32 +08:00
"outputs": [],
2024-10-10 06:23:15 +08:00
"source": [
"import matplotlib.pyplot as plt\n",
"# Assuming 'controller.hist_data[\"foot_contact_forces_mag\"]' is a dictionary with foot contact force magnitudes\n",
"foot_contact_forces_mag = np.array(controller.hist_data[\"foot_contact_forces_mag\"])\n",
"\n",
"# Number of feet (foot_nb)\n",
"foot_nb = foot_contact_forces_mag.shape[1]\n",
"\n",
"# Number of rows needed for the grid, with 3 columns per row\n",
"n_cols = 3\n",
"n_rows = int(np.ceil(foot_nb / n_cols))\n",
"\n",
"# Create the figure and axes for subplots\n",
"fig, axes = plt.subplots(n_rows, n_cols, figsize=(15, 5 * n_rows))\n",
"\n",
"# Flatten the axes array for easy indexing (in case of multiple rows)\n",
"axes = axes.flatten()\n",
"\n",
"# Plot each foot's contact force magnitude\n",
"for i in range(foot_nb):\n",
" axes[i].plot(foot_contact_forces_mag[:, i])\n",
" axes[i].set_title(f'Foot {i+1} Contact Force Magnitude')\n",
" axes[i].set_xlabel('Time')\n",
" axes[i].set_ylabel('Force Magnitude')\n",
"\n",
"# Remove any empty subplots if foot_nb is not a multiple of 3\n",
"for j in range(foot_nb, len(axes)):\n",
" fig.delaxes(axes[j])\n",
"\n",
"# Adjust layout\n",
"plt.tight_layout()\n",
"plt.savefig(\"foot_contact_profile.png\")\n",
"plt.show()"
]
},
2024-05-05 08:09:01 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Test on Real Robot"
]
},
{
"cell_type": "code",
2024-11-05 06:27:32 +08:00
"execution_count": null,
2024-05-05 08:09:01 +08:00
"metadata": {},
2024-11-05 06:27:32 +08:00
"outputs": [],
2024-05-05 08:09:01 +08:00
"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",
2024-10-10 06:23:15 +08:00
"execution_count": 2,
2024-05-05 08:09:01 +08:00
"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')"
]
},
{
"cell_type": "code",
2024-10-10 06:23:15 +08:00
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"remote = UnitreeRemote(robot)\n",
"safety_hypervisor = SafetyHypervisor(robot)"
]
},
2024-05-21 05:35:10 +08:00
{
"cell_type": "code",
2024-10-10 06:23:15 +08:00
"execution_count": 4,
2024-05-21 05:35:10 +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",
" 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"
]
},
{
"cell_type": "code",
2024-11-05 06:27:32 +08:00
"execution_count": null,
"metadata": {},
2024-11-05 06:27:32 +08:00
"outputs": [],
"source": [
2024-05-05 08:09:01 +08:00
"checkpoint = \"../Go2Py/assets/checkpoints/walk_these_ways/\"\n",
"controller = walkTheseWaysController(robot, remote, checkpoint)\n",
2024-05-05 08:09:01 +08:00
"safety_hypervisor = SafetyHypervisor(robot)"
]
},
2024-05-05 09:14:41 +08:00
{
"cell_type": "code",
2024-10-10 06:23:15 +08:00
"execution_count": 6,
2024-05-05 09:14:41 +08:00
"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\")"
]
},
{
"cell_type": "code",
2024-11-05 06:27:32 +08:00
"execution_count": null,
"metadata": {},
2024-11-05 06:27:32 +08:00
"outputs": [],
2024-05-05 08:09:01 +08:00
"source": [
"fsm = FSM(robot, remote, safety_hypervisor, user_controller_callback=controller.update)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pressing `L2+A` to command the robot to stand up. After the the robot is on its feet, pressing `start` will hand over the control the RL policy. When you want to stop, pressing `L2+A` again will act similarly to the factory controller and locks the robot in standing mode. Finally, pressing `L2+A` again will command the robot to sit down."
]
},
2024-05-05 08:09:01 +08:00
{
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}