|
|
( E- E! @! }& B& _# I2 Y5 u- #include <Arduino.h>
) W9 u$ ~' I' C. |( a" ^# Q7 Y/ { - #include <MeAuriga.h>
e" E6 \3 c; T; k7 \( i. s - #include "MeLineFollowerArray.h"7 |$ h5 k# t% N& H7 B
- + m" D1 o" B9 B0 C# d( F
- MeEncoderOnBoard Encoder_1(SLOT1);
* @+ U" ~. k, a! h: S - MeEncoderOnBoard Encoder_2(SLOT2);2 l+ E* i0 l ^& ]( ?0 I' q) O3 c
- MeLightSensor lightsensor_1(12);
: r3 X9 X: W2 G; ^6 _' x( ~) z - MeLightSensor lightsensor_2(11);3 k3 d) t' d# t( S; M# W
- MeBuzzer buzzer;
: P" S4 G+ m! a7 c* q - MeLineFollowerArray linefollower(PORT_6);
1 i8 {5 ~$ Q8 E/ |" M5 \$ {/ l
) M9 a4 ~( S2 ~- `; P* ^- #define Error1 11 x( d2 D: a, p h. O) K
- #define Error2 2
* |# C. S& ?- t& g4 l" [ - #define Error3 3
z# G0 T( b5 O2 C R. S6 M" X1 L; z
7 A M5 i6 e# ]) M( r/ C E& [* V- #define Kp 15
0 [$ G6 D+ @/ o+ j - #define Ki 0.15
5 Y9 p; D* r; }0 @8 u# F$ P - #define Kd 0.03
0 h6 z8 P6 y* A$ f' J6 k% k) }
; l- K( E2 W# G, d- uint8_t sData;" i& U4 B! Z9 Z
- uint8_t D1;) K- G! j1 Q0 x# B1 C3 P! J- A: C
- uint8_t D2;0 Z7 t% a/ p4 t. \8 h
- uint8_t D3;
& _4 t* B: i! j% O0 U/ X: ?/ ? - uint8_t D4;
' _, l; e. | w2 B8 d - uint8_t D5;
2 i3 w3 `. B& P5 N# x - uint8_t D6;
+ p, K) p) ?( E" I2 Z1 W - * x w' U9 o- T; T
- float previous_error = 0;
0 u' m0 @7 k) R* {: E3 H% m - float integral = 0;# Z- }1 l* i9 q8 y4 [2 x) ]
- float derivative = 0;
1 E% f, |* j1 ? y& y - int Speed = 160;
# j5 F/ K! A" W, M$ j, K7 z5 U - float output;- W2 Y7 b& f' B) b+ W
4 M% b3 i: B* k" {* E$ {- byte Left;8 w3 o8 m7 Q3 D/ b' e
; ~5 f) r1 T: x; f& D6 h; f- void setup()
2 j( y9 f/ p. J( T4 @' w7 {* S - {
4 u/ w! E& |" p4 M S) h; i - //Set PWM 8KHz
. h9 N) a' w( H( M - TCCR1A = _BV(WGM10);1 c( D; K7 G2 z
- TCCR1B = _BV(CS11) | _BV(WGM12);
1 l& d" A# [& r F6 h - TCCR2A = _BV(WGM21) | _BV(WGM20);
3 @3 Z! d% ?. X - TCCR2B = _BV(CS21);) Q9 B* v% j+ @* ]. g4 l0 [' v
- Serial.begin(9600);
, R: f3 c$ `- F" w3 r5 D; a - buzzer.setpin(45);
9 J- e( D4 f3 h5 o - }3 K0 A. a7 J/ A" G0 Y
$ Z6 F1 e: i% u. j M: w- void loop()7 L2 B4 z5 a5 b, `0 Y' D# y4 ] H) B
- {! Y1 n; p$ D, l$ p
- while(!((lightsensor_1.read()) < (10)));
7 a& I& p* c9 i4 I, |$ w: H9 l. \ - buzzer.tone(1047, 500);
8 z# u# q$ K' L& V* L2 }! W$ Z - delay(500);
/ I/ V9 R+ x$ A - do
% C+ O# \6 y- r" t0 E - {
) v$ q5 e0 ~- ~: ~' u - int Err = getErr();
) g2 {8 Z; M5 I3 h: A ~* P - if(D1 == 1)' d: \* g: |# V4 [) I% D0 J
- {1 p1 I3 r7 [/ G# {8 M H3 e2 ~
- Left = 1;3 N9 Y! U$ c# b$ t1 Y" f
- }
" d; v, D0 ^' ^ - if(D6 == 1)
- s: ~/ D- z$ _: _/ p0 G - {
& j# w( n @4 `2 ] - Left = 0;
9 y* J0 _% O/ N) ~( A" Q+ w* K - }# g; z& I' z& B2 X
- if(Err == 99)
; W$ ^9 B, V/ E* L! c- E4 r - {
+ o" y/ F" O- o/ S. \$ @3 ]' P r1 S - if(Left == 1)2 {/ {: i3 i1 t2 A+ n
- {
, D# s3 a4 o; P$ G' v - Speed -= 5;
1 z$ ?+ L6 [. U* R - moto(0,Speed);
' p" U( x% r2 f2 r: p5 p, ^% m - do
3 X, J1 ~6 j5 {' k, r f - {5 y! W! x8 l+ q5 e# V
- Err = getErr();
6 z6 U. z. l1 _+ a6 o5 T' W. G7 A - }while((D1+D6) == 0);' i: s1 \; D$ h6 M+ K* D) U' K
- }, W I# ^4 z* `: c
- else
+ S& N' H/ Y! g) G5 ^2 A( Y$ N" A - {
) l3 v2 S6 A0 T0 t' m - Speed -= 5;' O. T6 R" g7 h, E0 {. B% E
- moto(Speed,0);
' \ X/ h2 l2 a3 m1 B4 O2 j6 r* p - do
5 V; |' ^1 ~0 R. C - {5 b3 e7 `: x* S* {* X( J; R
- Err = getErr();
6 q+ i1 O0 Y& \/ P* E3 p - }while((D1+D6) == 0);0 M3 z R! ?. J, m! }* u! o! O
- }
8 K$ w8 i& \& ] s Z7 n# n! k - }
7 ~' D5 D) Z% { C5 ^% b. U8 C1 A - else, c" D% m5 K M$ Q
- {
" C K/ f8 B2 Y2 e$ M" B - if((Speed < 160) & (Err < 2)) Speed+=1; ; s5 F/ R+ f- n# c! d- R: W9 `
- if((Speed > 100) & (Err > 2)) Speed-=2;
6 j, w( L4 o) V8 Y - integral = integral + Err;
m+ R9 m% N2 L! c4 \ - derivative = Err - previous_error;
9 S1 ~; t4 T8 k) I, W - output = Kp*Err + Ki*integral + Kd*derivative;
- U" q% L, F6 s9 C - moto(int(Speed-output),int(Speed+output));
* Z( G0 H" e3 j/ A - previous_error = Err;: ]2 ^1 {" D" V/ `0 V- C- w+ {1 m
- }9 ^4 p* q) H, j. T- m4 a
- }while(!((lightsensor_2.read()) < (10)));" `1 w6 @& g* F, R3 Q
- moto(0,0);7 Z- L3 v1 p# f! {: D
- delay(500);) m/ I1 L F/ X6 V3 \
- buzzer.tone(262, 500);
`- K& x! R$ Q: i - }- T6 s' [# B$ \) w" p
3 s B( v( T' p! i: [- c- int getErr()
; X; y: s0 w! _7 D% G7 V3 D - {
" D! T& e, u3 ^4 |$ L0 d; }: C! d - sData = linefollower.getValue();2 l" F9 x+ [( B1 x6 @
- D1 = ~(sData>>0)&1;
9 w& O R \* `! {+ k" {$ g - D2 = ~(sData>>1)&1;
r' |5 J M2 ^, F" J j - D3 = ~(sData>>2)&1;0 P1 E! e. G# ?# O' @+ g
- D4 = ~(sData>>3)&1;
8 y+ ?0 Z0 c7 h5 o- k' k( i - D5 = ~(sData>>4)&1;
% w* N& r M3 R - D6 = ~(sData>>5)&1;
+ T5 v/ o! x8 x - int downD = D1+D2+D3+D4+D5+D6;
5 l* I% e0 B9 x" k9 p) s0 B - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);; I/ w9 K2 }. r9 c8 X, O1 A, @7 i4 |
- if(downD == 0)
4 F1 k) c& n( u - {
/ p# v0 D* z# u. J - return 99;0 X) n* }# m2 y4 H$ N: `$ e
- }" ?$ O" Y3 s' T% Y
- else# a' R3 U( m F \% F8 \
- {* R: h& e v# I- `7 m3 V, B
- return upD/downD;
6 @# f8 S- q% D/ x/ {9 ^9 m - }
- H/ R0 p' o: Q& D$ V: K8 [ u' n, n - }
- O( g( n, [5 F% P% X s4 ]0 n* m
5 P+ k! d7 p8 C p* v5 e5 `- void moto(int Speed_L,int Speed_R)
6 @; q0 n" C! `5 [" \4 ? - {
1 e/ D- w _7 F F* q3 v - Encoder_2.setMotorPwm(Speed_L);; B- C: z9 [% p, S/ q6 ~* Q8 J
- Encoder_1.setMotorPwm(-Speed_R);( x8 w3 D% C+ p& g
- }
複製代碼
$ `* N/ o, K4 D' Q& L9 Z2 aMeLineFollowerArray.cpp
. J8 {' f0 m: g+ N1 f- #include "MeLineFollowerArray.h"
# i0 ~8 i1 l. k1 ?
% x$ A, R/ ?7 M2 |- #ifdef ME_PORT_DEFINED; i) X" x: V5 Q5 O
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
- C; J, B) y! ?9 g5 l - {6 ?$ [$ y p, y; }0 K! R: |: m
- ^# z+ s8 p# f& \* }) j- }9 U7 X9 |1 s& |5 h* S/ l
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
6 o$ a* {+ x7 _9 |4 R: ]- N. m. [: M - {3 f; U' ^1 m1 `# U% Y
- _DataPin = mePort[port].s2;
4 w( u* d m1 |) H6 ~ - pinMode(_DataPin, OUTPUT); # M! y" T" X) x
- digitalWrite(_DataPin, HIGH);& U( ?' N) w& b q; Y
- }$ m5 X% R( v8 ]. i3 c+ Q
- #else // ME_PORT_DEFINED
9 Z& \1 Y+ E. r: V0 @ - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
. O7 y0 ]8 X2 j7 V$ E - {
. l: a4 K0 M" t* a2 Y - _DataPin = pin;# U% m+ W; X0 B* u/ |3 E
- pinMode(_DataPin, OUTPUT); % U4 u3 }+ Y9 X9 a& V/ J; \
- digitalWrite(_DataPin, HIGH);! ]1 R( [9 C; u/ o8 O
- }
- i7 P" N. P2 |/ i _% j: D1 P - #endif // ME_PORT_DEFINED
; B K3 k$ Z, w' \& [: T - $ C; {3 _$ G" F1 f5 r. N' V
- # i2 @$ B; \6 w" Q5 F9 c8 x, {9 @
- void MeLineFollowerArray::setpin(uint8_t pin)6 |9 F1 \0 x B0 N) A y, S
- {
$ V' u9 p+ D2 j5 k1 L) L - _DataPin = pin;* r; [+ b7 u+ K7 h, R; |
- pinMode(_DataPin, OUTPUT);
$ K" Q1 y5 {( v0 E - digitalWrite(_DataPin, HIGH);
+ J( |; f3 F- a5 Y
" V' ]7 P- s: s( u! R. G- #ifdef ME_PORT_DEFINED& ^- I: F, D; N7 `! K2 |( ^
- s2 = pin;( J3 I" j+ U b: _0 Y j7 j+ u& j/ e
- #endif! m7 `* Q: A' S& c
- }% {9 k1 O( P! S0 p! r4 @, |2 ]
9 @% |, \; s) Q: s' s- uint8_t MeLineFollowerArray::getValue()
8 w$ Y9 d* v/ \8 M - {
% f) v* i9 t5 q- n+ g: o - uint32_t LOW_level_read_time;7 q7 p- U1 t2 Q% E+ E
- uint32_t HIGH_level_read_time; L O; t' u7 m9 {3 B- G. G
- uint32_t time_out_flag;
% z7 |- E6 J8 d0 O7 ?" Q - uint8_t Sensor_Data[3];' ?, I) |( {1 Y) S/ w2 {
- static uint8_t old_data = 0xff;) s: X: p+ e: z# b/ F D, t
$ [6 f0 z/ j9 u$ j, X }- pinMode(_DataPin, OUTPUT);7 o1 u+ `' f9 X4 T/ @; q& Z9 P5 m
- digitalWrite(_DataPin, LOW);
7 z$ ~) G7 X G! w) x* o - delayMicroseconds(980);
1 W7 K! N3 h+ j# [* \4 n - digitalWrite(_DataPin, HIGH);& f" E3 k; V- r: U! Y) x
, x3 f; v7 }( L* i% R6 ^0 w( @- pinMode(_DataPin, INPUT_PULLUP);
3 {3 a( N# k5 \0 K - delayMicroseconds(10);9 }# }- O! h9 J
- " H4 e# F+ L2 v; x7 h
- time_out_flag = millis(); V% ~5 D; b, x9 C6 j
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
( V4 {! Q& P1 h% S8 @" Z4 T$ h - : @& [7 T1 |5 K' V3 B+ e- z3 n
- LOW_level_read_time = micros();
. ]! h2 G4 c7 @: K# b - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
. q7 C, a r/ f% i8 H0 d8 y! | - {# M0 p3 P, R" K
- return 0xff;! }' M4 K( g; u- t. k( j) b8 v
- }
9 W/ g+ v& f+ s" F; S - " b+ Y9 C/ F9 Y' {$ p% A
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );5 h1 o/ |- E5 z$ R6 F# w
- / o5 E. T2 [$ }; I) p& b
- HIGH_level_read_time = micros();
8 B" b) ` O" X2 E: U. e - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
; W6 y! d" A4 G/ Z" T" \, m2 V Q
5 L5 n* x( ?/ h" l- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out' N0 p5 d6 @2 d( E- v" E. z
- {' n T5 B7 L$ w' a5 I
- return 0xff;0 y6 J, D' r3 P5 G/ D
- }$ M3 X! d1 p3 e: n% N
- r1 U* H9 C( u) P- Y. }/ V- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))# M# f, b; Z" Q/ l2 S5 T
- {
0 g- M/ k6 p, e& E - return 0xff;3 T1 y( C! y5 W" s! n0 \
- }% Z/ T; X$ Y2 I
# x7 G3 ?% I" m7 C1 W0 w! X3 ?; Y- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
! l* Z' G2 ?% N/ Z/ M% s9 V - LOW_level_read_time = micros();* U( l! r- a4 q% ?6 @
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
* {$ Z7 v" p6 R& S8 ? - , Q" \# s) j6 X+ ^
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out: ^5 D7 t0 k6 h& M1 R; s0 m
- {0 j) l% `9 a. T/ G# c. h' f. Y
- return 0xff;! j0 L0 d+ _* ?3 i+ @7 y% A2 p
- }
% g* ]3 m: Y5 W& B( W! |) ^" m4 y
9 x+ ~, ?- U% l* `" i+ V- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
7 ]! D0 j/ ?$ E - {7 b6 z5 O( Q: ]* x8 {* i/ I
- return 0xff;/ L/ a0 u2 |. T
- }+ L( I# ~ K' x# q+ H
: Z# b5 N' I) b* G5 `4 v4 a- for(uint8_t k=0; k<3; k++)
, Y1 ]( {2 I- G4 z - {
0 i7 p/ _( q5 u - Sensor_Data[k] = 0x00;
( X4 C2 q; x& c! o# Q3 K3 ]) M
' B$ g( Y# e8 p0 M- for(uint8_t i=0;i<8;i++)6 s v0 |9 k+ h$ |( \5 M& X
- {3 _0 U, ?+ O8 O) i. M5 Z+ ]
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level j( ]" A7 l4 U; q
- HIGH_level_read_time = micros();
( s# v/ q# }" z/ t7 `" } - LOW_level_read_time = micros() - LOW_level_read_time;3 t$ U1 e1 n$ e/ w" x
8 z. ^9 ~/ u: n2 G$ v- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
7 h2 g1 T7 f+ s: F4 q$ L - {
9 }" @6 S: M+ K# Z4 W9 @ - return 0xff;/ l# g# }3 e8 p" [3 K4 Q: X
- }
6 f& X1 n; i/ u
: |; C6 M: Z! E, H: y' R! {- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );& l7 I; X& ]/ I! @
- LOW_level_read_time = micros();0 A% p) }4 i5 w! k
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
a; r% n( K/ G! L& y+ S
- @+ w6 ^9 X$ U2 H6 p- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
1 t" f* E; M; Z3 q- K5 e4 m; C - {
% b" ~6 }. d i' Z( x* N& t- z( \3 m - Sensor_Data[k] |= (0x80 >> i);
* ]" e( J$ h5 _$ I5 j5 e. T) a - }* g/ r/ g' n& B1 M' p
- else if(HIGH_level_read_time >= 100)7 @% q/ c' _* y
- {* j4 ^+ v B7 S/ }: i; q5 r
- return 0xff;, Q) w6 ^% \) W) r
- }' Z, R9 c2 j+ u% G P$ {5 ~4 A
- / T0 l& t( \5 ]: t5 n; i% u
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
3 ^7 `7 A" l8 @1 h# R - {
( O0 M6 T K1 S: _9 D0 m7 q. K0 S# K - return 0xff;
2 O2 ^4 }6 }! ?, [0 a& y7 |3 u: [ - }
, C" t" W" j' h4 ` U, x - }
; G) K( @/ z( F9 T) E. N - }
/ q) C& v `$ Q; U$ `7 h - : n5 l, o! K5 `: N9 ]
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level3 o+ G) n8 P6 F& m) _! F5 v' D g# F
- HIGH_level_read_time = micros();
3 Q/ C: l8 ?' V2 ]8 [! u - LOW_level_read_time = micros() - LOW_level_read_time;$ z: _, n* R) C$ R. G
! G0 s( ?6 e2 S- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
6 o: d0 ?+ q* O9 _% z+ @6 a/ _ - {/ i! X" d2 M% R. l+ k# Q
- return 0xff;1 s" W; c3 V- R; o+ c0 d1 ?
- }
8 r- w* p4 s$ Y. s7 @! S4 j - 7 |9 j7 [& A- ]1 f' v: w- Q
- pinMode(_DataPin, OUTPUT);
. f0 \, g4 \6 g; ^- K - digitalWrite(_DataPin, HIGH);
1 }, U3 {. y6 g - ! Z+ T: U( T6 {+ J& L
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
" `& j: G3 B, o - {
6 a _6 ?( q S: i, R1 n! h9 m# [ - old_data = Sensor_Data[0];
3 A' u, H! k8 e4 r4 w( j - return Sensor_Data[0];/ x4 a# @3 A: j7 R
- }
% V4 g1 b, v8 m, g2 y1 o - else) A6 O% Z @6 l2 C; O1 g+ r
- {
. B. U$ Z. I3 P! U) e - return old_data;
5 ~& R% h: N) c9 j. G6 C9 z1 I+ N5 w - }' j! D5 p9 m+ _4 B
- }" E( G1 _4 A& F4 F6 b( p: K8 x9 z, P
複製代碼
8 I+ i! g1 R2 oMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
+ v9 Z+ {+ n" Q: y) ~; j- C& h5 @: {1 F2 J- n
|
|