Central Pattern Generator for Rythmic Movements in C++

Markus Buchholz
8 min readAug 6, 2023


Central Pattern Generators (CPGs) are neural circuits or networks found in the central nervous system of animals, including humans. They are responsible for generating rhythmic motor patterns, such as walking, swimming, or breathing, without requiring constant input from the brain. CPGs play a crucial role in controlling basic rhythmic movements, and they are essential for maintaining coordinated behaviors.

All simulations discussed in this article you will find on my GitHub.

One example of a simple neural network organized as a Central Pattern Generator is the half-center oscillator. The half-center oscillator consists of two neurons that inhibit each other in an alternating fashion, leading to rhythmic pattern generation. This type of CPG is commonly found in the locomotor system of many animals, like the lobster.

CPGs are known for their ability to produce coordinated and rhythmic movements, which makes them highly relevant for soft robotics applications.

Let’s take an example of the lobster’s stomatogastric nervous system, which controls the rhythmic movements of its gastric mill and pyloric muscles. The gastric mill is responsible for chewing, while the pyloric muscles regulate the passage of food through the stomach.

In this example, we have two neurons, Neuron A and Neuron B, organized as a half-center oscillator:

  1. Neuron A: It activates the gastric mill muscles and inhibits Neuron B.
  2. Neuron B: It activates the pyloric muscles and inhibits Neuron A.

Here’s how the circuit operates:

  1. Initially, both Neuron A and Neuron B are inactive, and the gastric mill and pyloric muscles are at rest.
  2. Neuron A receives a weak external stimulus, causing it to fire. As a result, the gastric mill muscles contract and initiate the chewing motion. At the same time, Neuron A sends inhibitory signals to Neuron B, suppressing its activity.
  3. Due to the inhibition from Neuron A, Neuron B becomes quiet, and the pyloric muscles relax, allowing the passage of food.
  4. After a short delay, Neuron B’s inhibition on Neuron A wears off, and Neuron A becomes active again. Neuron A starts inhibiting Neuron B while activating the gastric mill muscles.
Central Pattern Generator concept (by author)

As a consequence, the pyloric muscles relax. This process repeats in a rhythmic pattern, creating a coordinated cycle of activity between Neuron A and Neuron B. The alternating inhibition and activation of the muscles result in rhythmic movements of the gastric mill and pyloric muscles, producing chewing and regulating the passage of food through the lobster’s stomach.

Simulation of lobsters' stomatogastric nervous system (by author)

The half-center oscillator in the lobster’s stomatogastric nervous system is just one example of how Central Pattern Generators can control rhythmic behaviors. CPGs can vary significantly in complexity and are found in various animals, playing a fundamental role in producing coordinated and rhythmic movements essential for survival and daily activities. They are an excellent example of how neural networks can generate and control rhythmic patterns without continuous input from higher brain centers, allowing for efficient and autonomous control of motor behaviors.

Kuramoto model

The Kuramoto model is a mathematical model used to describe synchronization phenomena in complex systems of coupled oscillators. It was proposed by the Japanese physicist Yoshiki Kuramoto in 1975. The model’s main idea is to study how a large number of interacting oscillators synchronize their phases over time.

In the Kuramoto model, each oscillator is represented by a phase variable θi​, where i denotes the oscillator’s index. The phase θi​ represents the state of the ith oscillator at a given time. The dynamics of the oscillator is governed by the following first-order differential equation:


  • ωi​ is the intrinsic (natural) frequency of the ith oscillator. It represents the oscillator’s preferred frequency in the absence of interactions with other oscillators.
  • K is the coupling strength, which determines the degree of interaction between the oscillators.
  • N is the total number of oscillators in the system.

The equation describes how each oscillator’s phase θi​ changes over time. The first term ωi​ represents the intrinsic frequency, causing the oscillator to evolve at its natural frequency. The second term,

represents the interaction with other oscillators.

The interaction term involves the sum of the sine of the phase differences between each pair of oscillators. This term quantifies how much the ith oscillator is influenced by the phases of all the other oscillators. When the oscillators are in sync (i.e., their phases are similar), the sum is maximized, leading to a stronger synchronization effect.

In the Kuramoto model, when the coupling strength K is strong enough, the phase differences between the oscillators tend to disappear over time, leading to synchronization. As time progresses, the oscillators adjust their phases in response to the coupling with other oscillators, and they eventually settle into a synchronized state where their phase differences become constant or stabilize at a specific value.

The synchronized state corresponds to a collective rhythmic behavior where all the oscillators in the system are locked in phase, exhibiting coherent and coordinated motion. This synchronization phenomenon is a common feature observed in many natural systems, such as the synchronization of firefly flashing, the rhythmic firing of neurons in the brain, or the synchronized motion of animal swarms.

We can describe the behavior of the three-neuron CPG network with the Kuramoto model using a system of differential equations. Each neuron’s (A, B, and C) phase will be represented by a differential equation that describes how the phase changes over time due to its intrinsic frequency and interactions with other neurons.

I found the solution of differential equations using the Runge-Kutta method.

Simulation of CPG described by differential equations (by author)

In the first example with three neurons, we used the Kuramoto model with Hodgkin-Huxley neurons to simulate the CPG. The Hodgkin-Huxley model introduces dynamic behavior to the neurons, allowing them to generate action potentials (spikes) and exhibit complex electrical behavior. The Kuramoto model then synchronizes the phases of these neurons, leading to rhythmic patterns in their activities. This rhythmic pattern can be interpreted as the rhythmic firing of motor neurons controlling leg movement in the walking robot.

In the second example with multiple legs, we have a more complex CPG with multiple oscillators, each representing one leg of the robot. We used the Kuramoto model with simple sinusoidal patterns for leg movements. The CPG generates rhythmic patterns for each leg’s movement, and by adjusting the natural frequencies, coupling strengths, amplitude, and phase offsets, we can create different walking gaits, such as walking, trotting, or galloping. The robot’s legs move in a coordinated and rhythmic fashion, mimicking the rhythmic pattern seen in natural locomotion.

Overall, the CPG provides a decentralized control mechanism for the robotic system, allowing it to generate rhythmic patterns of leg movements without requiring complex central computations for each leg. By using the CPG, the robot can achieve stable and adaptive locomotion patterns, even in changing environments, and it can walk or move with relatively simple control strategies.

In real-world robotics, CPGs are often combined with sensory feedback and other control mechanisms to achieve more sophisticated locomotion and adapt to various terrain conditions. The CPG concept provides a biologically inspired approach to control legged locomotion in robots, making them efficient, stable, and versatile in their movements.

Hodgkin-Huxley model

The Hodgkin-Huxley model is a mathematical model that describes the generation and propagation of action potentials in excitable cells, particularly neurons. It was proposed by Alan Hodgkin and Andrew Huxley in 1952 and has since become a fundamental tool in neuroscience for understanding the electrical behavior of neurons and other excitable cells.

The Hodgkin-Huxley model can be used to understand the dynamics of CPGs. In this context, CPGs consist of interconnected neurons that interact through synaptic connections. Each neuron in the CPG is modeled using the Hodgkin-Huxley equations, which describe the ionic currents that flow across the neuron’s membrane during an action potential.

The Hodgkin-Huxley model accounts for the following ionic currents:

  1. Sodium (Na+) current (INa), is responsible for the rapid depolarization phase of the action potential.
  2. Potassium (K+) current (IK), is responsible for the repolarization phase of the action potential.
  3. Leak current (IL), represents a small non-specific ionic current that contributes to the resting membrane potential.
Hodgkin-Huxley model (by author)

Membrane Potential (V),

Sodium Current (INa),

Potassium Current (IK),

Leak Current (IL),

m-gate (activation gating variable for sodium channels),

h-gate (inactivation gating variable for sodium channels),

n-gate (activation gating variable for potassium channels),


  • V is the membrane potential of the neuron.
  • C is the membrane capacitance.
  • I is the injected current, representing external inputs to the neuron.
  • gNa, gK, and gL are the maximum conductances of the sodium, potassium, and leak channels, respectively.
  • m, h, and n are the gating variables representing the fraction of open ion channels for sodium and potassium.
  • ENa, EK, and EL are the equilibrium potentials for sodium, potassium, and the leak current, respectively.
  • alpha_m, beta_m, alpha_h, beta_h, alpha_n, and beta_n are rate constants governing the kinetics of the ion channel gating variables.

The dynamics of the CPG are determined by the interactions between these neurons, their synaptic connections, and their intrinsic membrane properties. The network of neurons in the CPG generates rhythmic patterns of activity through mutual excitatory or inhibitory connections, resulting in coordinated rhythmic motor outputs.

By simulating the Hodgkin-Huxley model in a network of interconnected neurons, researchers can study how different patterns of connectivity and synaptic strengths give rise to specific rhythmic motor behaviors. This has been particularly important in understanding how animals produce rhythmic movements such as walking or swimming, as well as how these patterns can be modulated by sensory inputs and higher brain centers.

Overall, the Hodgkin-Huxley model has been a foundational tool in the study of CPGs and has greatly advanced our understanding of the neural mechanisms underlying rhythmic motor behaviors.

Hodgkin-Huxley model (by author)

Thank you for reading.