你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32送药小车:Openmv实现数字识别,巡线以及串口通信

[复制链接]
STMCU小助手 发布时间:2022-5-10 22:55
前言
' ^5 h9 @' B! t* Z/ _我们使用Openmv中的特征点检测进行数字识别,并通过设置红色阈值实现巡线,最后通过串口通信将数字及红线坐标发送给主控芯片。& P' b+ O+ v- g; l

& g; t" ^& S& p* ?# x5 t一、Openmv实现数字识别- j; Y) w+ W$ [2 N/ Y
对于数字识别,一开始我们使用的是模板匹配,后来发现实现过程较为繁琐,且准确率不如特征点检测,故最终采用特征点检测的方法识别6 l& O# l, r  N5 M4 G

* X  H8 S* Z8 K& J5 n% U$ ^9 q1.这里简单讲一下我们使用模板匹配的思路:  `- n2 a1 V0 f0 N  t
        我们首先采用方框检测,将数字卡片的外边框找到,再对外边框内的图像进行模板匹配,这样做的准确度确实是要比采用整张图像要好得多,但这种方法也有一个非常大的弊端,由于一些外界的干扰,方框大小很容易小于我们采集的模板大小,导致程序不能正常运行,虽然我们适当增加了roi的宽度和长度,但仍会发生这种错误。后来我们经过测试发现,特征点匹配可以达到我们想要的要求。
0 K1 Z. G. D8 |# m8 ^9 `8 L; h2 O- p9 C8 f0 X, f2 D
附上部分代码:
4 J. k$ ^4 o8 q0 S% w, l2 s( J* f* ?$ k. p& _( ~9 N! }. E
  1.     for r in img.find_rects(threshold = 10000):
    7 D" B8 u7 P( o0 m- E0 |
  2.         img.draw_rectangle(r.rect(), color = (255, 0, 0))
    0 Y0 d2 r1 s# i. i' h' ^
  3.        # in r.corners(): #img.draw_circle(p[0], p[1], 5, color = (0, 255, 0))4 f# y! a# U" h  z9 g  s
  4.         if r:
    4 }6 |4 j; @2 b; }- ?1 i
  5.             if r[2]<69 or r[3]<75 or r[2]>300 or r[3]>180:#如果小于模板图像的大小,那么ROI设置为整幅图像
    4 c# @( J6 S/ k  X5 O
  6.                  template[0]=0& t4 d, `9 G" J4 }
  7.                  template[1]=0( e* j# P5 h. G: y
  8.                  template[2]=320#宽度要调5 D& e  ?3 v4 [+ I
  9.                  template[3]=240#高度要调
    ) X, Q' L1 v/ r) \/ Y
  10.             else:# I6 B# U0 Z+ j5 E
  11.                 template[0]=r[0]
    4 z( o$ P' d/ ]3 Q9 H! S
  12.                 template[1]=r[1]+ H6 X1 k9 [5 y+ F
  13.                 template[2]=r[2]+5#宽度要调2 f0 z& [1 j% P/ A% i2 ~
  14.                 template[3]=r[3]+5#高度要调
    1 F5 T. B9 z8 F, i. P
  15.             print(template)# ~0 j0 u  z) S% M  f7 a! m
  16.         #近端
    " @; c& r8 @6 T  ^% ]/ L1 ?1 Z
  17.             for n in templates1:
    # |+ f! F: {, g2 I
  18.                 template1 = image.Image(n)1 _  d3 l2 j+ U/ L. t/ w9 t
  19.                 #对每个模板遍历进行模板匹配
    % M* ?) X% F) G8 v3 Z1 d2 i
  20.                 r1 = img.find_template(template1, 0.70, step=4, search=SEARCH_EX,roi=(template[0], template[1], template[2], template[3])) #, roi=(10, 0, 60, 60))
    , F2 U& J# X' V3 I* D+ a9 I
  21.         #find_template(template, threshold, [roi, step, search]),threshold中3 A* x7 c6 [. w$ ]# a9 a: K8 |1 G
  22.         #的0.7是相似度阈值,roi是进行匹配的区域(左上顶点为(10,0),长80宽60的矩形),% y- W$ }- @; }+ C: h- l
  23.         #注意roi的大小要比模板图片大,比frambuffer小。
    7 A; T. p" Q" F
  24.         #把匹配到的图像标记出来- X7 P' g( c3 Z) G( j
  25.                 if r1:' d( A+ E* ^- G0 q$ Y1 z
  26.                     #img.draw_rectangle(r1)
    ' I; n  L6 q/ o8 l+ ~& D' n0 X
  27.                     d="1"6 L, W# W2 O6 v3 }7 K' J5 y
  28.                     x=r1[0]
    ; F% G% E3 i; U- G; O7 A
  29.                     y=r1[1]& g4 [# E& u; s; U, y- O6 C
  30. % e/ H# V# _* f8 Q' H& R
  31.                 else:
    ; F- s+ f* P& W: w0 [5 M7 o
  32.                     d="NO NUM"
    : W9 C- k9 L% U3 o, a6 p- L
  33. * y% _! _' ~2 e
  34.                 img.draw_string(x,y, d, scale = 2, mono_space = False,
    9 l  ^( E; `) E; U4 o  W" b! Z0 r
  35.                                 char_rotation = 0, char_hmirror = False, char_vflip = False,
    & F! M3 b& v  T
  36.                                 string_rotat
复制代码

8 Y& N3 K6 k5 Q& h( }" N1 v. O5 S2.我们利用特征点检测的思路:
& O$ d; ~/ q3 _  j* u( m        对于特征点检测,我们通过检测图像特征点,并与我们保存在SD卡的模板进行比对,找到满足特征点数最多的那个模板,即为我们要找的数字,然后我们通过比对一定次数,选出比对成功次数最多的那个数字,作为我们最终的识别结果。在小车对于任务的实现中,我们给出以下思路:
- x/ z9 y2 R, C- Q9 Y# A( g$ u9 J5 n1 @! x: o  g" ^$ [9 B
        因为近端病房只采用数字1和2,所以我们便想到了一个策略:当识别到为近端病房时,只对数字1和2的特征点进行比对,当远端病房时,对其余数字进行特征点比对,这样很大程度上增加了我们识别的准确性。) V+ n# W; |2 b, y
: \0 E+ R$ g% R$ J' ?
部分代码如下:
( C' r' x, \3 ~4 P4 t2 [- c/ q5 M" E7 N$ Y: ?5 b7 g) e0 \
  1. if describe_Times<15:5 v/ [) M! d/ `' X4 r
  2.     kpts_run = img.find_keypoints(max_keypoints=220, threshold=1, normalized=False)* u+ Y! ?/ _- l+ s
  3.     match1 = image.match_descriptor(kpts1, kpts_run, threshold=85)
    + v+ N( r) \3 j5 d+ D
  4.     match2 = image.match_descriptor(kpts2, kpts_run, threshold=85)% @* v3 f! \: S! U( c# n* m5 h
  5. 8 X" P- |. A8 e) a  g9 y6 H
  6.     match3 = image.match_descriptor(kpts3, kpts_run, threshold=85)$ x6 g8 m- K  w
  7.     match4 = image.match_descriptor(kpts4, kpts_run, threshold=85)
    , T1 X3 P5 t0 b5 z/ g( X2 A

  8. * X. p: [; g- c0 p! N6 P
  9.     match5 = image.match_descriptor(kpts5, kpts_run, threshold=85)
    , b: z! v% p0 q3 U0 Y
  10.     match6 = image.match_descriptor(kpts6, kpts_run, threshold=85)
    ' g8 n, ^1 L. x, s$ a8 s

  11. ! }1 x" x/ T1 x
  12.     match7 = image.match_descriptor(kpts7, kpts_run, threshold=85)
    4 f5 {% i5 H& t. Z1 W
  13.     match8 = image.match_descriptor(kpts8, kpts_run, threshold=85)
    % N) p. e  K; V# |
  14. ! e7 S' G) I2 \4 S
  15.     match1_count = match1.count()7 M4 [* \; P( t9 k2 _# X1 O
  16.     match2_count = match2.count()' `& w% X/ `/ G2 E* X( F
  17. 1 L: q, ^- [! [. u0 f
  18.     match3_count = match3.count()- m! a5 B/ t  b* C: V8 B
  19.     match4_count = match4.count()/ h, {# i8 X0 D; Z
  20. 6 ^& \4 I2 x& ^% e- S3 Q) b
  21.     match5_count = match5.count()7 e/ Q$ ?4 l1 d3 z% K4 y7 C
  22.     match6_count = match6.count()% B- F2 ~0 n7 w

  23. + p% g- q# Z" m% o, m7 |5 K  n
  24.     match7_count = match7.count()9 [: E2 N" a  f, X# u. G
  25.     match8_count = match8.count()- }6 x2 s/ q& g( k  B6 z% q

  26. # [/ `: }" p. F0 L
  27.     match_count = max(match3.count(),match4.count(),match5.count(),match6.count(),match7.count(),match8.count())#,match3_2.count(),match4_2.count(),match5_2.count(),match6_2.count(),match7_2.count(),match8_2.count())2 p. w6 s) m& h4 a4 k. `+ q

  28. 2 P& i) c8 i1 F3 Z& X+ K3 J$ Q
  29.     if (match1_count == match_count):7 b% \) m9 N: j6 u7 d" g, j
  30.         match1_times=match1_times+17 @( P) }; v+ j1 U0 @1 g# t# A
  31.         x[0] = match1.cx()
    * h, U. P' ?: c+ K" z
  32.         #sending_data(1,x)& n, u$ W5 L: J! \* K% g7 Y
  33.         #print(x1, end = ',')
    & A8 a# V  |  B
  34.         print(1,"matched:%d "%(match1.count()))4 I" h! W, j' ?1 o! c2 r$ n

  35. , u: l% T4 Y# R: ?
  36.     if (match2_count == match_count):) [$ K( v  {. O$ {* n
  37.         match2_times=match2_times+1
    7 v5 s* p0 s: Z: U* _
  38.         x[1] = match2.cx()
    9 n2 ^: @0 @( ^3 L- @
  39.         #sending_data(2,x)
    ! V) p' Y* a- H! ?  U3 r" u$ M
  40.         #print(x2, end = ',')  `; [- o2 D% p
  41.         print(2,"matched:%d "%(match2.count()))2 X0 S- ~4 y. R

  42. ! e" H7 S, N, H0 H/ W
  43.     if (match3_count == match_count):' r# ]3 _* T- e
  44.         match3_times=match3_times+11 m; N! U8 A+ D. ^' c5 M6 F
  45.         t_list[2]=match3_times
    " l# H4 J! a. X7 P; ]
  46.         x[2] = match3.cx()
    % l- s8 ?7 M* F4 ^$ |' K1 `+ T
  47.         #x3 = match3.cx()
    5 r; W0 u  L- d9 @  ]0 b' _, b* m
  48.         #sending_data(3,x)
    ) ^+ l! S( M) E: o. t  `2 w
  49.         #print(x3, end = ',')1 O& e& n+ Y, {, [' N3 ~3 D
  50.         print(3,"matched:%d "%(match2.count()))+ V9 |' {7 E" Z( o, N+ v+ t4 M& n
  51. $ A' ~# i+ a. x  L2 H
  52.     if (match4_count == match_count):
    3 ?. C/ t1 z8 w# ~/ j0 v
  53.         match4_times=match4_times+1, x4 v0 y6 r. X- K- ^/ p
  54.         t_list[3]=match4_times+ V; ]4 |/ q: u# f) v# E
  55.         x[3] = match4.cx()- X3 g6 v, f: S! A, r
  56.         #x4 = match4.cx()$ }; J: q6 v' K' |$ C
  57.         #sending_data(4,x)9 u4 X3 K& ]9 r1 S* o
  58.         #print(x4, end = ',')
    " G& ]' Y4 k7 `% }
  59.         print(4,"matched:%d "%(match4.count()))
    : I: m' X7 h6 L

  60. 2 m3 t3 x' ?. L/ J
  61.     if (match5_count == match_count):
    7 k+ B0 ^6 u9 c+ p3 _
  62.         match5_times=match5_times+15 l- c8 ~8 H7 p5 |& w+ E) S3 @
  63.         t_list[4]=match5_times
    7 O2 W8 U. \+ h; w5 |3 W( c) C3 ^
  64.         x[4] = match5.cx()
    + Z6 i+ a# X9 s- _6 r% z6 t7 X7 W
  65.         #x5 = match5.cx()2 \& f# L, F; O
  66.         #sending_data(5,x)
    4 [! g( M0 t5 v, V8 Y4 b/ L
  67.         #print(x5, end = ',')% _" ~- `: Q/ Y0 ]! E$ X1 K
  68.         print(5,"matched:%d "%(match5.count()))" i' o" M, G# T9 p) W! R4 v! b7 [

  69. 1 i4 i2 u& U; i
  70.     if (match6_count == match_count):6 ~/ h8 t- _/ {) {! @. y9 r
  71.         match6_times=match6_times+1
    $ q8 R2 ?7 o, e! I
  72.         t_list[5]=match6_times# ?  d( I6 b+ F5 C9 V
  73.         x[5] = match6.cx()
    5 ]; X8 D9 R! i$ L, V
  74.         #x6 = match6.cx(). F. r. u- V9 G  ?. k2 e9 l
  75.         #sending_data(6,x)
    + ^+ q( h$ d/ H, i2 ?) C
  76.         #print(x6, end = ',')
    9 V9 S' f0 p3 A4 e3 ~2 a
  77.         print(6,"matched:%d "%(match6.count()))/ d. \( P0 T7 T/ W
  78. ' _' J9 F! R0 ?& `4 B
  79.     if (match7_count == match_count):; b4 X! H; Y' W0 r8 I, u
  80.         match7_times=match7_times+1
      y6 v/ P' U" j. P
  81.         t_list[6]=match7_times2 G* @4 X3 p9 x: m2 F9 C1 V2 a
  82.         x[6] = match7.cx()
    / D" P1 s, i. H6 v4 ]4 c7 v$ D7 P
  83.         #x7 = match7.cx()
    1 O' E$ ]/ a; Y# n' o8 M1 M
  84.         #sending_data(7,x)1 t. q# \, Y; j' @$ z' a  t
  85.         #print(x7, end = ',')3 f2 A1 k: b9 ]& O9 M% P. O7 g
  86.         print(7,"matched:%d "%(match7.count()))4 l; b) _$ B8 l6 ]! U/ d

  87. 5 Y/ g3 D6 }' I' a* t2 C; C7 M$ F! w
  88.     if (match8_count == match_count):! g# k0 G. o* b! ^8 K
  89.         match8_times=match8_times+16 k2 Q3 |8 b3 P' Y
  90.         t_list[7]=match3_times
    " h. k5 ?, S$ O, e
  91.         x[7] = match8.cx()
    ( R/ w" y  k/ D# }9 g4 c5 y- }
  92.         #x8 = match8.cx()
    / q% y0 X7 J& Y0 ~
  93.         #sending_data(8,x)
    5 K, U, v& U0 }$ O4 H2 p
  94.         print(8,"matched:%d "%(match8.count()))$ Y( n5 ?/ a' s" {8 @
  95. 3 L! D3 C% e+ t1 W* F. x
  96.     describe_Times=describe_Times+17 n1 Z& q% q1 A
  97. else:
    - @) _' X7 u/ }: H8 R* y& u
  98.     turn_num=t_list.index(max(t_list))+1- Y( a* s$ X' a- i
  99.     sending_data(turn_num,x[turn_num])#数字和坐标
    # Z7 \; u1 d( \6 _) t7 `! n: W
  100.     print(turn_num,x[turn_num])
    + |& ?) T7 q6 k" \& b! L: b5 t' _
  101.     break;2 e" M8 H; f. m/ Z  K
复制代码

/ p5 X2 A) E( J- N/ C4 U二、巡线* a9 V1 G, R" J
1.基本巡线

8 s+ ~6 d, `. {思路:将摄像头一帧图片的上半部分划分为三个平行的感兴趣区,在三个感兴趣区中分别寻找最大的红色色块,获得三个中心坐标,然后给予其不同的权重后计算平均质心坐标,用此质心坐标计算得到巡线时的偏转角度。- V$ I2 O6 l6 ~4 P" z
( R" ?4 s, S9 d' G& R5 b! q
      为什么只将一帧图片的上半部分?因为下半部分容易受到小车的阻挡或者阴影干扰  
7 w* @$ `! ?/ b) G9 |; v, G& H3 E6 O) h
附上部分代码:# R' x: v: W% K7 d4 A/ T3 H' z
5 o$ i) b: C4 m" Q+ Z+ e8 b
  1.     img = sensor.snapshot()$ F+ J& l+ }8 ^4 ]
  2.     for r in ROIS:#在划分的上半图像中找红线0 J, E0 \4 G1 u5 Y/ z( }; Q1 K
  3.         blobs = img.find_blobs(RED_THRESHOLD, roi=r[0:4], merge=True) # r[0:4]是感兴趣区域的矩形元组.
    1 R  d* |7 H: Z) s
  4.         if blobs:5 J2 G3 t1 z- h) s1 V
  5.             # Find the blob with the most pixels.
    . V. W) R0 a4 |2 R+ r' [
  6.             largest_blob = max(blobs, key=lambda b: b.pixels()) # 在blobs中找最大像素值8 V9 V7 I/ }7 n# c; m! T8 F
  7.             img.draw_rectangle(largest_blob.rect())" R) ~3 \' G* _3 J. q
  8.             # 将此区域的像素数最大的颜色块画矩形和十字形标记出来
    9 |% V6 |0 _* e/ k; O/ }, x9 E4 \
  9.             img.draw_cross(largest_blob.cx(),largest_blob.cy())+ [( t" d3 n9 b0 q, z& j% B
  10.             Moban_flag=1; q8 l8 l1 V2 e) F
  11.             #print(largest_blob.cx()): }9 h. f( U, Q) n' e7 E- u, N, e
  12.             centroid_sum += (80-largest_blob.cx())*r[4] # 最大值像素点的x坐标*权值 与 图像中心线的偏差值- u4 [; f' D% ~' z6 N0 G& c
  13.             weight_sum = weight_sum + r[4] #权值综合
    : m* Z+ ?, F! E! V+ L) J
  14.             #计算centroid_sum,centroid_sum等于每个区域的最大颜色块的中心点的x坐标值乘本区域的权值
    ( ^  N1 Y: w. L8 ]: ^, Y
  15.     if weight_sum:
    5 k4 l+ u0 G* @. T6 q' c
  16.         center_pos = (centroid_sum / weight_sum)
    ! r/ B. [% M4 k: @$ p5 ]. p) \
  17.         weight_sum = 0
      F) I0 V9 s; Y! k4 s0 W
  18.     deflection_angle = 0
    % S* s. H' R4 n8 N
  19.     ns = center_pos/60  # QQVGA 160x120.# M. S( l0 x/ k# [
  20.     deflection_angle = -math.atan(ns)
    $ ?% A& u: T0 j1 r7 p, ]% _
  21.     deflection_angle = math.degrees(deflection_angle)' t5 P6 v; n( Q1 S4 b
  22.     deflection_angle = 0 - int(deflection_angle)
    ) Z8 `5 T( ?' w; d* j) l1 r6 M
  23.         #print(str(deflection_angle))8 x, t5 ~' i3 P/ Y4 ?. M5 q
  24.     ##------------------------------------------ 直行 ----------------------------------------------------
    " F2 y! \" b9 I0 M$ T% `
  25.     #uart.write(" ")
    . z) h$ q4 X1 Y) G- n; o
  26.     #uart.write(str(int(deflection_angle)))
    : A8 O# ?! d" ?! G3 C, c7 `
  27.     #uart.write(".")
    , h' x1 K' ?+ u$ M! A* ~0 f$ v
  28.     sending_data(0,deflection_angle)#发送偏移角度
复制代码
. Q- U" M! ^3 O- I8 g7 ?
2.识别十字

: @3 r  k" _% e/ c$ b3 u$ ^我们通过判断图像左上方以及右上方区域是否有红线对十字进行识别,若左上角和右上角都有红色部分,那么我们认为小车到达十字路口处。; a( v1 \/ F8 {& R# i0 W
& M( @6 h9 T" P- `) i* h  A
部分代码:0 `( l' K7 L. j# N

4 v( x; N& _/ Y, z# @- w- w
  1.     ROI = [(10,0,40,50),(110,0,50,50)]#x分别为10和110,Y为0
    2 t$ R! ], O  J3 P( a
  2.     blobs1 = img.find_blobs(RED_THRESHOLD,roi = ROI[0],area_threshold=150,merge=True)#先找左上角红线6 \: ~, O: B9 P4 x* T/ E
  3.     if blobs1:
      D* w/ E+ o) ?. y6 |. _: \5 W# A4 v
  4.         blobs2 = img.find_blobs(RED_THRESHOLD,roi = ROI[1],area_threshold=150,merge=True)6 {& v1 l( J: e
  5.         if blobs2:: [* j% q, K& ~+ m5 M' L9 }
  6.             print("+")
    ) Q% t7 A  @" I# q* X0 Y
  7.             sending_data(10,10)#表示识别到十字
复制代码
4 u1 a4 f! {) \  d: }
三、串口通信( R8 ]) _) v* N
1.数据打包
$ t) t7 `9 c3 @( ~# O6 h/ |/ D
有些同学可能会问为什么不直接通过串口发送字符串然后使用sscanf来解析呢呢?
9 N: \. Q2 b9 O7 E& c# m% E9 X2 v$ y( w" [8 G( Q
        串口发送字符串(ASCAII编码)的方式比较简单, 用C语言的sscanf()语句解析从语法上是完全可以的, 但是在实际工程上测试,解析不是很稳定,易丢数据。 ASCAII编码易出错,缺乏纠错功能。所以我采用二进制传输的,整数直接发送,浮点数放大去除小数位,然后以C语言的int,short,char的拆分逐8位形式逐位发送。 接收以后先计算校验累加,再重组。这种方式长期使用稳定可靠。5 k8 W  I( @" v
    这样发出来的数据(int,short型)都是低位的字节在前,比如发送整型数9,得到的数据为(0x09 0x00 0x00 0x00)- a* G3 l* m: [* U; N! @

1 k8 y" ?1 b' }) b附上代码:+ _$ c2 N( P, Z0 f+ a( i

% |% F, O& f) |* N- X
  1. def sending_data(data1,data2):
    $ b/ D) [! z9 L- ?8 O' W2 m- I
  2.     global uart;$ w. G8 ?/ p7 M! R* p
  3.     #frame=[0x2C,18,cx%0xff,int(cx/0xff),cy%0xff,int(cy/0xff),0x5B];
    5 K& t, T6 C4 q- R# Q. k& v
  4.     #data = bytearray(frame)
    0 P. E+ h/ s" A  F0 X6 @
  5.     data = ustruct.pack("<bbii",#<b,              #格式为俩个字符俩个短整型(2字节)# n  R& k; a: Y3 C
  6.                    0xAA,#帧头1
    ! W# D% h! ^. o5 M. Z* g
  7.                    0xAE, #帧头2+ P6 A, k/ |8 o3 ^' e: n) W) h
  8.                    data1,#用于判断
    0 W' U8 R) l+ a1 F
  9.                    data2 #数字
    5 c$ E# |6 y& j2 c  I. M
  10.                    )
    + ^( y4 j+ y8 t1 X. d- ~  P, ~
  11.     uart.write(data);   #必须要传入一个字节数组
    & x3 R9 ^+ _: ^
  12. #接收9 i- |( {. R: [* k4 \, L0 O, L
  13. def receive_data():5 W6 }1 Z+ K2 ~; G" ~* ~
  14.     global uart
    9 T1 H$ I; I# N6 P0 i) |9 S5 a5 ~+ N
  15.     if uart.any():+ ]1 L' g8 B$ p! C% _1 o. Y
  16.         tmp_data = uart.readline();
    ' x% E+ {% Q8 E0 z6 ]# G5 a* j5 R
  17.         return tmp_data;7 d. P( N7 v  M* |% l: ^2 q7 Y
  18.         #print(tmp_data)
    9 x- |9 ^, [* o" d0 e
复制代码

1 L; ]5 F. C( I+ F2.Openmv及STM32的串口通信
: t  ~' [3 Y. S/ X& {% k直接附上STM32解码代码:
4 Z# i$ ?# e7 }/ E9 j
, p# M, ^/ r  l
  1. void Optical_Flow_Receive_Prepare(u8 data)  `- j( H! y7 Y1 ?9 P
  2. {" O8 J# {  R9 B/ d
  3.     /* 局部静态变量:接收缓存 */0 s3 L$ X. k8 ]( y
  4.     static u8 RxBuffer[10];8 Y- t1 E6 h0 U+ u  X
  5.     /* 数据长度 *//* 数据数组下标 */
    & A9 ], h5 s- }0 W& [9 E& J
  6.     static u8  _data_cnt = 0;
    " A( f: Q! n, q* R6 t; H7 b
  7.     /* 接收状态 *// N3 `9 l$ A; y" j
  8.     static u8 state = 0;1 K& r$ O$ |6 A, T' g% Y4 M( ]

  9. , D# @( X3 r8 w5 H. L
  10.     /* 帧头1 */2 W3 }2 [% B" O+ t
  11.     if(state==0&&data==TITLE1)) J) {% \) C+ n" s
  12.     {
    . ?7 U. i. M% |' ~. z5 C% H
  13.         state=1;
    ' _3 u! W1 m, k# J+ B
  14.     }
    # Q/ Z3 s" D1 n* G+ u9 `
  15.     /* 帧头2 */
    / V' f7 q8 F4 c3 l- {9 }+ I
  16.     else if(state==1&&data==TITLE2)
    % f8 D4 A' h8 p5 v" H
  17.     {
    , W7 v. {1 x. \, R
  18.         state=2;
    3 r" U/ P, M% X( ^. v5 A
  19.         _data_cnt = 0;
    , ~8 L2 Q  r( t
  20.     }- h& Q! N6 l- b- A  i
  21.     /* 接收数据租 */
    1 _: b, C. ^, I+ {. U
  22.     else if(state==2)
    : X- H; ]9 M9 J" m; v9 {
  23.     {2 r! t4 c) r4 `9 x
  24.         RxBuffer[++_data_cnt]=data;
    ( X$ g& O5 [( D' D: v
  25.         if(_data_cnt>=8)* y, |& ]9 d; O# |4 c% t
  26.         {
    & Z& ?# N" f1 J2 {! L! F$ B- S
  27.             state = 0;+ f$ T# [9 t8 F7 d/ |/ L
  28.             Data_Processing(RxBuffer,_data_cnt);
    9 O6 B0 ?/ k6 K* k3 [: ^- q! [6 l# `
  29.         }
    # S, x6 {- z& a( ^+ h8 E
  30.     }6 A/ h) L4 R( k
  31.     /* 若有错误重新等待接收帧头 */
    ( [5 ]4 F( K# s+ ]7 q, D' i' H3 f* n
  32.     else6 _8 S2 n% u' Y: v5 U' e- K
  33.         state = 0;
    ! C! a8 |9 U6 z, }
  34. }# w1 S: U" o/ h/ ^5 y
  35. 0 k' A4 P: ]3 s3 j+ H1 [) G
  36. . A  a. s! S4 N! i
  37. void Data_Processing(u8 *data_buf,u8 num)
    1 ~6 ^% r& q1 g2 \- r0 o7 p0 U' Z
  38. {/ A0 C& \2 [7 @: r& P, t# f
  39.         int theta_org,rho_org;2 n# }4 `$ G$ |
  40.     /* 读取偏移角度原始数据  */
    5 B# P( M/ X: {3 f7 z; ]
  41.     theta_org = (int)(*(data_buf+1)<<0) | (int)(*(data_buf+2)<<8) | (int)(*(data_buf+3)<<16) | (int)(*(data_buf+4)<<24) ;
    6 l3 @7 x1 g: f/ M
  42.     theta_err = theta_org;0 f2 y0 S, a; |0 [0 }% p1 S
  43. 2 h) T3 K% B8 x" P5 M
  44.     /* 读取偏移尺寸原始数据 */
    # M5 y. o  J* p0 r; U
  45.     rho_org = (int)(*(data_buf+5)<<0) | (int)(*(data_buf+6)<<8) | (int)(*(data_buf+7)<<16) | (int)(*(data_buf+8)<<24) ;
    * y' J* J/ v6 X4 i6 F7 S1 \5 M# w7 {
  46.     rho_err = rho_org;% {# [$ B8 }2 D% i
  47. }
复制代码

. F) e6 T4 I, m总结: x5 M* K6 S3 R
Openmv功能强大,不仅可以用来做图像的识别处理,其他大家可以去星瞳科技官网去学习,里面有例程的讲解以及丰富的资料$ {$ e* W& m6 E, f+ q8 Q& [
! u0 }# m' i6 h0 x& q2 {) x

3 A* H9 u* D* A7 i
收藏 评论0 发布时间:2022-5-10 22:55

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版