|
|
- #include <Arduino.h>7 n6 t; Z9 q' i$ s, h+ l
- #include <Wire.h>; h3 n6 |. b, q' |- s. Z
- #include <Servo.h>
8 y5 B& p! c, _5 K - / `1 X/ T& X$ M) ^6 A0 M9 ^
- #include "MePort.h"0 q o! n9 ?: u# H' i
- #include "MeUltrasonic.h"7 l L( R; L- S9 @7 e7 g
- #include "MeDCMotor.h"
6 T M& t7 H8 c5 Z - 2 o6 g& w0 G+ P( L. J
- //double Input, Output ;: X! F) E5 a$ N1 }3 ]
- float MaxSpeed = 255; D6 i4 Z0 q1 z" t* n$ G
- float MaxPower = 180;/ J$ f+ B0 t/ D5 ~
- float MinPower = 120;6 h- G6 W* h: n" p2 F
- float Error,ErrorAcc,ErrorDec;2 X4 T( g+ Z; {3 N$ J; \
# \% x; ?* @9 p+ ]- float Kp=0.14;9 U' a5 \( [- }* k$ h7 z- q
- float Kd=0.00020;//23;
% z1 S: }5 v5 k. W - float Ki=0.000201;; _* n$ z7 \2 C
7 _1 W* n. d* q! m# y- float nPower;
9 s2 L, R' l7 j. ~, ?) D - MePort lightsensor_6(6);
9 A' H0 s/ Q O) O& F0 i+ p0 z7 ] - MePort lightsensor_8(8);: P+ O' p# t2 ]4 A
- MeDCMotor motor_9(9);
; O0 V) F4 |: V D - MeDCMotor motor_10(10);
' S9 E0 f( ?2 o4 T! p - unsigned long previousMillis = 0;8 n2 g% z& w7 Q3 ^, V6 {
- const long interval = 1;+ C0 r2 @7 R% h6 c2 m4 Y, {
- # S& h) n, M. ^
- void setup(){! F0 {! h) ]4 l2 }) d# K
- lightsensor_6.dWrite1(1);
8 X% {- N z6 Q' F- _, F - nPower = 160;
A3 S8 s+ `( }- H7 m8 A# W1 ^ - Error=0;
4 ^5 R/ J2 O' [9 Z5 N2 q. p% P" J+ g - ErrorAcc=0;1 u* C1 X9 l6 b
- }# B) Z/ b5 B ?! u7 ?/ b6 {
9 q8 M' n z0 y' L- void loop(){ T' D. i3 B0 L$ F' @) [5 i
- unsigned long StartTime = millis();
Z" X' m" a( Y% y4 i9 A( X - if(ErrorAcc < 18000 && ErrorAcc > -18000){
& D) {& y$ R) T. }6 T* H - float nError = lightsensor_6.aRead2() - lightsensor_8.aRead2();, O" V8 u$ c: @" H2 b9 d
- ErrorAcc += nError*Kd ;; K3 E, ]8 I# y% f
- ErrorDec -= nError*Ki ;7 X r/ `8 Y0 N! g2 r
- Error = int(nError*Kp+ErrorAcc+ErrorDec);$ z' |7 p1 b' h, C% E
- if(nError < 80 && nError > -80){
+ u' M: g/ d1 F+ |0 D) y - if(nPower < MaxPower){
' P5 `/ p' |) x: i- N2 p - nPower += 3;# n# ?1 P( Q% Z" f% p/ c s
- }8 N# g) q" }" N# W5 o# B& o
- } else{
# j2 L& ?& I" t f - if(nPower > MinPower){& ~" X: j# @' d0 e8 o8 |
- nPower -= 2;
" H, Q3 e `: Q( ?' l' w& X - }5 y* R, P0 L$ T7 L+ _
- }
2 n5 v0 @8 T! l" b# H% j3 K0 ^! [ - MotoL(nPower-Error);
( V$ m1 i6 ?5 W& O - MotoR(nPower+Error);
* X; R. ^- {' j* N k5 v& e - }else{
3 g. c: T' L3 e - motor_9.run(0);
/ }) c) h( m& ^5 t3 C6 }) C1 u - motor_10.run(0);9 z& Q' p" j/ E! d* Q: D9 e
- }
7 f5 S0 V$ g$ m: X( h - do{}while(millis() - StartTime < interval);
( K# W( {; f9 l3 i - }5 N. R, K. N7 r/ g3 O
- $ t& U/ i7 E+ p* G
- void MotoL(int Power){
/ @0 V; p! P- C+ X) W$ D+ C - if (Power > MaxSpeed){9 m" V: E% o- L# m+ X
- Power = MaxSpeed;/ L/ I5 j6 v6 R' b- {
- }
; Q. q; `/ P9 R - if (Power < -MaxSpeed){; f, p6 E& W$ W0 ^2 @! }
- Power = -MaxSpeed;/ U0 n. t ], f' }8 T9 d w
- }
* V# }' Q2 o' _( ^$ Z* V - motor_9.run(Power);
- J2 v: D1 P: ~ - }
5 L J: x' M1 z u0 `" g/ H - 3 g( K1 V+ z: R& U! B3 a7 x
- void MotoR(int Power){' h3 ?# J, |% j; z; S3 X
- if (Power > MaxSpeed){$ z9 U, p- C2 M2 q
- Power = MaxSpeed;
; B0 R" d& _/ r; s$ n; \ - }
' m' g: {. ]$ e3 r0 r" E - if (Power < -MaxSpeed){
$ O8 G" G" a; {* w5 Q+ g! j - Power = -MaxSpeed;, p( m8 I* f t
- }
- R( |- q; }2 r2 P% j) @- Y+ f, A% z - motor_10.run(Power);: ~7 f* F4 Y! {+ i4 b1 D+ `0 {
- }
複製代碼
' [' X+ A) n, S( O1 ~' F0 `4 a
|
|