|
|
- #include <Arduino.h>
; F' x- B" F3 @ - #include <Wire.h>8 L) z; e% F0 V
- #include <Servo.h>" L4 A, J# B) B7 W# B
- : o# W3 [ C; L* ?
- #include "MePort.h"! t$ `; V5 {& z2 B- L
- #include "MeUltrasonic.h") G) `& `) [' b' E
- #include "MeDCMotor.h"
0 Y2 O, K$ i3 M1 q2 J) J/ S/ B9 L% p
i; [- l! ^. i9 S$ O- //double Input, Output ;4 ]7 k% r( Z# Z/ |
- float MaxSpeed = 255;5 {" f$ M, d7 h$ L
- float MaxPower = 180;
, W9 `. f9 M7 L# w - float MinPower = 120;6 Z2 e) [ g& b: B3 e J
- float Error,ErrorAcc,ErrorDec;
9 D# ^( |) ^& M' b - ; B+ L& z I# K
- float Kp=0.14;1 Z. Q' e7 @+ v0 f; v, d9 M* X
- float Kd=0.00020;//23;
5 y3 |: C8 u: e0 [/ t - float Ki=0.000201;
2 b% ?5 O& M( ` - 5 Y5 |- _. @/ Z( o4 }
- float nPower;
1 m" A% g; j2 }, w z - MePort lightsensor_6(6); U! I9 k6 h7 B' g% B8 W
- MePort lightsensor_8(8);% E; T; C2 q9 T& z! z5 T
- MeDCMotor motor_9(9);
r9 [/ \0 f/ e4 W+ n: N - MeDCMotor motor_10(10);
: c7 J' w. F- h- d& }) q - unsigned long previousMillis = 0;
! i' D7 R8 H2 t- t# d$ z - const long interval = 1;
! P/ W# l; }- d V8 \ - ) X, g+ C( k( H& P; n/ i
- void setup(){
$ |! r; T2 @8 a4 Q* z+ W - lightsensor_6.dWrite1(1);
; ^- a, \5 g3 C: s- a9 S - nPower = 160;9 k8 P9 q. J$ i3 t, Y
- Error=0;- d. Y0 [! I. O9 p$ U
- ErrorAcc=0;
$ [! r& }' [0 s' h6 m( s - }2 R2 L. P" \' v
- 1 H9 w8 |, W! |( a$ E( v
- void loop(){8 P/ F0 {* H% m, |
- unsigned long StartTime = millis();
" \, l4 [, p- O5 X; J1 ^ - if(ErrorAcc < 18000 && ErrorAcc > -18000){
; e$ B! G2 t7 ]) m - float nError = lightsensor_6.aRead2() - lightsensor_8.aRead2();
- p$ [& _: T5 O) g - ErrorAcc += nError*Kd ;, E) N6 y6 ^, T' `$ e3 h
- ErrorDec -= nError*Ki ;
5 l! n& U9 S( T8 H) W. w; K1 B - Error = int(nError*Kp+ErrorAcc+ErrorDec);+ o& o$ h* _- I* f, x+ k7 R
- if(nError < 80 && nError > -80){, [: \, k! M+ z, w
- if(nPower < MaxPower){
0 o) h; ?, A- B8 X) j - nPower += 3;
; @, y. R- k5 N9 a+ s3 Y+ M - }
/ b9 b- D5 f) y, {8 C% b5 G6 O - } else{* b5 \; _7 b8 e, b0 c( F
- if(nPower > MinPower){+ F4 h* L0 i% I! c8 `1 w
- nPower -= 2;; K' ^6 M* F/ A5 _7 l8 E
- }, F; D$ }8 P; E' R/ h& r5 k
- }
2 u9 c8 e! \" l - MotoL(nPower-Error);
; V" b }2 N9 c( w - MotoR(nPower+Error);
7 Y9 _. i# {; V4 z* l1 w0 i+ F/ P# } - }else{
' s6 b( R% J4 g/ Q - motor_9.run(0);
, u: }& r# h# O/ S. }9 I, p - motor_10.run(0);
. E/ r+ [6 o( g6 R& p/ f5 G/ F - }6 h& _/ ~- L# R) {
- do{}while(millis() - StartTime < interval);9 A7 q1 K' T6 H) Z7 W* t
- }( B6 J8 r7 A- L& c5 X4 f
- ; J5 i' c! {$ L" c! @; V
- void MotoL(int Power){1 ?$ N- J8 R. w7 _! V' ?" i3 z
- if (Power > MaxSpeed){
; J4 l, ~( E- ?4 s9 j' { - Power = MaxSpeed;, w- \& E$ A# @
- } / `9 O/ g* X Q( r& b) ]3 f
- if (Power < -MaxSpeed){% N i$ [! A, ]' \' i1 @; S5 g s' U
- Power = -MaxSpeed;
) [8 @( F7 C- N. ~$ p1 ~ - }
0 V- x: r$ v4 ^4 z% _/ y0 ?! W - motor_9.run(Power);+ G" u8 s; K' i4 q5 L; C3 Z3 p
- }
9 P* a- g# S* a, Z; T* E6 J6 n - 0 K/ J8 R/ j9 M5 r3 t: b9 W% r" Z
- void MotoR(int Power){
- t5 t$ D/ h' {/ Z$ Q$ h - if (Power > MaxSpeed){
1 h0 @6 Q) `3 b' n7 I - Power = MaxSpeed;
6 P& F# f- f6 w) b3 l" y I - } 5 ~7 p/ O+ g7 U4 P$ R9 }* w2 G
- if (Power < -MaxSpeed){! B" m8 a$ [3 \( N: s1 u$ B
- Power = -MaxSpeed;
. k% h3 y1 h, W - }
3 t0 R) Q9 j* o+ w8 _3 G - motor_10.run(Power);
5 h3 Y% r* i/ Z6 m+ Q0 F0 M( o& W - }
複製代碼 + ^' v8 j! R% m) a% V
+ V. P$ p# z$ S! z. J8 ?5 K0 G. c |
|