0
$\begingroup$

ERROR Error in ROS connection: {"isTrusted": false, "message": "Failed to connect to /10.0.2.15:9090"}

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Ros, Topic } from 'roslib';
import { useState, useEffect } from 'react';

const ChargeLeft = () => {
  const [chargeLeft, setChargeLeft] = useState('');

  useEffect(() => {
    const ros = new Ros({
      url: 'ws://10.0.2.15:9090',  // Replace with your ROS server URL
    });


    ros.on('error', (error) => {
      console.error('Error in ROS connection:', error);
      // Handle the error as needed
    });


    const batteryPercentageTopic = new Topic({
      ros,
      name: '/battery_percentage',
      messageType: 'std_msgs/Int16',  
    });

    const handleBatteryPercentage = (message) => {
      // Assuming the message.data is the battery percentage value
      setChargeLeft(${message.data}%);
    };

    batteryPercentageTopic.on('error', (error) => {
      console.error('Error in batteryPercentageTopic:', error);
      // Handle the error as needed
    });

    batteryPercentageTopic.subscribe(handleBatteryPercentage);

    const updateInterval = setInterval(() => {
      batteryPercentageTopic.publish();
    }, 30000);

    return () => {
      batteryPercentageTopic.unsubscribe();
      clearInterval(updateInterval);
      ros.removeAllListeners('error');
      batteryPercentageTopic.removeAllListeners('error');
    };

  }, []);

  const rem = Number.parseInt(chargeLeft);

  let textColor;
  if (rem <= 20) {
    textColor = 'red';
  } else if (rem <= 80) {
    textColor = 'yellow';
  } else {
    textColor = 'green';
  }

  const styles = StyleSheet.create({
    chargeText: {
      color: textColor,
      fontSize: 16,
      fontWeight: 'bold',
    },
  });

  return (
    <View>
      <Text style={styles.chargeText}>{chargeLeft}</Text>
    </View>
  );
};

export default ChargeLeft;
```
$\endgroup$
1
  • 1
    $\begingroup$ Welcome to Robotics Stack Exchange! Make sure that machine 10.0.2.15 responds to ping command. You may look at vue-ros-demo for a similar application. $\endgroup$
    – ravi
    Jan 19 at 12:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.