|
|
`. e6 L& s% N* f. y" n
- #include <Arduino.h>0 X1 E4 `3 Q, z/ T& n/ z
- #include <MeAuriga.h>( V1 @4 c# r7 }7 f- u* u
- #include "MeLineFollowerArray.h"* ?" ?. o2 M+ i- C; u6 u
0 [' w5 X. z$ w# m- MeEncoderOnBoard Encoder_1(SLOT1);
$ Q* Y( t3 N# }3 a7 ~ - MeEncoderOnBoard Encoder_2(SLOT2);
1 @9 P* @9 |7 E5 O' _& m' h - MeLightSensor lightsensor_1(12);
) _0 u) @5 F7 l# c - MeLightSensor lightsensor_2(11);* |1 o; D$ v5 `( E
- MeBuzzer buzzer;6 P7 q! C# O4 ?: c: w% F
- MeLineFollowerArray linefollower(PORT_6);
! i0 @8 a+ W% I/ U
: Z0 J% @0 P* ~, L; v8 w8 a9 `. W- #define Error1 15 F; u; O# e5 T" s: t
- #define Error2 27 z6 w1 K- E7 M& g7 }
- #define Error3 3
7 t" b; r9 i% ? d, p6 W - * w# f* c; A; U/ c( c! I
- #define Kp 158 | }& S9 f& v
- #define Ki 0.15
) D9 @. Z( O1 [( q - #define Kd 0.037 L2 |* [' J5 Y9 g9 S0 ^, C; g% U
3 J7 X+ @7 V- k% h: q- uint8_t sData;+ S$ ]& }: q3 ?# y
- uint8_t D1;5 @# G8 [! t: L7 f8 B2 T4 H) C
- uint8_t D2;
7 F, W/ ]+ s$ v3 o& y" U - uint8_t D3;! w3 J0 m- e6 s! n* W3 L
- uint8_t D4;" L" r. X; `% l4 e/ B" ^8 g" g9 y
- uint8_t D5;
3 E* k7 w$ N4 V) ?. I - uint8_t D6;( r1 u3 J- ?% v5 J9 X. W% `
0 G. w9 g8 G. Y- float previous_error = 0;' l: `: b M% ]. |
- float integral = 0; r) }! e+ u0 n
- float derivative = 0;# j" F. j6 O% u) I& k
- int Speed = 160;- t+ @, ~- R- E8 c3 I6 ]+ W Q
- float output;
- u6 V' O( x" r$ W, R7 N6 I6 ^+ N
" F6 N' E: b2 K, e1 n3 H- M% V7 D- byte Left;! f; e( ]/ z6 b$ S4 P
. r7 I& J. @8 R' }! ]4 [- void setup()
* ] m2 i' k V( o! l4 R - {
: G: Z7 S4 [6 F) s/ I - //Set PWM 8KHz
4 b4 i) A1 i; Z - TCCR1A = _BV(WGM10); I- y @0 t& Z
- TCCR1B = _BV(CS11) | _BV(WGM12);
* q& R# E) U* e5 U- ? - TCCR2A = _BV(WGM21) | _BV(WGM20);
+ Z7 B' B3 r7 d7 L) X - TCCR2B = _BV(CS21);
) |: b% q* p& _7 S8 W( C - Serial.begin(9600);
$ s7 q. l3 F B0 v0 X* ?% {2 z- U6 g - buzzer.setpin(45);% i: l: @2 q+ ?0 n( k1 M
- }, Y9 _3 v4 U9 u
1 i. H' r( e) \+ H! ^3 M$ ?' N- void loop()
; ]7 o! I9 N% o5 Y6 V5 c y5 I3 c - {7 r T T( {& B9 z" l
- while(!((lightsensor_1.read()) < (10)));
0 N( B) _- m9 ~* }! |3 A" O4 G - buzzer.tone(1047, 500);% O$ C3 l: H+ i M) n
- delay(500);7 w7 x, p% N' m1 W$ r
- do( c" V! K* A1 [. n* |" z
- {$ t. @3 A! w4 i6 y) t2 F
- int Err = getErr();- X/ b. @$ B7 Y% f8 j' R
- if(D1 == 1): D; I h: W2 b8 l% n; b8 k
- {# o' S6 t5 L9 W* L3 b9 e4 [2 L" F
- Left = 1;
& f: a: ?% r! N; O) A% i - }8 B# Y" ^$ n2 }4 h
- if(D6 == 1)
5 `# _0 u) j( K - {' H4 P5 X) [3 e0 D; I# u9 L
- Left = 0;
' g: L; u' j# {# { T1 R$ q3 X - }/ | p0 K- s2 x0 p
- if(Err == 99)
4 g5 j1 G1 h& i A0 [* y8 y) ` - {
! h# v" E$ H, U( e - if(Left == 1)1 Y9 g8 Y0 {( @8 P$ W% M+ s
- {0 ]0 y# I" w d! _6 q' _$ h; C
- Speed -= 5;7 y, c) t) J& D8 e) x6 [( J7 [3 A; |
- moto(0,Speed);, A! X0 S% T, y7 ^* q
- do
6 F) h* b- T5 w# y2 c8 H - {: H8 o! g2 t2 R1 h3 r: S: d
- Err = getErr();8 p+ n& b& P- c) y2 r
- }while((D1+D6) == 0);
8 H/ A9 V F% y8 v8 U - }
( m# L: |$ Z; M: `5 j - else" }8 w" H0 S) ?9 x: K( Y7 N1 m
- {
. U' `: n5 ~) b' L% Z& [ - Speed -= 5;
3 F& c( h* v- G! d - moto(Speed,0);
7 |* _/ C0 m& O - do7 D) [7 W! R% G7 E
- {
4 D7 r. c6 G8 n6 ~ - Err = getErr();
+ g+ n3 m p8 K - }while((D1+D6) == 0);
' a+ G8 _% p8 O3 g9 E, X( E - }) }% E2 j' l- C! Q! G4 [& X& D
- }
' O e" O9 @$ o; Q) l+ T# g5 e0 `' Y - else5 I$ O$ d& n" W, v, s
- {7 t5 A0 @# m: l% j4 v2 Y: R
- if((Speed < 160) & (Err < 2)) Speed+=1;
7 u6 |& @/ k j- N0 I, I" V - if((Speed > 100) & (Err > 2)) Speed-=2;
: b. u" b5 J% q6 l6 s+ i! q5 J0 f - integral = integral + Err;& E. r e" S- S9 J/ j& V
- derivative = Err - previous_error;+ C |0 J' X, h a$ R6 x: H1 x
- output = Kp*Err + Ki*integral + Kd*derivative;: ^* t1 _* S: O3 \2 @$ v
- moto(int(Speed-output),int(Speed+output));
' T* I8 j" ]7 e7 b - previous_error = Err;
1 A9 }6 d) }# ] H - }
3 T5 w3 u9 g5 s1 F! T2 Z# w - }while(!((lightsensor_2.read()) < (10)));
' I# ?$ ~# _7 _/ t - moto(0,0);
; l$ O1 J$ k% ]3 Q - delay(500);
$ n& q$ n( f7 W - buzzer.tone(262, 500);( x& j" K* i0 }( d# ]2 y
- }# l' x& L+ ^* t5 i( K& q" l
O' f. l# y* i) w" N- int getErr()+ M, G7 U5 |: o, L' v, F( Q ?
- { 9 ~4 b8 H* q2 G o9 E( x Q, U2 t
- sData = linefollower.getValue();" a( G+ q' r; h; ]* S; J. F
- D1 = ~(sData>>0)&1;! q6 V% b1 f9 h! j% B8 i( \
- D2 = ~(sData>>1)&1;
4 _; p$ L/ S$ Y+ y! e - D3 = ~(sData>>2)&1;
2 ^3 q( K. D( I o# s# I - D4 = ~(sData>>3)&1;. z* @. w# O9 @! L
- D5 = ~(sData>>4)&1;* m0 O! N W& }9 H
- D6 = ~(sData>>5)&1;
# R" B5 R2 z- h* p- f4 l, y - int downD = D1+D2+D3+D4+D5+D6;
, N4 i( H$ y/ o* S2 [" u - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);/ Q# k' x" r6 f
- if(downD == 0): G% r* y# v. a" r% {: t
- {$ ~$ @+ k, t7 I( D; S) n/ [
- return 99;
7 e" E4 U( y8 C5 B1 f8 ]8 J - }5 r% c- b( O9 V, i; `
- else% g- ]0 z9 P5 y( a/ B
- {: ?4 e0 D8 G0 J F R
- return upD/downD;
8 T0 F6 ^0 n C$ V3 {- _: n E - }
% C B* x- s, v - }
- J" h3 a0 q7 q k; | - 2 E/ N8 K. e& P/ z' F( t: E! \
- void moto(int Speed_L,int Speed_R)
7 A0 Z4 v7 X( O2 ]' m - {
3 ]( S6 x8 ?% y7 X - Encoder_2.setMotorPwm(Speed_L);
3 S. X) g. f: t; v: d$ ~# E! n - Encoder_1.setMotorPwm(-Speed_R);
+ x+ S" C' o" d k) C8 h - }
複製代碼
; j. C5 d' s7 zMeLineFollowerArray.cpp
) ]# A1 O/ W2 t9 M8 V. u; }* [1 J- #include "MeLineFollowerArray.h"! Z7 I! ?, B9 H- U8 t9 S& ^' V3 E
- " n$ @* B6 M% v; h9 ]* ?4 n
- #ifdef ME_PORT_DEFINED! M0 x. m- {+ e, @0 T
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)) E* d0 K( V' O
- {% f, z8 x& j4 H' N5 P, V+ P1 G& f
- $ _* L' A- l; I: [" O
- }& r, C4 ] q& @. ]* `# c9 U, \
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
0 z3 D, ?; d1 r6 d- C - {! H) |& V& t; P" n6 Z( z, Z
- _DataPin = mePort[port].s2;* f; B: J" M: D' o, R5 ]
- pinMode(_DataPin, OUTPUT); * L& L: J# V' d
- digitalWrite(_DataPin, HIGH);
. }. |" V% }( j* G - }% p: T1 ~* f y! E5 h0 {
- #else // ME_PORT_DEFINED
: b- q7 R& v- k - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin): z# H* N( O2 [
- {9 U* y# h; Z9 V6 Q$ U
- _DataPin = pin;/ j' h* G o; I0 n
- pinMode(_DataPin, OUTPUT);
7 `7 `4 A+ ^; e, G2 b! r( b - digitalWrite(_DataPin, HIGH);3 t9 A; r% p% z* U
- }& H/ W" z* a W t2 a
- #endif // ME_PORT_DEFINED
+ p+ {3 e. M7 Z& e0 Q - 5 K* Z, r% c' U( `
: F2 \$ |! Y3 L1 |) _1 A: o: F- void MeLineFollowerArray::setpin(uint8_t pin)
- Z3 F/ T9 U$ a1 H - {, F/ U! r: \2 j3 v& I/ r
- _DataPin = pin;
( j* q' ]: Q) v5 i6 T - pinMode(_DataPin, OUTPUT);
E) d4 a2 A6 R6 t3 R2 v - digitalWrite(_DataPin, HIGH);& \- d* K. [2 I" Q0 E M6 X% R
+ g: W9 L. i; h- #ifdef ME_PORT_DEFINED2 y' ?7 G5 k) o. O. e7 ^, R
- s2 = pin;& T$ y, j4 R5 G( d' `
- #endif
: l. [! \2 L; Y% h4 I - }3 _7 s ?- r X+ g. F) Q" }
7 f5 |! L; f1 i; G+ H! t# u- uint8_t MeLineFollowerArray::getValue()+ w$ p$ }: d, R& g- n
- {6 y2 w; y4 l" _- {
- uint32_t LOW_level_read_time;
& s7 f: W% _/ a! d" h/ T9 r5 Q - uint32_t HIGH_level_read_time;% I% y) `! e8 w9 N) m- ~% W2 s
- uint32_t time_out_flag;( ]7 x' _0 N% n5 F2 P# V5 {" N# _+ i
- uint8_t Sensor_Data[3];4 s( a# x% g: U! T D
- static uint8_t old_data = 0xff;
+ V; G( e3 m% Q4 V; {" w - - \: K; C6 V3 Q5 t! V+ J! V
- pinMode(_DataPin, OUTPUT);# E' e& _$ ]0 D! T
- digitalWrite(_DataPin, LOW);/ |" k7 ?) J8 _% v2 u6 I
- delayMicroseconds(980);! `5 c3 Y! B+ A, W1 g. i" I
- digitalWrite(_DataPin, HIGH);% h- L, J: T& S- t1 N
- * S$ q/ A! r9 [6 F1 s9 S# T
- pinMode(_DataPin, INPUT_PULLUP);4 Z/ w, k2 r$ X: V8 X5 d
- delayMicroseconds(10);
' C: d! q/ i& O) ~. k, c3 l
& U0 O$ ?& c7 Z# C! b2 M- time_out_flag = millis();2 x: `3 } r$ k, l/ ]
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
* P L/ w, S; Y+ X4 v' b - 1 T2 Y* h. f! ]
- LOW_level_read_time = micros();. P3 d- R6 B7 ]1 K5 k
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
5 D3 E* S$ p* m, t - {
# i0 p1 h( f# I# y6 C! U - return 0xff;
2 [( @& H5 f q( y. t% ^$ C( d - }
0 Y# Y. ~# X. n' }: G9 ^$ Y% j
9 R }5 p: X6 @; `. O d/ S- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
7 N8 C l8 d' \
' |8 q9 K2 v. n* }- y+ S# W- HIGH_level_read_time = micros();+ H" n; P4 @$ V! a3 J
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level- a# g4 c9 W* e. N/ k9 K
$ @4 T+ |& G" \- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out) P0 I! q& F! _
- {
0 W% f r* d1 R7 K. Z' G1 e3 P - return 0xff;
* \' i9 G. G8 a& l3 s! {( X - } l/ k9 Y" L6 W4 i. k
- ! m4 J7 ~5 y& I) n H# J
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
) j- h6 x/ f3 p K - { p6 y: x T" \" c4 C3 T& M+ O+ K
- return 0xff;
3 F, J p9 V M( O! E0 S - }
) h- \" b4 _& N: X- M0 D
/ T2 l8 | H7 b5 ^- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
5 H6 b) I. m$ m; [* b9 s, X7 m - LOW_level_read_time = micros();9 f, l6 m2 d( t& t' v' F3 D
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level: f4 J9 A% S3 { ]! B, g# z
' g; n, v" S' a. m" n- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
7 s. d" Y- `1 V0 D) P8 p - {
j; V, q& e! B( W - return 0xff;5 I' k C0 c, t- k
- }/ k! X( H8 h- d. D7 _) V" R
- 4 M0 |& d, W4 p7 ?" Y
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
3 G. h- D( W8 Y" p - {! C- F$ F& f! P3 K1 r" |# b
- return 0xff;6 y# ^( ]/ H2 P. W! q
- }
- z7 ~' M% |* z0 K* K! z
: `$ u6 h1 [1 l5 G- for(uint8_t k=0; k<3; k++)
/ j& p3 Q- ?1 n. X0 d! e; L0 t - {
. h4 {- M+ m, {# O |) z' j8 E - Sensor_Data[k] = 0x00;
/ ]6 @8 M: _# L. m7 M8 G - & O( P; ^* h- e% F3 X
- for(uint8_t i=0;i<8;i++)
' S7 _ {' ` h - {3 d5 K) r% Z+ X" }. z+ Q* \* ]5 ]
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level" a# W. c* I* N* E" e
- HIGH_level_read_time = micros(); x: X( Q# e- k- r- q* }
- LOW_level_read_time = micros() - LOW_level_read_time;
9 p6 Z6 K3 t& ?4 y/ l2 _! K5 i
. @; P9 c' I" p- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )2 w& e3 C. p+ t/ f: n* R: Y
- {6 T( q* v/ y& e0 O. ]9 N
- return 0xff;
/ }4 |4 T. S. ]; l/ F9 |! y! T - }' S& K, W& F& @ f) I
- . M- g7 p$ j4 c8 c c
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );' b6 z3 O0 M& X# ]
- LOW_level_read_time = micros();( s# l8 I2 D1 m( }. r
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
) g7 ?- `& a* I3 Z K2 O
+ L. y* F& ?4 t* O- D1 f# l- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1 H7 ~" H0 w7 |+ h
- {' z1 c, d( \, [/ G. l6 ]
- Sensor_Data[k] |= (0x80 >> i);
2 n* d# ]4 e" x5 X* j* w - }
. \9 O# i- h! p* G4 f* m. u5 S+ C - else if(HIGH_level_read_time >= 100)2 K( @7 P; t# |4 y+ Z- W
- {: \7 Y+ g+ t5 M0 S0 b) H
- return 0xff;1 K* C3 e5 L$ y! G# a1 f9 m
- }4 t; s$ P4 M) j; t
9 O1 x7 j2 ~+ Y' W& G/ \2 j% m- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
5 f; P' C$ _( R' B) u - {* C7 a8 K+ _ V3 ?. H( B7 ^
- return 0xff;- }& P. `3 _% `9 ], _, [
- }2 N1 o9 i: X6 n& N
- }4 z$ B% ], `$ [7 t
- }: v% {8 N0 A- t; m, ^4 G
- - \$ [# ?0 Q- g# \) A5 c$ `8 P8 c( G
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
% x1 P$ Y8 w% z; q L - HIGH_level_read_time = micros();
) c# A6 }5 O8 v7 A+ F' D6 u - LOW_level_read_time = micros() - LOW_level_read_time;$ X% R4 q, p, r1 [4 T, b2 b \
- ! X$ j& V$ T$ @7 }
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
. D1 o& x7 C0 {( P% O# e i( D3 N - {& U2 U) e8 m- Q) l& }2 U
- return 0xff;
; s) `" L6 n" ]; {) F7 { - }
0 ]4 |- d( W$ \$ j0 X - ) K2 m. a$ z" G M5 e6 s
- pinMode(_DataPin, OUTPUT);
l& h0 T; x; {$ g; d6 H - digitalWrite(_DataPin, HIGH);, f$ e& z3 p1 B
- 2 [) T7 d* }( C# V
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))2 ^7 J* y; W/ }6 Q
- {
) F4 E/ O s$ y! R- o# f - old_data = Sensor_Data[0];, O8 I0 Y: S$ p; |
- return Sensor_Data[0];$ `3 Q6 \& a, Z) W- h. f
- }# @9 e3 U3 g! r
- else
/ G) v6 {* g' H( [! E7 y% s - {- { d2 \7 s! N& _1 f) d" G
- return old_data;& K( `( _# O3 v9 B7 c
- }4 s K; C( Z) ]$ L5 @6 J) B `
- }
7 q2 x1 r! b, Q* h) _1 V& G
複製代碼
$ K9 D2 N/ O! Z. z4 i/ ?$ u/ I1 w7 {7 N+ QMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
, k, T) c* D+ m
5 K# M- k$ b# ^! { |
|