|
|
% C$ c V, i) k/ V
- #include <Arduino.h>
8 |% c! a" t; s6 a( ?; z6 @) J - #include <MeAuriga.h>9 C# ]) C. O$ l# J6 ?, D
- #include "MeLineFollowerArray.h": a+ v. o) q& b: h! s% f/ @
- 2 G, s) M* ]/ i" _4 o, r
- MeEncoderOnBoard Encoder_1(SLOT1);
. N) N" P$ A8 _8 k/ W! G+ _ - MeEncoderOnBoard Encoder_2(SLOT2);4 h3 S2 I# N6 \$ b" t& I q
- MeLightSensor lightsensor_1(12);
/ P& D' A6 Y. k. U" _2 b" F - MeLightSensor lightsensor_2(11);
2 Q j8 `1 m/ \& L& Y7 J1 B9 a2 R - MeBuzzer buzzer;3 S( I B ^( M9 K+ h+ a
- MeLineFollowerArray linefollower(PORT_6);
! ]2 b0 k1 V" s: }0 L$ s G# C0 j
1 u* R O: x- Y+ f- #define Error1 1( f2 h# |( p3 g8 C& N% h
- #define Error2 2
0 w. Y$ o; I8 _2 B3 Y* u3 {8 r* d; g - #define Error3 3 B0 D! {- r; |. [) q7 }& P
& U* D1 U; `. P1 T- #define Kp 15. s/ _. \7 h; k+ R" m) j3 g3 L/ g
- #define Ki 0.15- n* a' G3 n7 D( c
- #define Kd 0.03
/ o6 W6 @! N( O& ~3 P; D) s
: j4 w% f' y5 j) a" u9 H- uint8_t sData;4 S0 c8 N" B% {5 b$ B
- uint8_t D1;
+ _ J' v% o# r0 f2 q3 u - uint8_t D2;. ]4 m6 r% f( {# u$ D0 a
- uint8_t D3;- Z2 H6 B) A f$ r8 ^
- uint8_t D4;
# d$ _8 N. Y' o) V$ h q - uint8_t D5;1 D/ Y( M" R- \5 d8 y% e2 P
- uint8_t D6;6 ~7 Z* ^$ Y0 D8 }
1 q% B2 z. f$ T% G/ V9 k O+ c8 B- k6 C- float previous_error = 0;
: v* O/ j# _" B; C3 T - float integral = 0;3 u+ r: V- Z; V# E. ] M l
- float derivative = 0;
; H. D" I6 u& M4 u* B+ N4 r* r9 w - int Speed = 160;, O5 q/ t( e* a& Q
- float output;+ H% ^6 x3 h3 h9 ]8 v( q+ }
- 5 r- l+ c" e1 {0 \" u0 y' R- `
- byte Left; J1 g" K. E5 L7 T/ k
- ( g4 \: s# H: ]' `$ E( {' ~1 N
- void setup() # g( u* A, ^9 B' c8 |* \
- {# t, [& d' C% [1 Q
- //Set PWM 8KHz) P6 n$ V, `) _" e5 l& S( q
- TCCR1A = _BV(WGM10);1 p' i# |6 W& t8 Z3 p
- TCCR1B = _BV(CS11) | _BV(WGM12);
7 f2 K8 i% y6 ]: m! ? c+ V9 w - TCCR2A = _BV(WGM21) | _BV(WGM20);0 x6 K. U+ T+ d' O. |* b
- TCCR2B = _BV(CS21);# o; _# n; L4 i
- Serial.begin(9600);
; U9 ]2 I) i' W% ~ - buzzer.setpin(45);
n' u! n/ |! F5 Y7 Y - }" F+ i4 R+ I/ Z5 I, z/ g3 [
- & d3 |' \! i f' a: g
- void loop()- Y; T# Y3 ~5 f$ F) I/ K N
- {
! I! @+ m! u3 v% D, K/ R% H. j* F - while(!((lightsensor_1.read()) < (10)));
% k. p# c7 p2 B4 u/ H7 N8 L0 |+ o2 A - buzzer.tone(1047, 500);
2 K6 u7 p) z3 U; v2 z2 ?$ b - delay(500);) M2 L$ ^, s: p% m" j
- do
" z$ i$ a7 u/ n' L5 b - {! j! C& c4 v3 O& y7 \+ V6 x9 d4 @ W
- int Err = getErr();/ ?0 V4 N0 H8 y3 v
- if(D1 == 1)6 h7 F6 P# P! P) ?4 N2 I" z3 x
- {0 {6 @- U2 l- l8 f
- Left = 1;
4 c# p- Y. v- Y( z# g& i7 s' I: z2 } - }
/ b, F& D# \9 R$ g - if(D6 == 1)
1 Q& _& o4 r7 D( W8 D9 }! d+ | - {% I5 o6 E: |- D3 W' o2 Q
- Left = 0;3 a U& h* w( U2 x! t7 N& R" X
- }
w8 G; E& Z3 l0 V H4 G - if(Err == 99)1 D: J, U" q3 W3 i9 f( e
- {& x- p- w$ m; a7 L
- if(Left == 1)+ N a" U: U+ h. D! L) W+ l2 w
- {3 O& y! A3 b7 @5 @( {
- Speed -= 5;8 A9 ~8 C( M. X6 W
- moto(0,Speed);
( `6 o% |! K: R - do' y: _3 O4 e% T! ^3 e
- {
7 p3 Y: q8 U; S8 [" L5 P - Err = getErr();2 d: B5 g( F) Z& y/ U) M
- }while((D1+D6) == 0);
b& x2 O2 }- s/ l8 C; o - }
7 ~2 k5 ^. e9 F" ]& ~8 | - else& f0 B% p$ f' L
- {
+ \/ [% ^( x3 [( J! a/ | - Speed -= 5;
8 {- s& S, c+ d5 w - moto(Speed,0);
6 y2 O5 p3 Q" J5 _ - do% j7 [5 `9 i3 w8 Q
- {7 i. b6 P9 X2 a- \+ G% L# J
- Err = getErr();
4 q8 C' j' |, h! \# c/ ^ - }while((D1+D6) == 0);
7 a/ M7 r2 m% X& B4 E( G - }
+ M( u9 n% n& M& R - } l5 Q; i: @ ?0 O- M( t7 @
- else
; C- [$ }- s$ W8 `8 l8 u1 X - {3 s+ N9 H) m- o" {( p4 _
- if((Speed < 160) & (Err < 2)) Speed+=1; - K5 |! O* Z% Q0 Y* y- s
- if((Speed > 100) & (Err > 2)) Speed-=2; 8 A: A1 C+ E4 N, e% K
- integral = integral + Err;
; i" T$ X4 u3 h3 V - derivative = Err - previous_error;
9 T! k, |$ e" a" m5 t. o - output = Kp*Err + Ki*integral + Kd*derivative;2 X- F; A+ R; s" j: ]- }
- moto(int(Speed-output),int(Speed+output));& K! M2 B8 _# z" W) F
- previous_error = Err;" Q; m- @! P" a' F$ v
- }
# f ^ d" y% j9 l3 l9 l1 v - }while(!((lightsensor_2.read()) < (10)));
, _- Z; q, T5 @$ t - moto(0,0);; U5 @7 Z% g( k3 c. @
- delay(500);
- {1 M' x3 r8 j. K9 v1 \, T - buzzer.tone(262, 500);: u4 I3 r! Y( G
- }
3 s8 O4 P/ \4 G7 U- C( A
8 k9 u0 ]* g$ z% \0 @- int getErr()9 H" j$ e' F- v# J" k$ y5 U/ t
- { 3 Z; x+ A3 I* A
- sData = linefollower.getValue();
0 Q7 o, { m; }1 [: b - D1 = ~(sData>>0)&1;" o5 O7 L3 l8 T" ^' F# Q( e( g
- D2 = ~(sData>>1)&1;
8 G. H5 j+ J6 M& Y8 | - D3 = ~(sData>>2)&1;
; v& C, \6 C7 N& I7 w0 ~1 V% ^ - D4 = ~(sData>>3)&1;
$ ?) x1 O% X8 w - D5 = ~(sData>>4)&1;' t8 }& k9 `& q+ B; C& Z
- D6 = ~(sData>>5)&1;0 n# Q6 Q' t; Y k
- int downD = D1+D2+D3+D4+D5+D6;
) Y a4 [" T0 c% J6 K - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3); W4 l2 m* u$ b" W1 v+ V+ ^( [
- if(downD == 0); b& j8 \* @! s* I1 N, T! V$ G0 H& T
- {: {: ^' v+ f( P, Q
- return 99;0 d2 y2 ^& b) D
- }
6 y; m" v8 k, l+ _: x6 t+ ^ - else$ N* I( a1 D' [
- {
' D# D. @3 g6 u9 D. m! r; e7 F; `9 ? - return upD/downD;+ \. @9 z8 K. S; V3 q/ U
- }
$ F2 w! x0 }8 o - }
# `$ q c- I2 n
& h# Z# M% [, {' B- void moto(int Speed_L,int Speed_R)# ^0 J" v7 {; d. N3 P( w* B
- {" e6 X; j8 H) A% y) y9 P/ E( r: K
- Encoder_2.setMotorPwm(Speed_L);5 ~6 H) o4 e2 p% Z
- Encoder_1.setMotorPwm(-Speed_R);
/ Q. [/ G# j# f2 Z - }
複製代碼
5 v3 V8 t3 w! ] i5 u0 |7 h- D+ @( SMeLineFollowerArray.cpp, J# V: j8 q8 K1 m
- #include "MeLineFollowerArray.h"
! l1 G. p- e1 O( ]# ^0 K0 v
0 \/ z- F) t. Q) t! }- #ifdef ME_PORT_DEFINED7 B9 t, w- \' K
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)+ r' C& K( c3 R7 C' r
- {
8 O+ ]2 W1 D* H$ {! x8 N# V2 j - 7 W) D( E7 f/ |6 s
- }
Y0 @6 T V C2 f' {6 j - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)6 K& V( @0 r6 j% ~; k; E
- {$ i! |& I' K9 [9 X9 J! |/ P
- _DataPin = mePort[port].s2;
% I- @, E. k! b! n( D! h' g2 a - pinMode(_DataPin, OUTPUT); 9 a+ E3 U8 S* ^
- digitalWrite(_DataPin, HIGH);8 a5 P& K8 j C; j- Y# k
- }+ Q2 l/ h* Q' c2 b' H
- #else // ME_PORT_DEFINED+ y7 G+ w9 _7 T
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
- }' Z! `0 q6 |* i6 q. y3 ? - {+ g& S. S& t; }* o9 f8 v& e; Z# g3 p
- _DataPin = pin;
3 \# V) ?. F( L1 y6 l - pinMode(_DataPin, OUTPUT); ( @8 D' |$ z& `( }, a& Y
- digitalWrite(_DataPin, HIGH);( N; E/ p- ]# v: ?, w8 M2 _( R
- }3 p, G3 {* M! S& k7 }9 J o
- #endif // ME_PORT_DEFINED4 N2 ]! u# T" h7 v& P: v
1 s& _7 ~; x. P$ A7 {, `- * [2 v" L1 \/ i8 y% ?5 Z
- void MeLineFollowerArray::setpin(uint8_t pin)
( C9 A9 P/ k2 n# w# f - {% y/ c* z' r$ U
- _DataPin = pin;
7 F+ L. b% K; |2 _- Z5 f0 d - pinMode(_DataPin, OUTPUT); ! e5 G% \# A7 \8 J( b$ j! q
- digitalWrite(_DataPin, HIGH);
8 S. h S7 d6 D* c - : J$ d/ e9 ?/ W+ W( t! x3 ^# G
- #ifdef ME_PORT_DEFINED2 w7 a- R' P* D
- s2 = pin;
* Y; Z; B$ n, L R9 x - #endif
$ _5 W0 h, m3 B9 H - }
: K, `9 J8 e7 C' N" e
( x- c( _3 F0 b) b( E- uint8_t MeLineFollowerArray::getValue()5 ~ g% g+ G) [0 o5 Q) X
- {* j4 E3 m+ r1 j; V& U
- uint32_t LOW_level_read_time;
; ?4 x! t3 C( e) U' V - uint32_t HIGH_level_read_time;
. o# ^4 }& F7 i' `, [( O - uint32_t time_out_flag;# `6 T3 H2 B o
- uint8_t Sensor_Data[3];/ @, s# ~$ J& @: i
- static uint8_t old_data = 0xff;
' W6 @6 c2 N$ C) n P. ] - ! y7 v8 W, `1 E# t X0 ]
- pinMode(_DataPin, OUTPUT);
3 z1 m7 W4 b- n0 l - digitalWrite(_DataPin, LOW);
' @( w4 D8 J& P. h# J - delayMicroseconds(980);: O2 k' E( i1 V5 Y) }# U8 v
- digitalWrite(_DataPin, HIGH);9 ?5 q$ k- _1 z% s2 p
+ K' ?" C `5 E. g( q- pinMode(_DataPin, INPUT_PULLUP);. f$ D& v. M8 \' R9 c+ ~
- delayMicroseconds(10);
0 G$ q# v/ ~% i9 K- i) z* ?4 J - + b0 u# d% I' N2 @
- time_out_flag = millis();
7 n; E; u6 p7 V) I - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
, U8 y5 ^) x1 I# N( @ - ( l$ ~; ?+ G7 ^0 I4 m, g
- LOW_level_read_time = micros();
) ? M, j2 x+ m' Y @' w - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out B3 L2 J* \( D1 ~- i
- {! d# a( _0 P$ a* N* i
- return 0xff;
, _. W3 E; o& y2 y1 e% [ - }
5 y. l4 Y4 _' _% P. J
$ M; T% o5 X& j) \' p- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
1 B/ @: E, d( e- y: a4 k7 h1 e( ?0 v
) ]) t t+ P4 ]) C: U- HIGH_level_read_time = micros();& q8 s8 o# ]9 V9 p
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
( B+ f f6 X9 u- I8 l - - f8 r+ K1 I; H, t9 L& S# {3 v* j7 _& T
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
5 L, f$ o6 @4 k. |3 C1 L - {
. o: ~5 w& }( G* y. X6 | - return 0xff;6 {: l, v. z# I4 v2 I/ Y& V/ e8 t
- }$ x ^% a- ?1 [9 q9 ?- r1 t
0 l W9 ]7 e& q @7 G& T- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
" H( [. E4 s# [/ Q7 v - {+ N' X- s' Z( @+ z
- return 0xff;3 @& m3 D; ~5 H% u" M! N9 u
- }) t" L4 I" X5 F' W5 |+ d9 C
- ( l- d7 T* Y; r2 x1 a G8 C
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );6 }& S8 C; ?" r
- LOW_level_read_time = micros();0 r2 @! J- G$ ?) |' f( |3 S
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
" ^. ~9 h0 o, I9 R
8 X5 A$ l7 v; }5 E' b* v. `- g- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out7 B! Y- j {1 Z4 D
- {" u+ M/ x, M# Q+ c* l0 [6 N
- return 0xff;
( L9 y; W9 G, r8 O! _ - }2 R% ^; S6 d" l5 y
+ c* ?5 p) o, K& d+ s, v' ~- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))) s( Q1 X4 W8 q+ k4 _
- {
6 E2 C. B: H8 _& ?4 u) O0 P* h - return 0xff;
- I. K; S, N/ C" w - }7 V+ L3 e2 y% A& ~) h: D3 _
' ~9 J0 Y* n; H4 k+ Q/ C- for(uint8_t k=0; k<3; k++)+ G6 {/ {1 x5 ]) o. i3 v p) t. ]
- {
I0 H! N% w) `# P% _# I/ O; Z8 T% m - Sensor_Data[k] = 0x00;- N; L+ |' ~4 U
- 6 ^( }9 e3 G3 q- ~7 [/ i6 S% R
- for(uint8_t i=0;i<8;i++)2 h% V2 ~/ \7 v9 T8 l. H5 m* G
- {
7 n* {3 i8 s8 o. U) r; o - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
$ y* i! w4 S+ e5 S9 F - HIGH_level_read_time = micros();) Q: R" [. D& x3 W" ^
- LOW_level_read_time = micros() - LOW_level_read_time;
4 p- C# n0 f3 ~+ ]4 d" V" W0 \ - 0 G! y8 ]+ ?$ d+ C% M
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
' `" O' G% v) N! x$ Q - {/ w4 D! \3 x4 X7 h+ P
- return 0xff;
) h% U* N+ j- E" ? - }
! o9 O, ^1 q' F' c/ y
1 {# H. S. n9 R2 `2 U7 s" n# x- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
% O4 z9 i* O/ H1 u7 d - LOW_level_read_time = micros();
8 _& r' f6 R9 F+ G - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
8 q2 p+ g7 P7 M6 Y& ?$ E4 R - % i, a0 ^/ V8 Q* v. x6 q c" k
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1( |, B: x B6 K! @ H f' `9 f
- {7 M2 m; q% Q, ~2 x# f2 i/ S! ]
- Sensor_Data[k] |= (0x80 >> i);
* P# K1 } O, ?5 K( y - }
; d |. l8 E" }: I! w1 K- V - else if(HIGH_level_read_time >= 100)" K" r' `: d/ L% ~2 b
- {
! \, H& c% ]0 u; B: ?: v - return 0xff;* U6 Y% q1 A G7 W- i* |% c3 D
- }- r% B3 O7 d. y b7 P
- - V6 \. e9 ] o/ R
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out), D8 m$ w( F6 z' F+ f
- {: ]0 I4 ]+ S3 w$ a; |0 ]
- return 0xff;
. s0 I* I* l# \4 r' O9 m - }
# \' x4 n% s( ]" J - }
* C, y9 R X# e - }9 ^/ Z9 j: s: [1 c; j# d0 B2 `. t5 Y
& }7 o% N8 W9 ?, Y/ U4 d- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
- D0 H e. S7 O+ } - HIGH_level_read_time = micros();
! z5 I) h2 ^$ H, @& Z - LOW_level_read_time = micros() - LOW_level_read_time;+ _8 n6 h! U! T0 l0 }& i: O7 `( h
$ b. A1 ?# b1 M' T& \4 l, _- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) ), n4 e' K' Z: W9 p
- {3 V4 e+ Q f. Y1 j9 O, K8 s; N
- return 0xff;+ B k3 L6 e5 @0 N+ e1 W/ g) R- t
- }4 x. A6 U+ `' E+ j, u9 K8 g
- # v" H1 i/ q2 _2 @3 E: C: @
- pinMode(_DataPin, OUTPUT);( p1 j0 C) w& y V' a: i5 B) m
- digitalWrite(_DataPin, HIGH);
& t( D1 `4 D$ C9 ` - " [) l( K1 b& @! x% S! L% L6 \ J
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
: }. V' _% ]; f - {" r* e. u2 p0 M7 v$ V
- old_data = Sensor_Data[0];
- Y" K8 F( y* Y" l6 Z - return Sensor_Data[0];
& Q" m' b6 M% k, C5 V - }
& r8 ~2 R8 i' c, i; g9 X - else
9 U9 Y* e, _% h - {, W: i( c' `8 I; g. i
- return old_data;
7 ~* s0 B( ~- @9 q- A2 }- ? - }
[! D# G; ~ z- a - }- x: a5 [. u/ ?, x% n$ T3 Q; I; `3 D
複製代碼 ' {1 h) e3 F8 M6 m$ S; v5 y
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
2 |. I- W% @7 p' p4 M3 Z- Q K9 ?: r
|
|