Commit 6c3da0c1 authored by Rousseau Vincent's avatar Rousseau Vincent

ROS1 node

parent df439306
cmake_minimum_required(VERSION 2.8.3)
project(ads1115_driver)
set(${PROJECT_NAME}_CATKIN_DEPS rospy)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS ${${PROJECT_NAME}_CATKIN_DEPS})
## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()
################################################
## Declare ROS dynamic reconfigure parameters ##
################################################
## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
## * add a build_depend and a run_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
## * add "dynamic_reconfigure" to
## find_package(catkin REQUIRED COMPONENTS ...)
## * uncomment the "generate_dynamic_reconfigure_options" section below
## and list every .cfg file to be processed
## Generate dynamic reconfigure parameters in the 'cfg' folder
# generate_dynamic_reconfigure_options(
# cfg/DynReconf1.cfg
# cfg/DynReconf2.cfg
# )
###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if you package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES ads1115_driver
CATKIN_DEPENDS ${${PROJECT_NAME}_CATKIN_DEPS}
# DEPENDS system_lib
)
###########
## Build ##
###########
include_directories(
include ${catkin_INCLUDE_DIRS}
)
## Declare a C++ library
# add_library(ads1115_driver
# src/${PROJECT_NAME}/ads1115_driver.cpp
# )
## Declare a C++ executable
# add_executable(ads1115_driver_node src/ads1115_driver_node.cpp)
## Specify libraries to link a library or executable target against
# target_link_libraries(ads1115_driver_node
# ${catkin_LIBRARIES}
# )
#############
## Install ##
#############
# all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html
## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
# install(PROGRAMS
# scripts/my_python_script
# DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )
## Mark executables and/or libraries for installation
# install(TARGETS ads1115_driver ads1115_driver_node
# ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
# RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
# )
## Mark cpp header files for installation
# install(DIRECTORY include/${PROJECT_NAME}/
# DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
# FILES_MATCHING PATTERN "*.h"
# PATTERN ".svn" EXCLUDE
# )
## Mark other files for installation (e.g. launch and bag files, etc.)
# install(FILES
# # myfile1
# # myfile2
# DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
# )
#############
## Testing ##
#############
## Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_ads1115_driver.cpp)
# if(TARGET ${PROJECT_NAME}-test)
# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()
## Add folders to be run by python nosetests
# catkin_add_nosetests(test)
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="2">
<name>ads1115_driver</name>
<version>0.1.0</version>
<description>
......@@ -11,14 +11,12 @@
<author email="vincent.rousseau@irstea.fr">Vincent Rousseau</author>
<exec_depend>rclpy</exec_depend>
<buildtool_depend>catkin</buildtool_depend>
<depend>rospy</depend>
<exec_depend>std_msgs</exec_depend>
<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_pep257</test_depend>
<export>
<build_type>ament_python</build_type>
</export>
</package>
......@@ -12,19 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import rclpy
from rclpy.node import Node
import rospy
from std_msgs.msg import Float32
import Adafruit_ADS1x15
class ADS1115(Node):
class ADS1115(object):
def __init__(self, channel):
super().__init__('ads1115')
self.adc = Adafruit_ADS1x15.ADS1115()
# Choose a gain of 1 for reading voltages from 0 to 4.09V.
......@@ -40,10 +36,11 @@ class ADS1115(Node):
self.adc.start_adc(channel, gain=gain)
# Anonymous topic ?
self.pub = self.create_publisher(Float32, 'chatter')
self.pub = rospy.Publisher('chatter', Float32, queue_size=10)
timer_period = 0.01 # in second
self.tmr = self.create_timer(timer_period, self.timer_callback)
self.timer = rospy.Timer(rospy.Duration(timer_period), self.timer_callback)
rospy.spin()
def timer_callback(self):
msg = Float32()
......@@ -51,20 +48,15 @@ class ADS1115(Node):
# 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
self.get_logger().info('Publishing: "{0}"'.format(msg.data))
rospy.loginfo('Publishing: "{0}"'.format(msg.data))
self.pub.publish(msg)
def main(args=None):
rclpy.init(args=args)
rospy.init_node('ads1115', anonymous=True)
node = ADS1115(0)
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment