How to increase publishing rate of potentiometers
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
Asked by volvo2 on 2022-10-17 11:38:34 UTC
Answers
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.
Asked by duck-development on 2022-10-18 17:29:15 UTC
Comments
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!
Asked by volvo2 on 2022-10-19 01:36:53 UTC
Comments
rate = rospy.Rate(1000)
is 1000 Hz. Please reduce it.rospy.info
inside a high frequency loop. Raspberry Pi has limited resources and we can not simply waste them on printing task.rostopic hz /ads1115_data
Asked by ravijoshi on 2022-10-18 07:20:07 UTC