|
|
- #include <Arduino.h>
- ~: i. L# }/ R! n. u/ g2 Y - #include <Wire.h>! Q$ g" U1 c ~; @
- #include <Servo.h>
2 X3 H4 f* w, M# T8 I - / p) s1 `# q$ [$ d
- #include "MePort.h"
6 d$ r1 Q. J9 Q8 B& w0 D - #include "MeUltrasonic.h") V/ J" M7 d ~! b2 k: {
- #include "MeDCMotor.h"
_" l% ~$ v P$ B$ g8 i
5 `4 H4 m! \2 a- //double Input, Output ;9 W) Q1 v( G+ N1 q
- float MaxSpeed = 255;, ~' t9 H+ y' F
- float MaxPower = 180;
$ D) J& q, Q$ t2 g - float MinPower = 120;
# ?4 Q, }9 v; P6 ~7 w* p2 S# s - float Error,ErrorAcc,ErrorDec;
" [! ?, T8 N* d# g5 E3 S' c- t5 S
; ]4 R+ @9 d" Y6 i% P, S- float Kp=0.14;
- n6 r* Q N8 Z: s- U9 _ u9 T - float Kd=0.00020;//23;+ d7 E+ E4 J' O! Q! Y# |8 }& F
- float Ki=0.000201;) ^8 \% P7 g6 W$ c& q
- $ I0 [4 C. L5 H2 k
- float nPower;
% u0 v$ `4 o6 ^9 h" G8 _ - MePort lightsensor_6(6);
\8 x7 N8 u$ J$ z' G - MePort lightsensor_8(8);6 I. z" F5 C" J& F' j
- MeDCMotor motor_9(9);; ~5 c: O' J) h2 r& I
- MeDCMotor motor_10(10);$ J* p, _* Z4 r# a
- unsigned long previousMillis = 0;: u4 X% H" d9 P* x. q8 Q4 p: D/ E0 r
- const long interval = 1;: i2 _+ a" N1 g; i! \' e
- ' R" [2 k- o& J
- void setup(){. q) c/ O! G0 h) @. N( O
- lightsensor_6.dWrite1(1);( S/ X: n% |: `1 G' n" C3 O
- nPower = 160;9 j7 @$ d3 ]' n2 D- s, T
- Error=0;6 } c" b; m# W4 E( G
- ErrorAcc=0;+ V4 a5 T+ l/ w9 m2 A
- }7 f( S/ `( x8 v
9 P# w, Q, o+ n, C* ?; C U9 p+ c& y- void loop(){
+ T$ B3 u( B, W3 b3 }; O - unsigned long StartTime = millis();
6 z; Q! I1 `6 \& z - if(ErrorAcc < 18000 && ErrorAcc > -18000){
6 B3 X4 s3 ^/ F7 A& a8 X) Y/ G - float nError = lightsensor_6.aRead2() - lightsensor_8.aRead2();
0 c8 B0 d8 I$ C$ a2 e: G - ErrorAcc += nError*Kd ;1 H4 c- m4 ]" A+ v- l+ |
- ErrorDec -= nError*Ki ;; A' d. p& J, \, Q, W# N
- Error = int(nError*Kp+ErrorAcc+ErrorDec);
, n8 w$ Z# |0 i% o& j P - if(nError < 80 && nError > -80){- L1 W% n% J0 t, [- F: \
- if(nPower < MaxPower){
$ ]* y1 W0 D, N6 W7 O - nPower += 3;+ M* e3 l8 V2 }/ F) m; m
- }( C0 c9 `/ e- L
- } else{
& S5 X/ V6 S; W# e1 Q3 g - if(nPower > MinPower){
, o4 E1 G7 H! `9 c z - nPower -= 2;% P, b: @& D, C5 c2 v9 D
- }4 F' H7 K5 Y5 y0 i
- }
2 _6 S( q% `4 S* d, K - MotoL(nPower-Error);
0 }. u! ?/ R7 `7 h - MotoR(nPower+Error);
, x! X! E# R2 N9 Z! ?6 f - }else{
2 Z$ U: c0 J" A. A$ [' y - motor_9.run(0);
9 }2 P, R T( V - motor_10.run(0);/ ]6 L$ V) ^/ ~# r
- }
* F3 [( ?# ]: U2 x# ~ - do{}while(millis() - StartTime < interval);
. W" s! T, X" ]1 d+ u) _ - }2 `5 b1 A" G. Q* S
- 4 O* J' O& T# D
- void MotoL(int Power){0 M5 U: V9 o6 z# n6 T) ~, k
- if (Power > MaxSpeed){5 y( p9 e/ [7 F9 I7 W
- Power = MaxSpeed;" b; b8 C$ Q1 h6 V D2 Y; b6 e
- } 4 y( l. l! R/ a- l7 d% b
- if (Power < -MaxSpeed){
2 O, ]: R0 V0 q6 d9 P7 y A - Power = -MaxSpeed;! q5 a; X' v) o: ?1 Q: Z
- }
0 G4 m& V8 C, @6 v* X1 k - motor_9.run(Power);
) J. M2 m& p1 f' G' [/ T) [. `- J7 | - } 2 V. g7 M. {. O( J
- ; F# p) p" f7 U" q- R, D7 X. O! d
- void MotoR(int Power){. O8 c& q2 H3 G
- if (Power > MaxSpeed){
3 E9 @9 g3 u# _* {" K - Power = MaxSpeed;' I4 E8 _ { y2 V
- } - Z( ^: P& @4 z! d5 X
- if (Power < -MaxSpeed){& [* C8 a' D2 Q9 }. i; X
- Power = -MaxSpeed;
8 Y3 L7 e' R5 ^- v- m - } * G: V" c1 X1 c% r/ \$ o3 `3 ?
- motor_10.run(Power);
8 ^/ E4 W3 j% ^" y V; K# b4 }% q - }
複製代碼
9 A' c7 [% L% |7 a5 X8 T$ @% t! d7 d. f1 X+ f6 h: u2 A/ r
|
|