Skip to content

Commit

Permalink
Merge pull request #187 from cwruRobotics/temp-sensor
Browse files Browse the repository at this point in the history
Temp sensor
  • Loading branch information
benjaminwp18 authored Jun 8, 2024
2 parents f8636d0 + 12c8418 commit 75e58c4
Show file tree
Hide file tree
Showing 19 changed files with 351 additions and 2 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies = [
'pytest-qt',
'pytest-xvfb',
'TCA9555@git+https://github.com/InvincibleRMC/TCA9555',
'bluerobotics-tsys01@git+https://github.com/bluerobotics/tsys01-python',
'pymavlink',
'mavproxy'
]
Expand Down
14 changes: 13 additions & 1 deletion src/pi/pi_main/launch/pi_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ def generate_launch_description() -> LaunchDescription:
])
)

# Flood detection
flood_sensors_path = get_package_share_directory('flood_detection')

# Launches Realsense
flood_detection_launch = IncludeLaunchDescription(
PythonLaunchDescriptionSource([
os.path.join(
Expand All @@ -75,13 +75,25 @@ def generate_launch_description() -> LaunchDescription:
])
)

# Temperature sensor
temp_sensor_path = get_package_share_directory('temp_sensor')

temp_sensor_launch = IncludeLaunchDescription(
PythonLaunchDescriptionSource([
os.path.join(
temp_sensor_path, 'launch', 'temp_sensor_launch.py'
)
])
)

namespace_launch = GroupAction(
actions=[
PushRosNamespace(NAMESPACE),
manip_launch,
pixhawk_launch,
# cam_launch,
flood_detection_launch,
temp_sensor_launch,
pi_info_launch
]
)
Expand Down
37 changes: 37 additions & 0 deletions src/pi/temp_sensor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Flood Detection

## Overview

This package reads from the temperature sensor.

## Installation


## Usage

```bash
ros2 launch temp_sensor temp_sensor_launch.py
```

## Launch files

* **temp_sensor_launch.py:** Primary launch file for temp sensor.

## Nodes

### temp_sensor

Reads and publishes temperature.

#### Published Topics

* **`/temperature`** ([rov_msgs/temperature])

Temperature readings as floats


### test

Tests flooding.

[rov_msgs/temperature]: ../../rov_msgs/msg/Temperature.msg
25 changes: 25 additions & 0 deletions src/pi/temp_sensor/launch/temp_sensor_launch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from launch.launch_description import LaunchDescription
from launch_ros.actions import Node


def generate_launch_description() -> LaunchDescription:
"""
Generate LaunchDescription for temp_sensor.
Returns
-------
LaunchDescription
Launches temperature sensor node
"""
temp_sensor = Node(
package='temp_sensor',
executable='temp_sensor',
emulate_tty=True,
output='screen',
remappings=[('/pi/temperature', '/tether/temperature')]
)

return LaunchDescription([
temp_sensor
])
21 changes: 21 additions & 0 deletions src/pi/temp_sensor/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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>temp_sensor</name>
<version>0.0.3</version>
<description>Measures temperature</description>
<maintainer email="[email protected]">Benjamin Poulin</maintainer>
<license>Apache License 2.0</license>

<exec_depend>ros2launch</exec_depend>
<exec_depend>python3-smbus</exec_depend>

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

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

package_name = 'temp_sensor'

setup(
name=package_name,
version='0.0.3',
packages=[package_name],
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
# Include all launch files.
(os.path.join('share', package_name, 'launch'),
glob('launch/*launch.[pxy][yma]*'))
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='Benjamin Poulin',
maintainer_email='[email protected]',
description='Temperature sensor',
license='Apache License 2.0',
tests_require=['pytest'],
entry_points={
'console_scripts': [
'temp_sensor = temp_sensor.temp_sensor:main',
'dry_run = temp_sensor.temp_sensor_dry_run:main'
],
},
)
Empty file.
42 changes: 42 additions & 0 deletions src/pi/temp_sensor/temp_sensor/temp_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import rclpy
from rclpy.node import Node
from rclpy.qos import QoSPresetProfiles
import tsys01

from rov_msgs.msg import Temperature

READING_TIMER_PERIOD = 0.5 # Seconds


class TempSensor(Node):

def __init__(self) -> None:
super().__init__('temp_sensor', parameter_overrides=[])
self.publisher = self.create_publisher(Temperature, 'temperature',
QoSPresetProfiles.DEFAULT.value)
self.sensor = tsys01.TSYS01()
self.sensor.init()

self.timer = self.create_timer(READING_TIMER_PERIOD, self.timer_callback)

def timer_callback(self) -> None:
try:
self.sensor.read()
temp_reading = self.sensor.temperature()

# If any of the sensors detect water, send true to /tether/flooding
self.publisher.publish(Temperature(reading=temp_reading))
except OSError:
print('Failed to read temperature, skipping this read')


def main(args: None = None) -> None:
rclpy.init()
temp_sensor = TempSensor()
rclpy.spin(temp_sensor)
temp_sensor.destroy_node()
rclpy.shutdown()


if __name__ == '__main__':
main()
26 changes: 26 additions & 0 deletions src/pi/temp_sensor/temp_sensor/temp_sensor_dry_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from time import sleep

import tsys01 # https://github.com/bluerobotics/tsys01-python


def main() -> None:
sensor = tsys01.TSYS01() # Use default I2C bus 1

sensor.init()

while True:
try:
sensor.read() # Sometimes throws OSError: [Errno 121] Remote I/O error
print(
sensor.temperature(), # Get temperature in default units (Centigrade)
'\t',
sensor.temperature(tsys01.UNITS_Farenheit)
)
except OSError:
print('Failed to read temperature, trying again')

sleep(1)


if __name__ == "__main__":
main()
25 changes: 25 additions & 0 deletions src/pi/temp_sensor/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() -> None:
rc, errors = main_with_errors(argv=[])
assert rc == 0, \
'Found %d code style errors / warnings:\n' % len(errors) + \
'\n'.join(errors)
14 changes: 14 additions & 0 deletions src/pi/temp_sensor/test/test_mypy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Test mypy on this module."""

import os
import pytest
from ament_mypy.main import main


@pytest.mark.mypy
@pytest.mark.linter
def test_mypy() -> None:
"""Tests mypy on this module."""
path = os.path.join(os.getcwd(), "..", "..", "..", "pyproject.toml")
error_code = main(argv=["--config", path])
assert error_code == 0, 'Found code style errors / warnings'
23 changes: 23 additions & 0 deletions src/pi/temp_sensor/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() -> None:
rc = main(argv=['.', 'test'])
assert rc == 0, 'Found code style errors / warnings'
1 change: 1 addition & 0 deletions src/rov_msgs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ rosidl_generate_interfaces(${PROJECT_NAME}
"msg/Manip.msg"
"msg/CameraControllerSwitch.msg"
"msg/Flooding.msg"
"msg/Temperature.msg"
"msg/IPAddress.msg"
"msg/VehicleState.msg"
"msg/ValveManip.msg"
Expand Down
1 change: 1 addition & 0 deletions src/rov_msgs/msg/Temperature.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
float64 reading
18 changes: 17 additions & 1 deletion src/surface/gui/gui/operator_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from gui.widgets.float_comm import FloatComm
from gui.widgets.timer import InteractiveTimer
from gui.widgets.task_selector import TaskSelector
from gui.widgets.temperature import TemperatureSensor
from gui.widgets.heartbeat import HeartbeatWidget
from gui.widgets.ip_widget import IPWidget
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QGridLayout, QTabWidget, QWidget, QVBoxLayout


Expand All @@ -18,8 +22,20 @@ def __init__(self) -> None:
main_layout = QGridLayout()
main_tab.setLayout(main_layout)

right_bar = QVBoxLayout()
main_layout.addLayout(right_bar, 0, 1)

timer = InteractiveTimer()
main_layout.addWidget(timer, 0, 1)
right_bar.addWidget(timer)

temp_sensor = TemperatureSensor()
right_bar.addWidget(temp_sensor)

right_bar.addWidget(HeartbeatWidget(), alignment=Qt.AlignmentFlag.AlignTop |
Qt.AlignmentFlag.AlignLeft)

right_bar.addWidget(IPWidget(), alignment=Qt.AlignmentFlag.AlignTop |
Qt.AlignmentFlag.AlignLeft)

task_selector = TaskSelector()
main_layout.addWidget(task_selector, 1, 1)
Expand Down
Loading

0 comments on commit 75e58c4

Please sign in to comment.