|
|
本帖最後由 Shiichi 於 2016-3-3 14:02 編輯 ; {6 `. i4 m9 G: m( J
7 n8 N! `7 m$ M0 J( q, H0 o2 A您好,不知是否能向您請教。3 Q; @5 c2 j% I0 {
目前和宋修賢老師在處理Ardui Car
6 y' D+ ?" a, U) g1 r0 U2 M8 q雖然已使用較繁雜的方式處理了跑出黑線外的狀況
( Y+ N% O& g, e: }( \0 B' t" B% P
( R. W0 J3 B, w# c- s3 I但基於想追求更精簡的程式所以還是想請問一下) Y% z, ]1 t* P# [* j; C' s
就是如果我只以單純寫PID控制時,常遇到CNY70跑出黑線外後便往前衝
G) r! _# q/ B不知道您是否願意教我可以如何處理+ K+ h }2 X* ~: |7 v2 `
3 l: W7 L1 u; a
+ r# |7 \: D% ^( @' t% E* \: r以下是我挑出我一般寫純PID控制的Code:- int MotoSpeed = 250;
V0 A4 p( B' \ l! F! X - double CNY70Val = 1000;- N9 Z+ q6 ^) B6 B7 _
- int interror = 0;: p/ \! j& m# @8 H a& x9 S
- int olderror = 0;2 ]4 S: P9 a, h5 a m
- double values;
% Z* }5 J4 M+ u - : O0 A3 I# i" \
- 5 T* {9 t* l6 ?! u" O, N
- void CNY70()) s* [* V, J$ {4 `5 ]# a
- {! z) K6 e' a% n6 U/ j; D+ [
- valuesRR = analogRead(RR)
@; H/ B+ a4 s& c - valuesMR = analogRead(MR);
$ a3 v) U5 ]) V! B& t - valuesMM = analogRead(MM);7 t, [* W+ L& m& l
- valuesML = analogRead(ML);
! E( H4 l0 ^) f+ Z# C" Y0 W - valuesLL = analogRead(LL);
0 l4 g+ L2 `" x; O: ]
2 k! x; R. F5 e- e2 K( V4 q" I- if (valuesRR > CNY70Val)4 x! I- A, X* s4 [
- valuesRR = CNY70Val;: w- z% I, T. U0 i' m% O+ L
- if (valuesMR > CNY70Val), ~- E3 e9 I/ g
- valuesMR = CNY70Val;
* I2 l# E* k; \6 {* s, j2 d2 A x - if (valuesMM > CNY70Val)
; o" M8 n; o2 M3 ?7 _; O$ w N - valuesMM = CNY70Val;1 H0 Z- ~; d k# `* @ v4 j
- if (valuesML > CNY70Val)2 q8 R, @8 k$ y, T L! E( U/ |
- valuesML = CNY70Val;
H9 ?- C. e- c - if (valuesLL > CNY70Val)- C/ y/ q; [0 S* E
- valuesLL = CNY70Val;. W- M+ [4 T! I; k, j! V
- 0 F3 X% D! h7 ~
- values = valuesLL * -1 + valuesML * -0.5 + valuesMM * 0 + valuesMR * 0.5 valuesRR * 1;5 y. P( V$ _6 B; a' z
- }
+ o- L0 h/ D$ |" k2 b0 g - 3 u0 i% ~0 G8 z5 D3 f
- void Car()
0 t8 ]7 K% u1 e# D2 m0 f - {* ~' L+ R, w, P2 J7 D
- while (1) {
. V1 e4 U) m) a; e - CNY70();2 L9 _3 P( |) c7 `) k. {1 l3 I; r
- + e: Y. t! `8 A3 n6 W" V& @% i. T
- int error = ((int)values);" s; ^) [5 f8 v0 p3 r S8 Y X
- interror += error;
: h6 ]( H5 y2 h0 d7 k: M - int lasterror = error - olderror; j. m# P# d1 O: [
- olderror = error;
4 |! f; {1 ] l5 _) {' n3 H - int power = error / 5 + interror / 10000 + lasterror;
, M* u8 P' i5 m
3 c1 h5 b- x, R& Z8 Z* K- if (power > MotoSpeed)
1 u4 Z9 M: h5 w) {: z! G - power = MotoSpeed;3 x& T0 T& v7 g
- if (power < -MotoSpeed)9 l) T1 M; k* P/ `% o$ j- P' A
- power = -MotoSpeed;/ m' j" _ B1 z, b
* d# Y$ Z) A, k* r5 \- if (power > 0)% _9 L+ P" Y4 |7 b! R* h/ B( k/ J. ]
- Speed(MotoSpeed, MotoSpeed - power); //馬達動作,正數正轉 負數反轉。0 n. H% C' [, ` B, K& c
- else6 t2 p- W6 K9 C' \* ~" i6 R
- Speed(MotoSpeed + power, MotoSpeed);
0 H; e3 J( Z [ - }
# t9 ^7 \* \+ W) L8 z/ Q5 {% a( y& n - }
複製代碼 # p* j z& N# M+ y
6 {8 Z. e/ b9 b |
|