|
|
本帖最後由 Shiichi 於 2016-3-3 14:02 編輯
% h o! j" U: w1 X Q$ e0 {* Q* {3 k* W& t8 G8 T6 ~
您好,不知是否能向您請教。
8 Y$ o5 j! V5 p, I9 j目前和宋修賢老師在處理Ardui Car6 C; n+ ~1 v+ p) c
雖然已使用較繁雜的方式處理了跑出黑線外的狀況+ I4 P7 t+ [9 f' w/ G% n
7 Y9 u% Y8 c) u! _' A; d% T2 u但基於想追求更精簡的程式所以還是想請問一下. g; V7 ^$ K5 }! B1 L: M* d
就是如果我只以單純寫PID控制時,常遇到CNY70跑出黑線外後便往前衝' h% U4 n6 D. N$ f
不知道您是否願意教我可以如何處理
! G* w( Z) u" b% a# ?3 R' m
) K; Z) X6 H# i# G1 i) m9 X1 K
以下是我挑出我一般寫純PID控制的Code:- int MotoSpeed = 250;0 |/ C, A4 S! P/ N. D+ R. `
- double CNY70Val = 1000;
5 X p9 L' h7 ?8 O; W8 m4 C - int interror = 0;) u+ c5 l9 x7 G
- int olderror = 0;7 m b: t( c0 d
- double values;% `/ I8 t; x; @0 ]
8 B6 r; W% d. r$ S7 d
' Z: c4 g! c4 c3 }- r7 x- void CNY70()
/ ?! |/ g& s1 [9 v. { - {1 G0 p4 v' R) o2 \4 y* A
- valuesRR = analogRead(RR)7 l/ D2 F! b1 D! Z+ }1 C! Y
- valuesMR = analogRead(MR);! v; ~9 C, s8 G; ?$ ]2 W
- valuesMM = analogRead(MM);; A6 H8 F% p- b: v( |
- valuesML = analogRead(ML);( p& H9 O! j+ ^7 |" N
- valuesLL = analogRead(LL);, B1 P- P" i k% d
- 0 ^- h, t- u( N* v# Q* |; d) a& j
- if (valuesRR > CNY70Val)7 U( B4 M$ |1 b j
- valuesRR = CNY70Val;
7 p$ p: t9 T2 r% f# q - if (valuesMR > CNY70Val)1 g, B" q9 f" d) Z( Q
- valuesMR = CNY70Val;
! m, X' U# t" `5 D: W - if (valuesMM > CNY70Val), }) c' B7 Z1 l$ N) J
- valuesMM = CNY70Val;
. m+ T/ p/ E7 j0 o7 |# O4 R - if (valuesML > CNY70Val)
7 v( S2 Z2 i. F. i* f - valuesML = CNY70Val;1 ?: T+ D0 S0 k+ e1 A- p( A4 R/ h/ |0 h
- if (valuesLL > CNY70Val)
; |0 v o- y; y& d. ~; T - valuesLL = CNY70Val;( `8 V5 s5 N/ X. j1 I& o
- ( w5 F0 b \! y U: Y6 ^
- values = valuesLL * -1 + valuesML * -0.5 + valuesMM * 0 + valuesMR * 0.5 valuesRR * 1;5 o- V. L$ k' }1 Q4 ?9 ~' G
- }4 h+ r" x& p& D) H
3 X" c9 n; z+ f& G* v! m7 ~- void Car()" w3 {4 j; |4 I: F8 h% a1 m \
- {4 G$ s* W' b* d* e6 }
- while (1) {5 r) J1 a' s0 S* S8 O: V: g, R; \% J
- CNY70();: A/ \" I/ a4 n0 t
- ' @% f0 O [9 d1 x
- int error = ((int)values);
0 Q, s9 V( T' X6 I - interror += error;
% J! Q* m8 c. O7 p! x - int lasterror = error - olderror;; H0 S) G( ]; z( ~7 S
- olderror = error;, _" r$ K2 s# c* I! ]. o) s3 a
- int power = error / 5 + interror / 10000 + lasterror;
5 p+ {# {$ {& k# u% |# U% q - ' b( e6 O! V% K2 n: ]5 ]/ l$ y
- if (power > MotoSpeed)
4 l2 I( \! D# P9 z/ s: c9 g# Y( r. e - power = MotoSpeed;
8 r: L: {' `2 s# l* T! n/ L9 s V - if (power < -MotoSpeed)
0 d. _2 `% C* {3 K0 V; E, V" ] - power = -MotoSpeed;
$ j6 G+ Y# ]0 u: u4 e0 @! m8 N - ' O" O: b2 s& S) \$ I5 |
- if (power > 0)
' }, ^# U7 s7 W9 j* I - Speed(MotoSpeed, MotoSpeed - power); //馬達動作,正數正轉 負數反轉。! g3 g1 d A. [$ E9 I4 O' O
- else
. |% x- |/ U. \ - Speed(MotoSpeed + power, MotoSpeed);
) G& y5 Y) A! ~+ m - }
4 H7 _- I0 b, W2 z. H9 I5 i - }
複製代碼 + s$ o. ?" v( d( ]# e
7 j( w. Q; R, K; H' F4 v) i% A
|
|