{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Calculation of center of mass\n", "This example shows how the function *get_com* is used to calculate the center of mass movement in one direction from hip and shoulder movement trajectories." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import balancepy.biomechanics as bm\n", "import plotly.graph_objects as go\n", "from plotly.subplots import make_subplots\n", "\n", "# render plots as static image - only needed for documentation\n", "import plotly.io as pio\n", "pio.renderers.default = \"svg\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "−0.100.10.20.30.4050100150200−0.500.51Time (s)Stimulus (degrees)COM (degrees)Stimulus Across TimeCenter of Mass (COM) Across Time" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "\n", "height = 1.75 # height in meters\n", "weight = 70 # weight in kg\n", "\n", "data = pd.read_csv('data/d1_vis1.csv')\n", "\n", "time = data['time']\n", "stimulus = data['analog2']\n", "com = bm.get_com(data['shld_zpos'], data['shld_ypos'], data['hip_zpos'], data['hip_ypos'], height)\n", "\n", "\n", "# Create subplots\n", "fig = make_subplots(rows=2, cols=1, shared_xaxes=True, vertical_spacing=0.1,\n", " subplot_titles=(\"Stimulus Across Time\", \"Center of Mass (COM) Across Time\"))\n", "\n", "# Add stimulus plot\n", "fig.add_trace(go.Scatter(x=time, y=stimulus, mode='lines', name='Stimulus', line=dict(color='blue')), row=1, col=1)\n", "\n", "# Add COM plot\n", "fig.add_trace(go.Scatter(x=time, y=com, mode='lines', name='COM', line=dict(color='green')), row=2, col=1)\n", "\n", "# Update layout\n", "fig.update_layout(height=600, width=1000, showlegend=False)\n", "fig.update_xaxes(title_text=\"Time (s)\", row=2, col=1)\n", "fig.update_yaxes(title_text=\"Stimulus (degrees)\", row=1, col=1)\n", "fig.update_yaxes(title_text=\"COM (degrees)\", row=2, col=1)\n", "\n", "fig.show()\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.12 (balancepy)", "language": "python", "name": "balancepy-py312" }, "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.13.3" } }, "nbformat": 4, "nbformat_minor": 2 }