|
|
本帖最後由 Shiichi 於 2016-3-3 14:02 編輯 ! h: ?7 j$ m7 p. c
5 ` w; s- s# B6 c& Q4 s1 |
您好,不知是否能向您請教。+ z e* _2 D, d- q" \# G
目前和宋修賢老師在處理Ardui Car
, p4 \% | ?# L; T5 m" h雖然已使用較繁雜的方式處理了跑出黑線外的狀況
* D# R7 r/ {/ i+ c- y: W) Z3 |* S0 E' ^) m* V
但基於想追求更精簡的程式所以還是想請問一下
' f+ }! o3 s9 B+ f: N ~4 O就是如果我只以單純寫PID控制時,常遇到CNY70跑出黑線外後便往前衝
. W' c: H. Q# W7 Z1 f8 m不知道您是否願意教我可以如何處理' K! ?& }% S u' K5 A/ F
. l; u' F: e" z+ a+ @# W8 H( Q, q" ]# n& Z$ J6 X8 r, X
以下是我挑出我一般寫純PID控制的Code:- int MotoSpeed = 250;
$ d4 [6 F4 ^# u - double CNY70Val = 1000;
' Z. r4 R6 {7 F0 [. `. n6 e& \' ~ - int interror = 0;
5 S* F5 m$ u) Y+ s& M - int olderror = 0;' ?7 L4 v7 D) A; O
- double values;: ~$ t6 p$ i; O; u' x7 r
- $ b8 t; ~3 U: y2 e2 k
- * @) @) S; G% @2 B1 _. y/ ~; B" r
- void CNY70()1 U' R+ K, {$ e b( }. z4 k8 }
- {0 W. d6 ], h: B3 k' Z) R8 f4 }
- valuesRR = analogRead(RR)9 y3 k( v1 L0 g% b* d0 M) m, n2 P
- valuesMR = analogRead(MR);
+ I# |/ W5 V# @, R6 O - valuesMM = analogRead(MM); A# A: ~7 n0 `, j- A
- valuesML = analogRead(ML);5 U7 r$ P& h8 s9 c9 a% X
- valuesLL = analogRead(LL);
9 Z! P& _( t) l. o0 i: m5 h
b" K" s% D( W5 ^- if (valuesRR > CNY70Val)
6 n- P5 @1 w+ [" a# h - valuesRR = CNY70Val;
% |9 x2 X& ]4 T y3 I: W1 ^ - if (valuesMR > CNY70Val)
9 _6 i( S2 j" | W- _ - valuesMR = CNY70Val;7 x- u2 I1 D( Z& u. E7 Z
- if (valuesMM > CNY70Val), p" z1 t) ?3 d1 x
- valuesMM = CNY70Val;! ~/ w% x% K) j: H
- if (valuesML > CNY70Val)) y$ m/ O2 w$ e$ k3 X: D+ d) f% S: O
- valuesML = CNY70Val;$ ]8 a) I8 m/ y+ U% t; C
- if (valuesLL > CNY70Val)7 [" _' i- W, d) I. H. f( k! M
- valuesLL = CNY70Val;
+ | G5 _& O9 T3 X; I9 \( t5 | R! e - - E: N/ x! J& A4 Z% p
- values = valuesLL * -1 + valuesML * -0.5 + valuesMM * 0 + valuesMR * 0.5 valuesRR * 1;- F9 G+ [+ t _9 X( m$ _
- }
: a( j, d9 k, e$ y. z - 2 O! j. A8 a* G1 Z: `/ ^/ N, R
- void Car()
" M7 E0 ^" k# c - {1 C7 Q7 j- l3 w/ C8 H
- while (1) {
5 Z: J# O5 i' p! v2 ^! Z - CNY70();
9 N2 |. J: x' z# K- v: o - . U- j- T( W- q4 M
- int error = ((int)values);- e$ a% r* v. ?' A3 v7 e
- interror += error;
# T6 V) ?8 ]$ e# y0 B. e8 T - int lasterror = error - olderror;
3 d, z5 E+ U, K$ w! w# l- T - olderror = error;
; i3 t$ y; ?) }0 e- S# p: W9 _/ \ - int power = error / 5 + interror / 10000 + lasterror;8 e! j& s, m, X; p' {
, a9 N% p' I( Z- x9 G0 I- if (power > MotoSpeed)
$ Q2 V8 s w2 S5 u% | - power = MotoSpeed;
7 Q" k% r4 a: i% O: a% j! k9 ~- h/ q - if (power < -MotoSpeed)8 C* a: ], h, m7 ]. H4 S$ y, {
- power = -MotoSpeed;- R* W' \* [, U, X5 K0 {+ Z0 j6 ]! a7 u4 |
- , W, N8 |. g Y6 M: j
- if (power > 0)- Q: K3 G/ @# h; c( p/ s$ Z
- Speed(MotoSpeed, MotoSpeed - power); //馬達動作,正數正轉 負數反轉。2 j+ w5 ]' s9 i8 P) N
- else
4 w9 n: X) k; j5 l - Speed(MotoSpeed + power, MotoSpeed);
: o3 L, C! B { - }
7 Y: \; k9 m/ V. n8 A; S8 A - }
複製代碼
# q* y, i& r; [$ j$ D( @) @3 J8 j5 S; j
|
|