A cikin 'yan shekarun nan, fannin fasahar mutum-mutumi ya sami bunƙasa a cikin ci gaba da kuma amfani da fasaha. Ɗayan su shine ROS2 (Robot Operating System 2), tsarin buɗe hanyar tsakiya wanda ke ba da kayan aiki, dakunan karatu, da kuma tarurruka don haɓakawa, gwadawa da kula da aikace-aikacen robots. Wannan labarin yana zurfafa cikin ƙara fayilolin sabis a cikin fakitin ROS2 ta amfani da setup.py, ƙwarewa mai mahimmanci ga masu haɓaka aiki tare da wannan dandamali. Za mu fara da taƙaitaccen gabatarwa, gabatar da mafita ga matsalar, bi da ku ta hanyar jagora mataki-mataki, kuma mu tattauna batutuwa masu dangantaka da su don ƙarin fahimta.
Saita fayil ɗin sabis a cikin kunshin ROS2 ya ƙunshi manyan matakai guda biyu: ƙirƙirar fayil ɗin ma'anar saƙon sabis (.srv) da ƙara saitunan da suka dace a cikin kunshin.xml da fayilolin setup.py. A cikin wannan koyawa, za mu ƙirƙiri sabis mai sauƙi wanda ke ɗaukar zaren shigarwa kuma ya dawo da tsawon shigarwar da aka bayar.
Da farko, bari mu shirya filin aikinmu. Ƙirƙiri fakitin ROS2 mai suna "string_length_service" tare da umarni mai zuwa:
““
ros2 pkg ƙirƙira -build-type ament_python string_length_service
““
Yanzu, ƙirƙirar kundin adireshi mai suna "srv" a cikin fakitin "string_length_service" don adana fayil ɗin ma'anar saƙon sabis ɗin mu:
““
mkdir srv
““
A cikin kundin “srv”, ƙirƙirar fayil mai suna “StringLength.srv” tare da abun ciki mai zuwa:
““
string input_string
-
int64 tsayi
““
A cikin fayil ɗin package.xml, tabbatar da ƙara abubuwan dogaro masu mahimmanci:
"xml
““
Yanzu, bari mu matsa zuwa ainihin ɓangaren wannan labarin - ƙara fayilolin sabis a cikin setup.py. Bude fayil ɗin "setup.py" kuma haɗa da lambar mai zuwa:
"' Python
daga setuptools shigo da saitin
daga glob shigo da glob
package_name = 'string_length_service'
saitin(
name=kunshin_name,
sigar = '0.0.0',
fakiti = [kunshin_name],
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + sunan kunshin, ['package.xml']),
('share/' + sunan kunshin, glob('srv/*'))
],
install_requires=['setuptools'],
zip_safe=Gaskiya,
maintainer='developer_name_nan',
maintainer_email='developer_email_nan',
description='Sabis don ƙididdige tsawon kirtani',
lasisi = 'MIT',
tests_require=['pytest'],
shigarwa_points={
'console_scripts': [
'string_length_server =' + sunan kunshin + '.string_length_server: main',
'string_length_client =' + sunan kunshin + '.string_length_client: main',
],
},
)
““
Yanzu bari mu tattauna abin da wannan code yake yi: Fayil ɗin setup.py yana amfani da kayan aiki don rarrabawa da sarrafa fakitinmu. Fakitin sunan mai canzawa yana riƙe da sunan fakitinmu. Lissafin data_files yana ƙayyadad da mahimman albarkatu, package.xml, da fayilolin sabis don rarraba tare da fakitin. install_requires yana lissafin fakitin da ake buƙata, kuma shigarwa_points suna bayyana fayilolin aiwatar da ayyukan mu. Tabbatar maye gurbin 'developer_name_here' da 'developer_email_here' tare da ƙimar da ta dace.
Amfani da rclpy da std_srvs
Bayan kafa fayilolin sabis a cikin setup.py, ya zama dole a aiwatar da uwar garken da lambar abokin ciniki ta amfani da ROS2 Python Client Library, rclpy, da std_srvs don daidaitattun nau'ikan sabis.
Ƙirƙiri fayilolin "string_length_server.py" da "string_length_client.py" a cikin kundin adireshin ku kuma aiwatar da uwar garken da lambar abokin ciniki. Kuna iya samun cikakkun bayanai kan yadda ake rubuta uwar garken da lambar abokin ciniki a cikin takaddun ROS2.
Gina da gwada sabis
A ƙarshe, gina ku gwada kunshin ku ta amfani da umarni masu zuwa:
““
source /opt/ros/foxy/setup.bash
colcon gini - fakiti-zaɓi string_length_service
tushen shigar/setup.bash
““
Bayan nasarar ginawa, gudanar da uwar garken da abokin ciniki ta amfani da tashoshi guda biyu:
““
ros2 gudu string_length_service string_length_server
““
““
ros2 gudu string_length_service string_length_client
““
Wannan jagorar ya gabatar da bayani mai zurfi na ƙara fayilolin sabis a cikin setup.py don fakitin ROS2 yayin taɓa batutuwa masu alaƙa kamar rclpy da std_srvs. Ta bin waɗannan matakan, zaku iya ƙirƙira da rarraba ayyukanku na ROS2 a cikin ingantaccen tsari da tsari, sauƙaƙe mu'amala a cikin aikace-aikacen ku na mutum-mutumi. Sa'a da farin ciki coding!