|
|
- #include <Arduino.h>
8 {) P6 w3 N* K8 I* { - #include <Wire.h>
4 N3 p% b& L, T; Y; S - #include <Servo.h>
. v' l! c2 S" a h- b% B1 Z* @ - / p- ~- b I! h4 Z/ M: }. r
- #include "MePort.h"4 ]4 O1 U: S) N7 k2 c; _! w
- #include "MeUltrasonic.h"( P) z: x* R4 h1 O# w" w& h1 }9 \
- #include "MeDCMotor.h"6 Z- t. A* y5 A7 M) w9 |
- ! J. f/ D% G3 P4 J1 F
- //double Input, Output ;
' }0 I# g9 K, s5 P8 G: H9 | - float MaxSpeed = 255;
* u" a% Z) u; d+ I& h) @4 @2 [4 U! L - float MaxPower = 180;- `% l. X% f, V+ I0 x
- float MinPower = 120;/ g3 h- e1 Q+ h: Z% s' ]: g
- float Error,ErrorAcc,ErrorDec;
6 z* U" P7 ^3 E4 } - # o1 Z* W5 a! m2 G
- float Kp=0.14;
& P' {/ d! q& D( W - float Kd=0.00020;//23;& {1 n8 _; M8 b
- float Ki=0.000201;" M: m& @! c! U( M
0 J" C& d9 r; i3 U+ f" y4 l) @- float nPower;" h8 T, m% E& {. v
- MePort lightsensor_6(6);( Z# s# l1 d2 C2 j5 i
- MePort lightsensor_8(8);
( @9 t: d: f- T' h, w - MeDCMotor motor_9(9);6 [* J6 u) b; E1 Y# c
- MeDCMotor motor_10(10);8 l: {2 F# t, k, A1 c \8 l# V
- unsigned long previousMillis = 0;
Y0 f$ i; k' J. h6 b! w; u/ k8 V0 F - const long interval = 1;# s6 t3 J4 b3 E. u( a! M% u6 j: q
- + n& w4 Q8 z3 x s" ~% \
- void setup(){% N0 G0 n3 I/ Z9 e
- lightsensor_6.dWrite1(1);
' b, ?% [! b: p+ F - nPower = 160;! f( {( O1 G$ O5 `; }$ l. \
- Error=0;0 e, c$ W0 z( N
- ErrorAcc=0; Z8 b. j- E6 ]2 S3 o8 T& q$ c3 n( _
- }
- e$ v! a' H2 Z+ t, e - ; w# f# b, ?/ Z2 l; ], V
- void loop(){
9 X K' ]; B% {3 ? - unsigned long StartTime = millis();1 P: x4 P7 l2 S5 W7 K9 z
- if(ErrorAcc < 18000 && ErrorAcc > -18000){+ a. T/ ^! D1 z; N; X0 V* Z
- float nError = lightsensor_6.aRead2() - lightsensor_8.aRead2();
! Y( y; i( H; w - ErrorAcc += nError*Kd ;
/ U) L3 a( V( x) u - ErrorDec -= nError*Ki ;7 e5 H' H* o' C1 D( H" V" F
- Error = int(nError*Kp+ErrorAcc+ErrorDec);
# |" c7 E' d& k+ u4 v - if(nError < 80 && nError > -80){ `* X. n( }" _+ R; ?" J) s
- if(nPower < MaxPower){
& D- N6 l; L* Q% q1 k' D - nPower += 3;
! Z2 t+ h4 A) i - }
7 x2 E. Y$ I% D1 C! p6 L- l8 c - } else{
# N2 v. G' ~: p5 f - if(nPower > MinPower){' a1 a- r( z( l3 T4 e
- nPower -= 2;7 p3 W! A% A d# c* }; i
- }! _) P4 A+ Y7 ~! C
- } # o# U" r2 P9 m U
- MotoL(nPower-Error);0 R7 v, s: l' c) F% C: T
- MotoR(nPower+Error);
- X+ P) b* C! c% C - }else{
6 m* d/ ~% K: X3 t - motor_9.run(0);
+ s, V- u" d: H* ^, k1 l/ ^ - motor_10.run(0);# S6 [' {8 s. S' M* o3 R0 n7 n
- }
3 \2 V" a- e0 P% a( z6 M - do{}while(millis() - StartTime < interval);% q% ~4 I, m+ S8 k4 o
- }
3 q9 p! ~& K" m6 m& d! c - % c5 k; y- [3 n& _9 @& i3 ?8 E) `
- void MotoL(int Power){
) }0 \, ?8 R9 `( l$ w3 q - if (Power > MaxSpeed){
5 _" B) x" e# w {2 G( f, @0 M - Power = MaxSpeed;
" S i1 ^) Z4 q( l& E" v4 }' v - }
% {& W. P3 E/ e- |. W - if (Power < -MaxSpeed){
9 X1 w \, {" S' n; M, q( p - Power = -MaxSpeed;
/ {$ ^1 x5 V( u5 d9 j - } @; e! B v8 ?# Q* _0 [
- motor_9.run(Power);
5 Q, z5 X) T) p5 ?# d8 k - }
2 B5 P; i+ {6 j+ g/ x; V
7 ]; o5 w$ j& g7 [$ h' L- void MotoR(int Power){
, J/ X' h% r; K' W# z - if (Power > MaxSpeed){/ G1 ]8 A; |$ U1 R3 H
- Power = MaxSpeed;
2 o9 U9 A2 J; t' | - }
# M# c+ T! g2 O1 u* @$ C - if (Power < -MaxSpeed){) X: t& d4 r& \- f: s0 K
- Power = -MaxSpeed;
' w6 }$ i0 {* k6 x - } 2 y5 x! q, }) ^+ S) F/ H# d4 d
- motor_10.run(Power); @' g! y. m6 H9 n3 L! a$ [
- }
複製代碼
& H- e. Y7 s0 y6 P
1 W3 z( k2 E& @! J/ e |
|