Pepper机器人多JavaScript应用Choregraphe运行及ALMemory数据检索问询
Hey there! Let's tackle your two questions one by one—both are common hurdles when working with Choregraphe, and I’ve got straightforward solutions for you.
1. Switching Between JavaScript Apps in a Single Choregraphe Behavior
The key here is to use Choregraphe’s State Machine to manage different phases of your workflow. Each state will host one of your JavaScript apps, and you’ll set up triggers to transition between states when specific conditions are met (like animation completion or user input submission). Here’s a step-by-step breakdown:
- Set up a State Machine: Drag a "State Machine" box into your behavior. Inside it, create separate states for each phase: e.g.,
UserInputPhase,AnimationPhase,ButtonSelectionPhase. - Assign JavaScript Apps to Each State: For each state, add a "JavaScript" box. In the code for each box:
- Initialize your app when the state is entered (e.g., load the tablet input UI or button selection interface).
- Clean up resources when the state is exited (e.g., destroy the UI, remove event listeners) to avoid memory leaks.
- Add Transition Triggers:
- For user input completion: When your JS app detects the user has submitted data via the tablet, write a custom event to ALMemory (e.g.,
UserInput/Completed). Then add an "ALMemory Event" trigger to your state, listening for this event, and set it to transition to theAnimationPhase. - For animation completion: Most animation modules (like
ALAnimationPlayer) emit an event to ALMemory when an animation finishes (e.g.,AnimationPlayer/AnimationDone). Add an "ALMemory Event" trigger on theAnimationPhaseto listen for this, then transition toButtonSelectionPhase.
- For user input completion: When your JS app detects the user has submitted data via the tablet, write a custom event to ALMemory (e.g.,
Example snippet for a JS app in the UserInputPhase:
// Initialize tablet input UI when entering the state function onInput(input) { // Load your input app UI here ALTabletService.showWebview("http://your-tablet-app-url/input"); // Listen for user submission ALTabletService.onWebviewEvent.connect(function(event) { if (event === "inputSubmitted") { // Write input data to ALMemory ALMemory.insertData("UserInput/FormData", inputData); // Trigger state transition ALMemory.raiseEvent("UserInput/Completed", true); } }); } // Clean up when exiting the state function onExit() { ALTabletService.hideWebview(); ALTabletService.onWebviewEvent.disconnectAll(); }
2. Retrieving Data from ALMemory
You’ve already got writing data down—retrieving it is just as simple, and there are two main ways to do it depending on your needs:
Synchronous Data Retrieval
If you just need to fetch the current value of a key once, use ALMemory.getData():
// Get data from a specific key var userInput = ALMemory.getData("UserInput/FormData"); if (userInput !== null) { // Process the data (e.g., pass it to your button selection app) console.log("Retrieved user input:", userInput); }
You can also use Choregraphe’s built-in "Get Data From ALMemory" box—drag it into your flow, enter the key you want to read, and connect its output to other boxes (like a JavaScript box or speech synthesis module).
Real-Time Data Listening
If you need to react when the value of a key changes (e.g., if another app updates the data), use a subscriber to listen for changes:
// Create a subscriber for your key var dataSubscriber = ALMemory.subscriber("UserInput/FormData"); // Bind a callback to handle updates function handleDataUpdate(newValue) { console.log("Data updated to:", newValue); // Update your app UI or logic here } dataSubscriber.signal.connect(handleDataUpdate); // Don't forget to clean up when done! function onExit() { dataSubscriber.signal.disconnect(handleDataUpdate); }
Putting It All Together for Your Example
- In the
UserInputPhase, your JS app collects data, writes it to ALMemory, and triggers a state transition. - The
AnimationPhaseruns your animation, and when it finishes, theAnimationDoneevent triggers a transition toButtonSelectionPhase. - In the
ButtonSelectionPhase, your JS app retrieves the stored data from ALMemory (usinggetData()or a subscriber) and uses it to customize the button interface.
内容的提问来源于stack exchange,提问作者Sarinda

