Buckets:

download
raw
20 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Joystick Controller&quot;,&quot;local&quot;:&quot;joystick-controller&quot;,&quot;sections&quot;:[],&quot;depth&quot;:1}">
<link href="/docs/reachy_mini/pr_1069/en/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/entry/start.6cd5df39.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/chunks/scheduler.f05771bc.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/chunks/singletons.8ab55633.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/chunks/index.2528d289.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/chunks/paths.a8f2e6ae.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/entry/app.405911a4.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/chunks/preload-helper.e957381b.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/chunks/index.b379be46.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/nodes/0.b229c383.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/chunks/each.e59479a4.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/nodes/23.3ab99e6c.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/chunks/MermaidChart.svelte_svelte_type_style_lang.c489a506.js">
<link rel="modulepreload" href="/docs/reachy_mini/pr_1069/en/_app/immutable/chunks/CodeBlock.f56fd9c8.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Joystick Controller&quot;,&quot;local&quot;:&quot;joystick-controller&quot;,&quot;sections&quot;:[],&quot;depth&quot;:1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"><div class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg></span> <span>Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg></button></div> </div> <h1 class="relative group"><a id="joystick-controller" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#joystick-controller"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Joystick Controller</span></h1> <p data-svelte-h="svelte-9fdpfe">This example demonstrates how to control Reachy Mini’s head yaw angle using a joystick (PS4 or Xbox controller). The left joystick controls the head’s left-right rotation, providing intuitive real-time control of the robot.</p> <p data-svelte-h="svelte-gxp1l"><strong>Controls:</strong></p> <ul data-svelte-h="svelte-wu4gg1"><li><strong>LEFT JOYSTICK (Left/Right)</strong>: Control head yaw angle</li> <li><strong>CIRCLE / B BUTTON</strong>: Quit the application safely</li> <li><strong>CTRL-C</strong>: Quit the application</li></ul> <p data-svelte-h="svelte-11lzvnb"><strong>Requirements:</strong></p> <ul data-svelte-h="svelte-8n0q7r"><li>Install pygame: <code>pip install pygame</code></li> <li>Connect a PS4 or Xbox controller to your computer</li></ul> <p data-svelte-h="svelte-1en4vh8"><strong>Controller mappings:</strong></p> <ul data-svelte-h="svelte-haxk4a"><li>PS4: Button 1 = Circle (O), Axis 0 = Left Stick Horizontal</li> <li>Xbox: Button 1 = B, Axis 0 = Left Stick Horizontal</li></ul> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-python "><!-- HTML_TAG_START -->
<span class="hljs-comment"># Standard library imports</span>
<span class="hljs-keyword">import</span> os
<span class="hljs-keyword">import</span> sys
<span class="hljs-keyword">import</span> time
<span class="hljs-comment"># Third-party imports</span>
<span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">import</span> pygame
<span class="hljs-comment"># Local application/library-specific imports</span>
<span class="hljs-keyword">from</span> reachy_mini <span class="hljs-keyword">import</span> ReachyMini, utils
<span class="hljs-comment"># --- Configuration ---</span>
CONTROL_LOOP_RATE = <span class="hljs-number">0.02</span>
<span class="hljs-comment"># Maximum yaw angle. The joystick&#x27;s -1 to 1 input will be mapped to this range.</span>
YAW_ANGLE_LIMIT = np.pi / <span class="hljs-number">4</span> * <span class="hljs-number">1.3</span> <span class="hljs-comment"># Radians</span>
<span class="hljs-comment"># To use pygame &quot;headlessly&quot; (without a GUI window).</span>
os.environ[<span class="hljs-string">&quot;SDL_VIDEODRIVER&quot;</span>] = <span class="hljs-string">&quot;dummy&quot;</span>
<span class="hljs-comment"># --- Controller Bindings Comment ---</span>
<span class="hljs-comment"># PS4 controller:</span>
<span class="hljs-comment"># Button 1 = O (Circle)</span>
<span class="hljs-comment"># Axis 0: Left Joy Left/Right (-1 left, 1 right)</span>
<span class="hljs-comment"># Axis 3 or 4: Right Joy Left/Right</span>
<span class="hljs-comment">#</span>
<span class="hljs-comment"># XBOX controller:</span>
<span class="hljs-comment"># Button 1 = B</span>
<span class="hljs-comment"># Axis 0: Left Joy Left/Right</span>
<span class="hljs-comment"># Axis 2 or 3: Right Joy Left/Right</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">Controller</span>:
<span class="hljs-string">&quot;&quot;&quot;Handle joystick input using pygame.&quot;&quot;&quot;</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, deadzone: <span class="hljs-built_in">float</span> = <span class="hljs-number">0.08</span></span>):
<span class="hljs-string">&quot;&quot;&quot;Initialize the controller and find the first joystick.
Args:
deadzone (float): Axis value below which input is ignored.
Raises:
IOError: If no joystick is found.
&quot;&quot;&quot;</span>
pygame.init()
pygame.joystick.init()
<span class="hljs-keyword">if</span> pygame.joystick.get_count() &lt; <span class="hljs-number">1</span>:
<span class="hljs-keyword">raise</span> IOError(<span class="hljs-string">&quot;No joystick controller found.&quot;</span>)
self.joystick: pygame.joystick.Joystick = pygame.joystick.Joystick(<span class="hljs-number">0</span>)
self.deadzone = deadzone
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Initialized joystick: <span class="hljs-subst">{self.joystick.get_name()}</span>&quot;</span>)
<span class="hljs-keyword">def</span> <span class="hljs-title function_">_apply_deadzone</span>(<span class="hljs-params">self, value: <span class="hljs-built_in">float</span></span>) -&gt; <span class="hljs-built_in">float</span>:
<span class="hljs-string">&quot;&quot;&quot;Apply a deadzone to a joystick axis value.&quot;&quot;&quot;</span>
<span class="hljs-keyword">return</span> value <span class="hljs-keyword">if</span> <span class="hljs-built_in">abs</span>(value) &gt; self.deadzone <span class="hljs-keyword">else</span> <span class="hljs-number">0.0</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">get_horizontal_inputs</span>(<span class="hljs-params">self</span>) -&gt; <span class="hljs-built_in">tuple</span>[<span class="hljs-built_in">float</span>, <span class="hljs-built_in">float</span>]:
<span class="hljs-string">&quot;&quot;&quot;Read the horizontal axes of the left and right joysticks.
Returns:
tuple[float, float]: (left_joy_h, right_joy_h) from -1.0 to 1.0.
&quot;&quot;&quot;</span>
pygame.event.pump() <span class="hljs-comment"># Update pygame&#x27;s internal event state.</span>
left_joy_h = self._apply_deadzone(self.joystick.get_axis(<span class="hljs-number">0</span>))
<span class="hljs-comment"># Right joystick horizontal axis can be 2, 3 or 4 depending on controller</span>
right_joy_h = <span class="hljs-number">0.0</span>
<span class="hljs-keyword">if</span> self.joystick.get_numaxes() &gt; <span class="hljs-number">3</span>:
right_joy_h = self._apply_deadzone(self.joystick.get_axis(<span class="hljs-number">3</span>))
<span class="hljs-keyword">elif</span> self.joystick.get_numaxes() &gt; <span class="hljs-number">2</span>:
right_joy_h = self._apply_deadzone(self.joystick.get_axis(<span class="hljs-number">2</span>))
<span class="hljs-keyword">return</span> left_joy_h, right_joy_h
<span class="hljs-keyword">def</span> <span class="hljs-title function_">check_for_quit</span>(<span class="hljs-params">self</span>) -&gt; <span class="hljs-built_in">bool</span>:
<span class="hljs-string">&quot;&quot;&quot;Check pygame events for a quit signal.
Returns:
bool: True if the designated quit button (Circle/B) is pressed.
&quot;&quot;&quot;</span>
<span class="hljs-keyword">for</span> event <span class="hljs-keyword">in</span> pygame.event.get():
<span class="hljs-keyword">if</span> event.<span class="hljs-built_in">type</span> == pygame.JOYBUTTONDOWN:
<span class="hljs-keyword">if</span> self.joystick.get_button(<span class="hljs-number">1</span>): <span class="hljs-comment"># Button 1 is Circle/B</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;\nQuit button pressed.&quot;</span>)
<span class="hljs-keyword">return</span> <span class="hljs-literal">True</span>
<span class="hljs-keyword">return</span> <span class="hljs-literal">False</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">main</span>() -&gt; <span class="hljs-literal">None</span>:
<span class="hljs-string">&quot;&quot;&quot;Run the main joystick control loop.&quot;&quot;&quot;</span>
<span class="hljs-keyword">try</span>:
controller = Controller()
<span class="hljs-keyword">except</span> IOError <span class="hljs-keyword">as</span> e:
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Error: <span class="hljs-subst">{e}</span>&quot;</span>, file=sys.stderr)
<span class="hljs-keyword">return</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Connecting to Reachy Mini...&quot;</span>)
<span class="hljs-keyword">try</span>:
<span class="hljs-comment"># The &#x27;with&#x27; statement ensures the robot is properly handled on exit</span>
<span class="hljs-keyword">with</span> ReachyMini(automatic_body_yaw=<span class="hljs-literal">True</span>) <span class="hljs-keyword">as</span> mini:
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Robot connected.&quot;</span>)
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;\n&quot;</span> + <span class="hljs-string">&quot;=&quot;</span> * <span class="hljs-number">50</span>)
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot; Reachy Head Yaw Joystick Controller&quot;</span>)
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot; CONTROLS: [Left Stick] to turn | [Circle/B] to quit&quot;</span>)
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;=&quot;</span> * <span class="hljs-number">50</span> + <span class="hljs-string">&quot;\n&quot;</span>)
<span class="hljs-keyword">while</span> <span class="hljs-literal">True</span>:
<span class="hljs-keyword">if</span> controller.check_for_quit():
<span class="hljs-keyword">break</span>
<span class="hljs-comment"># Get scaled joystick values</span>
left_joy, right_joy = controller.get_horizontal_inputs()
<span class="hljs-comment"># Map joystick input (-1 to 1) to the desired angle range</span>
target_yaw = left_joy * YAW_ANGLE_LIMIT
target_body_yaw = right_joy * YAW_ANGLE_LIMIT
<span class="hljs-comment"># Define the target pose: x,y,z and roll,pitch,yaw</span>
target_position = np.array([<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0.0</span>])
target_orientation = np.array([<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, target_yaw])
<span class="hljs-comment"># Create and send the command to the robot</span>
mini.set_target(
utils.create_head_pose(
x=target_position[<span class="hljs-number">0</span>],
y=target_position[<span class="hljs-number">1</span>],
z=target_position[<span class="hljs-number">2</span>],
roll=target_orientation[<span class="hljs-number">0</span>],
pitch=target_orientation[<span class="hljs-number">1</span>],
yaw=target_orientation[<span class="hljs-number">2</span>],
degrees=<span class="hljs-literal">False</span>,
),
body_yaw=target_body_yaw,
)
<span class="hljs-comment"># Print status, overwriting the line</span>
<span class="hljs-built_in">print</span>(
<span class="hljs-string">f&quot;\rHead Yaw: <span class="hljs-subst">{target_yaw:<span class="hljs-number">6.2</span>f}</span> rad | &quot;</span>
<span class="hljs-string">f&quot;Body Yaw: <span class="hljs-subst">{target_body_yaw:<span class="hljs-number">6.2</span>f}</span> rad&quot;</span>,
end=<span class="hljs-string">&quot;&quot;</span>,
)
sys.stdout.flush()
time.sleep(CONTROL_LOOP_RATE)
<span class="hljs-keyword">except</span> KeyboardInterrupt:
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;\nCTRL+C detected. Shutting down...&quot;</span>)
<span class="hljs-keyword">except</span> Exception <span class="hljs-keyword">as</span> e:
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;\nAn unexpected error occurred: <span class="hljs-subst">{e}</span>&quot;</span>, file=sys.stderr)
<span class="hljs-keyword">finally</span>:
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;\n\nApplication finished. Robot will go to sleep.&quot;</span>)
pygame.quit()
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
main()<!-- HTML_TAG_END --></pre></div> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/pollen-robotics/reachy_mini/blob/main/docs/source/examples/joy_controller.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg> <span data-svelte-h="svelte-zjs2n5"><span class="underline">Update</span> on GitHub</span></a> <p></p>
<script>
{
__sveltekit_14cjvgd = {
assets: "/docs/reachy_mini/pr_1069/en",
base: "/docs/reachy_mini/pr_1069/en",
env: {}
};
const element = document.currentScript.parentElement;
const data = [null,null];
Promise.all([
import("/docs/reachy_mini/pr_1069/en/_app/immutable/entry/start.6cd5df39.js"),
import("/docs/reachy_mini/pr_1069/en/_app/immutable/entry/app.405911a4.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 23],
data,
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
20 kB
·
Xet hash:
e37705aff3916fa9a2d8c11073f9e74e9976e1f37ac6332c0a77b6270aad3e7e

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.