ros2dart / dartros1

A ROS1 client library for dart
Apache License 2.0
20 stars 11 forks source link

Cannot connect to roscore from android phone #39

Closed Cokthemhok closed 3 years ago

Cokthemhok commented 3 years ago

I build the app with the example code from this link: example, and of course, I use the rosMasterUri like that:

final nh = await dartros.initNode('ros_node_1', args, rosMasterUri: "http://192.168.2.142:11311"); 192.168.2.142 is my PC's IP (Ubuntu 18.04) Total my code:

import 'package:dartros/dartros.dart' as dartros;

Future<void> main(List<String> args) async {
  final nh = await dartros.initNode('ros_node_1', args,
      rosMasterUri: "http://192.168.2.142:11311");
  await nh.getMasterUri();
  await nh.setParam('/foo', 'value');
  var value = await nh.getParam('/foo');
  assert(value == 'value');
  print(value);

  print(await nh.setParam('/foo', 'new value'));
  value = await nh.getParam('/foo');
  assert(value == 'new value');
  print(value);
}

And when debug (with my android phone), the error is: Exception has occurred. SocketException (SocketException: OS Error: Connection refused, errno = 111, address = 192.168.2.142, port = 39883)

This error happen at line 25 of file "time.dart":

class Time {
  static RosTime simTime = const RosTime(secs: 0, nsecs: 0);
  static bool useSimTime = false;
  static Future<void> initializeRosTime() async {
    try {
      useSimTime =
          await nh.getParam<bool>('/use_sim_time', defaultValue: false);
      log.dartros.info('Sim time: $useSimTime');
      if (useSimTime) {
        nh.subscribe<Clock>(
          '/clock',
          Clock.$prototype,
          (msg) {
            simTime = msg.clock;
          },
          throttleMs: -1,
        );
      }
    } on Exception catch (e) {
      rethrow; //Error happened here
    }
  }

Screenshot from 2021-06-24 11-37-11

Please support me. Thank you so much.

TimWhiting commented 3 years ago

It is likely that you didn't set up your ROS_IP appropriately on the phone or your phone is not on the same subnet as the computer. Make sure that your phone has an IP address something like 192.168.2.XXX, and if it is then just make sure that when you initialize the node, pass in a custom ROS_IP as the IP address of the phone. If that looks fine it is probably a permissions problem. Note that ROS uses http not https and I know that in newer versions of Android you have to specifically opt out of only allowing https connections.

This kind of error is a network error not an error with this package to be clear, it has to do with how you set up your network for ROS. It could also be an issue with the firewall on your computer, to see if that is the issue you can temporarily disable the firewall.

Cokthemhok commented 3 years ago

Thank you for supporting me. I already fixed this problem. It's my mistake. In my PC, I use localhost for ROS_MASTER_URI, so it cannot connect with my phone. After changed to IP, it work normally. And I have an other question: Do you have plan to make Dartros work with web (in Flutter)? Thank you so much for this project. It's very awesome!

TimWhiting commented 3 years ago

It's technically impossible to support dartros on web since you can't create a server on a web-client (and ros nodes create a server). However, it is technically feasible to do it through rosbridge. Currently you can use rosbridge through the roslib package for dart. However, it exposes a different interface and is not as complete as this package, there will be stuff missing or incomplete. I've used it successfully in the past for simple publisher and subscribers. I'm thinking about providing a dartros compatible interface for rosbridge that will make the differences invisible to the user, but it requires time, and I don't have a lot of spare time right now.

TimWhiting commented 3 years ago

Opened #40 for web support