|
|
- #include <Arduino.h>
$ ?& X0 R7 M( ?9 ~6 w7 w - #include <Wire.h>
* @. e5 O; _2 A0 }5 [$ \. R" G6 E* ] - #include <Servo.h>; P$ u/ a6 u% K
0 D% ^, o9 W& z1 M1 P9 @; R- #include "MePort.h"* T, e; k* `2 Q( z
- #include "MeUltrasonic.h"- N7 X; D' q+ c, L3 z7 _
- #include "MeDCMotor.h", F) o! }: ~1 f4 |. a+ p
- 9 U! |+ i2 Z4 t c1 b
- //double Input, Output ;- k3 l$ M1 F, A3 i# Q
- float MaxSpeed = 255;
+ L6 y& R' I) \: e, Z4 ?2 ~ - float MaxPower = 180;
7 ]; v$ E! Y- X0 Q - float MinPower = 120;; X/ G& L- F; B. \
- float Error,ErrorAcc,ErrorDec;. G8 k% B8 w" J! @7 ^
3 h" r3 N0 `6 G! ^- float Kp=0.14;
& v, d2 Q5 ~. O5 V z - float Kd=0.00020;//23;: m: Z G+ P* g# J
- float Ki=0.000201;6 v- M o+ {3 O6 k$ L9 v
3 B5 F u8 o5 v( e3 Z- float nPower;7 s$ Z% s3 i. q) ~7 _+ {8 ~
- MePort lightsensor_6(6);
2 m3 ?3 [: }6 _& N - MePort lightsensor_8(8);% j& m" a& q. a7 h
- MeDCMotor motor_9(9);* c. y1 y N$ z8 b/ I+ }6 ~
- MeDCMotor motor_10(10);0 @( y/ o9 @# T6 L% y7 I
- unsigned long previousMillis = 0;
. y; a/ l% x8 x' U, L - const long interval = 1;8 r2 B! S! n! u) H& a! @
- ( N# J0 j q" r7 a2 z
- void setup(){
' e n/ `% \( s! W - lightsensor_6.dWrite1(1);
! D3 u' t+ ]$ J - nPower = 160;
2 D$ [1 I! n; D0 P/ f - Error=0; B4 y8 H; m5 e0 t% I M# V
- ErrorAcc=0;
( Q) g: b6 X- l$ M9 S4 f' g - }1 I% ^# B" \6 s5 g+ u# V' x/ N
- H6 f% ]; u9 i2 P! U- void loop(){' L! { k' `! y- l; _7 ]! |: r
- unsigned long StartTime = millis();( @* }4 `- d# u
- if(ErrorAcc < 18000 && ErrorAcc > -18000){
" h% w! B1 [* h& j; W - float nError = lightsensor_6.aRead2() - lightsensor_8.aRead2();% T5 y1 h7 _0 j1 J, x- D# F! z/ G
- ErrorAcc += nError*Kd ;/ j, c$ N% P8 Z2 h1 {
- ErrorDec -= nError*Ki ;" m$ G+ N2 D0 I/ U$ {
- Error = int(nError*Kp+ErrorAcc+ErrorDec);, I) o+ j5 j$ b* e* P$ @8 i; W
- if(nError < 80 && nError > -80){
0 u8 A, f, p8 t6 v t5 y6 E6 w# Y - if(nPower < MaxPower){% K8 ~6 Z* T* e' r1 \" U* a
- nPower += 3;6 k" g+ o( u' K* H+ I
- }- e* L3 b. ^+ S5 C+ j
- } else{
7 u( N$ l5 d! @+ C" v7 x! [ - if(nPower > MinPower){
2 `. a* \& y3 w. I - nPower -= 2;
/ e) ] c8 g: N4 Y/ j) L - }2 w% E/ q% X, b4 ^' {; F" ]/ }& l
- }
9 E) @, e! M2 Q. h9 ^4 Y" Q& I - MotoL(nPower-Error);4 K Z0 c' h) Q
- MotoR(nPower+Error);
# w5 d; s& u* V3 f( b( z7 |/ X - }else{
- c: T/ ], S% L; g - motor_9.run(0);# c( ^ G& j+ n! }2 J
- motor_10.run(0);( B' W1 C+ M9 K9 i% {9 y
- }5 _* u2 N8 q U9 R& u. q! u
- do{}while(millis() - StartTime < interval);
5 ~+ z+ A, P& C8 U$ I- ? - }6 q& P* ~) v I# H1 ?/ J
) C! X0 c! ?, C' F. T: z- void MotoL(int Power){4 W- E3 Z2 }2 {! Q2 K8 ?5 [2 Z3 [
- if (Power > MaxSpeed){# Q `1 q% ~, f7 n( Q; s& c+ L
- Power = MaxSpeed;
( P. }. I) V. c - } ; {) \) E+ n+ p( V3 ]
- if (Power < -MaxSpeed){
2 z7 s/ B" X+ K) s+ E4 K - Power = -MaxSpeed;: I9 E; w% R; y3 s' `
- } ( l s1 N* W) k3 F. @$ O. B( G1 e9 t
- motor_9.run(Power);) h) A) N+ w0 w/ b9 d# r2 U- m% Q
- }
- s5 a8 f- b8 D - : w! W- n& ?1 f& ~% G
- void MotoR(int Power){
+ @. `- \- {8 Z" x0 B K) Z - if (Power > MaxSpeed){6 I$ I4 H4 P$ p1 c. p; p2 b8 C) b; e
- Power = MaxSpeed;
8 \: F6 p! S& \2 K* T q- p - } " A+ ^ \# a( a s5 L, }4 c
- if (Power < -MaxSpeed){# \- h: e4 w5 k: T' f& t: R" ?9 e
- Power = -MaxSpeed;( j: ^) P, n+ k* k1 t5 d( v
- } 9 W0 E7 p* h; |" a* }6 Y8 ?
- motor_10.run(Power);2 c4 Y) n2 O$ x3 F0 R! F. d
- }
複製代碼 : t0 D( R" ]. H) N5 w; D# o: [
( s6 C& q7 q# f* ^ |
|