|
|
' D4 Q' w6 O9 J+ z/ f6 y* [6 e
- #include <Arduino.h>4 Z, f* e8 a7 p
- #include <MeAuriga.h>! T% n* N4 j' N) C, n
- #include "MeLineFollowerArray.h", M/ z9 v4 K+ b( _3 W% r/ J8 ~8 C
+ b; C2 _ `4 l. K) Q( u8 ?! y+ S- A- MeEncoderOnBoard Encoder_1(SLOT1);
7 \) h& D, D! t7 t' K% ?. a5 x - MeEncoderOnBoard Encoder_2(SLOT2);
; L! J. I( Z; f6 ?- R/ j - MeLightSensor lightsensor_1(12); d$ L1 {5 F" S4 i3 ~* e q
- MeLightSensor lightsensor_2(11);
9 n: F1 G+ }* o* ^2 f4 ^6 a6 z9 ^ - MeBuzzer buzzer;
0 ?8 ?4 p* q" d1 {9 H6 [- h9 C4 R - MeLineFollowerArray linefollower(PORT_6);
/ i0 E& V1 l$ |$ T! M3 T6 s( Q - 9 F7 m$ V \- N* N- j- j
- #define Error1 1
* G& f+ O+ A! K& p6 \ - #define Error2 2 D& L, p& ?* _% _
- #define Error3 3' C/ e9 g5 q3 P; v
- 7 B6 z9 A- `5 E8 f) s5 J
- #define Kp 15/ `! _2 m5 O! M6 _& w; q" o& F3 Y
- #define Ki 0.15% n- H2 B" i2 l% H$ S
- #define Kd 0.03
- t" g; E7 e/ U; K9 r: {
; H; S+ x! `2 X& h) r- uint8_t sData;
0 Q+ \6 ?6 B1 c3 ] - uint8_t D1;
3 G" z. A4 O5 R7 o# x - uint8_t D2;
9 g P4 ~5 n1 S' X, m( \ - uint8_t D3;" J" \# O- _. Y
- uint8_t D4;; u5 d" a7 j. w9 ?
- uint8_t D5;
3 _7 S: S0 R* V - uint8_t D6;+ ^' |* X5 f% ]5 Z6 h
- : @" @+ B( o, W. l7 h; @
- float previous_error = 0;
$ _- E3 M' Z) d - float integral = 0;
) a2 d- ]4 ?1 \" v, m0 c4 L - float derivative = 0;4 |6 H" x# |7 B, N
- int Speed = 160;& j! R/ z1 ~! }$ x* ]% o6 r, o: f
- float output;
/ b3 m% e, n0 Z3 r - ) _3 j, V5 t8 D x
- byte Left;5 ?0 l& @- A. e" @
) B& ^8 ^; K% M7 w- void setup() ; n1 F* ]7 C9 p
- {( n7 p$ ?# U1 L; e( O- t8 c
- //Set PWM 8KHz" L" L2 z- z' u% x9 T/ ^
- TCCR1A = _BV(WGM10);
% x& ~0 k! {+ ^; [ - TCCR1B = _BV(CS11) | _BV(WGM12);
1 C! r' d" k7 \( e- M - TCCR2A = _BV(WGM21) | _BV(WGM20);
6 t4 ^; c& j2 ~ ]9 W& v9 A' T1 T' E - TCCR2B = _BV(CS21);, y6 m& Z( f( Z# z! u+ b) K
- Serial.begin(9600); G; N" Z" T, v: z$ G
- buzzer.setpin(45);! \6 S; F/ p! Z9 ?
- }
' _9 h6 T7 {6 D" z* w! B! C - + l9 G6 X, a% |5 D: k
- void loop()
4 l% _4 }! m. r# p. r1 Q D - {
6 R- }% u4 p6 J* `9 y- ~ - while(!((lightsensor_1.read()) < (10)));
* Z7 R d/ j) J M - buzzer.tone(1047, 500);9 S5 _+ M! [% u) N/ z
- delay(500);. a. n- c. {& O6 I
- do. O0 Q2 M+ }+ h1 L) w+ r7 N
- {
( [! | x1 x5 d9 `& _ - int Err = getErr();# i$ K5 h a. {4 G3 y- @9 M1 Y
- if(D1 == 1)
, U! f: ?9 f. Q" r( c. s - {1 u7 S, H3 x- }: \; x
- Left = 1;
. W3 k: b5 T% b0 A - }( b/ m+ ~1 U! x6 f7 ]7 q
- if(D6 == 1)
; v$ e0 l# |( C - { `4 w+ I' u3 o( R0 A
- Left = 0;
6 \# O- s. q$ x - }: _2 d; g( i( _" S8 X2 [
- if(Err == 99)
+ R3 S. B n1 n* b9 z1 J* Q - {
# _/ y5 H7 e6 V5 g9 I - if(Left == 1)
: H! N( B# x) ^2 n! G4 j/ B3 T - { ?4 T, g, m- w+ b* k
- Speed -= 5;& u' G+ Z% p, h2 C# r [
- moto(0,Speed);
# J! ~, _+ x t$ I - do
/ l2 `+ D1 [. {% y - {: G2 b0 k' M: U) y+ V$ w
- Err = getErr();' l* H3 s- H$ {
- }while((D1+D6) == 0);
, L- c, R5 D7 ~! w7 ^ - }& g H% z( O/ Z. |' p8 q1 [9 ]0 A9 P
- else b! _/ U% g8 L! M0 h
- {8 w# {( D9 H6 o! {
- Speed -= 5;
$ P& @- n% D# D, {0 A6 q - moto(Speed,0);
: M8 ?% z# u) v. o9 Y - do
9 _; G, ~8 v1 h# J# K - {
`. _! F" G- j- c/ Q3 ? N - Err = getErr();* ^$ g# a n7 b' G
- }while((D1+D6) == 0);1 a; u, m( t4 ]1 c
- }# V& Q. l7 E2 [% z2 j
- }3 U2 Y3 ]( T K( Q( a7 C6 d+ ^" [: b
- else! F \4 m; U: J/ n
- {
8 Y8 t# W% ]7 Y, H% q - if((Speed < 160) & (Err < 2)) Speed+=1; : `9 S; x& J- i; ?% Z4 Q) @" M
- if((Speed > 100) & (Err > 2)) Speed-=2; 8 I" k" I, G) y7 q: q% j7 i
- integral = integral + Err;
: E0 i6 B" h2 {9 W, T- } - derivative = Err - previous_error;7 b' r" l7 F! ~( v5 N* W. D5 j, g5 j
- output = Kp*Err + Ki*integral + Kd*derivative;5 O- ~+ N6 Z, e* Y7 k
- moto(int(Speed-output),int(Speed+output));
1 D# D0 h& ^. C - previous_error = Err;1 ?6 L% V) E/ K" L7 s3 v
- }
5 S" B" g; G9 k) m - }while(!((lightsensor_2.read()) < (10)));+ n/ O) l& `# G7 G
- moto(0,0);
# W* P+ m1 A9 V7 B. M - delay(500);" N- J( `* v. t
- buzzer.tone(262, 500); k; [/ a# T: r2 a
- }
- B3 a5 {0 i, c J, F% ]
6 I, b/ A( { I/ e: |4 ^- int getErr()2 c# R0 R! ?4 ~1 x7 c0 p
- { + F+ o7 o3 x7 d p& C5 S2 G& `
- sData = linefollower.getValue();
* A: P/ k1 ~( m0 n - D1 = ~(sData>>0)&1;
7 x3 \/ R9 d+ R6 b9 G! `5 N - D2 = ~(sData>>1)&1;/ U7 W% n( ~+ y1 c
- D3 = ~(sData>>2)&1;
# k3 F7 b) G) t - D4 = ~(sData>>3)&1;2 y- o9 @ C7 k- D
- D5 = ~(sData>>4)&1;# d5 n2 s5 s: i' x; k
- D6 = ~(sData>>5)&1; J) S7 b/ Y' [
- int downD = D1+D2+D3+D4+D5+D6;
1 V: r0 f v d7 ~0 H% f - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);. b: u% J$ s2 [7 C( d! u% s
- if(downD == 0)" M. m! T1 F6 N) E9 Y2 E6 r
- {
# y/ ]7 S6 v1 J8 Y- i9 t F: T - return 99;, b n5 F- b( i* }* N
- }+ m8 a8 v# v) |* C
- else
: c% x" Q. G5 t - {4 Y2 ]( R' [$ s3 \7 q( `: |
- return upD/downD;
% j8 @" k( ?8 Y: I - }! Z" v( G, ~( m0 B
- }7 M1 d1 ?* M$ S/ H D7 v: R
8 {* f C/ i6 W3 B/ } D- m- void moto(int Speed_L,int Speed_R)# u0 R/ F& I6 S" A: g5 i
- {% ~* M! K- Y* ^- i
- Encoder_2.setMotorPwm(Speed_L);+ b. }7 K0 R! p0 T+ d
- Encoder_1.setMotorPwm(-Speed_R);/ `4 k; h# A/ Z2 P2 w
- }
複製代碼 ) ~% o2 D& c8 C2 I
MeLineFollowerArray.cpp
2 e# c5 {5 @3 o' _- #include "MeLineFollowerArray.h", f& k8 v; d" [% L4 X. S6 W, i! p! U* s
8 W, `9 e; h j! ~$ k- #ifdef ME_PORT_DEFINED
( m/ v4 w; x$ k3 {1 ]3 C - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
! Z$ |: O4 Y( w7 r - { f, v! Z5 V2 ^& K" _, m% Y8 k; g
+ {# R# [) ~! Z5 B9 m- }5 p1 {* n% U! N/ W) L
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
: r: z, P! ]8 k - {4 U$ T: w9 l( z0 N0 X
- _DataPin = mePort[port].s2;0 I, h( `1 w) I) u+ o3 T
- pinMode(_DataPin, OUTPUT); % [+ C8 v0 S* v% ~1 U5 |! G
- digitalWrite(_DataPin, HIGH);
/ a6 |3 ~% F; E3 d0 U3 o; ? - }
. D. T6 E& |, {0 `8 o# t6 @ - #else // ME_PORT_DEFINED# R' e0 x6 a% c* L# \. S. h3 y5 Q
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
6 P5 U$ f' @- v - {- t7 |" q# M; Z1 P) X3 c5 f# {
- _DataPin = pin;
; Y# H% r5 p/ n n4 L6 r - pinMode(_DataPin, OUTPUT); " E R$ e0 w0 v* _8 W) G* O
- digitalWrite(_DataPin, HIGH);
: y) T) O. [ C1 M# v - }
& g2 Q; q @5 h - #endif // ME_PORT_DEFINED- }& l0 r$ \. Z$ s' x; D- a$ L
7 k! [! Q K" s" j, ^+ ?- 0 d) L# o& `1 `
- void MeLineFollowerArray::setpin(uint8_t pin): D) B" O. m. x1 h" t1 R
- {
# | V D7 M% z4 I1 n - _DataPin = pin;
! o$ i! { d) x/ _& H3 p! v. ] - pinMode(_DataPin, OUTPUT);
' h& E' Y4 \* d" g' {4 Y - digitalWrite(_DataPin, HIGH);
9 f/ E+ ?4 [" b& ]. Y2 U - / U$ R1 A. S$ Q; i5 F
- #ifdef ME_PORT_DEFINED
* U5 L" }, n( A% F1 T5 ` - s2 = pin; t% ?& o7 u* A) @ c
- #endif
1 s5 V1 G6 N2 o+ V% W2 E - }
7 L- v( K; e* A' I
# H! R9 I: v: A) a! n- uint8_t MeLineFollowerArray::getValue()3 v6 j7 |- B! t$ e
- {
* ^3 d+ z( h6 K6 j - uint32_t LOW_level_read_time;
/ J3 P% S0 {+ E8 q( u/ ?9 L - uint32_t HIGH_level_read_time;
* n, a5 z6 R2 d1 ~$ ]5 Q - uint32_t time_out_flag;
& _ w& ^) E. [- s2 Y- s - uint8_t Sensor_Data[3];
( W4 x$ s/ p$ K2 U/ | - static uint8_t old_data = 0xff;
$ B) y! l+ M. \& L7 ]" ~
5 n$ K( H, s* w- pinMode(_DataPin, OUTPUT);: P; |. j6 M6 \( Y) s" b( d5 `$ G
- digitalWrite(_DataPin, LOW);) t1 W. X7 Y1 a* X
- delayMicroseconds(980);% [( n' A, U- B% h" Q* w0 J
- digitalWrite(_DataPin, HIGH);: I" P5 V" B/ c1 p+ y
* T0 J2 B% @: N% n2 g' t3 D- pinMode(_DataPin, INPUT_PULLUP); |- ~; Z+ K0 i5 m, ~! {
- delayMicroseconds(10);
9 x! O) |6 w7 ^! T# p) N
5 [6 O ?9 B% G+ w& ]- time_out_flag = millis();
1 ?; D [: x; q - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );5 b" e+ @5 h6 h. z5 l5 q/ B
, k( h; f1 i$ j- LOW_level_read_time = micros();
8 e, }0 Q! _/ A( ^ - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
8 J2 [* o; c1 {( E - {
% x+ b9 e: b8 N8 W/ h2 j6 G - return 0xff;6 ^6 C/ U2 w' w" h* B3 Y
- }
- D1 j z1 }# s' V- Z
5 |. H9 N7 K8 d! F% w* B8 q- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );6 @+ \- G8 L: g% Z
/ j4 [) l# B, t1 y( q9 y- HIGH_level_read_time = micros();
; @( ^. R8 S. b7 @ - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level \% e$ _% K0 X7 r% i4 D" H U' E: V
- . B( D& O, D( L/ ~* j6 Z/ E
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out9 ], X7 N0 y$ G3 T) E
- {' G( ` ]! I. _/ b) Q9 i
- return 0xff;
1 m' C3 e5 L# |7 y2 g+ l7 }( E - }/ a$ D, U% ?5 [, P" q" q
# j; j) v9 @ T- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
0 \! _5 e4 @" j- Y% m - {
! N: e$ t4 d d* l - return 0xff;8 Z9 C; A, t' |- l5 r0 q; y
- }6 S: s4 H* s) Q% R' [/ e n
; t' B6 a1 a B. T! ~, B' Z- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
/ W' y p' x0 q# l8 W - LOW_level_read_time = micros();1 h8 v; g; B( s
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level& l' j5 j- G* t& [
- : n0 T$ Q. `1 N6 K- b8 k! D
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
0 w$ {% M; Q) x5 @& W8 ?3 ? - {
# ~2 S. S6 G7 ~# i - return 0xff;9 U5 M4 c4 _) l
- }% M. \+ f" ^( B1 E, d" V
- k' ]" W( T1 a
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))% ]4 ~2 ~9 o2 v& H6 f3 r
- {
( a# p: q4 P! T( v+ n+ u, @ - return 0xff;' {: i' A8 O7 h0 t1 K+ A
- }( I6 v5 u! V2 y7 O) |5 m/ E. f
- 6 P; H# q4 i2 N# c8 C( g2 `
- for(uint8_t k=0; k<3; k++)8 r/ D# L/ Z. H$ j$ e% s/ X0 s
- {
2 V8 [! X& L9 Q$ y$ B/ m; N" k C- x - Sensor_Data[k] = 0x00;
. b1 x |" |7 }
# d9 D: h4 Z/ c! ?. q# ?( u- for(uint8_t i=0;i<8;i++)0 S, P2 l# v2 \ c3 C
- {/ Z7 d8 l6 j+ e' j; ?9 j4 R2 k% l
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
8 Z. _+ k F7 g& K; k9 c4 [, l - HIGH_level_read_time = micros();- I. e$ f! b% t
- LOW_level_read_time = micros() - LOW_level_read_time;
: j2 i+ [* a6 s3 G/ a& n+ d - 0 s2 c: H; w" {0 g8 Q6 g
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )8 f; N @7 s/ [. I
- {
) U; c" k+ I' p, [0 s! u, l - return 0xff;' x- h, N: M. W# D/ q' @1 B
- }
5 F, J' M, C1 X+ U+ V: D s* S - ) }: i. U0 \1 Z2 D( e
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );( e) g. G; \# _$ y. A
- LOW_level_read_time = micros();
& a- M' O% e, I8 P5 T - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
: y/ H2 I0 j$ I; s: o" X2 X - 4 d5 Z3 f8 ^+ T/ k
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 19 `$ U. |% p' q/ A- t: H: b) e( M
- {8 m/ |8 v. l f% T/ k- A
- Sensor_Data[k] |= (0x80 >> i);9 {! ^! }* f% R. x
- }
) G3 I$ T' ~" m: M& `4 X - else if(HIGH_level_read_time >= 100)/ z& o6 ]4 C+ M% ^! Q, i
- {6 i' d0 f) K- Y# c l0 N
- return 0xff;3 m2 ^- G. b _6 u( g& G
- }
+ x9 o0 ^( l- C9 m7 U8 {6 B
* r: d( {! ?9 ]# Z$ |- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
' @- X* l+ r R: v7 n - {
/ @! P! w4 F. |, K" r: r2 R' k# z - return 0xff;& O7 x+ f6 x$ L" c
- }! Y$ M$ Y6 E4 q8 ]: x- e4 U3 ^
- }- p2 Y; r. a* @% E' p {
- }
4 w" x* f/ t) Y7 E( J
u" o: j# f. d- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
7 h4 E5 P' R# B% B b - HIGH_level_read_time = micros();
1 y7 S5 O$ \. h6 C7 l: I9 H5 x - LOW_level_read_time = micros() - LOW_level_read_time;8 N9 ~" Z- h- s2 @1 J; z! p
9 i: I$ I# d9 N- Q4 d- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
6 w4 @5 v$ C3 N4 L' @9 r" Y+ i - {
) b" |: Z: k0 E+ C: D3 F1 n0 r1 g - return 0xff;
% D) z, }! a4 t5 Z. T8 R - }
& C/ e- ]- }' t9 N/ O
5 Y$ \' m, a6 A! |+ I- pinMode(_DataPin, OUTPUT);
3 v% c. O& g% r - digitalWrite(_DataPin, HIGH);
( E$ L' j8 x+ X4 @
. f" h( p, n" B2 z- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))6 q7 M0 C- s3 a$ {5 e
- {
' n/ J/ ]. V- e$ u; d$ F - old_data = Sensor_Data[0];9 M, ]8 [3 Y. U* M
- return Sensor_Data[0];" _4 G" m# y8 c6 |4 W+ d
- }3 ? h5 R7 S$ @
- else" _, {. K0 C. p0 }
- {
$ N+ H% `9 D3 v4 y r - return old_data;1 L( ?' E- _$ b* c' S
- }
& d: n1 K% G! j$ ? - }
% s4 m5 U: K. Z2 g2 E! _
複製代碼
" p6 W6 \# I5 y5 JMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
! K( X( n0 t" l% p$ u# O0 o$ r3 c& E$ _+ y
|
|