@@ -45,12 +45,12 @@ reaching, and grasping. FES rehabilitation mostly relies on empirical settings,
4545populations and muscles. Empirical settings often cause overstimulation and premature fatigue [ @ibitoye2016strategies ] ,
4646shortening rehabilitation sessions and diminishing therapeutic benefit. Consequently, advanced control approaches like
4747optimal control-driven FES are gaining interest in personalizing and improving FES rehabilitation efficiency, meanwhile
48- delaying muscle fatigue. To address this need, we designed ` Cocofest ` (Custom Optimal COntrol for Functional Electrical
49- STimulation), an open-source Python package for optimal control-driven FES. ` Cocofest ` provides a framework to generate
50- personalized pulse trains (Fig. 1) based on nonlinear dynamics models for FES (Table. 1), for several musculoskeletal
51- models and motor tasks. The package provides more than 10 examples, covering optimization of FES-related pulse train
52- parameters (including frequency, pulse width, pulse intensity), FES model parameters identification from in vivo
53- measurements, and long duration predictive simulations.
48+ delaying muscle fatigue [ @ co2025optimal ] . To address this need, we designed ` Cocofest ` (Custom Optimal COntrol for
49+ Functional Electrical STimulation), an open-source Python package for optimal control-driven FES. ` Cocofest ` provides a
50+ framework to generate personalized pulse trains (Fig. 1) based on nonlinear dynamics models for FES (Table. 1), for
51+ several musculoskeletal models and motor tasks. The package includes over 10 examples, covering optimization of
52+ FES-related pulse train parameters (including frequency, pulse width, pulse intensity), FES model parameters
53+ identification from in-vivo measurements, and long duration predictive simulations.
5454
5555![ Pulse train parameters that can be optimized in Cocofest] ( pulse_train.png ) { width=90% }
5656
@@ -64,147 +64,97 @@ hindering standardization and cumulative progress [@co2025optimal]. To address t
6464scientific progress, ` Cocofest ` fulfills the following four needs:
6565
6666Firstly, the relationship between the pulse train parameters (e.g., frequency, pulse width and intensity; Fig. 1) and
67- the resulting muscle force, joint torque, and muscle fatigue (termed as state variables) can be modeled with different
68- nonlinear dynamics [ @ding2003mathematical ; @veltink1992nonlinear ] (Table 1) . Gathering them within a unified package
69- would facilitate comparison for more informed modelling choices.
67+ the resulting muscle force, joint torque, and muscle fatigue can be modeled with different nonlinear dynamics
68+ [ @ding2003mathematical ; @veltink1992nonlinear ] . Gathering them within a unified package would facilitate comparison for
69+ more informed modelling choices.
7070
71- Secondly, no study has compared different optimal control problem (OCP) formulations ap-plied to FES, due to OCP
71+ Secondly, no study has compared different optimal control problem (OCP) formulations applied to FES, due to OCP
7272implementation challenges [ @co2025optimal ] . Easily customizable OCP formulation, involving objective functions, models,
7373and transcriptions is required to provide an adequate research framework. Having the possibility to switch between
74- several OCP transcriptions, such as direct collocation or direct multiple shooting, is essential when dealing with stiff
75- differential equations [ @puchaud2023direct ] , often embed in FES models. Muscle fatigue is the primary challenge in FES.
76- Enabling the development and comparison of different OCP formulations could help address research questions, yield novel
77- stimulation patterns and enhance fatigue reduction. Moreover, using receding-horizon estimation for longer simulations
78- reduces the computational complexity associated with time-varying dynamics (e.g., fatigue) [ @ding2003mathematical ] .
74+ various OCP transcriptions (e.g., direct collocation or direct multiple shooting) is essential when dealing with stiff
75+ differential equations [ @puchaud2023direct ] , often embedded in FES models. Muscle fatigue is the primary challenge in
76+ FES. Enabling the development and comparison of different OCP formulations could help address research questions, yield
77+ novel stimulation patterns and enhance fatigue reduction. Moreover, using receding-horizon estimation for longer
78+ simulations reduces the computational complexity associated with time-varying dynamics (e.g., fatigue)
79+ [ @ding2003mathematical ] .
7980
8081Thirdly, predictive simulations of FES-driven or FES-assisted motions (e.g., walking, cycling, reaching, and grasping)
8182require the coupling of FES models with the equations of motion as well as adequate muscle force-length-velocity
82- relationships. Predictive simulations are usually actuated through Hill-type muscle models [ @wakeling2023review ] . A
83- package capable of replacing muscle actuation by FES models in multibody musculoskeletal models will allow us to
83+ relationships. Predictive simulations are usually actuated through Hill-type muscle models [ @wakeling2023review ] .
84+ A package capable of replacing muscle actuation by FES models in multibody musculoskeletal models will allow us to
8485simulate realistic FES-driven tasks.
8586
8687Fourthly, personalized rehabilitation strategy is required to facilitate the motor recovery. Therefore, identifying the
8788patient-specific muscle response to FES is a crucial step. Unfortunately, current complex identification methods are a
8889barrier to clinical translation [ @le2010identification ] . Providing a robust and customizable framework for the
8990development of more patient-friendly protocols would help to overcome this barrier.
9091
91- Overall, despite its potential, optimal control–driven FES remains unadopted in clinical practice due to its low
92- technology readiness level [ @co2025optimal ] . ` Cocofest ` is a comprehensive package designed to bridge the gaps and
93- foster clinical adoption. It integrates nonlinear muscle dynamics dedicated to FES, manages muscle fatigue, interfaces
94- FES with musculoskeletal models, supports customizable cost functions and parameter identification routines. With the
95- goal of bringing this technology to patient care, we believe this package will contribute to the open-science effort.
96- ` Cocofest ` is expected to accelerate the increase of technology readiness level by strengthening knowledge foundation.
97-
98- \newpage
99- ## An optimization example: Pulse width optimization to match a force profile using the @ding2007mathematical model
100-
101- This example shows how to optimize a FES pulse width using ` Cocofest ` , coupled with bioptim [ @michaud2022bioptim ]
102- version 3.3.0.
103-
104- ``` python
105- import numpy as np
106- from bioptim import (ControlType, ObjectiveFcn, ObjectiveList, OdeSolver,
107- OptimalControlProgram, Node, SolutionMerge)
108- from cocofest import ModelMaker, OcpFes, FesModel
109-
110-
111- def prepare_ocp (model : FesModel,
112- final_time : float ,
113- pw_max : float ,
114- force_tracking : list ) -> OptimalControlProgram:
115- """
116- Prepare the Optimal Control Program by setting dynamics, bounds and cost functions.
117-
118- Parameters
119- ----------
120- model : DingModelPulseWidthFrequency
121- The chosen FES model to use as muscle dynamics.
122- final_time : float
123- The ending time for the simulation.
124- pw_max : float
125- The maximum pulse width, used for stimulation bounds.
126- force_tracking : list
127- The force to track.
128-
129- Returns
130- -------
131- ocp : OptimalControlProgram
132- The Optimal Control Program to solve.
133- """
134- # --- Set dynamics --- #
135- # Create the number of shooting points for the OCP
136- n_shooting = model.get_n_shooting(final_time = final_time)
137- time_series, stim_idx_at_node_list = model.get_numerical_data_time_series(
138- n_shooting, final_time
139- ) # Retrieve time and indexes at which occurs the stimulation for the FES dynamic
140- dynamics = OcpFes.declare_dynamics(
141- model,
142- time_series,
143- ode_solver = OdeSolver.RK4(n_integration_steps = 10 ),
144- # Possibility to use a different solver
145- # ode_solver=OdeSolver.COLLOCATION(polynomial_degree=3, method="radau"),
146- )
147-
148- # --- Set initial guesses and bounds for states and controls --- #
149- x_bounds = OcpFes.set_x_bounds(model)
150- x_init = OcpFes.set_x_init(model)
151- u_bounds = OcpFes.set_u_bounds(model, max_bound = pw_max)
152- u_init = OcpFes.set_u_init(model)
153-
154- # --- Set objective functions --- #
155- objective_functions = ObjectiveList()
156- # Reshape list to track to match Bioptim's target size
157- force_to_track = force_tracking[np.newaxis, :]
158- objective_functions.add(
159- ObjectiveFcn.Mayer.TRACK_STATE ,
160- key = " F" ,
161- target = force_to_track,
162- node = Node.ALL ,
163- quadratic = True ,
164- )
165-
166- return OptimalControlProgram(
167- bio_model = [model],
168- dynamics = dynamics,
169- n_shooting = n_shooting,
170- phase_time = final_time,
171- objective_functions = objective_functions,
172- x_init = x_init,
173- x_bounds = x_bounds,
174- u_bounds = u_bounds,
175- u_init = u_init,
176- control_type = ControlType.CONSTANT ,
177- n_threads = 20 ,
178- )
179-
180- def main ():
181- final_time = 1
182- stim = 33
183- model = ModelMaker.create_model(" ding2007" ,
184- stim_time = list (np.linspace(0 , 1 , stim,
185- end- point = False )))
186-
187- # --- Building force to track ---#
188- time = np.linspace(0 , 1 , 34 )
189- # Example of force to track between 10 and 150 N
190- force = 10 + (150 - 10 ) * np.abs(np.sin(time * 5 ))
191- force[0 ] = 0.0 # Ensuring the force starts at 0 N
192-
193- ocp = prepare_ocp(model = model,
194- final_time = final_time,
195- pw_max = 0.0006 ,
196- force_tracking = force)
197- sol = ocp.solve()
198-
199- # --- Show the optimization results --- #
200- sol.graphs()
201-
202-
203- if __name__ == " __main__" :
204- main()
205- ```
206-
207- ![ Tracked force (grey dots) and optimized force generated (red) by pulse width optimization (blue).] ( force_profile.svg )
92+ Despite its potential, optimal control–driven FES remains unadopted in clinical practice due to its low technology
93+ readiness level [ @co2025optimal ] . ` Cocofest ` is a comprehensive package designed to bridge the gaps and foster clinical
94+ adoption. It integrates nonlinear muscle dynamics dedicated to FES, manages muscle fatigue, interfaces FES with
95+ musculoskeletal models, supports customizable cost functions and parameter identification routines. With the goal of
96+ bringing this technology to patient care, we believe this package will contribute to the open-science effort. ` Cocofest `
97+ is expected to accelerate the increase of technology readiness level by strengthening knowledge foundation.
98+
99+
100+ # State of the Field
101+
102+ Several open-source toolkits support optimal control computations for musculoskeletal biomechanics, such as:
103+ - ` OpenSim Moco ` [ @dembia2020opensim ] , a C++ OpenSim extension that enables motion tracking and prediction using efficient
104+ direct-collocation formulations coupled to nonlinear programming solvers.
105+ - ` SCONE ` [ @Geijtenbeek2019 ] , a C++/C predictive-simulation environment for human and animal motion that optimizes
106+ neuromusculoskeletal controllers to achieve task-level objectives (e.g., stable walking at a target speed).
107+ - ` Bioptim ` [ @michaud2022bioptim ] , a Python optimal-control framework for biomechanics that supports both direct collocation
108+ and multiple shooting, with flexible interfaces to nonlinear programming solvers.
109+
110+ However, these toolkits are not tailored for FES. They control muscle activation as a piecewise linear/constant
111+ excitation, whereas FES requires optimizing deliverable stimulation patterns under device and safety constraints. As a
112+ result, they lack reusable, validated components for the stimulation-to-force pathway and fatigue/recovery dynamics,
113+ limiting reproducible comparison of FES models and slowing translation to practical stimulation design. ` Cocofest `
114+ addresses this gap by implementing published FES models that can drive musculoskeletal models. This design supports
115+ reproducible comparisons of FES modeling assumptions and accelerates prototyping of patient- and task-specific
116+ stimulation optimization. ` Cocofest ` also includes utilities for model identification and receding-horizon optimization
117+ to support FES research workflows.
118+
119+
120+ # Software Design
121+
122+ ` Cocofest ` is a Python library that relies on Biorbd, a musculoskeletal physics engine [ @michaud2021biorbd ] , and
123+ Bioptim, an open-source optimization framework for biomechanical problems [ @michaud2022bioptim ] . Specifically, Bioptim
124+ enables easy OCP customization including cost functions, bounds, constraints, transcription methods (e.g., direct
125+ collocation), integration methods, and solving methods (e.g., full- and receding-horizon OCPs).
126+
127+ In conventional Hill-type muscle model, muscle force ($F_m$) is the product of $a$ the muscle activation, $F_ {max}$ the
128+ maximal isometric muscle force, $f_l$ the force-length, $f_v$ the force-velocity and $f_ {pas}$ the passive force-length
129+ relationship: $F_m(t) = a(t)\, F_ {\max}\, f_l(\tilde{l}_ m)\, f_v(\tilde{v}_ m) + f_ {pas}(\tilde{l}_ m)$. ` Cocofest `
130+ replaces $a(t)$ × $F_ {max}$ by the force obtained using FES models. This approach allows motions driven-FES simulations,
131+ meanwhile benefiting from musculoskeletal model properties (e.g., muscle insertion, inertial parameters).
132+
133+ ` Cocofest ` was developed to maintain a consistent structure between classes and functions to facilitate the OCP
134+ customization and new FES model implementation. This shared interface promotes reproducible work and comparisons of
135+ optimal control–driven FES strategies.
136+
137+
138+ # Research Impact Statement
139+
140+ ` Cocofest ` was developed to address several gaps in the literature, including the lack of systematic comparisons of FES
141+ models and OCP formulations, accessible tools for FES model identification, and open-source software for reproducible
142+ research. It enables researchers to generate personalized stimulation patterns, compare alternative OCP formulations,
143+ and simulate realistic FES-driven tasks. By providing a consistent software structure and clear documentation,
144+ ` Cocofest ` aims to streamline research workflows and support translation toward FES rehabilitation applications.
145+ Although the project is new and targets a niche domain, it already offers a shared, reproducible environment that can
146+ foster discussion, collaboration, and broader adoption of open-source practices within the FES community, which is an
147+ important step toward clinical translation of this technique [ @co2025optimal ] .
148+
149+
150+ # AI Usage Disclosure
151+
152+ The authors used ChatGPT only to improve the manuscript clarity and readability.
153+ After using this tool/service, the authors reviewed and edited the content as needed and took full responsibility for
154+ the content of the publication.
155+
156+ GitHub Copilot and ChatGPT were used to assist in code refactoring and documentation.
157+ Authors made all the core design and architectural decisions.
208158
209159
210160# Acknowledgements
0 commit comments