|
|
7 t1 L0 L) n! H; R, w4 C6 S- #include <Arduino.h>
0 V+ M" G1 h2 ]$ P- Q - #include <MeAuriga.h>; X+ n- O/ A5 C" o
- #include "MeLineFollowerArray.h"
( o+ t% m {2 A3 ^, J0 \
/ Z6 o- O7 P! G( `( C- B- MeEncoderOnBoard Encoder_1(SLOT1);
3 |6 E4 z& |- L - MeEncoderOnBoard Encoder_2(SLOT2);
9 u1 X4 t( s2 k1 u+ } - MeLightSensor lightsensor_1(12);
; {1 \$ q3 G: z% E - MeLightSensor lightsensor_2(11);
" k: G3 _- f2 h7 X }( R - MeBuzzer buzzer;, `, h$ R# `# E
- MeLineFollowerArray linefollower(PORT_6);
) g9 `$ \0 {, Y/ a! N8 _- M( a - & O( P8 s4 I2 B; L- g0 }
- #define Error1 14 n$ `1 K# S: n; k
- #define Error2 2
3 ]$ m1 L, C3 \ - #define Error3 3
# b1 Y+ b" ^; I7 P L; v3 \
d- a0 @- V3 P0 A Z- #define Kp 15
) S$ M. I5 q7 j- m - #define Ki 0.15, H, b% J% R# \& c Z* T
- #define Kd 0.03) Z, b' a3 R7 c6 U0 ?1 U' w
+ N( N3 N. }* y$ X- uint8_t sData;
9 X) [6 }2 M y; w+ C" U; k - uint8_t D1;0 P+ O+ T+ ~5 E
- uint8_t D2;4 g' b9 W' b5 f3 d! u! S
- uint8_t D3;
/ _$ @% \! d2 v+ m) D7 K - uint8_t D4;
" Q" p. z' @- r3 d3 @1 ~ - uint8_t D5;
4 h6 ^- O+ B3 A5 C( p - uint8_t D6;
- V0 r& g; h/ t! W: n - # w! q2 K" _- t- q) g. r
- float previous_error = 0;
+ D! F; ?0 S, F+ X) j, n - float integral = 0;
$ Y% u( t1 q$ |" C7 d) Z7 A8 Q - float derivative = 0;( z9 d1 I! h/ l* _0 o
- int Speed = 160;
; u2 @3 Z9 S: C! {. G - float output;: X+ w8 ]# h0 e) r* [/ Q
- . V$ _7 g( Y- f. I. x5 c7 j$ |; \
- byte Left;6 _, w7 j- m. F' L
8 ~2 O5 N: g% k. l- void setup() / H7 @, \* {% F4 j( b
- {. p1 R: P0 p6 C/ \# K- v
- //Set PWM 8KHz
6 N% D1 Q- ^: @$ c - TCCR1A = _BV(WGM10);
o Q: P( y" Y6 w! M7 l2 }: _ - TCCR1B = _BV(CS11) | _BV(WGM12);
' c3 x }& l: c9 S+ w$ _ - TCCR2A = _BV(WGM21) | _BV(WGM20);. W7 c& V/ S" G* X% s* d$ |8 I
- TCCR2B = _BV(CS21);% k0 A; d: d' R& N
- Serial.begin(9600);
6 A6 B$ L n# Z; n# S8 A+ S* B" U - buzzer.setpin(45);7 X0 A/ E7 U1 t* i& ^
- }
( S3 J! u# T. V+ H) ~" ]; u; O
. M- ]6 a2 g" m- void loop()
( w; O& i: R+ P0 B9 p5 K: @ - {4 j% ^2 I: D9 R) C0 H6 E% l' C6 `
- while(!((lightsensor_1.read()) < (10)));8 s/ R/ H0 w( M4 X2 t
- buzzer.tone(1047, 500);
9 P; c: M3 [/ d: K8 D# z - delay(500);& A, z( r, S- s2 o, ]$ c9 T# ]
- do
: |0 a! T, s# A9 i - {
, C: n/ R/ V; N9 Q' b1 w$ H - int Err = getErr();; L1 a0 H$ z6 D& J# l
- if(D1 == 1)8 A4 K# w& `* u6 @& [
- {
+ L; t! P5 n2 T9 b5 B! ] - Left = 1;( A1 T9 E% x+ q9 s2 r6 c, [! |' U
- }6 @( y, L& J' E. S# {
- if(D6 == 1)
" A7 Z5 ]* i% X0 L1 p9 Y. x - {, U G3 q& w9 {& N
- Left = 0;6 ?8 H# |* \+ [* L; K+ g
- }* x% s5 ^ ~; T: M& [' p
- if(Err == 99): r' x5 ]3 |3 g4 `
- {
/ G- T0 j) m' ] U' C+ w+ f6 R - if(Left == 1)9 d5 _/ s. x( ~. F+ b
- {5 G/ E& a _/ g f
- Speed -= 5;: U3 X$ z/ p: t S3 T0 @) [- G
- moto(0,Speed);
' z) r) H- H8 U, F6 J" s! }7 @ - do
) s% z" J3 s- z - {
0 w O1 S9 ~: }7 [- W( t - Err = getErr();
6 u8 H" K' q9 W- y5 k - }while((D1+D6) == 0);
; }; o: i8 [+ _- j - }( n, k+ m0 M0 ` k) a; b; G5 E/ E
- else
' B; Z# X! \5 R& F5 w/ M* G W& X4 @ - {
5 N3 l! U j9 i - Speed -= 5;
4 Z ]3 r! S7 _' I+ L: _1 V! v - moto(Speed,0);
& x4 Z, o/ n3 A& u - do- w4 z* @0 {4 h& c9 `0 ^- a
- {* T, _/ j/ I: C$ l3 C
- Err = getErr();4 O' \1 R7 J4 U/ x/ F) s- c2 w7 S
- }while((D1+D6) == 0);: c6 c; S N, ?7 N
- }% i( p% c# C Z a/ \. B, T
- }
+ m- `& g0 l K9 {4 t - else3 ^6 G# ^) i, b6 z9 k ` N Y
- {1 S" G5 Z5 M; ?* X$ t7 A5 G
- if((Speed < 160) & (Err < 2)) Speed+=1;
- M _4 |5 Q" |$ X* u - if((Speed > 100) & (Err > 2)) Speed-=2;
' V! p% ?% W S! I - integral = integral + Err;
4 Z8 F1 J: s9 g& I) \ - derivative = Err - previous_error;, c- Y" W( q( n! z8 W0 e# u
- output = Kp*Err + Ki*integral + Kd*derivative;( L* c$ J6 e% _. Z0 M6 V
- moto(int(Speed-output),int(Speed+output));
1 o6 d# m; E6 ?8 c( m- v. H - previous_error = Err;1 v7 S* q+ }% D& e8 p
- }) {8 `8 M% R* Y% j4 h
- }while(!((lightsensor_2.read()) < (10)));
! y( V" A' y- P& F* f+ k* M - moto(0,0);0 {* n( t( {3 c! e$ ~! l
- delay(500);
4 v4 y9 m- w$ Z1 t% O4 F( @ - buzzer.tone(262, 500);
% }: q0 w( c# | - }: e* v0 G3 y* z) D" c
1 p( ], I3 C( c& G5 Q; p- int getErr()4 ^0 S6 z# b& y: r* P6 D) u8 n
- { $ f- r& \1 E; C; K9 r. C/ k
- sData = linefollower.getValue();6 j0 Y9 C. x" v$ S9 y# X+ b M
- D1 = ~(sData>>0)&1;; S4 w# J6 E6 \
- D2 = ~(sData>>1)&1;
# n3 B; u$ J# Y. ~9 _: T0 a% { - D3 = ~(sData>>2)&1;
- z/ G$ i8 g& _ - D4 = ~(sData>>3)&1;
! k( z4 Y V& {5 |3 p! x& H. S - D5 = ~(sData>>4)&1;0 C- M) U* ^- K" |+ ^; z8 l, s
- D6 = ~(sData>>5)&1;
. ?! X% @ P( R3 M* ^ - int downD = D1+D2+D3+D4+D5+D6;: u/ |. O+ d0 `" K9 Z: `
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);' D) [! m; v* d2 w
- if(downD == 0)1 o$ O$ f8 F7 W8 `- u$ K
- {
4 ]) v- f/ [2 P3 S! k$ T - return 99;" X8 a. d! O0 I1 j7 Q3 E H- S
- }6 {& e0 R% C/ j* S/ h4 x, g
- else
: w% k V0 M V - {
3 E7 H4 c1 }- k. e - return upD/downD;
0 Q: G9 m( F" q2 l - }
6 G, O8 `: C3 p2 n% Z3 { - }
$ q% g* u0 ?2 Y8 z9 \& k
- L ] Y! A- F- void moto(int Speed_L,int Speed_R)
" V: d2 }' J4 k6 O1 m2 z4 N - {& h f3 R* J& p1 x1 S& W/ }
- Encoder_2.setMotorPwm(Speed_L);/ U8 }- D$ R; q# m9 J ?3 P' E
- Encoder_1.setMotorPwm(-Speed_R);- X; @: Z' i- _, z8 F( G0 h" S( X
- }
複製代碼 9 q9 s+ Y, Z/ D7 ^: ^0 Z2 n2 i
MeLineFollowerArray.cpp' f( w, W+ T4 a$ j
- #include "MeLineFollowerArray.h"% ?% Q; d7 S ?7 G- `) y
- / Q/ E. f* p* i$ f
- #ifdef ME_PORT_DEFINED
: f/ K, b5 p1 {9 V" s$ p - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)2 L( D' I& Y k+ T; B! V
- {
0 h+ h2 k& o9 b; H' N7 j - 7 b/ I$ @! X6 @" U- [ Q$ i: ?# }
- }
6 k/ q D3 N# A4 I) t - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
- i, G2 k: a1 S; p3 X4 b - {) O O! Q H/ w$ Y8 `
- _DataPin = mePort[port].s2;9 u: x% `& x, o% p( ~
- pinMode(_DataPin, OUTPUT);
2 o4 _0 j6 r( v! b - digitalWrite(_DataPin, HIGH);
; t, V. Q$ u) a8 H3 e - }
% ?3 N E7 u$ J8 I - #else // ME_PORT_DEFINED3 J* h% V4 U3 k* K) H
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)8 n- F% ~" F6 o5 w+ n
- {
& E6 ~" Y" \: E# ^) y - _DataPin = pin;
; u* s) A/ U. J/ K3 P L" p - pinMode(_DataPin, OUTPUT);
: L. R9 f. h/ V: J - digitalWrite(_DataPin, HIGH);
! \) U" F' c. G8 l. f - }
/ m* o8 ]( K7 Q, T' @ - #endif // ME_PORT_DEFINED
3 } P E- U7 y( I - 0 H: D% C- p% B. U) e* m0 m
, h! T5 m" u6 e7 t+ i, r- void MeLineFollowerArray::setpin(uint8_t pin)# Q4 ~: |( @8 K+ B8 Z; b
- {0 b5 r. ~$ K9 |) D1 m5 l
- _DataPin = pin;/ Z( }: _/ x. G' ]; _/ d" ^7 b4 |
- pinMode(_DataPin, OUTPUT); ) H* i: l: K" U/ o0 }0 o5 \
- digitalWrite(_DataPin, HIGH);
- i( s) f& i" c! P: { - 1 R/ | T; Y# \* h9 m/ S3 w
- #ifdef ME_PORT_DEFINED
1 G8 Z1 x5 D9 p$ Z - s2 = pin;
- E' I) Q* R }! H6 k - #endif
2 B; F5 Y8 P& V% e% z - }
K/ s% c2 T- l5 f8 q. ]# X - ; J; v" C6 q, N8 r4 e
- uint8_t MeLineFollowerArray::getValue()
$ b1 S4 G& T, q8 i# F M - {
& f9 \" m" C; T; M - uint32_t LOW_level_read_time;
. n- k; U- e: D, N - uint32_t HIGH_level_read_time;
. W& Y G3 H, M - uint32_t time_out_flag;0 V! h; w) i, ~8 a; h/ p9 W/ C8 S
- uint8_t Sensor_Data[3];
/ X& i4 R; y9 h* G' a' }2 { - static uint8_t old_data = 0xff;
6 A5 C0 K; i. _1 X/ a/ N+ b - * @" G. M; J& S/ U% s
- pinMode(_DataPin, OUTPUT);/ R: Y D- n9 i
- digitalWrite(_DataPin, LOW);/ b- q3 {4 b* {, b3 Z$ F, z9 }
- delayMicroseconds(980);6 j% s3 N% ?' E( X0 {. p7 i6 m
- digitalWrite(_DataPin, HIGH);
- W* d$ J( Y. a; c - ! j$ }8 `0 g5 Y- w2 C
- pinMode(_DataPin, INPUT_PULLUP);0 P. h% C, y9 G+ X5 r
- delayMicroseconds(10);
7 U5 ?/ X( ^- K& A8 U/ |4 v2 B - " P5 s3 J, g4 M. a, p
- time_out_flag = millis();
* l" {- Y: p# W( W. G, Y - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );) }, W* v1 V; j! r
- % z( _! p( ]" }" `! o% m9 y, p
- LOW_level_read_time = micros();
2 ]6 d) p; j" O - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
6 k( D }! {+ h0 E+ j& V - {
$ U' a& f" W% ^, O/ A. m - return 0xff;
& Y: a, [0 o1 ^' I8 Y& | - }
3 O' ]! Y7 F8 s ^- Z7 j
* `1 O7 b3 }! W" K W7 H2 g0 g. o- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );' b n! M( {( w1 U; t/ [+ q2 O
- 6 G6 E/ H( k3 b0 ~$ G& y5 [& z4 n
- HIGH_level_read_time = micros();1 C- H f) h3 Y* ?+ p3 ]$ |2 ~0 I, O
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
4 }7 X0 l4 B' j! @% _
; _" `$ E# \. [2 p' U" `- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out; A. a% ^1 I. Q! q& J% r- {6 M
- {7 ~$ m" A& o& v6 {, |
- return 0xff;% f: m5 ~' F4 k* ]
- }
4 n Y8 W6 g" J0 A( }) |& D8 C - 4 H/ X; }! q8 @- j. x
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
* p' H- c4 G6 _* d$ o - {
. P$ ~2 R( ?3 X) r8 ^4 Y6 b Q - return 0xff;8 X( Q* ?' ^% O+ d
- }7 c7 i1 S& g1 L! M
- + q* c2 s# y( f2 t' m
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );2 P9 q9 p) S, K1 R8 q
- LOW_level_read_time = micros();
L# |1 h- y: `* o( H - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
/ i6 o5 A, r& L7 e - 3 c( p. |( C; F1 y* r
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
& m1 t7 s8 g& s e# z6 W! J - {9 n8 a9 x! E3 D/ ?3 M. ?, W3 u* ]/ ~
- return 0xff;
2 Q, }# _6 v% e+ k/ O) V: s$ i& ? - }! }6 h6 l% L! {' U" a
- $ l3 ]5 t2 \2 V
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
T# d0 Z4 s1 Y2 W/ l - {1 y6 P! l4 {8 n/ r3 w0 X% c0 l
- return 0xff;
5 b) p5 u0 F/ w' M/ Y% Y - }& A# q, ^0 F5 Q$ \) I+ S* a# l
- 1 Y8 S9 Z# c6 P7 `/ Q, w
- for(uint8_t k=0; k<3; k++)
" r j( O5 A3 ]+ ~4 H - {
' [( ? Z8 @4 G$ J: s7 e0 k - Sensor_Data[k] = 0x00;
7 L% j) J& B3 X4 h - 4 j2 l" p$ {) r1 p% B( a. ^
- for(uint8_t i=0;i<8;i++)
5 n$ `6 W% W- X8 C - {
6 Z. L d7 ]3 E - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level( v* T P) r2 j& H0 s8 f& Q1 D2 E
- HIGH_level_read_time = micros();1 `/ d v1 f$ |9 ~4 V, ?5 o3 W
- LOW_level_read_time = micros() - LOW_level_read_time;
/ |6 Z( G) R. O6 t- ^% J. i: l
8 }9 c9 a; p8 G2 S1 H& J( K- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
: k5 e! `2 g. I! Y7 r3 \ - {
* A' b+ }8 \2 K1 S `# f9 [/ x - return 0xff;
' L; g1 h# U' q1 c, }1 t - }
# s1 s" f3 b1 w# V
% x# o. L+ _8 ?# R; V- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );, H Z2 H* s3 [4 o- z( L9 o
- LOW_level_read_time = micros();( G. u+ X. U/ ~3 V
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level) x3 G1 |% N$ n& ^! Q7 l8 m
- ' _: @3 ` \. H
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1( \$ b6 W0 P% J5 e e; }* W3 G( [
- {
2 S9 Q# P5 v# [. p5 U9 g+ E$ P - Sensor_Data[k] |= (0x80 >> i);
+ Q8 z. x1 J, L& d$ u - }0 I: y" s8 p; u' J
- else if(HIGH_level_read_time >= 100)
" Q9 l$ q. X* q" T* g( u - {9 F6 D% l1 {& m7 {, n9 v
- return 0xff;
- C8 _. f. y3 y6 E+ o - }- Q; {$ B7 N$ k$ q. G: ]
% b' N% l! c; h- I9 e$ M- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
" [9 r# p8 [( s1 C% O: g6 r - {
, K' |$ E/ G9 \1 F3 }0 R - return 0xff;
8 b" \4 l$ a/ R0 V) b - }
& J& z4 h- d- b6 g9 d4 `- \7 l5 S8 P - }" ^3 L( R9 r. _# J9 X% q2 E
- }
( g0 [ C, ^6 j& e* M* _! a
6 R# d: u' ]; c. M V# n- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level/ y% c, m+ O3 _9 O0 q# U# \4 h+ v
- HIGH_level_read_time = micros(); W& w4 y0 u2 m3 D
- LOW_level_read_time = micros() - LOW_level_read_time;
$ Y8 Q& M' W# I, x3 g" _
7 `4 [7 S: x- b- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )* _7 P1 ~$ Q3 C+ f! o9 r
- {7 X! u# ]: ] T- K( a, ]) O2 U8 R
- return 0xff;9 g% z' |; @3 E$ ^& r
- }
1 Q; k4 s5 T+ D) N# v! S
* M( F& s; f1 ?4 }- pinMode(_DataPin, OUTPUT);2 }0 S, R/ \; b1 D" P& @: X4 I
- digitalWrite(_DataPin, HIGH);2 k( ?1 l; }" \! t, l
" W A. ]3 Z" L- W- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))8 f: W: l: ~4 D/ [: }6 \
- {
4 {9 u S4 S' S6 F7 a3 L3 S - old_data = Sensor_Data[0];
2 a( N1 O9 ]; `8 J. ?( I - return Sensor_Data[0];
& d, P" F: n* t0 O, a W& ` - }
8 G# ~7 R: R9 i - else; g+ S3 c: _) A. J' o
- {
* h) Y% C# }, c# o0 \! B8 [ - return old_data;
$ y+ O, D: J0 U0 x2 h9 v; U - }
/ W# x6 b" p4 k( N5 u - }
9 ~# v8 l! Z5 p3 v+ W! G
複製代碼 9 k* x( ^' F2 x6 E; ?, V
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
, z! d( ?5 p$ {/ w7 v4 N3 a2 T8 U4 z0 [! n% t1 B- R+ D0 |' g
|
|