|
|
- #include <Arduino.h>4 p7 c2 Q: D; U
- #include <Wire.h>
* X) L4 r* U9 p; [- J" s - #include <Servo.h>* ~" U6 }( N }: P. W9 T. R
7 g* s6 a2 {: d$ v) P9 ?& L( l- #include "MePort.h"
L5 V) Y" m( G - #include "MeUltrasonic.h"; ]0 r6 `. q- K+ N, {/ z
- #include "MeDCMotor.h"
" m4 ~ k; Q/ V, r9 Q - / t% R; b; y9 e6 G _
- //double Input, Output ;4 Y5 B& X6 t% D& g/ R
- float MaxSpeed = 255;
/ g7 U; O/ s; {! K, K/ }+ P) c - float MaxPower = 180;5 c7 y; _' W4 F; `9 Y( f1 P$ P
- float MinPower = 120;2 T3 t# V3 {1 F& e! ^
- float Error,ErrorAcc,ErrorDec;7 L6 q8 C4 C8 U! s8 a
- . \& m6 q! f# A ^; K F1 a |+ w( h6 D7 [9 m
- float Kp=0.14;
; z# Z) A, b9 l6 n f - float Kd=0.00020;//23;
$ o2 [9 @2 ~$ | - float Ki=0.000201;7 f3 K) T4 G( X) r$ R
# B- C/ Y& ^% ~ R: i; ~0 `- float nPower;
7 W T2 V- u! Q/ c - MePort lightsensor_6(6);
4 K" t$ i( f+ W, } - MePort lightsensor_8(8);3 e+ z+ m8 Q' D2 B. ^2 N6 F; ~
- MeDCMotor motor_9(9);
5 t- V4 |, O) ^2 c8 H - MeDCMotor motor_10(10);
8 W- ~/ f1 s9 a4 o8 }# k' Y - unsigned long previousMillis = 0;
5 ^ o3 G$ L% j2 H8 n6 b& b - const long interval = 1;
, i/ K. {7 G( {& b/ D
- n5 V' i U/ H$ b& a: x$ S- void setup(){6 i2 ]5 P' |8 ~- P
- lightsensor_6.dWrite1(1);0 M' A' o$ e( L6 Q: A
- nPower = 160;
9 N: [4 N( }- y: ]- _) U - Error=0;
' q/ b* b! m$ R! W7 o - ErrorAcc=0;; S3 Q) O7 s$ d8 [
- }
' Z" L( T! P6 A3 i+ _3 b - : j p5 D3 P0 _1 x
- void loop(){
, l$ n9 N i- f7 H; D - unsigned long StartTime = millis();
9 n [( K# a( f" ^ - if(ErrorAcc < 18000 && ErrorAcc > -18000){; g0 b1 i6 ?. Y2 t! K
- float nError = lightsensor_6.aRead2() - lightsensor_8.aRead2();
3 E& N" F% o; \: i - ErrorAcc += nError*Kd ;
% x' x% E/ H! _- D* ^& F6 T$ u: \6 s; | - ErrorDec -= nError*Ki ;
8 T9 m0 k! h# L% W" v" M7 L9 C - Error = int(nError*Kp+ErrorAcc+ErrorDec);
& f8 n0 ?" `* o( z0 ?$ V1 q - if(nError < 80 && nError > -80){
! n* w3 c8 p# J - if(nPower < MaxPower){
0 N: Y. V$ o! g5 X* k2 x - nPower += 3;4 b! }/ y+ E' [! v
- } ?/ a0 [: ~1 W' E. S7 O
- } else{
1 o& u7 @9 f- F. X2 t - if(nPower > MinPower){0 X4 r: I& z( C- M9 l8 z
- nPower -= 2;* N$ M7 i1 C/ f( k8 B
- }
6 ]( e! [4 S. |, J9 d+ K5 k - } 5 ]1 Z. f s- O z0 v
- MotoL(nPower-Error);; z8 e7 [, M: e4 k u1 `. b
- MotoR(nPower+Error); 3 F2 y8 v, _+ v
- }else{
4 H6 _! q& W" b: K - motor_9.run(0);1 g p+ j6 }$ m: |6 r! u
- motor_10.run(0);6 I: `9 ^4 {1 ` h7 s
- }
) u4 t4 U' Q$ _6 G& t1 B# u9 t - do{}while(millis() - StartTime < interval);
) g4 V8 m4 N. p - }
5 i" m( d- |1 Y- c; R - 7 k6 M# W, L" D* v+ Z7 B
- void MotoL(int Power){
4 f5 a: G5 G# D" e4 V D - if (Power > MaxSpeed){
% F7 `2 _) g2 [' C6 L( g - Power = MaxSpeed;- g, c# k p9 d7 U0 q' y
- } 7 r* @& k* t9 T0 R# u
- if (Power < -MaxSpeed){
, d" ^. P3 g; e7 L( Q9 a- [0 q: v) K - Power = -MaxSpeed;
, W! `; s z6 i+ w7 }3 v* H" U - } % n1 T" l# U! b) @4 X5 c
- motor_9.run(Power);$ F; O& O2 y; i: ]
- } ; }0 b4 @& E5 a P! k& V2 J
- , {+ m: R+ ~3 o* }# A
- void MotoR(int Power){- B- U+ T* w9 _; c& G- k& Z
- if (Power > MaxSpeed){+ u7 I% z6 d' i
- Power = MaxSpeed;0 P$ {" ?: J, Q) l" j8 W
- }
, C7 w. M( \6 \. { - if (Power < -MaxSpeed){
' e7 U/ C x5 F$ [0 m9 N1 p - Power = -MaxSpeed;
( L" r/ C- x e' E - } / r# Y; O' @/ N/ D) d
- motor_10.run(Power);
' S/ V P2 n& A0 _4 i2 q1 o - }
複製代碼 ) c, K& U9 S5 M% F' K) a
2 ?: Y' [7 s \9 X, R7 n
|
|