Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Flutter UI for ROS 2 Robot & Multimodal AI

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide walks you through building a Flutter interface that connects to a ROS 2 robot and displays multimodal AI outputs (like camera feeds, sensor data, and voice commands). You'll create a cross-platform app that communicates with your robot over Wi-Fi using the ros2dart package, and you'll integrate AI models via HTTP or WebSocket. By the end, you'll have a working dashboard that can send commands and visualize real-time data.

What You'll Need

  • Flutter SDK (3.x) and a code editor (VS Code or Android Studio)
  • A ROS 2 installation (Humble or later) on your robot's computer
  • Basic knowledge of ROS 2 topics, services, and actions
  • Your robot's IP address and the ROS 2 master's IP (if different)
  • Optional: A multimodal AI server (e.g., a Python script using TensorFlow or PyTorch) that exposes REST or WebSocket endpoints

Step-by-Step

  1. Set up your Flutter project. Run flutter create robot_interface and open it in your editor. Add the required dependencies to pubspec.yaml: ros2dart, http, web_socket_channel, and provider (for state management). Run flutter pub get.
  2. Configure ROS 2 on your robot. Ensure your robot's ROS 2 environment is running with the ROS_DOMAIN_ID set to a common value (e.g., 42). Note the IP address of the machine running the ROS 2 master. On your Flutter device (emulator or physical), make sure it can reach that IP over the network.
  3. Create a ROS 2 connection service. In your Flutter app, create a class (e.g., RosService) that uses ros2dart to connect to the ROS 2 master. Use the RosNode class to create a node, then subscribe to topics or call services. For example:
    final node = RosNode(domainId: 42);
    await node.init();
    final chatter = node.createSubscription('/chatter', 'std_msgs/String');
    Wrap this in a Future and handle connection errors gracefully.
  4. Design the UI layout. Use Flutter's Scaffold with a Drawer or bottom navigation to switch between views: Robot Control, Camera Feed, Sensor Data, and AI Output. Keep the design responsive—use LayoutBuilder to adapt to different screen sizes.
  5. Implement robot control. Add buttons (e.g., forward, backward, left, right) that publish to a geometry_msgs/Twist topic on the robot. Use the Publisher from ros2dart. For example, to move forward, publish a linear velocity of 0.2 m/s.
  6. Display camera feed. Subscribe to a topic that publishes sensor_msgs/Image (or compressed image). Convert the raw image data to a Flutter Image widget using ui.Image and Image.memory. For performance, consider using a StreamBuilder to update the image only when new frames arrive.
  7. Integrate multimodal AI. Create a service that sends sensor data (e.g., camera frames, audio) to your AI server via HTTP POST or WebSocket. The server processes the data and returns results (e.g., object detection labels, speech-to-text). Display these results in a dedicated panel using a FutureBuilder or StreamBuilder.
  8. Add state management. Use Provider or Riverpod to manage the connection state, robot status, and AI results. This keeps the UI in sync with real-time data and avoids rebuilding the whole widget tree unnecessarily.
  9. Test on a device. Run flutter run on your emulator or physical phone. Make sure your device is on the same network as the robot. Test each feature individually: connection, control, camera feed, and AI output.
  10. Optimize and polish. Add error handling for disconnections, loading indicators for AI requests, and a settings page to change the robot's IP and domain ID. Use Flutter's theming to make the interface look professional.

Common Mistakes

  • Wrong domain ID: If the Flutter app and ROS 2 use different domain IDs, they won't discover each other. Always set the same domain ID on both sides.
  • Network issues: Firewalls or VLANs can block the communication. Ensure port 7400 (ROS 2 discovery) and the data ports are open.
  • Blocking the UI thread: ROS 2 callbacks and HTTP requests should never run on the main isolate. Use async and await or move heavy work to a separate isolate.
  • Image format mismatch: ROS 2 images may be in BGR format, while Flutter expects RGB. Convert the color order before displaying.
  • Forgetting to dispose: Always close ROS 2 subscriptions and WebSocket connections in dispose() to avoid memory leaks.

Tips

  • Use a ValueNotifier or StreamController to broadcast robot status updates to multiple widgets.
  • For camera feed, use a lower resolution (e.g., 640x480) to reduce network load and latency.
  • Implement a reconnect button that attempts to re-establish the ROS 2 connection if it drops.
  • Use the ros2dart package's built-in message types; if you need custom messages, generate Dart classes from your ROS 2 interfaces.
  • For multimodal AI, consider using a WebSocket for continuous streaming (e.g., audio) and HTTP for one-shot requests (e.g., image classification).

Troubleshooting

  • App cannot connect to ROS 2: Check the IP address and domain ID. Run ros2 topic list on the robot to verify topics are being published. Use ping from your device to ensure network reachability.
  • No camera feed: Verify the topic name and message type. Print the image data size to ensure you're receiving frames. If using compressed images, make sure the codec is supported (e.g., JPEG).
  • AI responses are slow: Optimize the AI server by using GPU inference or reducing input size. In Flutter, show a loading indicator and avoid blocking the UI.
  • App crashes on startup: Ensure all dependencies are correctly added and the ROS 2 node is initialized before subscribing. Check the logs for any missing permissions (e.g., Internet permission on Android).
  • Unexpected disconnections: Implement a heartbeat mechanism (e.g., periodic ping) to detect dead connections and trigger a reconnect.

Frequently Asked Questions

  1. Can I use this with ROS 1? No, ros2dart is designed for ROS 2 only. For ROS 1, you'd need a bridge like rosbridge_suite.
  2. Do I need a physical robot? No, you can test with a simulator like Gazebo or a simple ROS 2 talker node that publishes dummy data.
  3. Can I run this on a web browser? Yes, Flutter supports web, but you'll need to enable CORS on your ROS 2 bridge and AI server. WebSockets work well for this.
  4. How do I add voice commands? Use the speech_to_text package to capture audio, send it to your AI server for speech recognition, and then publish the resulting command to ROS 2.
  5. Is it possible to control multiple robots? Yes, create multiple ROS 2 connections with different domain IDs or namespaces, and switch between them in the UI.
  6. What if my AI model is on a different machine? That's fine—just make sure the AI server is accessible over the network and configure its URL in the Flutter app's settings.