|
. J0 C) B. q: A( A; J
- #include <Arduino.h>% ?, w; y' o+ D/ g
- #include <MeAuriga.h>
, G+ _, A9 K2 x+ w - #include "MeLineFollowerArray.h", P5 {7 O& ]# i t" h
2 m( r. L- q2 V- s; }- MeEncoderOnBoard Encoder_1(SLOT1);
$ H; k# j& ~; `* b" c& n - MeEncoderOnBoard Encoder_2(SLOT2);/ k6 P( \" J3 N! u, s! n
- MeLightSensor lightsensor_1(12);; o% q: y( x& Y6 Y" k4 ]- p b" y
- MeLightSensor lightsensor_2(11);4 a- [5 F8 B# Q# |; z. p
- MeBuzzer buzzer;/ J' u, y9 W; e
- MeLineFollowerArray linefollower(PORT_6); A! h0 s/ p7 [* j" l
- / ^ g# W; ?# L0 `
- #define Error1 1
+ W: @" ?. k* C6 S; S - #define Error2 2
3 s% p) y. ~* Y+ C. a; e. d* B2 W - #define Error3 3+ d' Y$ ~' M6 x! \
4 g0 z& W3 V6 O4 X$ @- u& m. F- #define Kp 157 F* j. ?* x0 F6 p+ f6 ~2 W
- #define Ki 0.15
7 j7 w6 a$ m; l, d - #define Kd 0.03, I. Z/ W7 f# \/ @# ?4 F% r4 p5 S
- / Q7 G& }& m' d* E
- uint8_t sData;
- ?8 h+ e' i/ }5 i5 i/ I" Y - uint8_t D1;
6 i" Z/ g+ Q, x - uint8_t D2;; a) g/ D1 u0 U" X, y0 Q1 E
- uint8_t D3;) H1 ?8 i8 \2 b+ t6 n4 T! c- T$ M; E
- uint8_t D4;, O" T* P; }; o+ y
- uint8_t D5;
$ M4 v& Z' ]7 B - uint8_t D6;/ R v R* d9 K" d
- ! @4 O3 U, p/ k) R1 v8 a' t3 t& O. _
- float previous_error = 0;
6 {6 n: H3 R5 f% X - float integral = 0;, f* L( Y: {! Y) t: i* J
- float derivative = 0;( C; O0 k& k; m
- int Speed = 160;
5 v8 D- x0 R/ n; F& V4 e - float output;
+ B( ?6 U4 f! q7 i. `5 {& ` q - , z. R, ?# R8 E( s2 ^& \
- byte Left;
- \# g( i6 U, y - 7 o4 M# ?8 k0 `- }2 m: f- ~( ^
- void setup()
" i4 t q9 e: w2 ~, V4 w7 a1 [ - {' Q0 T: x; Z& n& `" w& s
- //Set PWM 8KHz
; f- @9 O D4 w& {! [ - TCCR1A = _BV(WGM10);
8 ?3 d \! z4 s* u( D$ F* w - TCCR1B = _BV(CS11) | _BV(WGM12);) L/ d% h9 |/ k. Q) O2 T! j
- TCCR2A = _BV(WGM21) | _BV(WGM20);
9 \' ~! G& ^: i1 o - TCCR2B = _BV(CS21);
- ^: w- }! C+ l/ j - Serial.begin(9600);, m6 I0 f$ g9 ~2 ]
- buzzer.setpin(45);9 X8 x8 V" r$ W; l* J
- }- B6 E7 [2 t" o' r
7 ]% P( E1 s: S; c) e; Q6 o- void loop()
: |6 l1 o5 @. }, ~ `; G$ L' z - {
i; |/ M5 R$ k0 J8 V! o) l - while(!((lightsensor_1.read()) < (10)));
) A9 } \3 l4 g% b$ C3 v, v - buzzer.tone(1047, 500);5 K9 c, E4 o, p. \2 k' h
- delay(500);
- L, m2 B. `: i' r0 B3 d- x2 T - do
* }$ s) t1 K. a - {* T" V' U; |! H1 n' K
- int Err = getErr(); E3 {. ?& G [; B
- if(D1 == 1)
7 I5 ?+ A" x( t) |) {# k - {
/ t: P1 V2 s* d1 H- O9 ]) R - Left = 1;$ ~! A0 k! ^2 ?* Y# |' j# I! \8 F
- }% N. P$ [+ f! x `2 L
- if(D6 == 1)
7 E0 K9 l1 U% }& D& F. S - {
( |- F# |2 Z3 C, |5 } - Left = 0;
: ^. m: Z$ o) I( i - }
: d1 T3 ? z( a1 u* F! Z& f - if(Err == 99)' n; i4 H8 r2 C; C# \
- {
% T" T& p/ d, w/ l9 q - if(Left == 1) @; T$ W3 w6 c& B
- {
" p3 `, n' f% k( x1 ^ - Speed -= 5;5 f4 i3 u4 d: w: j, U! T
- moto(0,Speed);- }: R+ C7 A q- h. X" E5 l' `
- do) r, D" | s% ?( q4 @
- {& v2 P1 i* Y1 P& W; p0 W
- Err = getErr();
& X% O; c2 @+ ~4 A8 l$ M H - }while((D1+D6) == 0);- \9 [, U" s# a# n/ V) H
- }
$ }$ v/ _9 t7 f, K - else, R& w& }7 T7 ^# G: U3 I4 C
- {
! m. q4 g6 {. `8 {% j2 f9 w0 _. M - Speed -= 5;
' W7 t5 J2 Z! [8 s: z; t - moto(Speed,0);
$ q' l4 p h$ ^0 d. ]* o - do/ _; @2 H8 |0 K. {1 f* i1 [8 {
- {
. k% j ~8 n9 z - Err = getErr();
! K. A5 u) c0 `7 `% S - }while((D1+D6) == 0);$ H1 _/ t7 @7 g2 d: { k8 U
- }
* n& A8 k/ F; j$ E - } I) H- z) K# T: P2 g
- else% v! \- ]. w( {1 X) C$ N
- {
% N; H/ f" y% ~ - if((Speed < 160) & (Err < 2)) Speed+=1;
+ { N$ l6 o- I; R - if((Speed > 100) & (Err > 2)) Speed-=2; , u- g+ S: I4 I; s
- integral = integral + Err;
6 I7 m; @8 b, F8 ~ - derivative = Err - previous_error;' |# b1 I+ [8 \+ l1 }0 F. W. ]
- output = Kp*Err + Ki*integral + Kd*derivative;
( K, \ l8 l$ R* ^* A - moto(int(Speed-output),int(Speed+output));
% S+ ~7 ^- x, I1 x" V0 E - previous_error = Err;
1 ~* V+ D3 c4 O; B8 b - }4 a0 A3 N& `+ \8 ^7 a( O
- }while(!((lightsensor_2.read()) < (10)));
. J( z' `1 V1 Z4 ~ - moto(0,0);1 ]: V! r$ W& N% S7 v
- delay(500);
% ?8 y r7 q2 e% K" a* ~ - buzzer.tone(262, 500);
* r v( U' g8 R$ }. J1 e% x - }+ z! k" N9 }% t0 j! P' _
. P/ C- y# q; F1 Q9 B3 R- int getErr()
- r- N( O* `& i3 S8 z5 e - { 8 F3 d: B# |" k( M, C
- sData = linefollower.getValue();
I+ g1 V8 {$ H- S/ M0 H; S - D1 = ~(sData>>0)&1;4 \0 n4 e# { C+ I2 G
- D2 = ~(sData>>1)&1;
% B7 R2 _* p3 Q& T8 f2 O x - D3 = ~(sData>>2)&1;
) I6 b. g8 R1 P+ y - D4 = ~(sData>>3)&1;* D- a3 {% `, i5 T3 U6 P5 t
- D5 = ~(sData>>4)&1;7 D3 b& v- _2 I, }1 `, S: X
- D6 = ~(sData>>5)&1;) g( A7 ~, I c1 S5 l
- int downD = D1+D2+D3+D4+D5+D6;
; a7 x+ h7 i5 M - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);4 U O# H% i% h6 r, @- ]
- if(downD == 0)
2 T& ]$ L, l6 e ^8 x - {; B5 S6 y5 I% o* v' Q
- return 99;
8 J; \: ~# Q& Z& ` - }& `! s" z7 }6 s2 }- Z# x: m" r
- else
/ |! m0 T4 k- l/ @% L a n* P - {
* a) B! q S' ?! h/ t - return upD/downD;, j5 P, I9 J9 L# H" S
- }- M( [9 N% M5 a! g) k' _
- }
; p. A2 J. k7 L2 N - - R, H6 D! p$ N% l! Q
- void moto(int Speed_L,int Speed_R)' q, V3 a8 h5 n' X# \8 g2 P
- {
- p' ^0 m |6 L! S" i1 B! q3 f - Encoder_2.setMotorPwm(Speed_L);$ `& i' J* p Z, Z* t; i
- Encoder_1.setMotorPwm(-Speed_R);. F* a W! J: v1 Z6 {
- }
複製代碼 3 k7 } M5 E6 j# k$ G6 a
MeLineFollowerArray.cpp
! i+ s* m9 F& g! E; P! d- #include "MeLineFollowerArray.h"; Z- t- G8 Y$ }# Y
. `: L6 r, ]: S3 D) v- #ifdef ME_PORT_DEFINED
5 Z4 E& G. U/ m; l2 p# n6 R - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)$ \5 D2 z8 |& j/ P% {; |$ A6 a
- {: s& B; v2 K1 X8 M7 X
- ( W+ M: t* x3 H) _, m1 M! K% R8 J
- }$ @9 {" ?/ b7 m! |/ `# _
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)9 U1 F# U* k/ R' I
- {' Z7 n: F+ T/ {! W
- _DataPin = mePort[port].s2;
+ B$ f5 w5 X1 _) r& c6 C - pinMode(_DataPin, OUTPUT);
* r: F! o2 o3 e9 d& S/ G& o7 ] - digitalWrite(_DataPin, HIGH);2 q1 Y: f& G, \( n: k& u* ?# ~
- }
9 X1 t$ o0 t5 i6 H - #else // ME_PORT_DEFINED
6 g) w, d; b3 W M r: A3 t* ` - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
# J0 ?# L2 \) `( ~ - {
# v4 d0 d# l5 n& m) p+ A - _DataPin = pin;: y% z+ }4 i( u4 `( V: L. h
- pinMode(_DataPin, OUTPUT);
3 C8 U9 T7 R% _9 O t' G - digitalWrite(_DataPin, HIGH);0 u# H/ k7 u3 q0 t
- }
4 O! J8 G8 P+ M8 }, L$ U - #endif // ME_PORT_DEFINED
$ M/ i0 v/ a: t5 j - + s* I7 F1 b9 h4 p! Y
- ) j. G6 v4 U9 L) P0 N
- void MeLineFollowerArray::setpin(uint8_t pin)
7 O b' n% M2 F - {
0 ^' ^ E5 z1 I+ g - _DataPin = pin;
' t, W6 a& T8 y: M# E# Y" l3 K# h - pinMode(_DataPin, OUTPUT); 8 j# W8 Z" L5 K
- digitalWrite(_DataPin, HIGH);/ R# f% h) {% A( h: a; [
: |5 f3 n! t' m' A* H+ n( }- #ifdef ME_PORT_DEFINED7 G/ _" H% m- p
- s2 = pin;
; }7 r0 V2 `$ Y w8 s0 g+ W - #endif
1 b! ]7 L2 \* [8 g( o - }
( Q+ ]: q" `* z. y( p9 O0 c - 4 f7 _' N; I) T6 }
- uint8_t MeLineFollowerArray::getValue()8 T0 |2 D, @' ]) }& `7 [6 e5 M
- {, P! ?4 N3 w; z6 u: Y8 X% r
- uint32_t LOW_level_read_time;
/ \7 p; E2 z& B, Q4 `8 @; R - uint32_t HIGH_level_read_time;
( H" A( C3 G( H2 m - uint32_t time_out_flag;$ j% u( s3 F6 k6 D* B2 Q! I% ?, \! |
- uint8_t Sensor_Data[3];
, U9 Z1 m( t. h2 l - static uint8_t old_data = 0xff;
% l$ y9 o8 U7 D& c6 M7 Y( _. Z, V
4 Q9 C' S# v5 @6 u* t% X- pinMode(_DataPin, OUTPUT); x3 h$ g% p* T
- digitalWrite(_DataPin, LOW);: g* B V+ d! P
- delayMicroseconds(980);
: b r! H0 ?& |" L. `* D - digitalWrite(_DataPin, HIGH);9 b# `( T' O: y& h! C9 q
) ]) l( ?' Z4 W( ?/ h2 A2 z7 ~- pinMode(_DataPin, INPUT_PULLUP);
+ ]2 I2 ?$ t* C8 z0 a - delayMicroseconds(10);9 H/ O9 T* D- X8 x U4 g
- + X& |1 h- a# A% c e' t
- time_out_flag = millis();2 x& j0 Y8 j2 j+ `8 D
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );* p' H- Z& @4 }- C5 z3 F& e/ L. f7 @
- 1 z% ~! c6 R' x2 E+ m
- LOW_level_read_time = micros();; g4 P$ K; {. J, g. a8 X
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
" v. f; @' i3 S, p - {
0 `% m4 X X8 d& @% n0 Q - return 0xff;( ?* I; b! V- N. {8 B0 b7 o
- }
, S$ x) R$ D* `6 A' X& C3 k - 8 S r5 g- Q' T" N! a. I# P
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); s6 h- T" v1 ^0 x
/ C, O9 D* Y4 m* R: D- HIGH_level_read_time = micros();
! I* X- E7 t% U2 x: O - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
8 f) z/ ^$ _0 L3 f6 I- B - % f4 ?: K! R; K* Y
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out- b* o$ P6 M- @) |
- {
+ ]: ^+ w' A& D: Y, a' B - return 0xff;/ g P3 j8 u( x7 N, p+ J8 S& h
- }, o2 Q) a5 A; B2 }4 i8 t
- * O: j8 N$ k s% n( a1 z7 V
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
9 `& o1 @6 Y/ o- _: N - {
7 z% M: f3 ]( @: D" O - return 0xff;
2 H' j8 d& G! y% d - }
" W4 L/ a% N& {- a/ n5 M2 D - 4 @! D/ @, K, z; S
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
! T! o J, V4 |; L/ Y5 `" T5 s - LOW_level_read_time = micros();: D( \$ A( h$ s9 j4 R9 A
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level6 R! ]; D" K3 a
& S/ C2 y' X' A& r) S3 O- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out+ y' s1 w6 @2 v1 f$ I! H
- {
/ n5 v7 V- g7 h- r, v - return 0xff;
2 Z: T+ X2 G% s' e' A. ` - }1 ~* q) z* Z* I2 d7 k" z
- 9 x8 _6 U- |& j1 c
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
, B5 \ i& M/ x* s9 w& e - {+ {4 f Q% U$ Q! ?* r9 t
- return 0xff;
& e" c9 t1 c: q$ J1 O1 E7 c% G - }7 R8 d# c' c4 h6 q
- 4 i. r0 l1 G' q5 e. k
- for(uint8_t k=0; k<3; k++)
: |; p" A( J# z0 S - {
# D* Z- ]+ i" Q) m5 h" p- @. s - Sensor_Data[k] = 0x00;
. g; T0 C" S4 C, i
+ C! @% I! A+ L; r- for(uint8_t i=0;i<8;i++)
, _6 f- K* k& r# p; [ - {
. {% @ v7 V- F+ b6 h/ q - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level+ K3 g: y* a Z/ {% D) ]
- HIGH_level_read_time = micros();
0 f8 t6 V6 c G1 C) x; H - LOW_level_read_time = micros() - LOW_level_read_time;
& r: n" f, \+ X4 { G
$ E: T, L3 m7 ?- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
" @6 |* M) Y" ^: _7 A0 A [" ` - {/ i% j$ m7 c7 v3 j( O1 {) r3 N
- return 0xff;
9 H3 r; Y* V' q6 T% s. Y8 n - }
+ ]2 i3 @% w ~: z, L
( N* W3 W. @; `/ y6 [: _" n6 r- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );- k% Y( W3 a- r, ?
- LOW_level_read_time = micros();
' X; I4 r9 \6 y6 H% C: C @1 H/ v, l - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level4 x# U V9 I) j0 y, U
; N5 ~ `' I: q' M& j) D9 ]- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1% e1 [# X1 q2 o& s: p
- {6 e; M% L$ {/ X; }
- Sensor_Data[k] |= (0x80 >> i);
" s9 t2 |6 C6 u) q. \2 D, ?' E - }
( |, m6 b1 r, J( _) s. p/ d- h - else if(HIGH_level_read_time >= 100)
& x0 f5 f6 Y. Y8 `# n - {' `& E" D* l6 @/ \
- return 0xff;
3 ^: L- D4 T ~3 X. h8 g9 y3 ?2 y - }
4 `" Z2 J* z2 r* [/ H1 Y
/ `: Y# P5 o7 ~) N5 _- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
J9 Q# R+ m7 q3 C - {
# `3 `- m) \, n - return 0xff;
2 Y8 t: |+ z4 P7 f - }
4 X2 v" {* Y% u$ g" Z - }
& i" e3 F4 z# k0 {8 q9 c; l - }" S8 B& r- k" t( d. L) T, A- S
( R: X# E% C2 ~% c- A- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
/ }/ T* ?2 A3 A9 F; V" y( b9 Y - HIGH_level_read_time = micros();
1 u' D6 M4 n b' c: ]: u* m6 ?8 h - LOW_level_read_time = micros() - LOW_level_read_time;
! [9 i" B. p+ A# q* R
! ?. ]! ?2 m; y) w! _- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
5 g% _- x6 q& c" R8 K. K( T - {
7 S% M" X9 d* U" P" p! s3 V* Y9 M' e - return 0xff;
6 m& d& N; u$ U7 ~1 @7 Y; S - }. I% h1 n6 D1 w ?) U6 Z# P
- ! I) k. b& d2 b& H( m
- pinMode(_DataPin, OUTPUT);
4 g: ?8 C+ D% c+ N; E l2 G9 O - digitalWrite(_DataPin, HIGH);
/ \1 X$ Z7 c7 x- n" c
$ z7 F2 }8 w( ]% y( I u- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
; c$ L: ]3 y+ r' E( n - {
$ p$ S: ~3 f& L* [* m - old_data = Sensor_Data[0];
) I8 ^# l; ]% E/ r - return Sensor_Data[0];
9 ^ b' g/ ?% F: J+ F - }/ i! f& V. v4 q/ D* A, }
- else) M* c0 Q0 b; ^$ R+ V
- {
0 j! T% m. c/ [2 D* N - return old_data;
4 Q/ ?* N! F; F B U; R% u - }
. ]# G. ?& v I# x9 P& Z - }6 u( u% L x* f. c, f2 p( l1 \
複製代碼
6 E( s* D+ X4 NMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
7 m, k! c' l% m% o" e# F
: L' k* A9 t( T0 ^5 Y |
|