Introduction

Survival and adaptation are supported by the somatosensory system, which provides signals from the body surface to drive and refine behavior. It links the skin and central nervous system, recruiting reflexes and shaping behavior through learning and fine-tuning responses. For instance, a noxious stimulus at the skin should generate appropriate responses that minimize harm and increase the odds of survival. However, probing the somatosensory system in freely moving animals presents significant challenges due to the trade-offs between precision and preserving natural behavior.

Current methods for cutaneous stimulation usually necessitate direct physical contact, which restricts the range of behaviors and environments that can be studied. These methods often involve restraining animals or confining them to small chambers where stimuli are applied to their body. While valuable for understanding immediate responses, such approaches fail to capture the complexity of naturalistic behaviors. There is a growing need to study freely moving mice in dynamic, naturalistic environments (15), moving away from restraining and restricting behavior. However, delivering precise cutaneous stimuli to mice navigating complex settings, such as mazes, remains a significant challenge.

Experiments can proceed in more complex environments without researchers in proximity when cutaneous stimuli such as electric shocks are delivered via a grid floor. This approach has contributed significantly to our understanding over decades, being used in learning studies to probe cells and circuits underpinning aversion, avoidance, fear, expectancy, and memory formation and retrieval. However, the underlying somatosensory processes cannot be resolved due to a lack of spatial precision and control — they indiscriminately stimulate multiple body parts in contact with the floor.

There have been attempts to bridge these gaps by applying localized cutaneous stimuli (e.g. von Frey filaments) to moving rodents. This has been shown to be useful in recent studies of circuits involved in pain processing (6, 7). However, these approaches require experimenters to be in close proximity to the animals, to continuously observe their movements as they explore, and to manually touch a hind paw at regular intervals (8, 9). This can cause observer bias and limits both precision and the complexity of the environments.

To address these challenges, we developed a closed-loop system to automatically deliver submillimeter-precision cutaneous stimuli in a remote and dynamic manner, targeting mice as they freely explored environments. The system leverages advances in remote transdermal optogenetic stimulation (10) and real-time markerless body part (keypoint) tracking (11). The approach moves away from restricted and restrained behavior to enable behaviorally relevant stimulation in more naturalistic environments (3). This addresses the traditional trade-offs between precision and naturalistic environmental settings, providing a framework in which to integrate reflex recruitment, motivation, learning, and decision making.

Results

To develop closed-loop cutaneous stimulation in mice exploring large environments, we built a system that could rapidly track and target mice, and stimulate them remotely (Figure 1). We used real-time pose estimation to monitor body parts and target lasers for spatiotemporally precise thermal stimulation, and optogenetic stimulation of genetically-defined nociceptor fibers at the skin surface (10, 11). The approach was demonstrated in large environments, allowing automated cutaneous stimulation in an arena and during goal-directed behavior as mice run through a maze.

Closed-loop cutaneous stimulation of mice freely moving in naturalistic environments.

A Mice can be remotely targeted with cutaneous stimuli while freely exploring complex environments, such as a maze. B Schematic illustrating the closed-loop control workflow. A freely-moving mouse is recorded using a camera feed, enabling real-time pose estimation to track multiple body part keypoints. The extracted frame keypoint (x, y) of a selected body part is converted to pre-mapped x, y mirror galvanometer control signals to direct the laser beam paths. The movement of the galvanometer mirrors and triggering of the laser are determined by pre-programmed behavioral or environmental conditions, allowing stimulation to depend on behaviorally-relevant states: for example, if the mouse was performing specific actions (running, sleeping, grooming, rearing) or making choices (turning right in a maze, exploring a specific area of the environment). Flexible, state-dependent laser targeting was accomplished using an infrared laser for thermal stimulation and a blue laser for optogenetic stimulation of genetically targeted primary afferent neurons, enabling high spatiotemporal control of stimulation to small areas of skin.

A system for closed-loop cutaneous stimulation

We designed a system that automatically tracks targets for cutaneous stimuli across a large environment, leveraging our laser-mirror galvanometer design (10). The environment was set up on a large glass platform, enabling the recording of exploring mice could be recorded with a camera from below (Figure 2A,B). The camera enabled real-time tracking with DeepLabCut-Live! (11), estimating the keypoints of multiple body parts for every frame of the camera feed. The x and y keypoints were converted to pre-mapped control signals for x and y galvanometer mirrors to steer lasers as required. This provides a closed-loop system for dynamic control of stimuli according to behaviorally-relevant criteria.

A system for closed-loop cutaneous stimulation.

A Rendering of the system shows camera and stimulation optics 1 m below the glass platform, accommodating a large circular arena for freely-moving mice. B Side, rear, and aerial views. The blue laser beam (blue) was aligned to the galvanometer mirrors (GM) using mirrors (M1, M2), and lenses (L1, L2, L3) via ND filters (F). The infrared laser beam (red) is directed through a beam shutter with mirrors (M3, M4) and lens (L4). Converged beams in purple. C Average image of the laser across a linear voltage grid (left) and a pixel grid after mapping (left-middle). Pixel-voltage mapping corrects distortion (right-middle to right). D A mouse on the platform. E Tracking in the arena. F Galvanometer mirrors tracking the left hind paw keypoint. G 2D histograms of paw keypoints highlight the dwell of the locomotor stance phase compared to tail base motion. H Histogram of tail base speed indicating categories from four wild type mice (16,000 frames). I Paw traces illustrating the out-of-phase swing-stance during locomotion. J Traces showing alternating left and right paw movement. K Accuracy of the laser targeting the hind paws across speed categories. L Laser targeting mean average Euclidean error, MAE. See also related figure supplements 1 and 2.

Spatial and temporal characterisation of the closed-loop optical system.

A Uniformity of laser spot area and optical power. Heatmaps of mean measurements taken from triplicates at 16 separate locations encompassing the entire glass platform, and fit with a two-dimensional polynomial. B Galvanometer mirrors directed in a spiral formation across the glass platform. C The shift in the spatial calibration map was negligible as shown every week for 10 weeks during intensive use. Automatic remapping takes 30 minutes to complete. D The relative timings of the camera exposures (blue), mirror galvanometers (green) and laser (red). The acquisition frame time is shown in grey, and corresponding galvanometer mirrors jumps and laser pulse occur around 80 ms later. E Histograms of the dwell time for hind paws spent in the swing and stance phases during locomotion. F Accuracy of the laser targeting the fore paws across the speed categories, which was limited by left-right confusion in the tracking of small bodyparts.

Hardware and software information flow design.

Primary computer (C1) runs real-time pose estimation on the camera feed to predict multiple body part keypoints. These are converted to voltage signals at DAQ device (DAQ1), to control the galvanometer mirrors (GM) to target the laser spot coordinates. C1 also sends a trigger to DAQ1 to trigger the blue light laser or the infrared laser shutter via an Arduino UNO (Arduino 2). To generate blue light pulse trains following a trigger an Arduino was used (Arduino 1). The second computer (C2) interfaces with another DAQ device (DAQ2) to generate audio during experimental sessions. DAQ1 can also interface with DAQ2 to trigger audio depending on processor class conditions and for analog modulation of the blue light laser. The reward delivery system in Figure 4 is controlled via two Arduinos (Arduino 3 and 4) interfacing with DAQ1.

The system was optically precise. The glass platform was just less than 1 m above the galvanometers, resulting in a maximum focal length variability of 3.49%, minimizing differences in laser spot size across the stimulation plane. The absolute optical power and power density were uniform across the glass platform (coefficient of variation 3.49 and 2.92, respectively; Figure 2—figure supplement 1A). The laser spot size was set to 2.00 ± 0.08 mm2 diameter (coefficient of variation = 3.85) at the stimulation plane with a series of lenses along the blue light beam path (10). The laser spot could be moved along specific trajectories creating patterns (Figure 2C, Figure 2—figure supplement 1B). We used 10,000 x and y voltage pairs to jump the laser across the stimulation plane and map the voltages to corresponding pixels (Figure 2C). Surface fits resulted in a pixel-voltage mapping dictionary that minimized non-linear distortions. This resulted in a mean average Euclidean error (MAE) of 1.2 pixels (0.54 mm) between predicted and actual laser spot locations. The system and glass platform were stable, showing a displacement equivalent to about half a hind paw’s width (MAE = 1.94 ±2.61 mm) each week during intensive use of the system (Figure 2—figure supplement 1C) which can be corrected in less than 30 minutes by remapping.

The system could accurately target moving mice. Real-time estimation of keypoints (Figure 2D,E) was used for closed-loop control of the galvanometer mirror angles, resulting in pairwise correlations of R = 0.999 along both x - and y -axes (Figure 2F). Thus, the laser could be targeted in real time to body parts when certain programmatic criteria were met (see Figure 2—figure supplement 2 for the information flow). For instance, the laser beam could be triggered if the distance of an individual keypoint moves with variance ≤v for time ≥t and keypoint estimation likelihood was ≥l; where v, t, and l are user-defined variables. To determine the targeting accuracy, we used wild type mice that did not express ChR2 so that blue light pulses did not cause behavioral responses. The latency between acquiring a frame 1100 × 1100 pixels, estimating keypoints, and targeting a laser was 84 ± 12 ms (mean ± SD using 16,000 trials across 4 wild type mice; Figure 1D). This delay is sufficient to target paws: during locomotion, the hind paws were static for 350 44 ms in the stance phase and moving for 100 ± 1 ms in the swing phase (Figure 2—figure supplement 1E). The positioning of paws during the stance phase of locomotion creates ‘footprints’ in keypoint space, indicating moments when the paws are momentarily static even as the mouse moves (Figure 2G).

Mice move at variable speeds while exploring, which can be categorized (Figure 2H). While stationary (58 ± 7% of the time), the hind paws were static in 99.8 ± 0.1% frames, and this remains high even as speed increases: 95.7 ± 0.1% frames for ‘low’ speed (28± 4% of the time), 79.3± 0.1% frames at ‘medium’ speed (8 ± 2% of the time), and 60± 0.3% at ‘high’ speed (6 ± 1% of the time). Therefore, even during locomotion the hind paws are static long enough for stimulation with short-latency body part tracking (Figure 2I,J). The laser was successfully targeted to the hind paws with a high success rate when the mouse was moving at low speeds (95.5 ± 2.6%; Figure 2K), as expected the success rate was reduced during very fast running. We found zero keypoint confusion across all speeds, with 657 out of 657 trials successfully targeting the correct hind paw. The optical system targeted the hind paws of wild type mice exploring an open arena 650 ± 30 times within 5 minutes at 30 fps (n = 4 mice), providing multiple stimuli in short periods of time. We also targeted the fore paws but confusion between them resulted in 5.7 ± 2.2% targets being directed to the incorrect paw. This resulted in a low success rate compared to the hind paws (Figure 2—figure supplement 1F). Laser spots were delivered with high accuracy to targeted body parts, showing an small error of ≈ 0.7 mm MAE (Figure 2L). Thus, this design resulted in a fully automated system that facilitates submillimeter optical targeting of freely-moving mice in a large environments.

Cutaneous stimulation in large environments drives behavioral responses

We next used the closed-loop system to automatically deliver optogenetic cutaneous stimuli and examine the resultant behavior. A large circular arena was chosen to encourage free exploration and movement (Figure 3A,B) and behavior was examined by pose estimation, reconstructing the movement trajectories of body parts in the arena (Figure 3C). Brief transdermal optogenetic stimulation of nociceptors was used to achieve minimal cutaneous stimulation: mice expressed the blue-light sensitive opsin, ChR2, in nociceptors innervating the skin (Trpv1::ChR2; 10, 12, 13). Mice explored the arena and when stationary were stimulated on the hind paw with brief 10 ms pulses of light, delivered at intervals of at least 10 minutes (Figure 3B). We found that every trial accurately targeted and hit the hind paw (Figure 3D).

Cutaneous stimulation in large environments drives behavioral responses.

A Schematic of the open arena. B Protocol for minimal cutaneous stimulation using transdermal optogenetic activation of cutaneous nociceptors. C A single frame showing a mouse exploring the open arena (left). Keypoints for the left hind paw for 750 frames prior to and 1750 frames after the frame (1 min 23.33 s duration, middle). The body and head orientation at four time points are shown as orange rhombi connecting snout, left and right fore paw, and tail base (middle). Keypoint skeletons (right). D Representative images of a 10 ms laser pulse spot targeting the plantar surface of the hind paw in littermate (top) and Trpv1::ChR2 (bottom) mice. E Keypoint traces during stimulation of the left hind paw for two trials. F Example keypoint skeletons from Trpv1::ChR2 mice showing orienting behavior to hind paw stimulation (indicated by the blue arrow).

The submillimeter cutaneous input was mapped to motor output as coordinated behavior in freely moving mice. The brief stimuli caused paw withdrawals along with coordinated whole-body behaviors, such as rapid head orientation and body repositioning (Figure 3E,F). These behaviors were quantified as time-locked keypoint traces for each body part (Figure 3E,F). Our system enables fully automated and precise optical delivery of cutaneous stimuli to freely-moving mice while simultaneously recording and quantifying behavior.

Multi-animal stimulation for automatic nociceptive testing

The system could deliver cutaneous stimuli across a large space, evoking behavioral responses. Next, we demonstrate the flexibility of the system design and establish automatic nociceptive testing across multiple mice simultaneously. A method for random access targeting was developed (Figure 4A) to target nine mice (3 × 3 configuration) in individual chambers; we detected idle mice by analyzing motion energy in each chamber, then rapidly selected and cropped to one chamber for real-time pose estimation and stimulation (Figure 4B). This reduced the computational burden by decreasing image resolution, compared to running real-time pose estimation across the whole environment (11). The process operated as a loop, ensuring that automated stimuli were spaced by at least one minute apart for each mouse.

Multi-animal stimulation for automatic nociceptive testing.

A Concept of the random access multi-animal stimulation. Motion energy was used to detect idle mice in multiple chambers, randomly selecting and cropping to one chamber for real-time pose estimation and stimulation. A laser spot was targeted to the hind paw of the mouse placed in the chamber. The process looped through each of the chambers, automatically targeting and stimulating the mice. B An example camera frame from below the stimulation platform, illustrating each chamber in different colors (left). Motion energy and body part keypoints shown for an individual chamber (right). C Representative paw responses and body repositioning following thermal stimulation (10 s pulse; top) and optogenetic stimulation (3 ms pulse; bottom). D Representative paw responses during thermal stimulation of wild type mice. Two motion energy traces are shown in the top panel, while two traces plotted with keypoints are displayed in the bottom panel. The grey dashed line indicates laser stimulation onset. The orange dashed line indicates the motion energy response threshold used to determine paw movement. E Cumulative distribution of paw response latencies to thermal stimulation from one mouse (10 trials). F Representative hind paw responses following optogenetic stimulation of Trpv1::ChR2 mice. The grey dashed line indicates stimulation onset. G Cumulative distribution of paw response latencies to optogenetic stimulation in 9 Trpv1::ChR2 mice (181 trials, range of 15 to 24 trials for individual mice). Response latency followed the rank order of the optogenetic stimulation intensity.

To test the system we used: 1) thermal stimulation with a 10 s pulse of infrared light (785 nm) on the hind paw of wild type mice; and 2) optogenetic stimulation of cutaneous nociceptors with a 3 ms pulse of blue light (473 nm) on the glabrous plantar surface of the hind paw of Trpv1::ChR2 mice. This greater than 3000-fold difference in pulse duration demonstrates the temporal resolution afforded by optogenetics. We varied the intensity of the optogenetic stimuli using 10 Hz pulse trains (0.5 - 8 mW/mm2) compared to a single pulse at higher intensity (40 mW/mm2).

Thermal and optogenetic stimulation induced similar nocifensive behaviors, including paw responses and whole-body movements (Figure 4C). This can be seen from traces of hind paw movement following thermal or optogenetic stimuli (Figure 4D,F). Mice concurrently turned their heads toward the stimulus location while repositioning their bodies away from it, consistent with previous studies demonstrating the coordination of whole-body behaviors like head orienting with local reflexes on a sub-second timescale (12, 14). In the case of thermal stimulation, the traces of the hind paw movements and cumulative distributions demonstrated nocifensive behaviors in response to infrared stimulation (Figure 4D,E). Optogenetic stimulation-induced response latencies followed the rank-order of stimulus intensity, illustrating the dynamic range possible with this system (Figure 4G). In contrast, littermate control mice did not exhibit responses (10, 12).

This demonstrates the system is both versatile and precise, enabling the study of local paw withdrawals and whole-body movements including head orientation and body repositioning. The platform is large enough to accommodate 25 mice (5 × 5 configuration) in a single session, enabling high-throughput experiments.

Closed-loop cutaneous stimulation in mice running through a maze

Next we demonstrate that freely-moving mice can be stimulated with submillimeter-precision as they were running through a maze environment. The ability to deliver cutaneous stimuli to mice moving through behaviorally-relevant tasks has previously been a significant challenge and experimenters typically apply stimuli to moving mice manually (69).

To motivate movement in a task, we built a novel maze that encourages alternation between two rewards at separate locations. The maze had one-way doors, ensuring that after making a decision to turn left or right, mice could obtain a reward but are required to circle back to the maze’s start point to re-initiate the action-reward cycle (Figure 5A,B). The reward ports were activated by a brief nose poke and delivered a drop of sucrose water, followed by a timeout period. In addition to the timeout, the reward ports were only reset once the mouse exited the reward chamber through a one-way door, as determined by real-time keypoint tracking. The combination of a long timeout and required exit from the reward chamber rendered it more time-efficient to cycle between the two reward chambers and encouraged running along the corridors.

Closed-loop cutaneous stimulation in mice running through a maze.

A Schematic of the maze design. A single trial was defined as the collection of a single reward, indicated by the orange and green arrows. The left and right corridors leading to the reward chambers were paired with stimulation of nociceptors using transdermal optogenetics. B Maze renderings from aerial, front, and side views. Mice entered via an entry chamber leading to a corridor and junction, choosing left or right through one-way doors. A sucrose-water reward awaited in the reward chamber, with exit through another one-way door. C Total number of rewards collected (left and right-hand side reward ports combined) for each training session. D Movement trajectories over an entire session (left) and a single trial (right). Trajectories are shown from one mouse for the first stimulation session. E Frame sequences (0.2 s apart) from four trials in four mice show runs along maze corridors toward the reward chamber. Blue arrows indicate targeted stimulation. F Relative timings of corridor entry and subsequent reward collection (n = 4 mice). G Transition matrix showing mice predominately alternate between rewards at the left and right reward ports. H Example movement trajectories (tail base) in the left and right corridors from one mouse (left). Bar plots showing path coherence and speed in the high stimulation corridor relative to the low stimulation corridor (right).

Mice were trained over three sessions in the maze. In the first training session, they rapidly navigated the one-way doors with little delay after a few attempts. This quickly led to the use of reward ports in the chambers on either side of the maze. The first reward was collected after 440 ± 117 s (4 mice). By the third training session, this time decreased to 82 ± 38 s, suggesting rapid learning. During the reward port timeout, mice typically explored the corridor connected to the reward chamber or exited it to explore the entry corridor. By the third day of training, the number of completed trials had increased for all mice. On average, mice completed 64 ± 24 rewarded trials in the third training session (<2 hour in duration), although individual performance varied considerably. For example, one mouse completed only 10 rewarded trials in the third session and another mouse completed 97 trials (Figure 5C).

Mice could be accurately stimulated while they were running. Example movement trajectories from one mouse are shown in Figure 5D. Using Trpv1::ChR2 mice, we demonstrated the system’s utility in studying localized cutaneous hypersensitivity. We employed a widely used model of inflammatory pain with a unilateral injection of complete Freund’s adjuvant (CFA) in the hind paw. A brief (3 ms) nociceptive stimulus was successfully targeted to the contralateral (right, non-injected) paw with negligible confusion between paws (705/706 stimuli targeted the correct hind paw). This allows for future studies where phasic and tonic pain can be separated. Despite ongoing hypersensitivity and phasic stimuli, mice remained actively engaged in the task, consistently collecting rewards sequentially from each side, as per their training (Figure 5F,G). Their rapid movement along the stimulation corridors (maximum speed of 241 ± 53 mm/s) required precise targeting (Figure 5E).

We found that nociceptive stimuli could slow goal-directed movement, highlighting their role in disrupting locomotion. The stimulation resulted in reflexive responses that interrupt locomotion as mice orient in response to the nociceptive stimuli, presumably to investigate the stimulated site. This resulted in slower locomotion speeds (p = 0.037 with paired t-test, n = 4 mice) and more variable trajectories along a higher frequency stimulation corridor (Figure 5H). Once past the stimulation corridors the mice successfully collected a reward in almost all trials. Accurately targeting and delivering cutaneous stimuli to mice as they explore naturalistic and complex environments could provide insights into how sensory inputs shape goal-directed behavior.

This establishes a robust system and framework for future investigations into complex behaviors, including decisionmaking and learning in the context of pain and somatosensation.

Discussion

The somatosensory system provides a critical link between the brain, body its immediate external environment. The complex ways in which this system supports movement, learning, and action in rodents have historically posed substantial methodological challenges. Traditional methodologies have varied widely, encompassing both innovative and practical approaches — from stimulation of whiskers or skin in head-fixed animals (13, 1522) to the more straightforward manual touching of paws in mice (6, 7, 9, 23). These approaches, however, have inherent limitations in replicating the dynamic and complex interactions experienced in naturalistic environmental settings. In response to these limitations, we have developed a system that enables cutaneous stimulation in more complex environments. This closed-loop system automatically tracks, targets, and stimulates mice remotely so that it is now possible to study the somatosensory system in naturalistic environmental settings.

Generating cutaneous inputs in freely moving mice requires stimuli that are spatially and temporally precise. We achieved millisecond-timescale stimulation of small skin areas using transdermal optogenetics (‘remote touch’; 10). Opsins were genetically targeted to specific afferent fibers innervating skin and activated with light targeted precisely via a laser in free-space. This beam path was aligned to a second laser system and employed for thermal stimulation on a timescale of seconds. Delivery of these stimuli was controlled by a feedback system consisting of three main components: (1) real-time tracking infers the keypoints of various body parts, continuously transmitting this data to the controller; (2) user-defined policies determine the stimulation conditions (spatial, temporal, and physiological state-dependence), providing control signals for the actuators; and (3) mirror galvanometers target the beam path to specified keypoints and signals trigger light delivery, following which real-time tracking is then resumed. We demonstrate that this system can precisely target the hind paw for stimulation, even as the mice are in motion.

We demonstrate the versatility of the system from automated multi-animal nociceptive testing to sparse stimulation of freely moving mice in a circular arena. We show that stimuli could be targeted with high accuracy and resulted in immediate behavioral responses that could be mapped. The system was used to deliver cutaneous stimuli to mice running through a maze. Mice were trained on an alternation task with stimuli applied en route to the reward, thus separating choice, punishment, and reward in a naturalistic environment. Mice with a model of inflammatory pain still readily engaged in this task during noxius cutaneous input (punishment). The recruitment of reflexes during locomotion caused immediate evaluative behavior that temporarily disrupted goal-directed behavior. Achieving such localized stimulation has been challenging with traditional methods: electric grid floors generate whole body stimuli that are considered incompatible with models of chronic pain that generate unilateral hind paw hypersensitivity; and manual stimulation lacks capacity, reliability, and is potentially confounded by experimenter and observer biases. Our system addresses these issues, allowing for the dissociation of touch, phasic pain, and tonic pain to better understand their relationships with behavior.

Stimulation of the body and paws enables the study of pain, touch, thermoception, and movement (2435). Paw stimulation is also ubiquitous in aversive learning and memory, using a crude shock stimulation with a grid floor (36, 37), and can now be carried out with microsecond and submillimeter precision. The stimulation can be static or dynamic and localized to small areas on the body in an automated manner. Automation improves the spatiotemporal precision of stimulus delivery compared to traditional manual methods; it reduces labor and enhances the reliability of the data. All experiments were conducted remotely from an adjacent room to minimize potential observer effects and biases on the mice. Automated nociceptive assays have principally focused on the initial rapid movements elicited by stimulation of mice in small chambers (3840). Here, we provide an approach to examine how these rapid movements are embedded within complex behavior in naturalistic environments, opening new ways to investigate nociception, and somatosensation more broadly.

While we demonstrate the utility of the optical system using nociceptive stimuli, this system can deliver various cutaneous inputs by targeting specific afferents for selective opsin expression, whether they are thermoreceptive, chemoreceptive, or mechanosensitive (10, 3841). Such “pure” stimuli do not occur in nature but offer crucial spatial, temporal, and genetic precision (36, 42, 43). Our system enables delivery of multiple wavelengths of light separately or together, to support combinations of opsins from the vast optogenetic toolbox. Opsins can be used to activate or silence neurons, with a range of kinetic properties, diverse light wavelength profiles allowing multi-color manipulations, or control different downstream signaling effectors (44). Thermal stimuli are also used routinely in research (15, 38, 45) but slow thermal dissipation can require mice to be stationary for consistent stimulation. Automation provides opportunities for development of analgesics, particularly for integrating reflexes with spontaneous, free operant behaviors. Indeed, cutaneous stimulation in naturalistic environments can be readily combined with approaches to quantify behavior (4653).

The system has many applications for sensorimotor reflexes, perception, memory, learning, and action. It is flexible enough to trigger stimulation based on various states, including periods of inactivity or locomotion, at specific spatial locations, and with precise timing. Future work made possible by this system is expected to include examining how cutaneous input can interrupt and modulate specific swing phases (33), self-grooming, posture states (12), and other spontaneous behavioral syllables (48). It can facilitate investigations of naturalistic learning, whether through mazes, social interactions, or engagement with the environment or objects (54, 55), and of sleep fragmentation (56), anxiety (57, 58), fear (36), stress (37). Finally, it has potential to provide free operant methods for analgesic development for chronic pain. These directions leverage tools for mechanistic dissection of cell and circuit biology in the context of naturalistic behaviors.

Establishing how behavior is shaped by somatosensation requires that mice can be stimulated while freely behaving. We describe a system that addresses this need, delivering cutaneous stimuli in a manner that is precise, remote, state-dependent, dynamic, and fully automated to target freely-behaving mice that are actively exploring complex environments.

Methods

Animals

Mice were housed at 21 ± 2°C, 55% relative humidity, following a 12-hour light: 12-hour dark cycle with ad libitum access to food and water. Optogenetic experiments were performed using mice with ChR2 selectively expressed in nociceptors (Trpv1::ChR2). Heterozygous Trpv1-Cre mice, which have Cre recombinase inserted downstream of the Trpv1 gene (RRID:IMSR_JAX:017769, B6.129-Trpv1tm1(cre)Bbm/J; 59), were crossed with mice homozygous for Cre-dependent ChR2(H134R)-tdTomato (RRID:IMSR_JAX:012567, Ai27(RCL-hChR2(H134R))/tdT-DChR2-tdTomato; 60). This produced progeny heterogeneous for both transgenes (Trpv1::ChR2) and control littermates that do not encode Cre recombinase but do encode Cre-dependent ChR2-tdTomato. Blue light directed to the glabrous plantar surface of the hind paw in Trpv1::ChR2 mice results in the direct time-locked activation of broad-class nociceptors with single action potential resolution (12). Experiments with the infrared (IR) laser were performed using wild type mice (RRID:IMSR_JAX:000664, C57BL/6J). Equal numbers of male and female adult mice were used (aged between 6 and 40 weeks), with 2 - 5 cohorts of mice per experiment. All animal work was carried out according to the UK Animal Scientific Procedures Act (1986), approved by the UCL Animal Welfare and Ethical Review Body (AWERB) and performed under licenses released by the UK Home Office.

Design and development

Several substantial improvements were made to the optical design (10) to enable automated, multi-color, closed-loop optical stimulation across a large environment. Part lists are provided in Tables S1, S2 and S3 (Supplementary file 1).

The optical system was mounted on a large aluminum breadboard (0.75 m x 0.75 m) to provide more space for optical components and stability to the large glass platform. The diode laser beam (blue light, 473 nm, Cobolt, 06-01 MLD) was focused to the center of the galvanometers using two broadband dielectric mirrors (M1 and M2) via an axial adjustable lens (L1, 30 mm focal length), a collimating lens (L2, 150 mm focal length), a long focal length lens (L3, 500 mm focal length). We added a second laser beam path to enable multi-color stimulation, using separate mirrors and lenses and an appropriate dichroic mirror. The infrared (IR) laser (785 nm, SLOC, RLM785TA-1500) beam passed through an optical beam shutter (Thorlabs, SH05RM) to pulse the light with a controller (Thorlabs, KSC101). Two additional mirrors (M3 and M4) aligned the IR beam through a long focal length lens (750 mm) to the DM, where the beam path was aligned to converge with the blue light laser beam path into a pair of galvanometer mirrors (GM).

For the large environment, a 0.55 m x 0.55 m glass stimulation platform was held in place above the optical components via a vertical optical construction rail (95 mm x 95 mm x 1500 mm) attached to the aluminum breadboard, as shown in Figure 2A. Aluminum construction rails (25 mm x 25 mm x 500 mm) were secured at each corner of the glass platform frame and the opposite side of the platform to the optical rail to ensure stability. The blue light laser spot size (1/e2 width) was calibrated to 2.3 mm2 using the non-rotating L1 adjustable lens housing and an optical beam profiler (BP209-VIS/M, Thorlabs). For the experiment using the IR laser, two near-IR hot mirrors (Thorlabs, FM201) were placed on top of the USB 3.0 camera (acA1920-40um camera, Basler) lens to minimize how much IR light was imaged by the camera.

For real-time markerless pose estimation to support automated, closed-loop stimulation, an additional camera was positioned below the glass stimulation platform. behavior was captured at 30 frames per second (fps) via a USB 3.0 to the primary computer (C1), which controls video recording, pose estimation, calculations, and directs the galvanometer mirrors to target lasers.

Optical system calibration

The optical parameters of the system were characterized using the blue laser due to the high quality beam. The uniformity of blue light diode laser spot size across the glass stimulation platform was measured with an optical beam profiler (Thorlabs, BP209-VIS/M) placed at 16 locations across the platform. The beam profiler aperture was positioned at these locations using a custom laser-cut acrylic plate. Laser power was attenuated by 25% with an ND filter (Thorlabs, NE506B, optical density 0.6) to be within the operating range of the beam profiler. Absolute power (mW) at the 16 locations was assessed with a S121C photodiode measured by an optical power meter (Thorlabs, PM100D). The laser beam area and the optical power meter were used to calculate power density (mW/mm2) at each location (Figure 1A).

There was negligible distortion in the acquisition camera across the glass platform. This was determined by imaging a chessboard camera calibration pattern of 20 mm x 20 mm squares in a 14 × 10 grid at 5 different locations across the glass. OpenCV was used to measure square sizes and we calculated the min-max range of all squares was <1 pixel, at

0.89 pixels, which is considered negligible. The Euclidean norm was computed for a matrix of the corners of all squares, providing a scale factor of 0.45 mm/pixel.

To generate a pixel-voltage coordinate dictionary that can be used to convert x,y pixel coordinates to x,y galvanometer voltage coordinates, the following steps were carried out. First, the galvanometers were raster stepped to direct the blue laser spot to a grid of 10,000 points (100 × 100), capturing these with the pose-estimation camera. For every point of the raster, the x,y voltages were mapped to the peak intensity pixel. A x,y voltage was then computed for every pixel by interpolation, fitting with a two-dimensional polynomial equation. This automated procedure took <30 minutes and resulted in a pre-computed pixel-voltage dictionary. Entering an x,y coordinate for a body part, inferred from the camera feed, returns the interpolated x,y voltages to target the laser to the same location. We repeated the mapping once every week over the course of 10 weeks to ensure the stability of the mapping. This was done during extensive experimentation to account for potential movements during cleaning and changes in arenas.

Pose estimation

Training a DeepLabCut network model

DeepLabCut installation (v2.2.0.2; 46, 61) was coupled to Tensorflow-GPU (v2.5.0, with CUDA v11.2 and cuDNN v8.1). Training of the DeepLabCut neural network model was used with default network and training settings in an Anaconda environment with Python v3.8.13 installed. Videos were selected based on their representation of the whole breadth of behavioral responses, and k-means clustering was used to select the training images. 437 frames were labelled from 22 selected videos, and the network was trained for 200,000 iterations. Following further optimization of lighting, 210 frames from 11 additional videos were manually labelled and machine labels from 171 outlier frames from 9 videos were manually refined. These were fed back to the training dataset and the network retrained for a further 200,000 iterations. Training resulted in an MAE of 3.29 pixels, which is comparable to human ground truth variability quantified elsewhere (see 46). This model was used for all pose estimation. The video resolution (1920 × 1200) required a processing time higher that the frame interval (33.33 ms), resulting in real-time pose estimation on a sub-sample of all frames recorded. Therefore, post-hoc pose estimation was carried out to analyze all frames.

Real-time tracking

DLC-Live! SDK (v1.0; 11) was installed on a computer with fast processing capabilities (AMD Ryzen 5 3600 Six Core CPU (3.6 GHz - 4.2 GHz), NVIDIA GEFORCE RTX 2080 Ti GPU, quad-core RAM (64 GB), Windows 10, custom manufactured by PC Specialist Ltd.) in an Anaconda virtual environment (Python v3.7.10) with DeepLabCut (v2.1.10.4) installed. DLC-Live! SDK installation was coupled to Tensorflow-GPU (v1.13, with CUDA v10 and cuDNN v7.4). Integration of the Basler camera and the DLC-Live! GUI (DLG) utilized Python wrapper, pypylon (v1.7.2, Basler), to facilitate communication with the pylon Camera Software Suite through a Linux subsystem in Windows 10 (WSL Ubuntu, v20.04). The trained DeepLabCut network model was loaded into the DLG, which captures the data from the camera and performs real-time pose estimation on the incoming camera feed. Custom code was written in Python for each experimental design; this comprised the conditions that defined the behavioral protocol and controlled stimulation as required (see Figure 22).

Optical system characterization

We characterized the latencies for real-time tracking, targeting, and stimulation. Control signals for the camera, mirror galvanometers, and laser were measured simultaneously at 100 kHz using a Digidata 1440a (Molecular Devices). During the exposure of each 5 ms frame, the tracking camera sent a voltage signal from its GPIO. The x - and y -axis scanner position outputs from the two mirror galvanometer drivers were used to monitor the movement of the mirrors. A 1 ms laser signal was sent to a microcontroller (Arduino UNO) to generate parallel digital outputs, which triggered the laser and monitored its timings. All four control signals were recorded during four 5-minute sessions with a wild type C57BL/6J mice exploring a circular arena. The tracking camera was set to record at a resolution of 1100 pixels x 1100 pixels, with a 5 ms exposure at 30 fps. The processor code identified frames with a likelihood >0.8 for the ‘left_hindpaw_mid’ keypoint. The x,y pixel location was then converted to mirror galvanometer x,y voltage signals using a pixel-voltage coordinate dictionary. A multifunction DAQ device (USB-6002, National Instruments) was used to send these x,y voltages and subsequently a 1 ms command to the laser-triggering microcontroller. The laser was triggered only if more than 500 ms had passed since the previous stimulation. The camera signal confirmed an exposure time of 5 ms and a frame rate of 30 fps. The latency between camera acquisition and stimulation was calculated by collecting timestamps immediately after stimulation and comparing these to the frame timestamp on which pose estimation was carried out. The latency between galvanometers moving and laser stimulation was determined by comparing the timings of galvanometer jumps and laser signals. This delay was 3.3 ± 0.5 ms (mean ± SD, for 245 trials). To synchronize the four voltage signals with frame and stimulation timestamps, we determined the timing of the first galvanometer jump when pose estimation was initialized.

The accuracy of real-time tracking for the ‘left_hindpaw_mid’ keypoint was assessed by manually identifying its coordinates (ground truth) and comparing these to the coordinates predicted by the DeepLabCut network model in real time on frames extracted from 5 videos of different mice exploring an open arena. Frames with a likelihood >0.8 were selected, as in experimental protocols. Euclidean distances were calculated pairwise between ground truth coordinates and model-generated coordinates, and averaged to give the mean average Euclidean error (MAE). The MAE between the predicted and actual coordinates was 1.36 mm (calculated on 1,281 frames).

The accuracy of body part targeting was determined using a high-speed Basler acA2000-165umNIR camera recording frames at 648 × 650 pixels, 270 fps during the 5-minute sessions described above. We used a >0.8 likelihood for the ‘left_forepaw’ keypoint in additional sessions. High-speed recordings captured each 1 ms laser pulse, and frames containing these pulses were identified using the reflection of the laser. We manually assessed 1279 frames and classified them as a ‘hit’ or ‘miss’, and whether a ‘hit’ was on the targeted paw to quantify confusion during keypoint tracking.

The accuracy of hitting the body part depended on how fast the mice were moving. To demonstrate this, we segmented the keypoint series using four speed categories: stationary, low, medium, and high. Speed was calculated using the Euclidean distance the ‘tail_base’ keypoint moved in each frame, divided this by the time elapsed, and smoothing the speed with a 10-frame rolling mean filter. The speed histogram informed the windows of categories (<20, 20-120, 120-220, >220 pixels per second, for stationary, low, medium, and high, respectively). The accuracy of hitting the ‘left_hindpaw_mid’ keypoint was calculated on frames across each speed category: 156 frames for stationary, 159 frames for low, 155 frames for medium, and 187 frames for high. Similarly, the accuracy of hitting the ‘left_forepaw’ keypoint was calculated on 155 frames for stationary, 156 frames for low, 155 frames for medium and 156 frames for high.

The keypoint-laser spot error for the ‘left_hindpaw_mid’ keypoint was determined in the same frames by manually identifying the body part coordinates (ground truth) on frames immediately prior to stimulation and the coordinates for the laser spot on stimulation frames. These estimates were first made for all pre-stimulation frames and then for the set of stimulation frames. The mean average Euclidean error (MAE) was approximately 0.7 mm across all locomotion speeds categories (463 frames).

Multi-chamber real-time pose estimation

To target and stimulate individual mice when multiple mice were present in chambers on the stimulation platform, we performed chamber-based cropping and subsequent real-time pose estimation. Nine mice were placed into nine chambers (100 mm x 100 mm wide, 120 mm tall), we monitored the motion in each chamber to find mice that were ‘idle’, the camera feed was cropped, body parts estimated, and the laser targeted to the hind paw coordinates.

The frame-to-frame absolute difference in pixel values (motion energy) was calculated in each region of interest for the individual chambers. Background noise was removed below <10 motion energy and the mouse was defined as ‘idle’ if the summed motion energy was less than a specified threshold (30,000 motion energy) for 2 seconds. Idle mice that had not been stimulated in the previous 10 seconds were pseudo-randomly selected and their chamber cropped. The pose estimation (x, y) coordinates generated by the DeepLabCut network model were used to target the laser to the hind paw. We modified the following scripts in the dlclive and dlclivegui packages in DLC-Live! SDK to develop the multi-chamber real-time tracking approach: dlclive, utils (dlclive package) and pose_process (dlclivegui package).

Assembly of a naturalistic task

The maze was constructed of 3 mm matte black acrylic (200 mm in height) and measured 500 mm x 180 mm (inner dimensions). The maze was constructed as a single junction, with 40 mm width corridors forming two chambers (70 mm x 100 mm) at either end of the junction corridors. The entrance to the maze was connected to a transparent acrylic chamber (100 mm x 100 mm, 130 mm tall). There were one-way doors (100 mm tall, 30 mm wide) designed as a push-through flap cut from 0.5 mm styrene and secured to the door frame with butterfly pins. The one-way doors were positioned at the junction and to leave the reward chambers; this created a one-way system, so once the mouse exited either chamber it was required to go back around the maze and through the junction decision point to re-enter the reward chamber. Each chamber contained a rectangular opening (20.5 mm x 11.5 mm) through which a water delivery port (Sanworks mouse behavior port) was fixed to the walls to allow the mouse to collect rewards. A water reward (∼ 5 µl of 10% sucrose water) was delivered when the mouse’s nose broke the IR beam in the water delivery port. The reward delivery system was controlled with an Arduino. In addition to a reward timeout period of 45 - 60 s, the mouse was required to leave the chamber before the water reward port was reset and another reward could be collected.

Behavioral protocols

Experimental room, arena and cleaning set-up

The experimental room was maintained at 21°C with relative humidity between 45 - 65%. All behavior experiments on the system were performed in custom-built arenas laser cut from matte black acrylic and placed on the glass stimulation platform. Two infrared LED panels illuminated opposite sides of the arena to optimize lighting and achieve high contrast images. White noise at 68 dB was generated with custom Python code, through a L60 Ultrasound Speaker (Petterson Elektronic AB) via a second DAQ device (USB-6211, NI) and amplifier. The white noise played continuously through the duration of the habituation sessions and the experiment. The glass stimulation platform was cleaned twice with 70% ethanol, while the acrylic arena was cleaned twice with an odorless surface disinfectant between each animal to minimize olfactory cues. The lasers were targeted to the hind or fore paw glabrous skin in all experiments, contingent on meeting specific conditions defined in the protocol.

Habituation

Animals were placed in custom matte black acrylic chambers (100 mm x 100 mm, 80 mm in height) placed on a von Frey wire mesh grid and underwent two habituation sessions to the experimental room for 1 - 2 hours. Mice also underwent 1 or 2 handling sessions prior to experiments.

Minimal cutaneous stimulation in an open arena

Mice were placed in an acrylic arena painted matte black (500 mm outer diameter, 150 mm in height, 5 mm thick). Dividers (160 mm tall, 116 mm wide, matte black, 3 mm thick) were slotted onto the arena wall to separate the arena into 6 segments to enrich the environment. Mice were allowed to freely explore for 60 minutes. Individual 10 ms duration blue light laser pulses were remotely targeted to the left hind paw with a ≥ 10-minute interstimulus interval. Each stimulation was delivered contingent on the conditions that the hind paw was still and had not been stimulated <10-minutes prior. The hind paw was considered still when both the standard deviation of its keypoint (x,y) was <1 pixel and the likelihood of this keypoint was >0.8 throughout a 2 s period. Stimulation was repeated over two sessions on consecutive days. Data was collected from 26 mice in total from 5 different cohorts. 16 Trpv1::ChR2 were split into two groups: 10 mice received blue light stimulation, and 6 mice received no stimulation as control. 10 littermate controls that received blue light stimulation were also used.

Somatosensory stimulation in a maze

Mice were first habituated to the maze without any doors during a 1 hour session. On three separate days following this, they underwent 3 training sessions with the one-way doors in place. Mice were water-deprived 16 to 18 hours prior to each experimental session to motivate the use of water rewards. A trial was defined as the mouse successfully collecting one reward; the collection of multiple rewards required the mouse to leave the reward chamber. Mice that had not made >10 trials by the third training session were excluded from the subsequent stimulation sessions due to poor engagement. 7 out of 12 Trpv1::ChR2 mice from 4 cohorts passed this criteria. As proof-of-principle for precise contralateral stimulation in the context of a unilateral pain state, mice received 7 µL of complete Freund’s adjuvant (CFA) via intraplantar injection in the left hind paw. CFA-injected mice showed significant mechanical allodynia compared to saline controls (p = 0.039 with Mann-Whitney test, n = 4 mice). After baseline measurements of mechanical sensitivity, mice were injected with CFA and mechanical allodynia was evaluated in both hind paws 2 days following injection. Mechanical allodynia resulting from injection of CFA into the left hind paw was measured by von Frey testing (Up-Down method). Mice were placed in individual chambers (100 mm x 100 mm) on a mesh wire floor and habituated to the test setup prior to testing. The von Frey test was conducted blind to experimental groups. Mice underwent two stimulation sessions in the maze, in which optogenetic stimuli were delivered to the right (uninjected) hind paw in the stimulation zones. There were two stimulation protocols: the left corridor was paired with 3 ms laser pulses at 5 Hz and the right corridor was paired with 3 ms laser pulses at 1 Hz. Laser power density was 40 mW/mm2. Training and experimental sessions lasted 1-2 hours.

Somatosensory stimulation in multiple chambers

Nine mice were placed in 100 mm x 100 mm individual chambers in a 3 × 3 configuration, covered by a lid. Mice were habituated to the chambers atop the glass stimulation platform for two hours in two sessions prior to the first experimental day.

For the experiment with thermal stimulation, 18 C57BL/6J mice from 2 cohorts were used. Mice were placed in the chambers for 2 hours, and a 10 s laser pulse was targeted to one of the hind paws, with up to 10 stimulations on each paw >1 minute apart. IR laser spot size was 2.2 mm2 and the optical power was set to 1.4 W in the first cohort of mice and to 1.65 W in the second cohort of mice to elicit paw responses between 10 - 12 s.

For the experiment with transdermal optogenetic stimulation, 9 Trpv1::ChR2 and 9 littermate controls from two cohorts were used. Mice were similarly placed in the chambers for 2 hours, with optogenetic stimulations delivered to each hind paw >1 minute apart. The stimulation protocol comprised 6 conditions: a single pulse stimulation at 40 mW/mm2, and a train of pulses (3 ms pulses at 10 Hz for 10 s) at 8, 4, 2, 1 and 0.5 mW/mm2. Spot size was 2.0 mm2. The order of stimulation intensity was pseudorandomized with Euler tours (?).

Data analysis

Data compression, analysis and visualization

Videos were acquired in AVI format and fed through offline DeepLabCut post estimation to generate (x,y) coordinates and likelihoods for each body part. For the analysis of the recordings with multiple chambers, AVI video files were converted to MP4 format using H.264 compression. The MP4 video files were cropped into individual mouse chambers (230 × 230 pixels) before running pose estimation. Analyses were based on the position of the hind paw or tail base coordinates. All analysis code was written in Python 3 (v3.9.7), using the NumPy, Pandas and OpenCV packages. Data was visualized using Matplotlib and Seaborn packages. Schematics were created using BioRender in Figures 1B (https://BioRender.com/fqettr4), 3A (https://BioRender.com/dkry6u2), 4A (https://BioRender.com/camqr45), 5A (https://BioRender.com/0q9m1bi) and Figure 2−figure supplement 2 (https://BioRender.com/diopdjf), and renderings in Figures 2 and 5 were created using Solidworks.

Calculation of paw response latency with motion energy

Motion energy was computed as the difference between neighboring frames, removing background noise below 50, and taking the mean. The trial window extends from 1 second before to 10 seconds after the initiation of the thermal stimulation. A trial was considered a response if motion energy >0.32 in the trial window 0.5 seconds post-stimulation onset so that the stimulation artefact was not included. The response probability was determined based on the number of responses recorded for each paw for every mouse. The response latency was calculated for the responses by taking the time point at which the motion energy exceeded the response threshold in the stimulation time window and subtracting this from the stimulation onset time point.

Calculation of paw response latency with pose estimation

The body part coordinates during the trial windows (2 seconds prior to, and 10 seconds following, the onset of the optogenetic stimulation) were used to calculate Euclidean distances from the baseline coordinate of the body part, which was taken at 2 seconds prior to the onset of the optogenetic stimulation (the beginning of the stimulation trial window). Analysis was conducted on the keypoint on the hind paw toes to reduce stimulation artefacts from light delivered to the center of the hind paw using coordinates with >0.8 likelihood values. If the keypoint moved more than 3 pixels within the stimulation trial window the trial was classified as a response. For the responses, the latency was determined by taking the time where the movement first exceeds 3 pixels, relative to the stimulation onset time.

Calculation of speed

The estimated tail base coordinates were used to visualize trajectories in the open arena and maze. These estimated coordinates were used if the likelihood >0.8 (open arena) and >0.85 (maze). Tracking errors were removed when the Euclidean distance jumped >30 pixels in a single frame and linear interpolation was performed using the 3 frames either side of the removed values. Speed was calculated by taking the difference in Euclidean distance (Δd) between frames as a function of the respective difference in frame times (Δt) and converting to mm/s using the scale factor calculated above. For the maze, we calculated the speed (vigor) by capturing each ‘corridor run’ from the point the tail base entered the corridor to when it exited the corridor. Speed for the corridor run was calculated within this time window as above.

Statistical analysis

Statistical analysis was performed in Python, with the SciPy, Statsmodels and Pingouin packages. Normality was determined using the Shapiro-Wilk normality test. The specific tests used for each comparison are detailed in the text. Statistical significance was considered as p <0.05. Data are reported as mean ± standard error of the mean (SEM) unless stated otherwise. The mouse was the experimental unit.

Data availability

Data will be made available upon reasonable request to L.E.B. The code for calibration and control are available at https://github.com/browne-lab/closed-loop-somatosensory-stimulation.

Supplementary tables

Optical components required for the assembly of the system.

Parts required for mounting optics in the system.

Parts required for acquisition and control.

Acknowledgements

We are grateful to Patrick Haggard and Andrew MacAskill for their comments on the manuscript. This work was supported a Sir Henry Dale Fellowship jointly funded by the Wellcome Trust and the Royal Society (109372/Z/15/Z) and funding from the Medical Research Council (MR/N013867/1).

Additional information

Author contributions

L.E.B. supervised and conceptualized the project. L.E.B and I.P. designed the experiments, built the closed-loop system, and wrote code. Q.G. set up the reward systems. A.S-P supported initial experiments. I.P. conducted the experiments. I.P and L.E.B. analyzed data and wrote the manuscript. All authors reviewed the manuscript.