|
2 p8 l% T) ` N3 n. }% m; U- #include <Arduino.h>
/ `. \, |; A" Y - #include <MeAuriga.h>( z/ M% Z! d' N8 k) k, G7 }7 e
- #include "MeLineFollowerArray.h"- H, i6 p; _" O5 |. n
# i/ U. m" F% ?+ c2 e- MeEncoderOnBoard Encoder_1(SLOT1);" @; v& b7 K0 h1 X3 E2 g. x
- MeEncoderOnBoard Encoder_2(SLOT2);
0 \: k" T2 P; q! F - MeLightSensor lightsensor_1(12);- j) S# B2 V. M; w& Y% ~ H: x5 d0 ]: n
- MeLightSensor lightsensor_2(11);
) D- m2 A& r U. f* S# f/ n; o - MeBuzzer buzzer;% l. }, X; d/ K* ?3 W% A
- MeLineFollowerArray linefollower(PORT_6);
. ?& c. t- ?! E9 E& N - - F% W% W2 J/ _0 `4 j7 M3 V
- #define Error1 14 p5 k% W# y1 k) c, h3 Y! g- R
- #define Error2 25 k0 o0 @: ^6 X2 O
- #define Error3 3/ Z- ?( _! s7 F1 A; h9 Q
4 ?9 q6 x; U+ I4 v; x7 C- #define Kp 15( M0 ?) b" v: G2 h6 y
- #define Ki 0.15
" {2 v, w+ }3 { v) Z( P8 C - #define Kd 0.030 ~% m! E/ d" l/ T
- C, Y0 w3 n5 Y" E% N
- uint8_t sData;: x& k% f4 m9 m9 N O5 G1 Y
- uint8_t D1;
. |0 e4 d5 S+ x - uint8_t D2;
; t& A; ^. e9 n/ j - uint8_t D3;
& u6 q' s4 |1 {3 G" s2 N - uint8_t D4;3 z6 Q8 A! R$ j5 b( I# S
- uint8_t D5;
4 J: p4 h$ v: O5 b& U: } - uint8_t D6;
- `. B8 t; C* y9 s, D
' G) s- x, Y' m& \. k- float previous_error = 0;! f% z1 _" G* k4 B# J% s. M
- float integral = 0;) j* k0 Q1 Z0 y* `
- float derivative = 0;, P- ~; `1 _0 C P
- int Speed = 160;
6 l1 j1 |) k& Y4 c7 U6 a- O - float output;
3 `3 L0 V" N+ u - , B- h8 P4 Q* I! Y0 G0 [" n
- byte Left;, \+ f. B4 ~/ \; E7 J0 A5 c6 s
- * T0 `! A6 O6 ] j3 U0 f4 [$ G
- void setup()
{+ i' J3 o+ a- W+ ^( Y - {
) [% a/ u% K$ n% F - //Set PWM 8KHz+ D1 g( x y% t% V/ [# u" Q
- TCCR1A = _BV(WGM10);
3 a& W9 G+ c3 t* v3 Z4 G - TCCR1B = _BV(CS11) | _BV(WGM12);
& C% b/ t& e5 K( K/ j: ^1 ~ - TCCR2A = _BV(WGM21) | _BV(WGM20);
4 u% t$ ?9 \2 W - TCCR2B = _BV(CS21);7 {2 X/ k4 K7 ]7 D
- Serial.begin(9600);
B! O+ z( Q8 G9 U9 o8 r6 e2 J; I - buzzer.setpin(45);
5 B- t+ I4 I" E - }/ \8 u5 [2 a9 |- p/ M3 t
5 ?* a' ^6 P( L' D8 Z, u m- void loop()% ^5 K+ t& _: B
- {
& \9 {# Z t. ]/ {% A - while(!((lightsensor_1.read()) < (10)));
F5 \2 C2 _8 e0 Z - buzzer.tone(1047, 500);
9 ^" r4 k+ v" Z% m u# X - delay(500);
, n( |+ j+ t) ]; F8 h; V - do% X3 [. f* {: ` V, z( b; o% ?, C9 [" K
- {
5 `0 R$ l; k8 }9 Y - int Err = getErr();# X3 C% }6 B7 r' D7 n
- if(D1 == 1)
: c- ]$ a0 b' p% K0 u6 B5 G3 i - {
! B4 i7 u+ {6 Y3 y) L; f+ T) w; e - Left = 1;
5 ~% n8 h) ?) s$ F8 A - }
" u& p4 d% K* O: ~6 m - if(D6 == 1)
@ |) g2 ?: W6 T& x" n - {
' \1 j0 N5 V7 M6 R; b; E5 } - Left = 0;2 Q: ^3 P/ b! Q; i' O
- }
; P8 Q# D# A( X6 P - if(Err == 99)
" k' R! ^# O4 b- ]9 t0 e7 C Q - {. @" R! `; Z& z% h2 q
- if(Left == 1)
& \( G+ S& u3 I$ G& B - {# t1 {9 e5 ?3 Z$ ^) S
- Speed -= 5;
: }5 c6 m7 y' z. D+ t0 t2 \, I - moto(0,Speed);
& ~9 h: o& m# X2 \( x2 F$ U/ f - do
3 J) |. E/ v% d - {
4 N0 \2 j) X4 | - Err = getErr();
/ [+ J x! V4 q& _+ Z - }while((D1+D6) == 0);
6 \) F% C; v# S3 `1 z: A9 p$ ^ - }0 v0 S. y. ~" }% m4 S
- else
2 j3 p/ q" ?4 k0 I - {- o6 I s3 {- j$ Q* h/ d
- Speed -= 5;
2 f+ O! F4 _$ t - moto(Speed,0); 8 H/ [* M' L9 r8 B$ f
- do; c8 j5 R6 C' E) h& S; K
- {4 @, Q) ~/ n2 h6 z# p; H( k
- Err = getErr();# B1 I+ f' H! W5 I- N. }
- }while((D1+D6) == 0);& t' U7 ^5 } o! K- l& c9 N. A+ N: p
- }; U* ~3 ^1 }0 n" w* G5 {% G
- }5 X/ b, j8 @' U5 c
- else
4 y# a+ ^. D% l - {0 ]# k: V4 g6 o/ I3 o4 W3 Z; q6 c, k) f
- if((Speed < 160) & (Err < 2)) Speed+=1; ; X0 T2 B( a: }# P, K
- if((Speed > 100) & (Err > 2)) Speed-=2; # ?# B# L* P2 m# i( K
- integral = integral + Err;
* r2 P+ @6 |( D6 O1 _0 O - derivative = Err - previous_error;6 Y4 Z" a) Z7 F5 y" M
- output = Kp*Err + Ki*integral + Kd*derivative;+ l, N; j" ]5 T& f* r3 ?/ e# c4 }% U
- moto(int(Speed-output),int(Speed+output));
" K# I) Y ?1 k1 u/ y' J6 C* W - previous_error = Err;% Z; v3 L+ v+ P$ r
- }
% l- V- D2 D7 G8 J3 ~+ ] - }while(!((lightsensor_2.read()) < (10)));
- n0 F; j2 S% k$ G+ ?$ E" g - moto(0,0);3 _% H. K0 ?+ o
- delay(500);
F- k0 F0 Y4 R; |/ Q8 Y - buzzer.tone(262, 500);
! `, p+ H7 P8 l C' ?" B; ` j& k- T - }) y! m" C/ `3 k$ w
7 D9 R& a% g( u- int getErr()( {2 ` p! s" g, d) U
- {
; ]" ?, a6 Q- _; {$ ] - sData = linefollower.getValue();
' [7 s1 V$ P$ {! S( l9 Q2 Y3 G - D1 = ~(sData>>0)&1;
/ n: C5 T0 a/ E7 ~) w' V+ S - D2 = ~(sData>>1)&1;% P9 M U* R" I7 f5 j/ ~
- D3 = ~(sData>>2)&1;. U3 w5 |5 x& U& t; [6 p
- D4 = ~(sData>>3)&1;: ?6 o l2 y( I' |' ^
- D5 = ~(sData>>4)&1;
* {8 x& W* {, A, I) \9 m1 s - D6 = ~(sData>>5)&1;
8 N% E/ F5 T; u' t7 d0 L - int downD = D1+D2+D3+D4+D5+D6;
7 W4 e0 m5 i3 M. x/ J2 l& P - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);
, ~" y( C; n- R - if(downD == 0)9 \& T2 ?! Y b" E) n3 i
- {
+ u! S: Y6 `; p h - return 99;% U9 _& ]4 H( f" Y5 D
- }
" _! e8 J0 s) }, A5 M - else
; H# h/ X, W- O. j! A4 V, @! w - {$ b$ g, t' h$ m" e$ O& Q% R
- return upD/downD;
" |, ~; r, r$ [$ x - }: b% z6 Y! _7 O% f! \$ O/ M
- }
j" A a* X8 S6 T1 L# y" j - 7 B1 T# ~9 U) o, |# b- `" f1 `( N7 |- W
- void moto(int Speed_L,int Speed_R)2 g( Y! y) N1 T9 D# F. u0 `
- {
S; K0 k9 L) ~' t! M - Encoder_2.setMotorPwm(Speed_L);/ m' @9 c6 O% M) C$ N
- Encoder_1.setMotorPwm(-Speed_R);5 l3 n0 N) [/ D: l0 r
- }
複製代碼
- q, c2 b$ z1 M* _. UMeLineFollowerArray.cpp
- O6 u0 |# e% [% F- G9 |9 c- #include "MeLineFollowerArray.h": m/ O+ w6 j' C& f
- + k* {7 R4 R2 A6 o- S/ L3 z6 z
- #ifdef ME_PORT_DEFINED
/ e0 D) v" I! Q& J - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
$ e) K' I; F4 V! g1 q - {
5 t l+ c) V9 ]3 `7 X
1 y# ?' f I2 h. ?( l0 ^0 C7 b8 i% u- }
" L* ^' K6 w+ a8 M2 G9 J0 ^ - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)- l0 \7 M- x ^* I3 k
- {
/ F. z" {4 `3 ]' e' J - _DataPin = mePort[port].s2;. Q- o0 l. N. f8 W$ l6 b9 i
- pinMode(_DataPin, OUTPUT); 8 }+ @0 T4 F, [7 O8 Z4 `
- digitalWrite(_DataPin, HIGH);
# x5 v( B: ~4 Z: D# F3 U4 h - }
1 N6 b! D+ b0 f - #else // ME_PORT_DEFINED
( ` \: C! ~$ y- z+ S+ {0 [; r - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin). O9 n/ u8 {; h. U( O$ D. j! }5 N4 h9 ?
- {# X* t+ U( |* H" P* N5 o
- _DataPin = pin;: w U0 h4 K1 i9 a" G( g
- pinMode(_DataPin, OUTPUT);
9 A8 |; f6 R. o: t- w2 d - digitalWrite(_DataPin, HIGH);$ @$ O) U' `& e
- }) ]! t' P6 c. H0 \8 K" W: l
- #endif // ME_PORT_DEFINED3 w$ B, b5 ?' T+ [+ W
3 D! G9 a8 s. j' L1 f7 I6 Z# T- A/ Y/ E0 N# t6 y8 V
- void MeLineFollowerArray::setpin(uint8_t pin)
3 a, h3 B+ X$ Q2 D T4 K; P - {
* t& e/ A" J0 @( f - _DataPin = pin;
2 f P# X2 N7 J/ {+ T; Y - pinMode(_DataPin, OUTPUT);
3 h7 B. m5 V: W* @1 }! u - digitalWrite(_DataPin, HIGH);
* y$ @ o4 o6 Z - 2 A( @& a. K& ^. o4 P# A) N+ |- N
- #ifdef ME_PORT_DEFINED! p6 T( A# c4 ?( f( g- u! |
- s2 = pin;
9 }% [% H% a2 } - #endif
' o( D6 U) S/ g; m: ~! u4 U - }
3 q. Y+ b3 l* w* G8 M
+ l# H0 X' r4 U3 T; K- uint8_t MeLineFollowerArray::getValue(), o- z$ t- q' \; p
- {* N6 I W+ m8 O' r2 O! c" Z
- uint32_t LOW_level_read_time;8 u0 l4 M$ W. V
- uint32_t HIGH_level_read_time;
# s3 Y! d# _8 a- U. P6 W1 k - uint32_t time_out_flag;. ^8 S6 _5 f2 C: X, W1 K
- uint8_t Sensor_Data[3];
, W" s* L$ E0 d a - static uint8_t old_data = 0xff;1 ]" Y6 G, w7 b4 j# h5 @
# H1 [6 i& `. Q8 |- pinMode(_DataPin, OUTPUT);/ U7 Q3 O4 E. h- r
- digitalWrite(_DataPin, LOW);
2 Z, a, `. v6 a" e/ F% d' h - delayMicroseconds(980);; `) \( v1 ~) Q# S0 F
- digitalWrite(_DataPin, HIGH);+ v( z2 S8 U1 y0 W
* a* h& j* Q2 t0 W: i: o x- pinMode(_DataPin, INPUT_PULLUP);
' p' \' e3 _# }0 g2 r% X3 k - delayMicroseconds(10);, r: C, \& o% [, k# k' ?
# w: I# Q7 D8 w3 ]3 {- time_out_flag = millis();
; G/ o4 A- z Q7 \+ A - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );( z6 _ P2 c6 e$ M* ^ C
& C# _2 C+ U( V9 b9 k- LOW_level_read_time = micros();
" S; ]) }- X! g; }* I - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
" p! o1 z# C# r" K2 l2 W/ ?4 X - {( L8 S6 A% I& w4 [
- return 0xff;
" Z4 f* x, `* L! `) \ - }0 H- f9 K9 J4 k4 b9 ~: c9 F+ l
- ' E1 O5 G, Z3 t
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
( L+ }2 M& S3 m - " y8 q0 J) e0 {
- HIGH_level_read_time = micros(); t% l2 s) F9 N. u
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
; ?3 |6 C; j* R6 T
j0 R7 D. U, {- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out6 x' |8 h) \7 Z% v, Q( h
- {1 S; v6 n, h7 M& s
- return 0xff;: G! R7 S0 T( t+ D; P/ e
- }, |! c7 G/ |( c6 r2 V
9 ^9 p* Y4 |9 q1 y- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))* U( O+ S! T/ K3 a, s0 E+ e$ V
- {9 Q2 D/ S, f4 `# A1 Q
- return 0xff;* L6 t, f! u$ i7 g! ~& Q
- }
1 M; N2 W. \7 j# K" ^ - + b3 i8 I5 b! G2 k6 G
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );& X4 |( @1 @- ^# o. u
- LOW_level_read_time = micros();
; a' U& ` e9 y" I4 c! K% @8 s - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
- H, f& a" G+ h: ?4 Z
; D% W& w4 D# m4 k2 i" K0 ]- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
! `% m) {- q6 i7 J* D - {
# O, T$ x: X3 p# \7 k9 f! ~/ ? - return 0xff;
% H0 n% ^$ P j' [6 f. ` - }5 g# Z; [6 o" R/ E: l
- 8 ^( k, k" U4 o b5 j
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
$ Y3 r8 E) u( B1 t/ w( a/ s5 e - {
9 K3 [. G4 s1 }4 z% d# T - return 0xff;
; k1 T8 j# e6 T: r$ I/ f - }
' p% U4 N1 S( H4 `* Y- ^7 \! S& J - ! d4 ]0 i& q" r' s8 F3 m: P
- for(uint8_t k=0; k<3; k++)0 P9 C$ }- x5 j2 ?. J
- {
7 }8 C; L: I* X. Y - Sensor_Data[k] = 0x00;
, _7 P9 U- R4 P7 W - 8 } |/ M/ A& _
- for(uint8_t i=0;i<8;i++). ~' [3 A, Q% S& [! V7 K" b' c n
- {/ G9 G" G/ D. I5 y0 R6 |; p' h
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
( F: M% ^% T1 W& ^1 A+ k6 e - HIGH_level_read_time = micros();
$ K2 v6 v' ?) K - LOW_level_read_time = micros() - LOW_level_read_time;
s3 I7 U: K/ P4 y; A( P% O9 C
# b* ]* q7 y. h- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )0 P, {3 V# S4 [; _
- {
9 m7 T! u' n' m/ g - return 0xff;# Q! w V' w j
- }
6 C4 U4 u, @' d9 [# F8 F2 ?8 K$ T
: `1 k6 X7 G8 F! `; T9 Q" e- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );5 O4 |, a8 w5 u# Y% \, c) H
- LOW_level_read_time = micros();+ ~# Y5 u: d5 g w+ ~( s
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level1 E3 Q! ~$ \. B' U% F" P3 s
d* p7 ?9 T/ |, N6 G6 Q, u) v. [- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
6 |2 O% h/ S0 L0 u7 u2 q/ p- V - {4 S' h( u3 E0 ]1 Q; n
- Sensor_Data[k] |= (0x80 >> i);7 U' n1 ?8 V* F+ e' k5 }" e& l5 T
- }$ ? z5 E- Z; \8 g, O
- else if(HIGH_level_read_time >= 100)
, B) ], }! o, v8 A w* Z$ b - {
* K$ O: q4 J# D% n - return 0xff;# Y. r9 f0 g: |! ?& ]$ q
- }
6 _6 @/ G3 `+ w+ h. t - * X/ f) m: P2 U& w
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
2 x0 v- i0 ]# o- Z - {, w% Q1 \8 A) L8 W
- return 0xff;
! S) T6 A4 W$ Y: a+ t# U: @ - }
5 p* y2 w! K6 @$ V S& }2 i7 y2 b6 F - }
, B0 H; Q, @8 W, y5 u1 m8 J - }
r5 \1 ~! L# k! c7 \/ E
" \; D1 {1 W) J/ f# P8 X7 v' U- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level% C) g& F3 X V% j. F5 |- ~7 K0 ^+ c$ e
- HIGH_level_read_time = micros();
9 e$ Y' f# M( u+ b/ k; y4 p' M - LOW_level_read_time = micros() - LOW_level_read_time;
5 s" c2 |3 ?3 V! ^+ g, v
& D. D G# _1 ? P- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
l% A$ W9 ~* ~% e' ^ - {: C. T' B3 x! Y2 _
- return 0xff;
0 u# w' a+ q4 V, E! q1 f7 z7 O - }$ @& `; A/ l) c; R9 K7 F9 i/ _
- 6 o' Z- ]4 `$ N; W& \% M
- pinMode(_DataPin, OUTPUT);$ L) J1 j6 s% S& J2 w7 N: ]
- digitalWrite(_DataPin, HIGH);
; k# k" i5 N2 x3 O - . A0 p# x; P. w# r( s1 B4 [0 ]
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
; b$ A/ V: b, S. N7 L$ @: g - {/ F( q( P1 K T; k3 W
- old_data = Sensor_Data[0];
) E( h# d6 b" n - return Sensor_Data[0];
/ X% T" }5 v1 ~- d - }
3 i- _# X; K5 \ - else
: M% g* H4 v' m2 }5 ~5 x3 e - {
8 K* P* ?8 j+ n; Y5 v - return old_data;! X3 P0 J0 f, d+ W7 q% d$ r5 c( M' T1 w
- }* p J k" J1 M
- }
0 ^8 X1 h' A. ^0 S
複製代碼 & ]6 r) b3 x( J" P8 x' S1 r W
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
7 n4 \% _4 E4 Z! H$ @
7 I8 D$ b0 X7 ^# _% e0 M
|
|