ads1115_node.py 1.97 KB
Newer Older
Rousseau Vincent's avatar
Rousseau Vincent committed
1
# Copyright 2018 Irstea
2 3 4 5 6 7 8 9 10 11 12 13 14
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Rousseau Vincent's avatar
Rousseau Vincent committed
15
import rospy
16 17 18 19 20

from std_msgs.msg import Float32

import Adafruit_ADS1x15

Rousseau Vincent's avatar
Rousseau Vincent committed
21

Rousseau Vincent's avatar
Rousseau Vincent committed
22
class ADS1115(object):
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    def __init__(self, channel):
        self.adc = Adafruit_ADS1x15.ADS1115()

        # Choose a gain of 1 for reading voltages from 0 to 4.09V.
        # Or pick a different gain to change the range of voltages that are read:
        #  - 2/3 = +/-6.144V
        #  -   1 = +/-4.096V
        #  -   2 = +/-2.048V
        #  -   4 = +/-1.024V
        #  -   8 = +/-0.512V
        #  -  16 = +/-0.256V
        # See table 3 in the ADS1015/ADS1115 datasheet for more info on gain.
        gain = 1
        self.adc.start_adc(channel, gain=gain)

        # Anonymous topic ?
Rousseau Vincent's avatar
Rousseau Vincent committed
39
        self.pub = rospy.Publisher('chatter', Float32, queue_size=10)
40 41

        timer_period = 0.01  # in second
Rousseau Vincent's avatar
Rousseau Vincent committed
42 43
        self.timer = rospy.Timer(rospy.Duration(timer_period), self.timer_callback)
        rospy.spin()
44 45 46

    def timer_callback(self):
        msg = Float32()
47 48 49 50
        # Conversion in milli volt for gain 1
        # https://github.com/kentegrate/rpi_drivers/blob/master/include/rpi_drivers/ads1115.hpp
        # https://github.com/jrowberg/i2cdevlib/blob/master/Arduino/ADS1115/ADS1115.h
        msg.data = self.adc.get_last_result()*0.125000
Rousseau Vincent's avatar
Rousseau Vincent committed
51
        rospy.loginfo('Publishing: "{0}"'.format(msg.data))
52 53 54 55
        self.pub.publish(msg)


def main(args=None):
Rousseau Vincent's avatar
Rousseau Vincent committed
56
    rospy.init_node('ads1115', anonymous=True)
57 58 59 60 61 62

    node = ADS1115(0)


if __name__ == '__main__':
    main()