-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1215 lines (828 loc) · 143 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon32.ico">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon16.ico">
<link rel="mask-icon" href="/images/logo.svg" color="#222">
<link rel="stylesheet" href="/css/main.css">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
<script id="hexo-configurations">
var NexT = window.NexT || {};
var CONFIG = {"hostname":"luozhexin.github.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
</script>
<meta name="description" content="怕什么真理无穷,进一寸有一寸的欢喜">
<meta property="og:type" content="website">
<meta property="og:title" content="onewall的博客">
<meta property="og:url" content="https://luozhexin.github.io/index.html">
<meta property="og:site_name" content="onewall的博客">
<meta property="og:description" content="怕什么真理无穷,进一寸有一寸的欢喜">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="luozhexin">
<meta name="twitter:card" content="summary">
<link rel="canonical" href="https://luozhexin.github.io/">
<script id="page-configurations">
// https://hexo.io/docs/variables.html
CONFIG.page = {
sidebar: "",
isHome : true,
isPost : false,
lang : 'zh-CN'
};
</script>
<title>onewall的博客</title>
<noscript>
<style>
.use-motion .brand,
.use-motion .menu-item,
.sidebar-inner,
.use-motion .post-block,
.use-motion .pagination,
.use-motion .comments,
.use-motion .post-header,
.use-motion .post-body,
.use-motion .collection-header { opacity: initial; }
.use-motion .site-title,
.use-motion .site-subtitle {
opacity: initial;
top: initial;
}
.use-motion .logo-line-before i { left: initial; }
.use-motion .logo-line-after i { right: initial; }
</style>
</noscript>
</head>
<body itemscope itemtype="http://schema.org/WebPage">
<div class="container use-motion">
<div class="headband"></div>
<header class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-container">
<div class="site-nav-toggle">
<div class="toggle" aria-label="切换导航栏">
<span class="toggle-line toggle-line-first"></span>
<span class="toggle-line toggle-line-middle"></span>
<span class="toggle-line toggle-line-last"></span>
</div>
</div>
<div class="site-meta">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<h1 class="site-title">onewall的博客</h1>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<div class="site-nav-right">
<div class="toggle popup-trigger">
</div>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="main-menu menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>
</li>
</ul>
</nav>
</div>
</header>
<div class="back-to-top">
<i class="fa fa-arrow-up"></i>
<span>0%</span>
</div>
<main class="main">
<div class="main-inner">
<div class="content-wrap">
<div class="content index posts-expand">
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="https://luozhexin.github.io/2021/04/14/stmyizhi/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/profile.jpg">
<meta itemprop="name" content="luozhexin">
<meta itemprop="description" content="怕什么真理无穷,进一寸有一寸的欢喜">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="onewall的博客">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/04/14/stmyizhi/" class="post-title-link" itemprop="url">stm32移植笔记</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2021-04-14 23:06:31 / 修改时间:23:13:35" itemprop="dateCreated datePublished" datetime="2021-04-14T23:06:31+08:00">2021-04-14</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/STM32/" itemprop="url" rel="index"><span itemprop="name">STM32</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="移植"><a href="#移植" class="headerlink" title="移植"></a>移植</h1><p>参考文章:<br><a target="_blank" rel="noopener" href="https://blog.csdn.net/wwt18811707971/article/details/83043257?utm_source=app&app_version=4.5.8">https://blog.csdn.net/wwt18811707971/article/details/83043257?utm_source=app&app_version=4.5.8</a></p>
<p>对于移植STM32F103VET6的代码到STM32F103RET6(12M晶振),步骤是1.1,2.1</p>
<h1 id="在keil下编译,已经定义好的代码提示未定义的解决办法"><a href="#在keil下编译,已经定义好的代码提示未定义的解决办法" class="headerlink" title="在keil下编译,已经定义好的代码提示未定义的解决办法"></a>在keil下编译,已经定义好的代码提示未定义的解决办法</h1><p>参考文章:<br><a target="_blank" rel="noopener" href="https://allen5g.blog.csdn.net/article/details/92842050">https://allen5g.blog.csdn.net/article/details/92842050</a></p>
<p>编译出现如下错误:<br>…\main.c(24): error: #20: identifier “TIM_TimeBaseInitTypeDef” is undefined<br>…\main.c: TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;<br>…\main.c: ^<br>…\main.c(47): error: #20: identifier “TIM_CounterMode_Up” is undefined<br>…\main.c: TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;</p>
<p>所有提示未定义的代码都已经确定定义过,但为什么会出现这样的情况?</p>
<p>最后发现,在stm32f10x_conf.h 里面包含stm32f10x_tim.h头文件的代码被注释掉了,重新包含这个头文件就把上面的问题解决了。</p>
<p>如果还有报错,勾选C99。</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="https://luozhexin.github.io/2021/04/14/nvidaubuntu/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/profile.jpg">
<meta itemprop="name" content="luozhexin">
<meta itemprop="description" content="怕什么真理无穷,进一寸有一寸的欢喜">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="onewall的博客">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/04/14/nvidaubuntu/" class="post-title-link" itemprop="url">解决安装NVIDIA驱动后不能正常安装ubuntu的问题</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2021-04-14 22:50:18 / 修改时间:23:05:07" itemprop="dateCreated datePublished" datetime="2021-04-14T22:50:18+08:00">2021-04-14</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/linux/" itemprop="url" rel="index"><span itemprop="name">linux</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>当电脑安装了NVDIA驱动后,在安装ubuntu的时候出现画面卡住,无法正常安装。</p>
<h1 id="卡死在logo界面"><a href="#卡死在logo界面" class="headerlink" title="卡死在logo界面"></a>卡死在logo界面</h1><p>在grub引导界面,用方向键选到install ubuntu,不要按回车,而是按e,进入参数配置编辑界面;</p>
<p>找到quiet splash,在后面空一格再加上<br><code>acpi_osi=linux nomodeset</code></p>
<p>然后按F10即可进入正常的安装流程</p>
<p>(这种方法是只使用核显,该方法不太好,如果你要使用英伟达显卡加速深度学习训练)</p>
<h1 id="编辑grub文件"><a href="#编辑grub文件" class="headerlink" title="编辑grub文件"></a>编辑grub文件</h1><p>在上述步骤完成后,进入ubuntu图形界面时,执行指令<br><code>sudo gedit /etc/default/grub </code><br>找到quiet splash,在后面空一格再加上<br><code>acpi_osi=linux nomodeset</code></p>
<p>保存后,更新grub配置:<br><code>sudo update-grub</code></p>
<p>参考文章:<br><a target="_blank" rel="noopener" href="https://blog.csdn.net/ysy950803/article/details/78507892?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161841262216780264033414%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=161841262216780264033414&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-78507892.first_rank_v2_pc_rank_v29&utm_term=%E8%A7%A3%E5%86%B3nvidia%E6%98%BE%E5%8D%A1%E7%9A%84%E7%94%B5%E8%84%91%E5%AE%89%E8%A3%85Ubuntu">https://blog.csdn.net/ysy950803/article/details/78507892?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161841262216780264033414%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=161841262216780264033414&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-78507892.first_rank_v2_pc_rank_v29&utm_term=%E8%A7%A3%E5%86%B3nvidia%E6%98%BE%E5%8D%A1%E7%9A%84%E7%94%B5%E8%84%91%E5%AE%89%E8%A3%85Ubuntu</a></p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/sherpahu/article/details/89792000?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161841269816780357232913%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=161841269816780357232913&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~baidu_landing_v2~default-1-89792000.first_rank_v2_pc_rank_v29&utm_term=ubuntu%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE%E8%B8%A9%E5%9D%91">https://blog.csdn.net/sherpahu/article/details/89792000?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161841269816780357232913%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=161841269816780357232913&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~baidu_landing_v2~default-1-89792000.first_rank_v2_pc_rank_v29&utm_term=ubuntu%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE%E8%B8%A9%E5%9D%91</a></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="https://luozhexin.github.io/2021/03/10/rosparam/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/profile.jpg">
<meta itemprop="name" content="luozhexin">
<meta itemprop="description" content="怕什么真理无穷,进一寸有一寸的欢喜">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="onewall的博客">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/03/10/rosparam/" class="post-title-link" itemprop="url">rosparam</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2021-03-10 12:20:37 / 修改时间:12:35:02" itemprop="dateCreated datePublished" datetime="2021-03-10T12:20:37+08:00">2021-03-10</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/ROS/" itemprop="url" rel="index"><span itemprop="name">ROS</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>参数服务器在ROS中主要用于实现不同节点之间的数据共享。参数服务器相当于是独立于所有节点的一个公共容器,可以将数据存储在该容器中,被不同的节点调用,当然不同的节点也可以往其中存储数据,关于参数服务器的典型应用场景如下:</p>
<p>导航实现时,会进行路径规划,比如: 全局路径规划,设计一个从出发点到目标点的大致路径。本地路径规划,会根据当前路况生成时时的行进路径</p>
<p>上述场景中,全局路径规划和本地路径规划时,就会使用到参数服务器:</p>
<p>路径规划时,需要参考小车的尺寸,我们可以将这些尺寸信息存储到参数服务器,全局路径规划节点与本地路径规划节点都可以从参数服务器中调用这些参数<br>参数服务器,一般适用于存在数据共享的一些应用场景。</p>
<h1 id="cpp实现"><a href="#cpp实现" class="headerlink" title="cpp实现"></a>cpp实现</h1><h2 id="参数服务器新增-修改-参数"><a href="#参数服务器新增-修改-参数" class="headerlink" title="参数服务器新增(修改)参数"></a>参数服务器新增(修改)参数</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">/*</span><br><span class="line"> 参数服务器操作之新增与修改(二者API一样)_C++实现:</span><br><span class="line"> 在 roscpp 中提供了两套 API 实现参数操作</span><br><span class="line"> ros::NodeHandle</span><br><span class="line"> setParam("键",值)</span><br><span class="line"> ros::param</span><br><span class="line"> set("键","值")</span><br><span class="line"></span><br><span class="line"> 示例:分别设置整形、浮点、字符串、bool、列表、字典等类型参数</span><br><span class="line"> 修改(相同的键,不同的值)</span><br><span class="line"></span><br><span class="line">*/</span><br><span class="line">#include "ros/ros.h"</span><br><span class="line"></span><br><span class="line">int main(int argc, char *argv[])</span><br><span class="line">{</span><br><span class="line"> /* code */</span><br><span class="line"> ros::init(argc,argv,"set_param_c");</span><br><span class="line"> ros::NodeHandle nh;</span><br><span class="line"> nh.setParam("type","xiaohuang");</span><br><span class="line"> nh.setParam("radius",0.15);</span><br><span class="line"> ros::param::set("type_","xiaobai");</span><br><span class="line"> ros::param::set("radius_param",0.15);</span><br><span class="line"></span><br><span class="line"> nh.setParam("radius",0.2);</span><br><span class="line"> ros::param::set("radius_param",0.2);</span><br><span class="line"> return 0;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="参数服务器获取参数"><a href="#参数服务器获取参数" class="headerlink" title="参数服务器获取参数"></a>参数服务器获取参数</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line">/*</span><br><span class="line"> 参数服务器操作之查询_C++实现:</span><br><span class="line"> 在 roscpp 中提供了两套 API 实现参数操作</span><br><span class="line"> ros::NodeHandle</span><br><span class="line"></span><br><span class="line"> param(键,默认值) </span><br><span class="line"> 存在,返回对应结果,否则返回默认值</span><br><span class="line"></span><br><span class="line"> getParam(键,存储结果的变量)</span><br><span class="line"> 存在,返回 true,且将值赋值给参数2</span><br><span class="line"> 若果键不存在,那么返回值为 false,且不为参数2赋值</span><br><span class="line"></span><br><span class="line"> getParamCached键,存储结果的变量)--提高变量获取效率</span><br><span class="line"> 存在,返回 true,且将值赋值给参数2</span><br><span class="line"> 若果键不存在,那么返回值为 false,且不为参数2赋值</span><br><span class="line"></span><br><span class="line"> getParamNames(std::vector<std::string>)</span><br><span class="line"> 获取所有的键,并存储在参数 vector 中 </span><br><span class="line"></span><br><span class="line"> hasParam(键)</span><br><span class="line"> 是否包含某个键,存在返回 true,否则返回 false</span><br><span class="line"></span><br><span class="line"> searchParam(参数1,参数2)</span><br><span class="line"> 搜索键,参数1是被搜索的键,参数2存储搜索结果的变量</span><br><span class="line"></span><br><span class="line"> ros::param ----- 与 NodeHandle 类似</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">*/</span><br><span class="line">#include "ros/ros.h"</span><br><span class="line"></span><br><span class="line">int main(int argc, char *argv[])</span><br><span class="line">{</span><br><span class="line"> /* code */</span><br><span class="line"> ros::init(argc,argv,"get_param_c");</span><br><span class="line"> ros::NodeHandle nh;</span><br><span class="line"> double radius=nh.param("radius",0.5);</span><br><span class="line"> ROS_INFO("radius=%.2f",radius);</span><br><span class="line"></span><br><span class="line"> double radius2=0.0;</span><br><span class="line"> bool result=nh.getParam("radius",radius2);</span><br><span class="line"> if (result)</span><br><span class="line"> {</span><br><span class="line"> ROS_INFO("radius is %.2f",radius2);</span><br><span class="line"> }else{</span><br><span class="line"> ROS_INFO("radius not exist");</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> std::vector<std::string> names;</span><br><span class="line"> nh.getParamNames(names);</span><br><span class="line"> for (auto &&name : names)</span><br><span class="line"> {</span><br><span class="line"> ROS_INFO("paramName is %s",name.c_str());</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> bool flag1=nh.hasParam("radius");</span><br><span class="line"> bool flag2=nh.hasParam("radiusxxx");</span><br><span class="line"> ROS_INFO("radius is exist? %d",flag1);</span><br><span class="line"> ROS_INFO("radiusxxx is exist? %d",flag2);</span><br><span class="line"></span><br><span class="line"> std::string key;</span><br><span class="line"> nh.searchParam("radius",key);</span><br><span class="line"> ROS_INFO("result is %s",key.c_str());</span><br><span class="line"></span><br><span class="line"> double radius_param=ros::param::param("radius",100.5);</span><br><span class="line"> ROS_INFO("radius_param =%.2f",radius_param);</span><br><span class="line"></span><br><span class="line"> std::vector<std::string> names_param;</span><br><span class="line"> ros::param::getParamNames(names_param);</span><br><span class="line"> for (auto &&name_param : names_param)</span><br><span class="line"> {</span><br><span class="line"> ROS_INFO("key:%s",name_param.c_str());</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> return 0;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="参数服务器删除参数"><a href="#参数服务器删除参数" class="headerlink" title="参数服务器删除参数"></a>参数服务器删除参数</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">/* </span><br><span class="line"> 参数服务器操作之删除_C++实现:</span><br><span class="line"></span><br><span class="line"> ros::NodeHandle</span><br><span class="line"> deleteParam("键")</span><br><span class="line"> 根据键删除参数,删除成功,返回 true,否则(参数不存在),返回 false</span><br><span class="line"></span><br><span class="line"> ros::param</span><br><span class="line"> del("键")</span><br><span class="line"> 根据键删除参数,删除成功,返回 true,否则(参数不存在),返回 false</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">*/</span><br><span class="line"></span><br><span class="line">#include "ros/ros.h"</span><br><span class="line"></span><br><span class="line">int main(int argc, char *argv[])</span><br><span class="line">{</span><br><span class="line"> /* code */</span><br><span class="line"> ros::init(argc,argv,"param_del_c");</span><br><span class="line"> ros::NodeHandle nh;</span><br><span class="line"> bool flag1=nh.deleteParam("radius");</span><br><span class="line"> if (flag1){</span><br><span class="line"> ROS_INFO("delet succes");</span><br><span class="line"> } else{</span><br><span class="line"> ROS_INFO("delet fault");</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> bool flag2=ros::param::del("radius_param");</span><br><span class="line"> if (flag2){</span><br><span class="line"> ROS_INFO("delet succes");</span><br><span class="line"> } else{</span><br><span class="line"> ROS_INFO("delet fault");</span><br><span class="line"> }</span><br><span class="line"> return 0;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h1 id="python实现"><a href="#python实现" class="headerlink" title="python实现"></a>python实现</h1><h2 id="参数服务器新增-修改-参数-1"><a href="#参数服务器新增-修改-参数-1" class="headerlink" title="参数服务器新增(修改)参数"></a>参数服务器新增(修改)参数</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">#! /usr/bin/env python</span><br><span class="line">#coding=utf-8</span><br><span class="line">import rospy</span><br><span class="line"></span><br><span class="line">if __name__ == "__main__":</span><br><span class="line"> rospy.init_node("param_set_p")</span><br><span class="line"> rospy.set_param("type_p","xiaohuache")</span><br><span class="line"> rospy.set_param("radius_p",10)</span><br><span class="line"></span><br><span class="line"> rospy.set_param("radius_p",20)</span><br><span class="line"></span><br><span class="line"> pass</span><br></pre></td></tr></table></figure>
<h2 id="参数服务器获取参数-1"><a href="#参数服务器获取参数-1" class="headerlink" title="参数服务器获取参数"></a>参数服务器获取参数</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">#! /usr/bin/env python</span><br><span class="line">"""</span><br><span class="line"> 参数服务器操作之查询_Python实现: </span><br><span class="line"> get_param(键,默认值)</span><br><span class="line"> 当键存在时,返回对应的值,如果不存在返回默认值</span><br><span class="line"> get_param_cached</span><br><span class="line"> get_param_names</span><br><span class="line"> has_param</span><br><span class="line"> search_param</span><br><span class="line">"""</span><br><span class="line">import rospy</span><br><span class="line"></span><br><span class="line">if __name__ == "__main__":</span><br><span class="line"> rospy.init_node("param_get_p")</span><br><span class="line"> radius=rospy.get_param("radius_p",15)</span><br><span class="line"> radius1=rospy.get_param("radius_p_",15)</span><br><span class="line"> rospy.loginfo("radius is %.2f",radius)</span><br><span class="line"> rospy.loginfo("radius is %.2f",radius1)</span><br><span class="line"></span><br><span class="line"> names=rospy.get_param_names()</span><br><span class="line"> for name in names:</span><br><span class="line"> rospy.loginfo("param name is %s",name)</span><br><span class="line"></span><br><span class="line"> flag1=rospy.has_param("radius_p")</span><br><span class="line"> if flag1:</span><br><span class="line"> rospy.loginfo("radius_p is exist")</span><br><span class="line"> else:</span><br><span class="line"> rospy.loginfo("radius_p not exist")</span><br><span class="line"></span><br><span class="line"> key=rospy.search_param("radius_p")</span><br><span class="line"> rospy.loginfo("key =%s",key)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"> pass</span><br></pre></td></tr></table></figure>
<h2 id="参数服务器删除参数-1"><a href="#参数服务器删除参数-1" class="headerlink" title="参数服务器删除参数"></a>参数服务器删除参数</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">#! /usr/bin/env python</span><br><span class="line">"""</span><br><span class="line"> 参数服务器操作之删除_Python实现:</span><br><span class="line"> rospy.delete_param("键")</span><br><span class="line"> 键存在时,可以删除成功,键不存在时,会抛出异常</span><br><span class="line">"""</span><br><span class="line">import rospy</span><br><span class="line"></span><br><span class="line">if __name__ == "__main__":</span><br><span class="line"> rospy.init_node("param_del_p")</span><br><span class="line"> try:</span><br><span class="line"> rospy.delete_param("type_p")</span><br><span class="line"> except Exception as e:</span><br><span class="line"> rospy.loginfo("deleted param is not exist")</span><br><span class="line"> pass</span><br></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="https://luozhexin.github.io/2021/03/09/git/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/profile.jpg">
<meta itemprop="name" content="luozhexin">
<meta itemprop="description" content="怕什么真理无穷,进一寸有一寸的欢喜">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="onewall的博客">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/03/09/git/" class="post-title-link" itemprop="url">git</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2021-03-09 17:16:04" itemprop="dateCreated datePublished" datetime="2021-03-09T17:16:04+08:00">2021-03-09</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2021-03-10 12:44:22" itemprop="dateModified" datetime="2021-03-10T12:44:22+08:00">2021-03-10</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="新工程"><a href="#新工程" class="headerlink" title="新工程"></a>新工程</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">git init </span><br><span class="line">git add . //.表示全部文件</span><br><span class="line">git commit -m "frist commit"</span><br><span class="line">git remote add origin https://github.com/usrname/repositoriesName.git</span><br><span class="line">git push -u origin master</span><br><span class="line">或者 git branch -M main</span><br><span class="line">git push -u origin main</span><br></pre></td></tr></table></figure>
<h1 id="再次提交"><a href="#再次提交" class="headerlink" title="再次提交"></a>再次提交</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git add . </span><br><span class="line">git commit -m "commit"</span><br><span class="line">git push -u origin master</span><br></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="https://luozhexin.github.io/2021/03/09/rossrv/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/profile.jpg">
<meta itemprop="name" content="luozhexin">
<meta itemprop="description" content="怕什么真理无穷,进一寸有一寸的欢喜">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="onewall的博客">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/03/09/rossrv/" class="post-title-link" itemprop="url">rossrv</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2021-03-09 16:40:43" itemprop="dateCreated datePublished" datetime="2021-03-09T16:40:43+08:00">2021-03-09</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2021-03-10 12:35:22" itemprop="dateModified" datetime="2021-03-10T12:35:22+08:00">2021-03-10</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/ROS/" itemprop="url" rel="index"><span itemprop="name">ROS</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>服务通信也是ROS中一种极其常用的通信模式,服务通信是基于请求响应模式的,是一种应答机制。<br>也即: 一个节点A向另一个节点B发送请求,B接收处理请求并产生响应结果返回给A。</p>
<h1 id="自定义srv"><a href="#自定义srv" class="headerlink" title="自定义srv"></a>自定义srv</h1><p>服务通信中,数据分成两部分,请求与响应,在 srv 文件中请求和响应使用—分割,具体实现如下:</p>
<p>功能包下新建 srv 目录,添加 xxx.srv 文件,内容:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"># 客户端请求时发送的两个数字</span><br><span class="line">int32 num1</span><br><span class="line">int32 num2</span><br><span class="line">---</span><br><span class="line"># 服务器响应发送的数据</span><br><span class="line">int32 sum</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>编辑配置文件</p>
<p>package.xml中添加编译依赖与执行依赖</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><build_depend>message_generation</build_depend></span><br><span class="line"></span><br><span class="line"><exec_depend>message_runtime</exec_depend></span><br></pre></td></tr></table></figure>
<p>CMakeLists.txt编辑 srv 相关配置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">find_package(catkin REQUIRED COMPONENTS</span><br><span class="line"> roscpp</span><br><span class="line"> rospy</span><br><span class="line"> std_msgs</span><br><span class="line"> message_generation</span><br><span class="line">)</span><br><span class="line"># 需要加入 message_generation,必须有 std_msgs</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">add_service_files(</span><br><span class="line"> FILES</span><br><span class="line"> AddInts.srv</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">generate_messages(</span><br><span class="line"> DEPENDENCIES</span><br><span class="line"> std_msgs</span><br><span class="line">)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h1 id="cpp实现"><a href="#cpp实现" class="headerlink" title="cpp实现"></a>cpp实现</h1><h2 id="vscode配置"><a href="#vscode配置" class="headerlink" title="vscode配置"></a>vscode配置</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">"includePath": [</span><br><span class="line"> "/opt/ros/noetic/include/**",</span><br><span class="line"> "/usr/include/**",</span><br><span class="line"> "/xxx/yyy工作空间/devel/include/**" //配置 head 文件的路径 </span><br><span class="line"> ],</span><br></pre></td></tr></table></figure>
<h2 id="服务端"><a href="#服务端" class="headerlink" title="服务端"></a>服务端</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line">/*</span><br><span class="line"> 需求: </span><br><span class="line"> 编写两个节点实现服务通信,客户端节点需要提交两个整数到服务器</span><br><span class="line"> 服务器需要解析客户端提交的数据,相加后,将结果响应回客户端,</span><br><span class="line"> 客户端再解析</span><br><span class="line"></span><br><span class="line"> 服务器实现:</span><br><span class="line"> 1.包含头文件</span><br><span class="line"> 2.初始化 ROS 节点</span><br><span class="line"> 3.创建 ROS 句柄</span><br><span class="line"> 4.创建 服务 对象</span><br><span class="line"> 5.回调函数处理请求并产生响应</span><br><span class="line"> 6.由于请求有多个,需要调用 ros::spin()</span><br><span class="line"></span><br><span class="line">*/</span><br><span class="line">#include "ros/ros.h"</span><br><span class="line">#include "demo03_server_client/AddInts.h"</span><br><span class="line"></span><br><span class="line">// bool 返回值由于标志是否处理成功</span><br><span class="line">bool doReq(demo03_server_client::AddInts::Request& req,</span><br><span class="line"> demo03_server_client::AddInts::Response& resp){</span><br><span class="line"> int num1 = req.num1;</span><br><span class="line"> int num2 = req.num2;</span><br><span class="line"></span><br><span class="line"> ROS_INFO("服务器接收到的请求数据为:num1 = %d, num2 = %d",num1, num2);</span><br><span class="line"></span><br><span class="line"> //逻辑处理</span><br><span class="line"> if (num1 < 0 || num2 < 0)</span><br><span class="line"> {</span><br><span class="line"> ROS_ERROR("提交的数据异常:数据不可以为负数");</span><br><span class="line"> return false;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> //如果没有异常,那么相加并将结果赋值给 resp</span><br><span class="line"> resp.sum = num1 + num2;</span><br><span class="line"> return true;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">int main(int argc, char *argv[])</span><br><span class="line">{</span><br><span class="line"> setlocale(LC_ALL,"");</span><br><span class="line"> // 2.初始化 ROS 节点</span><br><span class="line"> ros::init(argc,argv,"AddInts_Server");</span><br><span class="line"> // 3.创建 ROS 句柄</span><br><span class="line"> ros::NodeHandle nh;</span><br><span class="line"> // 4.创建 服务 对象</span><br><span class="line"> ros::ServiceServer server = nh.advertiseService("AddInts",doReq);</span><br><span class="line"> ROS_INFO("服务已经启动....");</span><br><span class="line"> // 5.回调函数处理请求并产生响应</span><br><span class="line"> // 6.由于请求有多个,需要调用 ros::spin()</span><br><span class="line"> ros::spin();</span><br><span class="line"> return 0;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="客户端"><a href="#客户端" class="headerlink" title="客户端"></a>客户端</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line">/*</span><br><span class="line"> 需求: </span><br><span class="line"> 编写两个节点实现服务通信,客户端节点需要提交两个整数到服务器</span><br><span class="line"> 服务器需要解析客户端提交的数据,相加后,将结果响应回客户端,</span><br><span class="line"> 客户端再解析</span><br><span class="line"></span><br><span class="line"> 服务器实现:</span><br><span class="line"> 1.包含头文件</span><br><span class="line"> 2.初始化 ROS 节点</span><br><span class="line"> 3.创建 ROS 句柄</span><br><span class="line"> 4.创建 客户端 对象</span><br><span class="line"> 5.请求服务,接收响应</span><br><span class="line"></span><br><span class="line">*/</span><br><span class="line">// 1.包含头文件</span><br><span class="line">#include "ros/ros.h"</span><br><span class="line">#include "demo03_server_client/AddInts.h"</span><br><span class="line"></span><br><span class="line">int main(int argc, char *argv[])</span><br><span class="line">{</span><br><span class="line"> setlocale(LC_ALL,"");</span><br><span class="line"></span><br><span class="line"> // 调用时动态传值,如果通过 launch 的 args 传参,需要传递的参数个数 +3</span><br><span class="line"> if (argc != 3)</span><br><span class="line"> // if (argc != 5)//launch 传参(0-文件路径 1传入的参数 2传入的参数 3节点名称 4日志路径)</span><br><span class="line"> {</span><br><span class="line"> ROS_ERROR("请提交两个整数");</span><br><span class="line"> return 1;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> // 2.初始化 ROS 节点</span><br><span class="line"> ros::init(argc,argv,"AddInts_Client");</span><br><span class="line"> // 3.创建 ROS 句柄</span><br><span class="line"> ros::NodeHandle nh;</span><br><span class="line"> // 4.创建 客户端 对象</span><br><span class="line"> ros::ServiceClient client = nh.serviceClient<demo03_server_client::AddInts>("AddInts");</span><br><span class="line"> //等待服务启动成功</span><br><span class="line"> //方式1</span><br><span class="line"> ros::service::waitForService("AddInts");</span><br><span class="line"> //方式2</span><br><span class="line"> // client.waitForExistence();</span><br><span class="line"> // 5.组织请求数据</span><br><span class="line"> demo03_server_client::AddInts ai;</span><br><span class="line"> ai.request.num1 = atoi(argv[1]); //转成整型</span><br><span class="line"> ai.request.num2 = atoi(argv[2]);</span><br><span class="line"> // 6.发送请求,返回 bool 值,标记是否成功</span><br><span class="line"> bool flag = client.call(ai);</span><br><span class="line"> // 7.处理响应</span><br><span class="line"> if (flag)</span><br><span class="line"> {</span><br><span class="line"> ROS_INFO("请求正常处理,响应结果:%d",ai.response.sum);</span><br><span class="line"> }</span><br><span class="line"> else</span><br><span class="line"> {</span><br><span class="line"> ROS_ERROR("请求处理失败....");</span><br><span class="line"> return 1;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> return 0;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="配置-CMakeLists-txt"><a href="#配置-CMakeLists-txt" class="headerlink" title="配置 CMakeLists.txt"></a>配置 CMakeLists.txt</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">add_executable(AddInts_Server src/AddInts_Server.cpp)</span><br><span class="line">add_executable(AddInts_Client src/AddInts_Client.cpp)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">add_dependencies(AddInts_Server ${PROJECT_NAME}_gencpp)</span><br><span class="line">add_dependencies(AddInts_Client ${PROJECT_NAME}_gencpp)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">target_link_libraries(AddInts_Server</span><br><span class="line"> ${catkin_LIBRARIES}</span><br><span class="line">)</span><br><span class="line">target_link_libraries(AddInts_Client</span><br><span class="line"> ${catkin_LIBRARIES}</span><br><span class="line">)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>需要先启动服务:rosrun 包名 服务</p>
<p>然后再调用客户端 :rosrun 包名 客户端 参数1 参数2</p>
<p>优化:</p>
<p>在客户端发送请求前添加:client.waitForExistence();</p>
<p>或:ros::service::waitForService(“AddInts”);</p>
<p>这是一个阻塞式函数,只有服务启动成功后才会继续执行</p>
<p>此处可以使用 launch 文件优化,但是需要注意 args 传参特点</p>
<h1 id="python实现"><a href="#python实现" class="headerlink" title="python实现"></a>python实现</h1><h2 id="vscode配置-1"><a href="#vscode配置-1" class="headerlink" title="vscode配置"></a>vscode配置</h2><p>配置settings.json</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> "python.autoComplete.extraPaths": [</span><br><span class="line"> "/opt/ros/noetic/lib/python3/dist-packages",</span><br><span class="line"> ]</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="服务端-1"><a href="#服务端-1" class="headerlink" title="服务端"></a>服务端</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">#! /usr/bin/env python</span><br><span class="line">"""</span><br><span class="line"> 需求: </span><br><span class="line"> 编写两个节点实现服务通信,客户端节点需要提交两个整数到服务器</span><br><span class="line"> 服务器需要解析客户端提交的数据,相加后,将结果响应回客户端,</span><br><span class="line"> 客户端再解析</span><br><span class="line"></span><br><span class="line"> 服务器端实现:</span><br><span class="line"> 1.导包</span><br><span class="line"> 2.初始化 ROS 节点</span><br><span class="line"> 3.创建服务对象</span><br><span class="line"> 4.回调函数处理请求并产生响应</span><br><span class="line"> 5.spin 函数</span><br><span class="line"></span><br><span class="line">"""</span><br><span class="line"># 1.导包</span><br><span class="line">import rospy</span><br><span class="line">from demo03_server_client.srv import AddInts,AddIntsRequest,AddIntsResponse</span><br><span class="line"># 回调函数的参数是请求对象,返回值是响应对象</span><br><span class="line">def doReq(req):</span><br><span class="line"> # 解析提交的数据</span><br><span class="line"> sum = req.num1 + req.num2</span><br><span class="line"> rospy.loginfo("提交的数据:num1 = %d, num2 = %d, sum = %d",req.num1, req.num2, sum)</span><br><span class="line"></span><br><span class="line"> # 创建响应对象,赋值并返回</span><br><span class="line"> # resp = AddIntsResponse()</span><br><span class="line"> # resp.sum = sum</span><br><span class="line"> resp = AddIntsResponse(sum)</span><br><span class="line"> return resp</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">if __name__ == "__main__":</span><br><span class="line"> # 2.初始化 ROS 节点</span><br><span class="line"> rospy.init_node("addints_server_p")</span><br><span class="line"> # 3.创建服务对象</span><br><span class="line"> server = rospy.Service("AddInts",AddInts,doReq)</span><br><span class="line"> # 4.回调函数处理请求并产生响应</span><br><span class="line"> # 5.spin 函数</span><br><span class="line"> rospy.spin()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="客户端-1"><a href="#客户端-1" class="headerlink" title="客户端"></a>客户端</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line">#! /usr/bin/env python</span><br><span class="line"></span><br><span class="line">"""</span><br><span class="line"> 需求: </span><br><span class="line"> 编写两个节点实现服务通信,客户端节点需要提交两个整数到服务器</span><br><span class="line"> 服务器需要解析客户端提交的数据,相加后,将结果响应回客户端,</span><br><span class="line"> 客户端再解析</span><br><span class="line"></span><br><span class="line"> 客户端实现:</span><br><span class="line"> 1.导包</span><br><span class="line"> 2.初始化 ROS 节点</span><br><span class="line"> 3.创建请求对象</span><br><span class="line"> 4.发送请求</span><br><span class="line"> 5.接收并处理响应</span><br><span class="line"></span><br><span class="line"> 优化:</span><br><span class="line"> 加入数据的动态获取</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">"""</span><br><span class="line">#1.导包</span><br><span class="line">import rospy</span><br><span class="line">from demo03_server_client.srv import *</span><br><span class="line">import sys</span><br><span class="line"></span><br><span class="line">if __name__ == "__main__":</span><br><span class="line"></span><br><span class="line"> #优化实现</span><br><span class="line"> if len(sys.argv) != 3:</span><br><span class="line"> rospy.logerr("请正确提交参数")</span><br><span class="line"> sys.exit(1)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> # 2.初始化 ROS 节点</span><br><span class="line"> rospy.init_node("AddInts_Client_p")</span><br><span class="line"> # 3.创建请求对象</span><br><span class="line"> client = rospy.ServiceProxy("AddInts",AddInts)</span><br><span class="line"> # 请求前,等待服务已经就绪</span><br><span class="line"> # 方式1:</span><br><span class="line"> # rospy.wait_for_service("AddInts")</span><br><span class="line"> # 方式2</span><br><span class="line"> client.wait_for_service()</span><br><span class="line"> # 4.发送请求,接收并处理响应</span><br><span class="line"> # 方式1</span><br><span class="line"> # resp = client(3,4)</span><br><span class="line"> # 方式2</span><br><span class="line"> # resp = client(AddIntsRequest(1,5))</span><br><span class="line"> # 方式3</span><br><span class="line"> req = AddIntsRequest()</span><br><span class="line"> # req.num1 = 100</span><br><span class="line"> # req.num2 = 200 </span><br><span class="line"></span><br><span class="line"> #优化</span><br><span class="line"> req.num1 = int(sys.argv[1])</span><br><span class="line"> req.num2 = int(sys.argv[2]) </span><br><span class="line"></span><br><span class="line"> resp = client.call(req)</span><br><span class="line"> rospy.loginfo("响应结果:%d",resp.sum)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="设置权限"><a href="#设置权限" class="headerlink" title="设置权限"></a>设置权限</h2><p>终端下进入 scripts 执行:chmod +x *.py</p>
<h2 id="配置-CMakeLists-txt-1"><a href="#配置-CMakeLists-txt-1" class="headerlink" title="配置 CMakeLists.txt"></a>配置 CMakeLists.txt</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">catkin_install_python(PROGRAMS</span><br><span class="line"> scripts/AddInts_Server_p.py </span><br><span class="line"> scripts/AddInts_Client_p.py</span><br><span class="line"> DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}</span><br><span class="line">)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>需要先启动服务:rosrun 包名 服务</p>
<p>然后再调用客户端 :rosrun 包名 客户端 参数1 参数2</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="https://luozhexin.github.io/2021/03/09/rosmsg/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/profile.jpg">
<meta itemprop="name" content="luozhexin">
<meta itemprop="description" content="怕什么真理无穷,进一寸有一寸的欢喜">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="onewall的博客">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/03/09/rosmsg/" class="post-title-link" itemprop="url">rosmsg</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2021-03-09 11:19:00" itemprop="dateCreated datePublished" datetime="2021-03-09T11:19:00+08:00">2021-03-09</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2021-03-10 12:35:14" itemprop="dateModified" datetime="2021-03-10T12:35:14+08:00">2021-03-10</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/ROS/" itemprop="url" rel="index"><span itemprop="name">ROS</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="自定义消息"><a href="#自定义消息" class="headerlink" title="自定义消息"></a>自定义消息</h1><h2 id="1-定义msg文件"><a href="#1-定义msg文件" class="headerlink" title="1.定义msg文件"></a>1.定义msg文件</h2><p>功能包下新建 msg 目录,添加文件 Person.msg</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">string name</span><br><span class="line">uint16 age</span><br><span class="line">float64 height</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="2-编辑配置文件"><a href="#2-编辑配置文件" class="headerlink" title="2.编辑配置文件"></a>2.编辑配置文件</h2><p>package.xml中添加编译依赖与执行依赖</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><build_depend>message_generation</build_depend></span><br><span class="line"><exec_depend>message_runtime</exec_depend></span><br></pre></td></tr></table></figure>
<p>CMakeLists.txt编辑 msg 相关配置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">find_package(catkin REQUIRED COMPONENTS</span><br><span class="line"> roscpp</span><br><span class="line"> rospy</span><br><span class="line"> std_msgs</span><br><span class="line"> message_generation</span><br><span class="line">)</span><br><span class="line"># 需要加入 message_generation,必须有 std_msgs</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">## 配置 msg 源文件</span><br><span class="line">add_message_files(</span><br><span class="line"> FILES</span><br><span class="line"> Person.msg</span><br><span class="line">)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"># 生成消息时依赖于 std_msgs</span><br><span class="line">generate_messages(</span><br><span class="line"> DEPENDENCIES</span><br><span class="line"> std_msgs</span><br><span class="line">)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">#执行时依赖</span><br><span class="line">catkin_package(</span><br><span class="line"># INCLUDE_DIRS include</span><br><span class="line"># LIBRARIES demo02_talker_listener</span><br><span class="line"> CATKIN_DEPENDS roscpp rospy std_msgs message_runtime</span><br><span class="line"># DEPENDS system_lib</span><br><span class="line">)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>C++ 需要调用的中间文件(…/工作空间/devel/include/包名/xxx.h)</p>
<p>Python 需要调用的中间文件(…/工作空间/devel/lib/python3/dist-packages/包名/msg)</p>
<p>后续调用相关 msg 时,是从这些中间文件调用的</p>
<h1 id="cpp实现"><a href="#cpp实现" class="headerlink" title="cpp实现"></a>cpp实现</h1><p>vscode配置</p>
<p>将前面生成的 head 文件路径配置进 c_cpp_properties.json 的 includepath属性:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">"includePath": [</span><br><span class="line"> "/opt/ros/noetic/include/**",</span><br><span class="line"> "/usr/include/**",</span><br><span class="line"> "/xxx/yyy工作空间/devel/include/**" //配置 head 文件的路径 </span><br><span class="line"> ],</span><br></pre></td></tr></table></figure>
<h2 id="发布方"><a href="#发布方" class="headerlink" title="发布方"></a>发布方</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">/*</span><br><span class="line"> 需求: 循环发布人的信息</span><br><span class="line"></span><br><span class="line">*/</span><br><span class="line"></span><br><span class="line">#include "ros/ros.h"</span><br><span class="line">#include "demo02_talker_listener/Person.h"</span><br><span class="line"></span><br><span class="line">int main(int argc, char *argv[])</span><br><span class="line">{</span><br><span class="line"> setlocale(LC_ALL,"");</span><br><span class="line"></span><br><span class="line"> //1.初始化 ROS 节点</span><br><span class="line"> ros::init(argc,argv,"talker_person");</span><br><span class="line"></span><br><span class="line"> //2.创建 ROS 句柄</span><br><span class="line"> ros::NodeHandle nh;</span><br><span class="line"></span><br><span class="line"> //3.创建发布者对象</span><br><span class="line"> ros::Publisher pub = nh.advertise<demo02_talker_listener::Person>("chatter_person",1000);</span><br><span class="line"></span><br><span class="line"> //4.组织被发布的消息,编写发布逻辑并发布消息</span><br><span class="line"> demo02_talker_listener::Person p;</span><br><span class="line"> p.name = "sunwukong";</span><br><span class="line"> p.age = 2000;</span><br><span class="line"> p.height = 1.45;</span><br><span class="line"></span><br><span class="line"> ros::Rate r(1);</span><br><span class="line"> while (ros::ok())</span><br><span class="line"> {</span><br><span class="line"> pub.publish(p);</span><br><span class="line"> p.age += 1;</span><br><span class="line"> ROS_INFO("我叫:%s,今年%d岁,高%.2f米", p.name.c_str(), p.age, p.height);</span><br><span class="line"></span><br><span class="line"> r.sleep();</span><br><span class="line"> ros::spinOnce();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> return 0;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="订阅方"><a href="#订阅方" class="headerlink" title="订阅方"></a>订阅方</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">/*</span><br><span class="line"> 需求: 订阅人的信息</span><br><span class="line"></span><br><span class="line">*/</span><br><span class="line"></span><br><span class="line">#include "ros/ros.h"</span><br><span class="line">#include "demo02_talker_listener/Person.h"</span><br><span class="line"></span><br><span class="line">void doPerson(const demo02_talker_listener::Person::ConstPtr& person_p){</span><br><span class="line"> ROS_INFO("订阅的人信息:%s, %d, %.2f", person_p->name.c_str(), person_p->age, person_p->height);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">int main(int argc, char *argv[])</span><br><span class="line">{ </span><br><span class="line"> setlocale(LC_ALL,"");</span><br><span class="line"></span><br><span class="line"> //1.初始化 ROS 节点</span><br><span class="line"> ros::init(argc,argv,"listener_person");</span><br><span class="line"> //2.创建 ROS 句柄</span><br><span class="line"> ros::NodeHandle nh;</span><br><span class="line"> //3.创建订阅对象</span><br><span class="line"> ros::Subscriber sub = nh.subscribe<demo02_talker_listener::Person>("chatter_person",10,doPerson);</span><br><span class="line"></span><br><span class="line"> //4.回调函数中处理 person</span><br><span class="line"></span><br><span class="line"> //5.ros::spin();</span><br><span class="line"> ros::spin(); </span><br><span class="line"> return 0;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="配置-CMakeLists-txt"><a href="#配置-CMakeLists-txt" class="headerlink" title="配置 CMakeLists.txt"></a>配置 CMakeLists.txt</h2><p>需要添加 add_dependencies 用以设置所依赖的消息相关的中间文件。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">add_executable(person_talker src/person_talker.cpp)</span><br><span class="line">add_executable(person_listener src/person_listener.cpp)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">add_dependencies(person_talker ${PROJECT_NAME}_generate_messages_cpp)</span><br><span class="line">add_dependencies(person_listener ${PROJECT_NAME}_generate_messages_cpp)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">target_link_libraries(person_talker</span><br><span class="line"> ${catkin_LIBRARIES}</span><br><span class="line">)</span><br><span class="line">target_link_libraries(person_listener</span><br><span class="line"> ${catkin_LIBRARIES}</span><br><span class="line">)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h1 id="python实现"><a href="#python实现" class="headerlink" title="python实现"></a>python实现</h1><h2 id="vscode配置"><a href="#vscode配置" class="headerlink" title="vscode配置"></a>vscode配置</h2><p>为了方便代码提示以及误抛异常,需要先配置 vscode,将前面生成的 python 文件路径配置进 settings.json</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">{</span><br><span class="line"> "python.autoComplete.extraPaths": [</span><br><span class="line"> "/opt/ros/noetic/lib/python3/dist-packages",</span><br><span class="line"> "/xxx/yyy工作空间/devel/lib/python3/dist-packages"</span><br><span class="line"> ]</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="发布方-1"><a href="#发布方-1" class="headerlink" title="发布方"></a>发布方</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">#! /usr/bin/env python</span><br><span class="line">"""</span><br><span class="line"> 发布方:</span><br><span class="line"> 循环发送消息</span><br><span class="line"></span><br><span class="line">"""</span><br><span class="line">import rospy</span><br><span class="line">from demo02_talker_listener.msg import Person</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">if __name__ == "__main__":</span><br><span class="line"> #1.初始化 ROS 节点</span><br><span class="line"> rospy.init_node("talker_person_p")</span><br><span class="line"> #2.创建发布者对象</span><br><span class="line"> pub = rospy.Publisher("chatter_person",Person,queue_size=10)</span><br><span class="line"> #3.组织消息</span><br><span class="line"> p = Person()</span><br><span class="line"> p.name = "葫芦瓦"</span><br><span class="line"> p.age = 18</span><br><span class="line"> p.height = 0.75</span><br><span class="line"></span><br><span class="line"> #4.编写消息发布逻辑</span><br><span class="line"> rate = rospy.Rate(1)</span><br><span class="line"> while not rospy.is_shutdown():</span><br><span class="line"> pub.publish(p) #发布消息</span><br><span class="line"> rate.sleep() #休眠</span><br><span class="line"> rospy.loginfo("姓名:%s, 年龄:%d, 身高:%.2f",p.name, p.age, p.height)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="订阅方-1"><a href="#订阅方-1" class="headerlink" title="订阅方"></a>订阅方</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"> #! /usr/bin/env python</span><br><span class="line">"""</span><br><span class="line"> 订阅方:</span><br><span class="line"> 订阅消息</span><br><span class="line"></span><br><span class="line">"""</span><br><span class="line">import rospy</span><br><span class="line">from demo02_talker_listener.msg import Person</span><br><span class="line"></span><br><span class="line">def doPerson(p):</span><br><span class="line"> rospy.loginfo("接收到的人的信息:%s, %d, %.2f",p.name, p.age, p.height)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">if __name__ == "__main__":</span><br><span class="line"> #1.初始化节点</span><br><span class="line"> rospy.init_node("listener_person_p")</span><br><span class="line"> #2.创建订阅者对象</span><br><span class="line"> sub = rospy.Subscriber("chatter_person",Person,doPerson,queue_size=10)</span><br><span class="line"> rospy.spin() #4.循环</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>终端下进入 scripts 执行:chmod +x *.py</p>
<h2 id="配置-CMakeLists-txt-1"><a href="#配置-CMakeLists-txt-1" class="headerlink" title="配置 CMakeLists.txt"></a>配置 CMakeLists.txt</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">catkin_install_python(PROGRAMS</span><br><span class="line"> scripts/talker_p.py</span><br><span class="line"> scripts/listener_p.py</span><br><span class="line"> scripts/person_talker.py</span><br><span class="line"> scripts/person_listener.py</span><br><span class="line"> DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}</span><br><span class="line">)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="https://luozhexin.github.io/2021/03/08/rostopic/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/profile.jpg">
<meta itemprop="name" content="luozhexin">
<meta itemprop="description" content="怕什么真理无穷,进一寸有一寸的欢喜">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="onewall的博客">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/03/08/rostopic/" class="post-title-link" itemprop="url">rostopic</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2021-03-08 21:51:42" itemprop="dateCreated datePublished" datetime="2021-03-08T21:51:42+08:00">2021-03-08</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2021-03-10 12:35:28" itemprop="dateModified" datetime="2021-03-10T12:35:28+08:00">2021-03-10</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/ROS/" itemprop="url" rel="index"><span itemprop="name">ROS</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="话题通信"><a href="#话题通信" class="headerlink" title="话题通信"></a>话题通信</h1><h2 id="发布方cpp实现"><a href="#发布方cpp实现" class="headerlink" title="发布方cpp实现"></a>发布方cpp实现</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">1、包含头文件</span><br><span class="line"> ROS中文本类型 ---》 std_msgs/String.h</span><br><span class="line">2、初始化ROS节点</span><br><span class="line">3、创建节点句柄</span><br><span class="line">4、创建发布者对象</span><br><span class="line">5、编写发布逻辑并发布数据</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">#include "ros/ros.h"</span><br><span class="line">#include "std_msgs/String.h"</span><br><span class="line"></span><br><span class="line">int main(int argc , char *argv[])</span><br><span class="line">{</span><br><span class="line"> //初试化ros节点</span><br><span class="line"> ros::init(argc,argv,"nodeName"); //节点名称</span><br><span class="line"> //创建节点句柄</span><br><span class="line"> ros::NodeHandle nh;</span><br><span class="line"> //创建发布者对象</span><br><span class="line"> ros::Publisher pub=nh.advertise<std_msgs::String>("topicName",10); </span><br><span class="line">//范型:话题类型,话题名称,话题数据队列长度</span><br><span class="line"> //编写发布逻辑并发布数据</span><br><span class="line"> //先创建被发布的数据</span><br><span class="line"> std_msgs::String msg;</span><br><span class="line"> //循环发布数据</span><br><span class="line"> while(ros::ok()) //ros::ok() 节点不死</span><br><span class="line"> {</span><br><span class="line"> msg.data="hello";</span><br><span class="line"> pub.publish(msg);</span><br><span class="line"> }</span><br><span class="line"> return 0;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h3 id="CMakeList"><a href="#CMakeList" class="headerlink" title="CMakeList"></a>CMakeList</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">add_executable()</span><br><span class="line">target_link_libraries()</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rostopic echo topicName</span><br></pre></td></tr></table></figure>
<h3 id="以10HZ发布数据-数据后面有编号"><a href="#以10HZ发布数据-数据后面有编号" class="headerlink" title="以10HZ发布数据,数据后面有编号"></a>以10HZ发布数据,数据后面有编号</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">#include "ros/ros.h"</span><br><span class="line">#include "std_msgs/String.h"</span><br><span class="line">#include <sstream></span><br><span class="line">int main(int argc , char *argv[])</span><br><span class="line">{</span><br><span class="line"> setlocale(LC_ALL,""); //解决中文乱码</span><br><span class="line"> //2、初试化ros节点</span><br><span class="line"> ros::init(argc,argv,"nodeName"); //节点名称</span><br><span class="line"> //3、创建节点句柄</span><br><span class="line"> ros::NodeHandle nh;</span><br><span class="line"> //4、创建发布者对象</span><br><span class="line"> ros::Publisher pub=nh.advertise<std_msgs::String>("topicName",10); </span><br><span class="line">//范型:话题类型,话题名称,话题数据队列长度</span><br><span class="line"> //5、编写发布逻辑并发布数据</span><br><span class="line"> //要求以10HZ的频率发布数据,并且文本后添加编号</span><br><span class="line"> //先创建被发布的数据</span><br><span class="line"> std_msgs::String msg;</span><br><span class="line"> //发布频率</span><br><span class="line"> ros::Rate rate(10); //10HZ</span><br><span class="line"> //计数器</span><br><span class="line"> int count=0;</span><br><span class="line"> //循环发布数据</span><br><span class="line"> while(ros::ok()) //ros::ok() ros进程没死</span><br><span class="line"> {</span><br><span class="line"> count ++;</span><br><span class="line"> // 字符串拼接 sstream</span><br><span class="line"> std::stringstream ss;</span><br><span class="line"> ss << "hello --->" << count;</span><br><span class="line"> msg.data=ss.str();</span><br><span class="line"> //msg.data="hello";</span><br><span class="line"> pub.publish(msg);</span><br><span class="line"> //日志输出</span><br><span class="line"> ROS_INFO("发布的数据是%s",ss.str().c_str());</span><br><span class="line"> rate.sleep();</span><br><span class="line"> ros::spinOnce();//官方建议添加,处理回调函数</span><br><span class="line"> }</span><br><span class="line"> return 0;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="订阅方cpp实现"><a href="#订阅方cpp实现" class="headerlink" title="订阅方cpp实现"></a>订阅方cpp实现</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">1、包含头文件</span><br><span class="line"> ROS中文本类型 ---》 std_msgs/String.h</span><br><span class="line">2、初始化ROS节点</span><br><span class="line">3、创建节点句柄</span><br><span class="line">4、创建订阅者对象</span><br><span class="line">5、处理订阅到的数据</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">#include "ros/ros.h"</span><br><span class="line">#include "std_msgs/String.h"</span><br><span class="line">void doMsg(const std_msgs::String::ConstPtr &msg) //常指针类型</span><br><span class="line">{</span><br><span class="line">//通过msg获取并操作订阅到的数据</span><br><span class="line">ROS_INFO("订阅方订阅到的数据是:%s",msg->data.c_str());</span><br><span class="line">}</span><br><span class="line">int main(int argc , char *argv[])</span><br><span class="line">{</span><br><span class="line"> setlocale(LC_ALL,""); //解决中文乱码</span><br><span class="line"> //初试化ros节点</span><br><span class="line"> ros::init(argc,argv,"nodeName"); //节点名称</span><br><span class="line"> //创建节点句柄</span><br><span class="line"> ros::NodeHandle nh;</span><br><span class="line"> //创建订阅者对象</span><br><span class="line"> ros::Subscriber pub=nh.subscribe("topicName",10,doMsg); //doMsg是回调函数用于处理收到的数据 </span><br><span class="line"> ros::spin(); //回头,处理回调函数</span><br><span class="line"> return 0;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="发布方python实现"><a href="#发布方python实现" class="headerlink" title="发布方python实现"></a>发布方python实现</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">1、导包</span><br><span class="line">2、初始化ros节点</span><br><span class="line">3、创建发布者对象</span><br><span class="line">4、编写发布逻辑并发布数据</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">#! /usr/bin/env python</span><br><span class="line">import rospy</span><br><span class="line">from std_msgs.msg import String</span><br><span class="line"></span><br><span class="line">if __name__ == "__main__":</span><br><span class="line"> #2、初始化ros节点</span><br><span class="line"> rospy.init("nodeName")</span><br><span class="line"> #3、创建发布者对象</span><br><span class="line"> pub = rospy.Publisher("topicName",String,queue_size=10)</span><br><span class="line"> #4、编写发布逻辑并发布数据</span><br><span class="line"> #创建数据</span><br><span class="line"> msg=String()</span><br><span class="line"> #使用循环发布数据</span><br><span class="line"> while not rospy.is_shutdown():</span><br><span class="line"> msg.data ="hello"</span><br><span class="line"> pub.publish(msg)</span><br><span class="line"> pass</span><br></pre></td></tr></table></figure>
<h3 id="CMakeList-1"><a href="#CMakeList-1" class="headerlink" title="CMakeList"></a>CMakeList</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">catkin_install_python()</span><br></pre></td></tr></table></figure>
<h3 id="以10HZ发布数据-数据后面有编号-1"><a href="#以10HZ发布数据-数据后面有编号-1" class="headerlink" title="以10HZ发布数据,数据后面有编号"></a>以10HZ发布数据,数据后面有编号</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">#! /usr/bin/env python</span><br><span class="line">import rospy</span><br><span class="line">from std_msgs.msg import String</span><br><span class="line"></span><br><span class="line">if __name__ == "__main__":</span><br><span class="line"> #2、初始化ros节点</span><br><span class="line"> rospy.init("nodeName")</span><br><span class="line"> #3、创建发布者对象</span><br><span class="line"> pub = rospy.Publisher("topicName",String,queue_size=10)</span><br><span class="line"> #4、编写发布逻辑并发布数据</span><br><span class="line"> #创建数据</span><br><span class="line"> msg=String()</span><br><span class="line"> #指定发布频率</span><br><span class="line"> rate=rospy.Rate(1)</span><br><span class="line"> #设置计数器</span><br><span class="line"> count=0</span><br><span class="line"> #使用循环发布数据</span><br><span class="line"> while not rospy.is_shutdown():</span><br><span class="line"> count +=1</span><br><span class="line"> msg.data ="hello" +str(count)</span><br><span class="line"> pub.publish(msg)</span><br><span class="line"> rospy.loginfo("发布的数据是%s",msg.data)</span><br><span class="line"> rate.sleep()</span><br><span class="line"> pass</span><br></pre></td></tr></table></figure>
<h2 id="发布方python实现-1"><a href="#发布方python实现-1" class="headerlink" title="发布方python实现"></a>发布方python实现</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">1、导包</span><br><span class="line">2、初始化ros节点</span><br><span class="line">3、创建订阅者对象</span><br><span class="line">4、回调函数处理数据</span><br><span class="line">5、spin()</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"># usr/bin/env python</span><br><span class="line">#coding=utf-8</span><br><span class="line">import rospy</span><br><span class="line">from std_msgs.msg import String</span><br><span class="line"></span><br><span class="line">def doMsg(msg):</span><br><span class="line"> rospy.loginfo("data is %s",msg.data)</span><br><span class="line"></span><br><span class="line">if __name__ == "__main__":</span><br><span class="line"> rospy.init_node("nodeName")</span><br><span class="line"> sub=rospy.Subcriber("topicName",String,doMsg,queue_size=10)</span><br><span class="line"> rospy.spin()</span><br><span class="line"> pass</span><br></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="https://luozhexin.github.io/2021/03/04/python/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/profile.jpg">
<meta itemprop="name" content="luozhexin">
<meta itemprop="description" content="怕什么真理无穷,进一寸有一寸的欢喜">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="onewall的博客">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/03/04/python/" class="post-title-link" itemprop="url">python</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2021-03-04 22:08:32" itemprop="dateCreated datePublished" datetime="2021-03-04T22:08:32+08:00">2021-03-04</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="https://luozhexin.github.io/2021/03/02/linux/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/profile.jpg">
<meta itemprop="name" content="luozhexin">
<meta itemprop="description" content="怕什么真理无穷,进一寸有一寸的欢喜">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="onewall的博客">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/03/02/linux/" class="post-title-link" itemprop="url">linux基础</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2021-03-02 18:54:19" itemprop="dateCreated datePublished" datetime="2021-03-02T18:54:19+08:00">2021-03-02</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2021-03-04 15:51:14" itemprop="dateModified" datetime="2021-03-04T15:51:14+08:00">2021-03-04</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/linux/" itemprop="url" rel="index"><span itemprop="name">linux</span></a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="linux文件系统"><a href="#linux文件系统" class="headerlink" title="linux文件系统"></a>linux文件系统</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line">/ 根目录。一般根目录下只存放目录,在linux下有且只有一个根目录,所有的东西都是从这里开始。当在终端输入/home,其实是先从</span><br><span class="line">/(根目录)开始,再进入到home目录。</span><br><span class="line"></span><br><span class="line">/bin,/usr/bin 可执行二进制文件的目录,如常用的命令ls、tar、mv、cat等。</span><br><span class="line"></span><br><span class="line">/boot 存放linux系统启动时用到的一些文件,如linux的内核文件:/boot/vmliunz,系统引导管理器:/boot/grub</span><br><span class="line"></span><br><span class="line">/dev 存放linux系统下的设备文件,访问该目录下的某个文件,相当于访问某个设备,常用的是挂载光驱mount /dev/cdrom/mnt</span><br><span class="line"></span><br><span class="line">/etc 系统配置文件存放的目录,不建议在此目录下存放可执行文件,重要的配置文件有,/etc/inittab,/etc/fstab,/etc/init.d,/etc/X11,</span><br><span class="line">/etc/sysconfig,/etc/xinetd.d</span><br><span class="line"></span><br><span class="line">/home 系统默认的用户家目录,新增用户账号时,用户的家目录都存放在此目录下。</span><br><span class="line"></span><br><span class="line">~ 表示当前用户的家目录,~edu 表示用户edu的家目录。</span><br><span class="line"></span><br><span class="line">/lib,/usr/lib,/usr/local/lib 系统使用的库函数的目录,程序在执行过程中,需要调用一些额外的参数时需要函数库的协助。</span><br><span class="line"></span><br><span class="line">/lost+found 系统异常产生错误时,会将一些遗失的片段放置于此目录下</span><br><span class="line"></span><br><span class="line">/mnt,/media 光盘默认挂载点,通常光盘挂载于/mnt/cdrom 下,也不一定,可以选择任意位置进行挂载。</span><br><span class="line"></span><br><span class="line">/opt 给主机额外安装软件所摆放的目录</span><br><span class="line"></span><br><span class="line">/proc 此目录的数据都在内存中,如系统核心,外部设备,网络状态,由于数据都存放于内存中,所以不占用磁盘空间,</span><br><span class="line">比较重要的文件有:/proc/cpuinfo,/porc/interrupts,/proc/dma,/proc/ioports,/proc/net/*等</span><br><span class="line"></span><br><span class="line">/root 系统管理员root的家目录</span><br><span class="line"></span><br><span class="line">/sbin,/usr/sbin,/usr/local/sbin 防止管理员使用的可执行命令,如fdisk,shutdown,mount等。</span><br><span class="line">于/bin不同的是,这几个目录是给系统管理员root使用的命令,一般用户只能查看而不能设置和使用</span><br><span class="line"></span><br><span class="line">/tmp 一般用户或正在执行的程序临时存放文件的目录,任何人都可以访问,重要数据不可放置在此目录下</span><br><span class="line"></span><br><span class="line">/srv 服务启动之后需要访问的数据目录,如www服务器需要访问的网页数据存放在/srv/www 内</span><br><span class="line"></span><br><span class="line">/usr 应用程序存放目录</span><br><span class="line"></span><br><span class="line">/usr/bin 存放应用程序</span><br><span class="line"></span><br><span class="line">/usr/share 存放共享数据</span><br><span class="line"></span><br><span class="line">/usr/lib 存放不能直接运行,却是许多程序运行所必需的一些库函数文件</span><br><span class="line"></span><br><span class="line">/usr/local 存放软件升级包</span><br><span class="line"></span><br><span class="line">/usr/share/doc 系统说明文件存放目录</span><br><span class="line"></span><br><span class="line">/usr/sharc/man 程序说明文件存放目录</span><br><span class="line"></span><br><span class="line">/var 放置系统执行过程中经常变化的文件</span><br><span class="line"></span><br><span class="line">/var/log 随时更改的日志文件</span><br><span class="line"></span><br><span class="line">/var/spool/mail 邮件存放的目录</span><br><span class="line"></span><br><span class="line">/var/run 程序或服务启动后,其pid存放在该目录下。</span><br></pre></td></tr></table></figure>
<h1 id="文件及文件夹查看"><a href="#文件及文件夹查看" class="headerlink" title="文件及文件夹查看"></a>文件及文件夹查看</h1><p>ls -a 查看所有文件及文件夹,包括隐藏文件和目录</p>
<p>ls -l 以列表形式查看所有文件和目录</p>
<p>ll 等同于 ls -al 等同于以上两个命令的结合</p>
<h1 id="切换目录"><a href="#切换目录" class="headerlink" title="切换目录"></a>切换目录</h1><p>cd 切换到当前用户的主目录(home/用户目录)</p>
<p>cd ~ 切换到当前用户的主目录(home/用户目录)</p>
<p>cd . 切换到当前目录</p>
<p>cd .. 切换到上级目录</p>
<p>cd - 可进入上次所在的目录</p>
<p>cd / 切换到系统根目录/</p>
<p>如果路径是从根路径开始的,则路径的前面需要加上 “ / ”,如 “ /mnt ”,通常进入某个目录里的文件夹,前面不用加 “ / ”。</p>
<h1 id="文件夹操作"><a href="#文件夹操作" class="headerlink" title="文件夹操作"></a>文件夹操作</h1><p>rm -f 才能删除文件夹,而且使用命令行是永久性删除,而使用GUI是移到回收站可恢复的。</p>
<h1 id="文件复制"><a href="#文件复制" class="headerlink" title="文件复制"></a>文件复制</h1><p>cp test.txt . ‘.’ 表示当前路径</p>
<p>cp test.txt hello.txt 将test复制并命名为hello</p>
<p>cp -r 复制文件夹 -r表示文件夹</p>
<p>cp test/<em>.md Downloads/ ‘</em>‘表示模糊匹配</p>
<h1 id="文件剪切"><a href="#文件剪切" class="headerlink" title="文件剪切"></a>文件剪切</h1><p>mv test.txt Downloads/ </p>
<p>mv test.txt a.txt 重命名</p>
<p>mv test/* Downloads </p>
<h1 id="输出、重定向"><a href="#输出、重定向" class="headerlink" title="输出、重定向"></a>输出、重定向</h1><p>‘echo’ 控制台输出</p>
<p>echo “hello world”</p>
<p>‘>’重定向符号</p>
<p>Linux允许将命令执行结果重定向到一个文件,本应显示在终端上的内容保存到指定文件中。</p>
<p>echo “hello world” > test.txt</p>
<p>ls > test.txt</p>
<p>test.txt 如果不存在,则创建,存在则覆盖其内容</p>
<p>注意:>输出重定向会覆盖原来的内容,>>输出重定向则会追加到文件的尾部</p>
<p>echo “hello” >> test.txt</p>
<h1 id="cat-查看-amp-合并文件内容"><a href="#cat-查看-amp-合并文件内容" class="headerlink" title="cat 查看&合并文件内容"></a>cat 查看&合并文件内容</h1><p>cat xxx.py 可以用来快速查看某个文件内容,输出到控制台</p>
<p>cat test.txt >> haha.txt 将test.txt的内容追加到haha.txt中</p>
<p>cat 1.txt 2.txt > 1_2.txt将1.txt 2.txt内容合并到1_2.txt中</p>
<h1 id="tar-归档管理"><a href="#tar-归档管理" class="headerlink" title="tar 归档管理"></a>tar 归档管理</h1><p>tar [参数] 打包文件名 文件</p>
<p>打包并压缩</p>
<p>打包指定目录或文件<code>tar -czvf xxx.tar.gz my-file my-dir</code></p>
<p>解压缩包</p>
<p>解包到指定目录:<code>tar -xzvf xxx.tar.gz -C my-dir</code> (需要先创建my-dir目录)</p>
<h1 id="zip-文件压缩"><a href="#zip-文件压缩" class="headerlink" title="zip 文件压缩"></a>zip 文件压缩</h1><p>压缩文件:zip [-r] 目标文件(没有扩展名) 源文件</p>
<p>文件:zip bak * 当前目录所有文件,也可以指定文件</p>
<p>文件夹:zip -r bak * 当前目录所有文件&目录递归 -r表示递归 不然仅仅压缩了一个空的文件夹</p>
<p>解压文件:unzip -d 解压后目录文件 压缩文件</p>
<p>解压到指定目录:unzip -d ./target_dir bak.zip</p>
<p>解压到当前目录:unzip bak.zip</p>
<h1 id="wget-文件下载"><a href="#wget-文件下载" class="headerlink" title="wget 文件下载"></a>wget 文件下载</h1><p>下载普通文件</p>
<p><code>wget http://p1.qhimgs4.com/t01ce0387e64e3428ca.jpg</code></p>
<p>以指定文件名保存文件</p>
<p><code>wget -O girl.jpg http://p1.qhimgs4.com/t01ce0387e64e3428ca.jpg</code></p>
<p>断点续传</p>
<p><code>wget -c http://p1.qhimgs4.com/t01ce0387e64e3428ca.jpg</code></p>
<h1 id="tree-目录树状结构"><a href="#tree-目录树状结构" class="headerlink" title="tree 目录树状结构"></a>tree 目录树状结构</h1><p>使用<code>tree</code>命令可以查看指定目录的树状结构</p>
<h1 id="which-查看命令位置"><a href="#which-查看命令位置" class="headerlink" title="which 查看命令位置"></a>which 查看命令位置</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">poplar@PoplarTang:~/Lesson$ which python</span><br><span class="line">/usr/bin/python</span><br><span class="line">poplar@PoplarTang:~/Lesson$ which ls</span><br><span class="line">/bin/ls</span><br></pre></td></tr></table></figure>
<h1 id="ifconfig-查看ip地址"><a href="#ifconfig-查看ip地址" class="headerlink" title="ifconfig 查看ip地址"></a>ifconfig 查看ip地址</h1><h1 id="ping-测试网络是否联通"><a href="#ping-测试网络是否联通" class="headerlink" title="ping 测试网络是否联通"></a>ping 测试网络是否联通</h1><h1 id="ssh-连接远程电脑"><a href="#ssh-连接远程电脑" class="headerlink" title="ssh 连接远程电脑"></a>ssh 连接远程电脑</h1><p>如果远程电脑是新装电脑,有可能会因为ssh服务证书问题而无法连接,可以通过在远程电脑上重装ssh服务解决。</p>
<ol>
<li><p>重装ssh服务,生成证书</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-get remove openssh-server</span><br><span class="line">sudo apt-get install openssh-server</span><br></pre></td></tr></table></figure></li>
<li><p>重启ssh服务<br><code>sudo service ssh restart</code></p>
</li>
<li><p>检查sshd服务是否存活<br><code>sudo service ssh status</code></p>
</li>
<li><p>推出ssh<br><code>exit</code></p>
</li>
</ol>
<h1 id="chmod-修改文件权限"><a href="#chmod-修改文件权限" class="headerlink" title="chmod 修改文件权限"></a>chmod 修改文件权限</h1><p>使用<code>ls -l </code>可以查看文件和目录的权限</p>
<p>第一个 ‘-‘表示文件,’d’表示文件夹</p>
<p>字母法权限修改:rwx</p>
<p>chmod u/g/o/a +/-/= rwx 文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">u user 表示该文件的所有者</span><br><span class="line">g group 表示与该文件的所有者属于同一组( group )者,即用户组</span><br><span class="line">o other 表示其他以外的人</span><br><span class="line">a all 表示这三者皆是</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">+ 增加权限</span><br><span class="line">- 撤销权限</span><br><span class="line">= 设定权限</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">r read 表示可读取,对于一个目录,如果没有r权限,那么就意味着不能通过ls查看这个目录的内容。</span><br><span class="line">w write 表示可写入,对于一个目录,如果没有w权限,那么就意味着不能在目录下创建新的文件。</span><br><span class="line">x excute 表示可执行,对于一个目录,如果没有x权限,那么就意味着不能通过cd进入这个目录。</span><br></pre></td></tr></table></figure>
<p>数字法权限修改:421</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">r 读取权限,数字代号为 "4"</span><br><span class="line">w 写入权限,数字代号为 "2"</span><br><span class="line">x 执行权限,数字代号为 "1"</span><br><span class="line">- 不具任何权限,数字代号为 "0"</span><br></pre></td></tr></table></figure>
<p>如执行:chmod u=rwx,g=rx,o=r filename 就等同于:chmod u=7,g=5,o=4 filename</p>
<h1 id="系统信息命令"><a href="#系统信息命令" class="headerlink" title="系统信息命令"></a>系统信息命令</h1><p>查看系统版本</p>
<p>发行版本号</p>
<p><code>lsb_release -a</code></p>
<p>内核版本及系统位数</p>
<p><code>uname -a</code></p>
<p>内核版本及gcc版本</p>
<p><code>cat /proc/version</code></p>
<p>查看硬件信息</p>
<p>cpu信息</p>
<p><code>cat /proc/cpuinfo or lscpu</code></p>
<p>内存信息</p>
<p><code>sudo dmidecode -t memory</code></p>
<p>运行时信息</p>
<p><code>top</code>实时CPU&内存使用情况(可以查看pid号,user,和哪个命令占用)</p>
<p><code>free</code>当前内存占用情况</p>
<p><code>ps -aux</code>查看当前进程状态(CPU、内存占用、开启时间)</p>
<p><code>kill</code>根据进程pid杀死指定进程,可以配合参数-9强制杀死</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
<link itemprop="mainEntityOfPage" href="https://luozhexin.github.io/2021/02/19/ubuntuclash/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="image" content="/images/profile.jpg">
<meta itemprop="name" content="luozhexin">
<meta itemprop="description" content="怕什么真理无穷,进一寸有一寸的欢喜">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="onewall的博客">
</span>
<header class="post-header">
<h2 class="post-title" itemprop="name headline">
<a href="/2021/02/19/ubuntuclash/" class="post-title-link" itemprop="url">clash for linux 安装使用</a>
</h2>
<div class="post-meta">
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2021-02-19 00:17:52" itemprop="dateCreated datePublished" datetime="2021-02-19T00:17:52+08:00">2021-02-19</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-calendar-check"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2021-03-10 12:36:27" itemprop="dateModified" datetime="2021-03-10T12:36:27+08:00">2021-03-10</time>
</span>
<span class="post-meta-item">
<span class="post-meta-item-icon">
<i class="far fa-folder"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/linux/" itemprop="url" rel="index"><span itemprop="name">linux</span></a>
</span>
</span>
</div>
</header>