How to increase publishing rate of potentiometers

Hi,
I am building a manipulator with joint potentiometers.
I am using a Adafruit ADS1115 with a raspberry pi to read angular values from a Bourns 3382 rotary position sensor. I have used the knowledge from the ROS basics in 5 days course to create a publisher for the positional sensor. I am publishing the readings to a ros topic however I cannot seem to publish with a rate higher than 7Hz. As far as i understand the ADS is possible of up to 860Hz. I have tried increasing the rospy.rate, however when using the rospy.info it never exceeds 7 readings per second. My code is shown below- am I somehow limiting the publishing speed?

    #!/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

Hi @volvo2 ,

I would recommend you to do some preliminary checks:

  1. What is your code execution time without rospy? As in, how long does it take to get one set of all readings? Is it a few milliseconds, if so how much?
  2. What is the code execution time with rospy and topic publisher enabled?
  3. rospy.Rate(1000) actually means 1000 messages per second. I am not sure if RosPy can go to that amount of messages. Try something between 30 and 100. 20 or 40 is a good measure. 1000 is way too much.
  4. Python is generally slow, try using C++ if possible. Better option would be to use C++ for publishing data from sensor to ROS instead of using Python. That way you can use Python for main program.

Are you sure this is not bit rate? Do you get the entire sensor measure at this rate of 860Hz?

Regards,
Girish

PS: This issue is not exactly Course Support, better to classify this under General Discussions.

Hello @volvo2,

I am going to place here some additional ideas:

Hope this helps,

Roberto

Hi guys,

Thank you very much for the answers. It helped a lot. I commented out all the print() and loginfo() and in the adafruit library managed to find a place where I could increase the sampling frequency to maximum. Now, using rostopic hz, I achieve 60 Hz, which is much much better than before. Thank you and have a great day!

Kind regards,
Anders

2 Likes

@volvo2 ,

I am glad you figured out that setting. That idea never occurred to me, I thought the sensor ran at full potential.

Regards,
Girish

This topic was automatically closed after 4 days. New replies are no longer allowed.