ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to increase publishing rate of potentiometers

asked 2022-10-17 11:38:34 -0500

volvo2 gravatar image

updated 2022-10-18 07:15:25 -0500

ravijoshi gravatar image

Hi, I am using an Adafruit ADS1115 with a Raspberry Pi to read angular values from a Bourns 3382 rotary position sensor. I am publishing the readings on a topic. However, I cannot seem to publish with a rate higher than 7Hz.

As far as I understand, the ADS is possible for up to 860Hz. I have tried increasing the rospy.rate. However, when using rospy.info, it never exceeds 7 readings per second. Am I somehow limiting the publishing speed?

My code is shown below:

#!/usr/bin/env python

import rospy
from ads1115.msg import IntList

import time
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
from adafruit_extended_bus import ExtendedI2C as I2C
import numpy as np
import random

PKG = "ads1115"
import roslib

roslib.load_manifest(PKG)

################## RETRIVE DATA FROM ADS1115 ##################
# Create the I2C buses
i2c1 = I2C(1)
i2c2 = I2C(3)
i2c3 = I2C(4)

# Create the ADC object using the I2C bus
ads11 = ADS.ADS1115(i2c1, 1, 16, 0x0100, 0x48)
ads12 = ADS.ADS1115(i2c1, 1, 16, 0x0100, 0x49)

# Create single-ended input on channel 0
chan1480 = AnalogIn(ads11, ADS.P0)
chan1481 = AnalogIn(ads11, ADS.P1)
chan1482 = AnalogIn(ads11, ADS.P2)
chan1483 = AnalogIn(ads11, ADS.P3)

chan1490 = AnalogIn(ads12, ADS.P0)
chan1491 = AnalogIn(ads12, ADS.P1)
chan1492 = AnalogIn(ads12, ADS.P2)
chan1493 = AnalogIn(ads12, ADS.P3)

adc_value1 = [
    chan1480.value,
    chan1481.value,
    chan1482.value,
    chan1483.value,
    chan1490.value,
    chan1491.value,
    chan1492.value,
    chan1493.value,
]
print(adc_value1)
print(type(adc_value1))

adc_voltage2 = [
    chan1480.voltage,
    chan1481.voltage,
    chan1482.voltage,
    chan1483.voltage,
    chan1490.voltage,
    chan1491.voltage,
    chan1492.voltage,
    chan1493.voltage,
]


def talker():
    pub = rospy.Publisher("ads1115_data", IntList, queue_size=10)
    rospy.init_node("ads1115_talker", anonymous=True)
    rate = rospy.Rate(1000)  # 10hz

    while not rospy.is_shutdown():
        chanVoltage = np.array(
            [
                chan1480.voltage,
                chan1481.voltage,
                chan1482.voltage,
                chan1483.voltage,
                chan1490.voltage,
                chan1491.voltage,
                chan1492.voltage,
                chan1493.voltage,
            ],
            dtype=np.float32,
        )
        rospy.loginfo(chanVoltage)
        pub.publish(chanVoltage)
        rate.sleep()


if __name__ == "__main__":
    try:
        talker()
    except rospy.ROSInterruptException:
        pass
edit retag flag offensive close merge delete

Comments

1
  1. In your code, rate = rospy.Rate(1000) is 1000 Hz. Please reduce it.
  2. I am not sure, but please confirm that ADS is capable up to 860Hz.
  3. Please do not use rospy.info inside a high frequency loop. Raspberry Pi has limited resources and we can not simply waste them on printing task.
  4. To monitor the message frequency, please use rostopic hz /ads1115_data
ravijoshi gravatar image ravijoshi  ( 2022-10-18 07:20:07 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2022-10-19 01:36:53 -0500

volvo2 gravatar image

Hi guys, Thank you for the answers. I managed to increase the sampling frequency greatly by increasing the 16 in this line to 860.

ads11 = ADS.ADS1115(i2c1, 1, 16, 0x0100, 0x48)

Now I achieve 60Hz when running rostopic hz, which is much much better than before. Thank you!

edit flag offensive delete link more
0

answered 2022-10-18 17:29:15 -0500

duck-development gravatar image

You like to be fast then use c++, what is the cpu load while you execute the script. Do you have UIs on the pi? How long does it take to read the i2c suff. I think this is the slowest part. Wjdriting to the console is also a slow process. Remove the raspy.logInfo line. Use the rostopic hz function to measure the refresh rate of you topic as @ravijoshi has written.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2022-10-17 11:38:34 -0500

Seen: 58 times

Last updated: Oct 19 '22