Skip to content

Commit

Permalink
Merge pull request #112 from vortexntnu/feature/temperature
Browse files Browse the repository at this point in the history
Feature/temperature
  • Loading branch information
PizzaAllTheWay authored Nov 12, 2023
2 parents 5b08048 + 91a0451 commit 8594697
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 0 deletions.
10 changes: 10 additions & 0 deletions sensors/temperature/launch/temperature_system_launch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
return LaunchDescription([
Node(
package='temperature',
executable='temperature_publisher_node'
)
])
22 changes: 22 additions & 0 deletions sensors/temperature/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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">
<name>temperature</name>
<version>1.0.0</version>
<description>TODO: Package description</description>
<maintainer email="[email protected]">vortex</maintainer>
<license>MIT</license>

<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_pep257</test_depend>
<test_depend>python3-pytest</test_depend>

<exec_depend>std_msgs</exec_depend>
<exec_depend>freya_bms</exec_depend>
<exec_depend>ros2launch</exec_depend>

<export>
<build_type>ament_python</build_type>
</export>
</package>
Empty file.
4 changes: 4 additions & 0 deletions sensors/temperature/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[develop]
script_dir=$base/lib/temperature
[install]
install_scripts=$base/lib/temperature
29 changes: 29 additions & 0 deletions sensors/temperature/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os
from glob import glob
from setuptools import find_packages, setup

package_name = 'temperature'

setup(
name=package_name,
version='1.0.0',
packages=find_packages(exclude=['test']),
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
(os.path.join('share', package_name), glob('launch/*.py')),
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='vortex',
maintainer_email='[email protected]',
description='Temperature sensor data gartehring from temperature sensor system',
license='MIT',
tests_require=['pytest'],
entry_points={
'console_scripts': [
"temperature_publisher_node = temperature.temperature_publisher_node:main"
],
},
)
Empty file.
32 changes: 32 additions & 0 deletions sensors/temperature/temperature/temperature_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import smbus
import time
import struct

class TemperatureModule:
def __init__(self):
# to read temperature from arduino through I2C
self.i2c_address = 0x22

# init of I2C bus communication
self.bus = smbus.SMBus(1)
time.sleep(1)

def get_data(self):
try:
# Define the number of floats and the corresponding byte length
num_floats = 6
byte_length = num_floats * 4 # Each float is 4 bytes

# Read a block of bytes from the I2C device
data = self.bus.read_i2c_block_data(self.i2c_address, 0, byte_length)

# Convert the byte list to a list of floats
floats = struct.unpack('<' + 'f' * num_floats, bytes(data[:byte_length]))

return floats
except Exception as e:
print(f"Error: {e}")
return None

def shutdown(self):
self.bus.close()
65 changes: 65 additions & 0 deletions sensors/temperature/temperature/temperature_publisher_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import rclpy
from rclpy.node import Node
from std_msgs.msg import Float32 # Import the Float32MultiArray message type

import temperature.temperature_lib # Import your custom temperature library

class TemperaturePublisher(Node):
def __init__(self):
super().__init__("temperature_publisher")
# Create a publisher that publishes Float32MultiArray messages on the 'temperature_data' topic
self.publisher_ESC1_ = self.create_publisher(Float32, "/asv/temperature/ESC1", 10)
self.publisher_ESC2_ = self.create_publisher(Float32, "/asv/temperature/ESC2", 10)
self.publisher_ESC3_ = self.create_publisher(Float32, "/asv/temperature/ESC3", 10)
self.publisher_ESC4_ = self.create_publisher(Float32, "/asv/temperature/ESC4", 10)
self.publisher_ambient1_ = self.create_publisher(Float32, "/asv/temperature/ambient1", 10)
self.publisher_ambient2_ = self.create_publisher(Float32, "/asv/temperature/ambient2", 10)

# Create a timer that calls the timer_callback method every 1.0 seconds
timer_period = 1.0
self.timer = self.create_timer(timer_period, self.timer_callback)

# Initialize the TemperatureModule to interact with the temperature sensor
self.TemperatureObjcet = temperature.temperature_lib.TemperatureModule()

def timer_callback(self):
# Get the temperature data from the sensor
temperature_data_array = self.TemperatureObjcet.get_data()
if temperature_data_array is not None:
# If data is received, create a Float32 messages
msg_ESC1 = Float32()
msg_ESC2 = Float32()
msg_ESC3 = Float32()
msg_ESC4 = Float32()
msg_ambient1 = Float32()
msg_ambient2 = Float32()

# Assign the received temperature data to the correct message
msg_ESC1.data = temperature_data_array[0]
msg_ESC2.data = temperature_data_array[1]
msg_ESC3.data = temperature_data_array[2]
msg_ESC4.data = temperature_data_array[3]
msg_ambient1.data = temperature_data_array[5] # Inverse because of conections
msg_ambient2.data = temperature_data_array[4] # Inverse because of conections

# Publish the messages
self.publisher_ESC1_.publish(msg_ESC1)
self.publisher_ESC2_.publish(msg_ESC2)
self.publisher_ESC3_.publish(msg_ESC3)
self.publisher_ESC4_.publish(msg_ESC4)
self.publisher_ambient1_.publish(msg_ambient1)
self.publisher_ambient2_.publish(msg_ambient2)

# Log the published data for debugging purposes
self.get_logger().info(f"Temperature: {temperature_data_array}")

def main(args=None):
rclpy.init(args=args) # Initialize the ROS2 Python client library
temperature_publisher = TemperaturePublisher() # Create the TemperaturePublisher node
rclpy.spin(temperature_publisher) # Spin the node so the callback function is called
# After shutdown, destroy the node and shutdown rclpy
temperature_publisher.destroy_node()
rclpy.shutdown()

if __name__ == '__main__':
main() # Run the main function if the script is executed
25 changes: 25 additions & 0 deletions sensors/temperature/test/test_copyright.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2015 Open Source Robotics Foundation, Inc.
#
# 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.

from ament_copyright.main import main
import pytest


# Remove the `skip` decorator once the source file(s) have a copyright header
@pytest.mark.skip(reason='No copyright header has been placed in the generated source file.')
@pytest.mark.copyright
@pytest.mark.linter
def test_copyright():
rc = main(argv=['.', 'test'])
assert rc == 0, 'Found errors'
25 changes: 25 additions & 0 deletions sensors/temperature/test/test_flake8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2017 Open Source Robotics Foundation, Inc.
#
# 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.

from ament_flake8.main import main_with_errors
import pytest


@pytest.mark.flake8
@pytest.mark.linter
def test_flake8():
rc, errors = main_with_errors(argv=[])
assert rc == 0, \
'Found %d code style errors / warnings:\n' % len(errors) + \
'\n'.join(errors)
23 changes: 23 additions & 0 deletions sensors/temperature/test/test_pep257.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2015 Open Source Robotics Foundation, Inc.
#
# 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.

from ament_pep257.main import main
import pytest


@pytest.mark.linter
@pytest.mark.pep257
def test_pep257():
rc = main(argv=['.', 'test'])
assert rc == 0, 'Found code style errors / warnings'

0 comments on commit 8594697

Please sign in to comment.