|
|
! P$ N+ ?0 |+ s2 h+ v1 w- #include <Arduino.h>
5 L6 p: F% Q) Z8 `$ f - #include <MeAuriga.h>
, a0 J7 W! ~/ m. V7 }, T - #include "MeLineFollowerArray.h"
( f6 \% p+ s/ H& ]* y% x8 F) z
# G$ O3 b0 n# y' M; F; u" l# H- MeEncoderOnBoard Encoder_1(SLOT1);/ J; T+ [( k! }" \' N+ A
- MeEncoderOnBoard Encoder_2(SLOT2);
) `9 h, [* H9 P1 k! W' u. D0 u - MeLightSensor lightsensor_1(12);! {4 n; r" m; B
- MeLightSensor lightsensor_2(11);! P# K& m9 ?% q9 C: w
- MeBuzzer buzzer; D& n1 v5 w3 [7 C8 n
- MeLineFollowerArray linefollower(PORT_6);
5 l3 K" @* D- H8 [' E6 t: w
; a( H. c( H# j! ~9 ?- #define Error1 1
! a7 n/ r% |: |4 z9 j: V$ b9 a - #define Error2 2
) N6 `8 j3 J5 Z( t! @1 P: v+ s - #define Error3 38 ^1 \5 S* z1 e
$ w$ u" T" Q7 N( _: J- #define Kp 15 ~# m* d1 C5 j
- #define Ki 0.157 k/ @: X' e# L8 k7 q! v4 a" k
- #define Kd 0.03! c+ ^3 u( S ` n6 P4 D% E& `
- $ K F* [0 D7 I5 j. S
- uint8_t sData;
: F' r( B. h9 r7 D - uint8_t D1;4 F) \, g d* x
- uint8_t D2;6 C' i. D/ D2 r5 e1 D; r5 s% Z, U6 Z4 ^) ]
- uint8_t D3;+ r* D) j8 J7 s" D
- uint8_t D4;+ ^+ B/ [ W. J0 v3 V2 q
- uint8_t D5;
; c- j( F% A) }0 P7 v - uint8_t D6;( r6 V( z& p5 b
* b+ w9 H2 T" h: g8 s- float previous_error = 0;
# B" B1 `' Q, A - float integral = 0;0 b' c; b- k/ Q Z4 V5 C
- float derivative = 0;4 s: H2 ~3 v5 U
- int Speed = 160;, @( N; @8 W; K8 x; v; P
- float output;
, _" M/ a3 w+ y! J- K+ F2 R: P - 3 |& \+ n6 W0 ?2 c4 J V/ t
- byte Left;& |2 b b7 Y) K# Q4 ~8 h( S
- H* K* n; I6 g }& F
- void setup() 9 u- {5 P; N% W$ G1 ?5 }5 \
- {
3 Z3 ~! V" u* c3 Z - //Set PWM 8KHz/ I3 ]- T1 k$ R6 ?
- TCCR1A = _BV(WGM10);
& y' M; \4 q' j, R+ o - TCCR1B = _BV(CS11) | _BV(WGM12);* g' {* [( g( g6 S- I0 x
- TCCR2A = _BV(WGM21) | _BV(WGM20);! g% i3 ]: ~ M6 B! m9 u
- TCCR2B = _BV(CS21);
8 d' C* g7 t* N2 `0 W - Serial.begin(9600);
' ?1 Y4 v* V I" ] - buzzer.setpin(45);
- G5 l( ]6 n1 V( A) i- Z6 X) I - }; K) \# F3 K0 N* ?( x, W
- 6 Q$ Q w& _# l; d/ D
- void loop()+ \/ U1 E$ @7 J$ b V/ q- x5 }
- {
3 y, g: V( u( g1 M: ]* M - while(!((lightsensor_1.read()) < (10)));& n2 s4 W3 v) G3 ~, W: j
- buzzer.tone(1047, 500);
! v& i6 N1 _, g& ~0 k a1 z - delay(500);
5 M5 x" x$ b/ U) m& E! ^: `1 T - do
0 Q7 C1 l3 g# p0 [( r - {7 Y, i2 P: L8 ~% J
- int Err = getErr();
9 p/ k3 Q7 R& {8 h - if(D1 == 1)* W1 @* r- s% l! e
- {
% n2 D2 m/ `: P# \/ Q9 x - Left = 1;/ C# B; _% n2 p* r- h4 E
- }
$ F1 }" |. s$ c6 Q( D9 k) [ - if(D6 == 1)
- t2 W" ]( b6 U( W2 H9 g3 _ - { {4 X4 Y) o! Y4 ^3 x# @
- Left = 0;
/ E- b! G( ]# g9 c1 L3 ] - }) Z# |6 z, G& B( u; [
- if(Err == 99)/ g! @& R( u: A) N2 Y& T, d3 z( z/ s
- {
* T' `' k% h% F- v4 _1 ^ - if(Left == 1)# v4 H6 Z+ f3 R; q: v H
- {
- d9 ~0 r. q7 L* Y$ P' d - Speed -= 5;
7 R5 Y6 V4 U2 Z, G. D5 N6 x( ^6 n7 u( x - moto(0,Speed);
# {; m e9 n, I( k* j" Y - do! }+ i: a" ?$ ~; y& z6 e( T+ |: Y- `
- {
; E' Z# ]( n8 Q: x9 u0 }: b - Err = getErr(); R* i/ k6 m. G6 d! i# |. @5 J
- }while((D1+D6) == 0);
1 p9 k7 S5 m5 k8 U- F* }7 p) E - }
# P% D9 ]+ C: Q' A. z - else/ f* @( x$ J( w$ b, S6 U
- {. C3 W3 F2 `2 z
- Speed -= 5;! X* L9 ]6 n0 ]- a6 O0 ]7 f" {
- moto(Speed,0);
% \) y W; v$ A1 R - do
/ g' e9 Z% b! X4 Y/ G - {4 }3 M( j: j8 E/ q
- Err = getErr();; F2 O, Z- b" Y& D: L$ ~" @* P
- }while((D1+D6) == 0);
$ D; H7 a4 y; T* n$ I6 I - }
" O" z! a8 y" m: U - }- g2 u F% E" }5 W8 w/ W
- else- T0 T' b* Y+ V$ O( }
- {9 E9 G! m# M0 O/ |8 k7 S
- if((Speed < 160) & (Err < 2)) Speed+=1; % h; _: _, C5 d, F1 P: F3 ?
- if((Speed > 100) & (Err > 2)) Speed-=2;
; C- D) c' w8 t9 o* W" [ - integral = integral + Err;4 E4 [6 h" d7 |/ u4 I
- derivative = Err - previous_error;/ j6 {+ @* g2 M
- output = Kp*Err + Ki*integral + Kd*derivative;
& a; U+ p! N8 r, q: T - moto(int(Speed-output),int(Speed+output));5 Q$ b g: b0 s! M1 C
- previous_error = Err;% O+ F: I$ r0 N- R$ E0 ^
- }3 x' h$ v& ^0 Q8 A: S5 c2 y r& s) f
- }while(!((lightsensor_2.read()) < (10)));
' x; u M7 P! D- H - moto(0,0);
4 M% p. k1 L9 @/ y, U9 V - delay(500);
2 N3 K1 ?) j) m9 s0 S+ E, W - buzzer.tone(262, 500);
. D4 e# ]0 l3 K& P - }
9 L. x+ r5 c8 O& ?. l: E6 O
2 b' R1 X3 @" f6 o! s# {( x F' q- int getErr()+ H* E4 I& Z+ j7 W, l0 A0 Y. l8 {: m* ]
- { & j }& w4 C! G) g) u. m
- sData = linefollower.getValue();
8 T+ i( g1 i. u7 a, M6 N/ L - D1 = ~(sData>>0)&1;
2 `# ^) P/ b" X: f+ w* K6 O5 u% X - D2 = ~(sData>>1)&1;
' l# w9 ?. R; [: O k - D3 = ~(sData>>2)&1;7 l. n& \) y9 H+ V
- D4 = ~(sData>>3)&1;
5 R8 G: A5 T# h% M - D5 = ~(sData>>4)&1;
# ~! z( p, D# Z0 j8 Y - D6 = ~(sData>>5)&1;
Y5 U3 S, O# }/ Q2 T! L - int downD = D1+D2+D3+D4+D5+D6;6 W: n4 m3 ?0 q4 t$ t) H
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);
/ ?0 @* w. M$ P9 }% ` - if(downD == 0)* X3 D# r$ I2 u. ]
- {# u/ c/ f0 a' ?& ~8 F
- return 99;
' p B) Y3 t% @, A - }! z4 f- V; |/ J' U
- else. C1 X# J" Q* C
- {
! Q- h1 U9 j" ]1 }: g: y) ?. W1 k - return upD/downD;9 D( r6 E! U' L* ?( Q. ~
- }
+ B3 x& k! {& Q( o: H/ Z - }
$ ~+ ~( L( \1 V4 V/ p - ) T0 O7 p- B( V- P* g! J9 t
- void moto(int Speed_L,int Speed_R)
8 Z# T$ M8 o$ c% Z - {
) J) M/ a Z4 g' Z7 W0 G - Encoder_2.setMotorPwm(Speed_L);
& N6 T4 B( w3 x$ ^ - Encoder_1.setMotorPwm(-Speed_R);
# H. ]- F1 M6 Y j. s - }
複製代碼 : d+ b! i/ B$ X/ f" p, s1 y* W( p
MeLineFollowerArray.cpp
4 @6 l& C$ q, k; S- #include "MeLineFollowerArray.h"
/ s' Q' X5 @8 i: X* Z* \, @. j - " V/ `" G7 h7 S: Z
- #ifdef ME_PORT_DEFINED9 x6 w+ N N" f' l( K3 x
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
7 I# T U! V! w4 O - {* d$ I9 c9 `' B4 D+ g6 C
# Q) q; v+ V. y: w- }, y. p2 Y/ r% p' k
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)' m2 {$ M+ v V& R/ p2 O& } R. T
- {
+ f* A' _5 c7 E% \6 V: j - _DataPin = mePort[port].s2;: B* v/ ]7 h3 ]2 a1 ]
- pinMode(_DataPin, OUTPUT); ) ^3 h' G! _ L. Q0 v
- digitalWrite(_DataPin, HIGH);
, {6 q g% c% m - }) \! _. @0 r/ B( A2 j, J
- #else // ME_PORT_DEFINED
2 I; @- T, s: C9 ^8 O1 j" n, Q - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
/ K% P& ~# t! x& C& v - {
" K' q( F! U0 c$ S - _DataPin = pin;2 b# R* S3 Z1 b1 D9 k8 n; @' }5 _6 e
- pinMode(_DataPin, OUTPUT);
* g. ^! Q4 T" `* H/ y2 R$ N - digitalWrite(_DataPin, HIGH);
. K+ F( s, r) k, n - }( s! @3 t9 @5 K! \) ~
- #endif // ME_PORT_DEFINED
, Y3 c* M0 e* l$ a+ o
5 b @. `# t9 l5 \9 D! f
& ^# i b5 t2 N# h- s4 B; X0 e- void MeLineFollowerArray::setpin(uint8_t pin)7 R' Q7 `" r6 U7 }
- {" z- V8 |5 l3 t5 k! D7 R
- _DataPin = pin;
$ w8 x" b! T7 n - pinMode(_DataPin, OUTPUT); : b5 H0 X8 A- |2 _
- digitalWrite(_DataPin, HIGH);
" Z4 S! C/ T, @& j( u - 6 r" k0 L3 P) r- c( u# U) e0 b3 U4 {9 @
- #ifdef ME_PORT_DEFINED0 M, k* U* K# Q' l; Y
- s2 = pin;( ?$ s- Y$ J; ~, l5 G, s4 q. s2 p4 C4 P
- #endif5 O! f( `6 j8 g2 I0 A
- }8 i. @& @( k# [+ ]$ _# o
# j. c8 R! ?+ ]$ l3 a( O- uint8_t MeLineFollowerArray::getValue()3 f+ R9 j& K$ |! d0 O
- {
: u' x) ?4 ~6 S U, c3 t - uint32_t LOW_level_read_time;3 S M- Z1 n& f r& d
- uint32_t HIGH_level_read_time;" b% q4 J, w3 u8 c- L) o2 l
- uint32_t time_out_flag;
1 v/ H' g1 e& A, O8 y; l* h - uint8_t Sensor_Data[3];
5 B5 U- @. n( y7 F2 d2 q1 O( g - static uint8_t old_data = 0xff;, w" S. D9 v- k, o: M( K$ E
- % s$ d% L& ]6 c; G
- pinMode(_DataPin, OUTPUT);- X z) V" l1 R
- digitalWrite(_DataPin, LOW);
4 @# M3 g8 g# `3 S* N4 g: V' P& o - delayMicroseconds(980);1 V0 }9 Q5 [. _
- digitalWrite(_DataPin, HIGH);
1 W: q" @- E% G - - W' d2 _& e8 p7 S9 x2 A) P( A: q1 g
- pinMode(_DataPin, INPUT_PULLUP);3 a% l$ j) `# l2 z" C- Q
- delayMicroseconds(10);
, a4 \. @3 @! s; X* w, c
6 {7 B, P" ~1 z& M% L- time_out_flag = millis();
9 z9 R7 I" s+ e - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
% G. ?* U" h6 u% j - 6 O/ \/ t' `; a
- LOW_level_read_time = micros();& V: X! @, r( c
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
/ |- B# e' Q) W3 a2 B4 w - {3 P7 T" Q" X& X; r/ ]4 g& \
- return 0xff;
$ I/ U0 w6 z3 O- o x1 Y - }" {) x) y# l" r8 o! _7 c$ q
- . y+ u( W: ~- y0 R
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );; d$ ~9 y" q. }# ~" s* e5 c! Q! U) J' v
: h0 N; F" @! ^$ V/ ^- HIGH_level_read_time = micros();
& B/ K" i' f1 x; w6 w0 u/ O4 U, } - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level4 T6 ?) L3 P; ]
- % H9 S2 k3 C8 A, H
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out+ O: n! P- m% r* @& q1 ~
- {4 c+ e' f# s0 x* z9 r
- return 0xff;9 _5 @& a0 O1 g) B
- }0 M2 {# ?0 T, U+ b( z+ T: c
- 0 U1 ^: H" h( T; S+ |" d7 K
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))2 o e; r* a* Z
- {8 k2 J4 X# p. x9 T5 k7 I8 p8 C
- return 0xff;) T: H7 c/ z' T+ k" W
- }# e4 Z3 g; A; n, T7 L! z* U
- 8 C4 h% n% H' q( R# A# J2 F+ G" k
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );0 _" Q! _& {- z$ G* e1 A# u3 i
- LOW_level_read_time = micros();1 U1 n! R% | B5 w
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
# s) {6 D9 l* X - & F; i2 x6 A6 Z- B2 Q2 f' S
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out1 U; H6 S# `+ ?& S4 {- l+ ^
- {3 Z1 c5 ?6 k; ? x/ O
- return 0xff;' g, _ A4 ]- }+ m T3 I) \9 x
- }
$ T4 b9 b/ t* B O) O$ I, j - # P) N/ i7 |* o) f# e2 L- e
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
+ I4 Q4 N( l9 [ }( f - {
1 M; P$ O8 h4 M* p* q - return 0xff;6 y, r! Z/ C& S3 }- L. s
- }
4 e# j9 S2 i( v$ J. p7 \ S; s - ! g9 f$ S- a2 Q k' o
- for(uint8_t k=0; k<3; k++)
6 y7 j( D! L4 r% O. s - {/ \! W6 Z) x$ P9 A1 t- j( C
- Sensor_Data[k] = 0x00;
5 v" m, M% g6 v: R* |0 q - & L' _6 b0 G" y0 l# e
- for(uint8_t i=0;i<8;i++)8 U, F3 J0 j O3 l, k. ?3 ?
- {
# \! q8 @. }: k3 B8 Q5 F - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level; O6 F; T3 N! }, I- v% Y
- HIGH_level_read_time = micros();6 Y( d4 Q( p" q' j2 Y& U
- LOW_level_read_time = micros() - LOW_level_read_time;3 d* _# m. V n. R" x6 d! F
- * G5 P; `' U) c4 E
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
' o# L$ ?+ V; x$ ~+ S - {) Z% G7 s, u( m8 v8 o9 V
- return 0xff;
4 N) ]& z- d% R2 |. ~2 _) I - }$ X, O3 P4 ]/ d
- 3 G0 s9 |; X0 j2 T" y+ ]. ]
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );! I; j: Y M1 r7 z1 h4 Z m
- LOW_level_read_time = micros();) V. t3 E7 A$ m) P
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level# S* i4 X) n1 s3 E& F
- 3 t# u, {2 U& N2 S. R5 M# p; w
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
8 S6 A0 q, v ^4 v - {
! w: X* t0 k& X' C8 r - Sensor_Data[k] |= (0x80 >> i);
, r+ s/ k a4 [ - }
0 y1 b3 D K. p' R+ \( T - else if(HIGH_level_read_time >= 100)
" ]7 J7 B4 c9 W+ l/ x4 z - {
* N0 I8 g. {7 m. ]6 V, q& M - return 0xff;
& J6 u- h0 c. m d) G8 M% x/ ~# ^ - }
4 F9 f# {; ^5 ^" s
* g) y' z9 P0 p3 P. s, I6 [# f- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)0 j) U* q* Y! ]2 { ]
- {6 _" @; B! n; C% t( A& r' b
- return 0xff;6 A& z+ z9 z3 A6 E
- }$ p& N4 Y g" Z& V5 v2 A
- }
s2 p c7 q; a: B' B/ B - }
2 g7 a/ Y; A# {: o X
' o6 G% R8 C0 K$ T+ }5 d; M- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
# X2 g9 z6 q+ `/ e% Y, c& j* R - HIGH_level_read_time = micros();
( k6 k* W+ ~8 \, ] - LOW_level_read_time = micros() - LOW_level_read_time;8 X6 W' n0 E. t- d" B
- 7 U5 l. `' h O$ Q# P
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
$ g/ S' ]/ Z7 h5 S' T- {) d - {
+ Y' |# t# c' {( x; Y7 S - return 0xff;
6 _& o, ]6 i/ D, m! m! i - }+ k" A* s, y& p/ L! j
1 x! X: } u; @- pinMode(_DataPin, OUTPUT);' X0 }# m' O1 J( `( C& \
- digitalWrite(_DataPin, HIGH);
/ a- a1 S5 U! O: u) w D
' @ A/ q$ T( L8 u2 h4 c- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))2 H2 O( P- P8 B
- {/ D* i" V5 c1 u8 K% l7 g3 ?0 N
- old_data = Sensor_Data[0];8 A5 `& a+ _! O- M2 P1 |) f6 F- k
- return Sensor_Data[0];
& ^, }( L% S* |1 A: X4 _5 ~& _# ] - }/ x+ M! p/ B2 d. U; p# T
- else; R) p. e) D3 W! ?, p2 ^+ z0 G8 ^, h
- {- ]. H0 W( e$ m8 J. |# f1 {
- return old_data;
6 }9 s) q% U, g - }( ~; M7 P/ b/ U% P3 H0 H( b
- }
1 E* r7 M+ o; P
複製代碼 , t$ y' l, l, X) r+ a
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
2 j% `+ h5 d- |# J
, h* c& J8 g, F& N3 x& _ |
|