Eternali / roslib

ROS interface library in Dart.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No. of clients connect increase when publish the data

sachinkum0009 opened this issue · comments

 [Client 16] [id: publish:/counter:11] publish: Expected a JSON object for type std_msgs/String but received a <type 'unicode'>

This is the error getting from rosbridge_websocket

import 'package:flutter/material.dart';
import 'package:roslib/roslib.dart';

class ExampleApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Roslib Example',
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);

  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  Ros ros;
  Topic chatter;
  Topic counter;

  @override
  void initState() {
    ros = Ros(url: 'ws://192.168.43.124:9090');
    chatter = Topic(
        ros: ros,
        name: '/chatter',
        type: "std_msgs/String",
        reconnectOnClose: true,
        queueLength: 10,
        latch: true,
        queueSize: 10);
    counter = Topic(
      ros: ros,
      name: '/counter',
      type: "std_msgs/String",
      reconnectOnClose: true,
      queueSize: 10,
      queueLength: 10, 
    );
    super.initState();
  }

  void initConnection() async {
    ros.connect();
    await chatter.subscribe();
    await counter.advertise();
    //publishCounter();
    
    setState(() {});
  }

  void publishCounter() async {
    ros.connect();
    String msg = "'data': 'hello'";
    await counter.publish(msg);
    print('done publihsed');
  }

  void destroyConnection() async {
    await chatter.unsubscribe();
    await counter.unadvertise();
    await ros.close();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Roslib Example'),
      ),
      body: StreamBuilder<Object>(
          stream: ros.statusStream,
          builder: (context, snapshot) {
            return Center(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  StreamBuilder(
                    stream: chatter.subscription,
                    builder: (context2, snapshot2) {
                      if (snapshot2.hasData) {
                        var str = '${snapshot2.data['msg']}';
                        str = str.split(":")[1];
                        str = str.split("}")[0];
                        return Text(
                          str.toUpperCase(),
                          textAlign: TextAlign.center,
                          style: TextStyle(
                            
                            color: Colors.green,
                            fontSize: 24,
                          ),
                        );
                      } else {
                        return CircularProgressIndicator();
                      }
                    },
                  ),
                  SizedBox(
                    height: 30,
                  ),
                  ActionChip(
                    label: Text(snapshot.data == Status.CONNECTED
                        ? 'DISCONNECT'
                        : 'CONNECT'),
                    backgroundColor: snapshot.data == Status.CONNECTED
                        ? Colors.green[300]
                        : Colors.grey[300],
                    onPressed: () {
                      print(snapshot.data);
                      if (snapshot.data != Status.CONNECTED) {
                        this.initConnection();
                        // publishCounter();
                      } else {
                        this.destroyConnection();
                      }
                    },
                  ),
                  RaisedButton(onPressed: (){
                    publishCounter();
                  })
                ],
              ),
            );
          }),
    );
  }
}

This might be helpful to debug the problem

Instead of this

String msg = "'data': 'hello'";
await counter.publish(msg);

try this:

String msg = {'data': 'hello'};
await counter.publish(msg);

publish takes a json encoded object, not a String (A Map<String, dynamic> or dynamic).

Thank you for your response
This works for me
var msg = {'data': 'hello'}; await counter.publish(msg);

Hi, everytime, I publish the topic, the /client_connect topic increases

rostopic echo /client_count 
data: 1
---
data: 0
---
data: 1
---
data: 0
---
data: 1
---
data: 0
---
data: 1
---
data: 2
---
data: 3
---
data: 4
---
data: 5
---
data: 6
---
data: 7
---
data: 8
---
data: 9
---
data: 10
---
data: 11
---
data: 12
---
data: 13
---
data: 14
---
data: 15
---
data: 16
---
data: 15

can you please tell, why is it happening, or is it normal?

sorry the problem was from my side of code