add led dev

This commit is contained in:
hehe
2025-10-23 11:54:24 +08:00
parent f55b8f6dfd
commit ece5a41a6d
9 changed files with 211 additions and 0 deletions

View File

View File

@@ -0,0 +1,88 @@
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
import serial
import time
class TM512AC_RS485_Driver:
def __init__(self, port='/dev/ttyUSB0', baudrate=250000, num_ic=5):
self.ser = serial.Serial(
port=port,
baudrate=baudrate,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_TWO,
timeout=0.1,
rtscts=False,
dsrdtr=False
)
self.num_ic = num_ic # TM512芯片数量
self.channels = 3 * num_ic # 总通道数RGB模式
def _send_break(self):
self.ser.sendBreak(0.000088) # 88μs Break
time.sleep(0.000008) # 8μs MAB
def _send_dmx_byte(self, value):
self.ser.write(bytes([value]))
def send_frame(self, rgb_data):
self._send_break()
self._send_dmx_byte(0x00)
for val in rgb_data[:self.channels]:
self._send_dmx_byte(val)
remaining = 512 - self.channels
if remaining > 0:
self.ser.write(bytes([0] * remaining))
def close(self):
self.ser.close()
class LedCommand(Node):
def __init__(self):
super().__init__('led_dev_node')
self.create_subscription(String,'/led_cmd',self.led_cmd_cb,10)
self.driver = TM512AC_RS485_Driver(port='/dev/ttyUSB0', num_ic=5)
self.get_logger().info("led_dev_node start....")
def all_color(self,rgb_data,xnum):
num_ic=5
rgb = [0, 0, 0] * num_ic
start_id=0
end_id=256
if xnum<0:
start_id=256
end_id=0
for i in range(start_id,end_id,xnum):
offset=i*5
val=i#val+offset
if val > 255:
val=255
if rgb_data=='red':
rgb = [val, 0, 0,0] * num_ic
elif rgb_data=='green':
rgb = [0, val, 0,0] * num_ic
elif rgb_data=='blue':
rgb = [0, 0, val,0] * num_ic
elif rgb_data=='cyan':#青色
rgb = [0, val, val,0] * num_ic
elif rgb_data=='yellow':#黄色
rgb = [val, val, 0,0] * num_ic
elif rgb_data=='purple':#紫色
rgb = [val, 0, val,0] * num_ic
elif rgb_data=='white':#紫色
rgb = [val, val, val,0] * num_ic
#print(rgb)
self.driver.send_frame(rgb)
time.sleep(0.1)
def led_blink(self,color,val):
self.all_color(color, -val)
self.all_color(color, val)
def led_cmd_cb(self,msg):
xcmd=msg.data
print('led cmd:',xcmd)
cmd,val=xcmd.split(',')
self.led_blink(cmd,int(val))
if __name__=='__main__':
rclpy.init()
node=LedCommand()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()

21
led_dev/package.xml Normal file
View File

@@ -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>led_dev</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="you@example.com">h</maintainer>
<license>TODO: License declaration</license>
<depend>std_msgs</depend>
<depend>rclpy</depend>
<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_pep257</test_depend>
<test_depend>python3-pytest</test_depend>
<export>
<build_type>ament_python</build_type>
</export>
</package>

0
led_dev/resource/led_dev Normal file
View File

4
led_dev/setup.cfg Normal file
View File

@@ -0,0 +1,4 @@
[develop]
script_dir=$base/lib/led_dev
[install]
install_scripts=$base/lib/led_dev

25
led_dev/setup.py Normal file
View File

@@ -0,0 +1,25 @@
from setuptools import find_packages, setup
package_name = 'led_dev'
setup(
name=package_name,
version='0.0.0',
packages=find_packages(exclude=['test']),
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='h',
maintainer_email='you@example.com',
description='TODO: Package description',
license='TODO: License declaration',
tests_require=['pytest'],
entry_points={
'console_scripts': [
],
},
)

View File

@@ -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'

View File

@@ -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)

View File

@@ -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'