当前位置:   article > 正文

Vue3+Typescript+Ros2简单示例_vue ros

vue ros

ROS2的引入

main.ts中添加ros库的引入:

  1. import 'roslib/build/roslib'; // 注意build
  2. ros = new window.ROSLIB.Ros({
  3. url: 'ws://127.0.0.1:9090'
  4. });

Ros-Topic

一段封装过topic之后的代码:

  1. import getVProxy from '@/utils/app-proxy';
  2. import 'roslib/build/roslib';
  3. import assetKit from '@/utils/asset-kit';
  4. import * as autopic from '@/types/autopic';
  5. const proxy = getVProxy();
  6. const curTopic = ref('');
  7. const curTopicResponse = ref('');
  8. const topicOptions = autopic.AU_TOPIC_ARR;
  9. const topicMap = autopic.AU_TOPIC_MAP;
  10. let topicListener = new window.ROSLIB.Topic({
  11. ros: proxy.ros,
  12. // name: '/topic_path/topic',
  13. // messageType: '/msg_path/type'
  14. });
  15. onMounted(() => {
  16. console.log('TopicTestWidget.onMounted');
  17. // console.log(topicOptions)
  18. // console.log(topicMap)
  19. });
  20. onActivated(() => {
  21. console.log('TopicTestWidget.onActivated');
  22. if (!curTopic.value) return;
  23. subscribeTopic(curTopic.value, topicMap.get(curTopic.value)?.msgType);
  24. });
  25. onDeactivated(() => {
  26. // console.log('TopicTestWidget.onDeactivated')
  27. unsubscribeCurTopic();
  28. });
  29. onUnmounted(() => {
  30. console.log('TopicTestWidget.onUnmounted');
  31. });
  32. function subscribeTopic(name?: string, msgType?: string) {
  33. topicListener.name = name;
  34. topicListener.msgType = msgType;
  35. console.log(topicListener);
  36. topicListener.subscribe(function (message: any) {
  37. // console.log('******%s start*******', curTopic.value)
  38. // console.log(message)
  39. curTopicResponse.value = JSON.stringify(message);
  40. // console.log('******%s end*******', curTopic.value)
  41. });
  42. }
  43. function unsubscribeCurTopic() {
  44. topicListener.unsubscribe();
  45. }
  46. function onTopicClear() {
  47. // ElMessage.info('onTopicClear ' + curTopic.value)
  48. topicListener.unsubscribe();
  49. // topicListener.unsubscribe(() => {
  50. // console.log('unsubscribe change to=>%s', curTopic.value)
  51. // })
  52. }
  53. function onTopicChange() {
  54. // ElMessage.info('onTopicChange ' + curTopic.value)
  55. unsubscribeCurTopic();
  56. curTopicResponse.value = '';
  57. if (!curTopic.value) {
  58. console.log('onTopicChange to empty:' + curTopic.value);
  59. return;
  60. }
  61. subscribeTopic(curTopic.value, topicMap.get(curTopic.value)?.msgType);
  62. // topicListener.name = curTopic.value
  63. // topicListener.msgType = topicMap.get(curTopic.value)?.msgType
  64. // console.log(topicListener)
  65. // topicListener.subscribe(function (message: any) {
  66. // console.log('******%s start*******', curTopic.value)
  67. // console.log(message)
  68. // curTopicResponse.value = JSON.stringify(message)
  69. // console.log('******%s end*******', curTopic.value)
  70. // })
  71. }

Ros-Service

简单的service调用:

  1. const aService= new window.ROSLIB.Service({
  2. ros: proxy.ros,
  3. name: '/service1',
  4. serviceType: 'serviceType',
  5. });
  6. let aServiceRequest= new window.ROSLIB.ServiceRequest({
  7. });
  8. aService.callService(
  9. aServiceRequest,
  10. function (result: any) {
  11. console.log('---------aServiceresponse-----');
  12. console.log(result);
  13. }
  14. );

以上,mark一个简单的ros-vue3中topic/service示例。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/107792
推荐阅读
相关标签
  

闽ICP备14008679号