|
|
: I4 x' b& M7 e$ _& ^- #include <Arduino.h>
* U9 t3 k% c1 G/ ?& b6 q2 ^$ s" O - #include <MeAuriga.h>
8 E9 h; b, P. R3 @* E ^& x - #include "MeLineFollowerArray.h": A2 m u R% U7 f3 Z0 C. ?( c
g% y0 Z! H6 u. ?; ]- MeEncoderOnBoard Encoder_1(SLOT1);
" ]4 ?, B6 N* q# f7 Y - MeEncoderOnBoard Encoder_2(SLOT2);& v7 i! X" S& Z( a; [0 @, n) U" n# y
- MeLightSensor lightsensor_1(12);: ~! K" e9 v8 h$ H
- MeLightSensor lightsensor_2(11);4 Z' t& X" q8 u- b' X
- MeBuzzer buzzer;7 v9 G6 C8 T# v8 ^4 e& X, k
- MeLineFollowerArray linefollower(PORT_6);( H2 d z2 U! P% s
3 L5 s5 Y h0 K$ O" e) ]6 |5 r0 W- #define Error1 1, u% v# A4 O9 |- r4 H" j
- #define Error2 2
. S- e, [( k r7 P' F: m. o# p" v% s - #define Error3 3/ f9 D) Y, J$ L$ R* _
- 1 u6 w( p7 N2 o% i/ R( l: Y7 J
- #define Kp 15; q& V, K1 _) o9 x: G1 C U$ M
- #define Ki 0.15" u& V( X0 t0 j7 H5 d' }
- #define Kd 0.03
" B0 p* r! A+ N - $ w9 P; d8 G# v' O0 G
- uint8_t sData;8 ]& ]& U& _' }. m7 F P& E R
- uint8_t D1;
! w* D- e) i3 c) W - uint8_t D2;
2 C4 [5 g V8 s6 H" j - uint8_t D3; ?' k; f# w* u# l0 B$ L# v
- uint8_t D4;
# \% s: Y- ]0 c" y0 I( r, i - uint8_t D5;! i: q8 g& J6 O2 L3 n4 h9 B4 V
- uint8_t D6;
, e) w2 i$ Q" `- [! b
- o1 c6 _6 p* r. ~/ N' U- float previous_error = 0;
: N' {7 i( h* B. ~' O6 l2 E6 l0 @6 v - float integral = 0;
( f. e- x X u5 b - float derivative = 0;9 b+ {& K: g( b3 C
- int Speed = 160;4 Z" e$ q* ~ w- O
- float output;/ a5 i9 \5 d& Z& R. W5 Q" T
$ w0 D. x3 @8 z' N- byte Left;
, T( _9 L. x7 I/ `: j2 X - t0 |8 z3 H* w1 s' N5 k
- void setup() # S6 n4 b' N. e% t' _, d5 h0 [
- {8 v" Y7 O+ A6 ?! b/ a$ h
- //Set PWM 8KHz/ F( W$ F/ {% c9 ~2 l0 M
- TCCR1A = _BV(WGM10);/ b1 D) }& }8 V- c1 d# y
- TCCR1B = _BV(CS11) | _BV(WGM12);" W. J! G; b/ r( t! U* C2 R: s5 W
- TCCR2A = _BV(WGM21) | _BV(WGM20);" m! d8 s4 _& Q" C, F' w5 X5 t
- TCCR2B = _BV(CS21);
- S& a; A) X! l9 ^5 h M - Serial.begin(9600);" V! g6 L! s: H ]
- buzzer.setpin(45);( h; z9 ?) u$ ^. N9 v
- }6 D7 X! X C/ y3 N( D9 \8 o
- % b3 g0 t, [, @. Z5 A7 D
- void loop()
2 C( m% N0 s2 \/ Y! m0 q' | - {, S7 E! I+ X/ s) @+ \
- while(!((lightsensor_1.read()) < (10)));; U) V. V% j3 n9 l
- buzzer.tone(1047, 500);
1 }; x5 e1 U8 w0 t0 O( u; ~ - delay(500);5 X# U6 p6 W7 L
- do
- v% J$ V7 p; f. E - {7 ~$ _. l8 }; n( V# y
- int Err = getErr();! w0 L" U N, P' A3 ?& t, }1 @; i
- if(D1 == 1)
! y2 }3 I9 Y* d8 a1 n: i - {
0 a8 \- g5 n# b3 ]# |* N! K5 @ - Left = 1;
: W9 J6 O; M" Q; h, e" G& l - }1 r" j, N9 m6 x! R- _7 {
- if(D6 == 1)
; g* d1 P4 G, d A - {% `$ M' X" u, C) K7 R$ R
- Left = 0;- W* [' c* M1 k1 ~2 Q9 O
- }
1 `+ \0 \, N. l6 ]' J$ ] - if(Err == 99)' e) n* f! s, Y& d6 O+ V
- {4 J* C( z2 }9 M- R. S5 ~- q& S* X
- if(Left == 1)8 f8 x) P4 F, b4 l3 e% r/ A' H2 K
- {1 [; D' ?) [8 y* S* m+ P
- Speed -= 5;$ o+ G5 E6 R8 L& T) O1 ^
- moto(0,Speed);1 i+ j2 ]! ~% E7 p
- do$ F) D- U; A$ P- Q. O' }6 ~ W9 b
- {
- x2 W: H" z U7 k8 r - Err = getErr();1 S8 Z7 h1 \! E7 y2 Y |, m8 x
- }while((D1+D6) == 0);
* u7 w" T" v! o4 z/ i - }
2 g) H/ [& P. H9 M - else
9 G2 `9 ]+ k! ^ - {
- A! x* \/ |- f - Speed -= 5;* `& M& e/ d/ t, S. X
- moto(Speed,0); ! K: k( z: S; A) J* Z
- do
% f* a- W$ [. I; s. U - {, o' p- i% K! G: z, U8 p, S! h
- Err = getErr();
; j' j6 F/ N- ]5 q6 X5 { ?' W - }while((D1+D6) == 0);
; L( U N! O8 G& P, _ - }+ H& u: @3 U( q& Z+ B, n1 F8 C
- }
2 M2 K/ b2 z- w- \; L - else; ?" [2 \ q9 W8 d
- {
. x m: j/ U0 _3 j" l! E, B% D+ _ - if((Speed < 160) & (Err < 2)) Speed+=1;
, p- |: V$ o% Q; a) K - if((Speed > 100) & (Err > 2)) Speed-=2; $ x0 A2 f! n8 M0 o" Q: s2 W( h) C
- integral = integral + Err;
6 E& _7 W6 C* t1 e3 U - derivative = Err - previous_error;
+ Y( ]2 }' M1 }' N! J - output = Kp*Err + Ki*integral + Kd*derivative;1 B. y" f" n0 w& x" U! r1 B
- moto(int(Speed-output),int(Speed+output));
0 o4 }% C. z# d: a, }, R/ h - previous_error = Err;
$ c9 g' j m, x$ K* x. ^0 t - }
2 U# q2 F B+ t; H: v - }while(!((lightsensor_2.read()) < (10))); i7 p& K. g/ r; Z3 u, G/ S8 Z
- moto(0,0);$ n G2 y b |
- delay(500);+ h) |$ h% ^4 h( P; \4 c
- buzzer.tone(262, 500);
2 l9 ]9 n1 H# _: p - }2 q% I* q8 W/ R) A+ F6 y
- 2 J8 X9 @6 }1 s/ z* g" ^; K3 W9 j1 E
- int getErr()( W" X; _$ Q: C2 U
- {
( h5 B) Z: o' @) m) W# X - sData = linefollower.getValue();
! W8 d5 H) A ?& e; s; p4 ^5 s - D1 = ~(sData>>0)&1;% ^+ X* |: F" w. q. t; J, ? n5 r3 x
- D2 = ~(sData>>1)&1;5 M6 X) T0 ]4 h9 c' o
- D3 = ~(sData>>2)&1;
$ F% \! q3 B& f, \( l" ]# X - D4 = ~(sData>>3)&1;$ T, f) r. F9 _: X2 p
- D5 = ~(sData>>4)&1;) k, J: E. Y/ P6 X+ }+ k1 q/ X
- D6 = ~(sData>>5)&1;. v' R4 b" s# G7 i5 ]1 J. C
- int downD = D1+D2+D3+D4+D5+D6;! y) E$ Q/ u1 ?# V! R
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);4 \5 V, g0 I& R( q6 \
- if(downD == 0)' i6 o! H4 B2 g, f; G( `
- {) q% h M3 i# z: ^& S* Y
- return 99;
% P0 T5 `! y# \: G! o' v+ S - }
$ J5 Y! C2 J8 w/ a - else; a: M+ E2 Z8 [ m
- {
O1 D4 N; g; n/ m/ e - return upD/downD;; K1 O& l% v3 }$ V
- }
* B) u+ \ P5 Z: l - }
, j. o* g2 E" M% O - 9 Y* M8 f8 U( u6 q+ Z: v' J: P
- void moto(int Speed_L,int Speed_R)
) t7 @2 S) z n) v( ` - {: x$ Q4 ]. L: ]5 r3 O! ~' o$ H1 I
- Encoder_2.setMotorPwm(Speed_L);
$ ]4 T ]) p: Z4 F - Encoder_1.setMotorPwm(-Speed_R);
0 k! y/ e7 R) C# k {: q P - }
複製代碼
# C) w+ C8 m( w A/ x" `MeLineFollowerArray.cpp; i" G y- C4 Q9 D4 ?" U6 p2 v% O
- #include "MeLineFollowerArray.h"
& E) c+ t' X y5 @* b3 h
) o s' ]8 I6 v9 x7 t! m- #ifdef ME_PORT_DEFINED
7 Z1 X9 [$ p7 p8 k7 T& W* F8 t - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
: X B4 z# l" k& i. x* g: d- g - {; z. Y, K0 V! O% Q, b
- , e9 l/ J$ l) O1 ]
- }! h; W P/ f7 d- @8 k
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
6 H, p5 Y+ \, b! V6 V' r. A* |- c0 } - {
; w! ]: D, o9 l0 _( N6 j! ] - _DataPin = mePort[port].s2;* w5 p W) n% t) y& \
- pinMode(_DataPin, OUTPUT);
& [& \/ Z; J. ~2 M - digitalWrite(_DataPin, HIGH);
/ |( O( |+ k: x) C) F - }9 y5 l: P+ l/ _* {9 g! X
- #else // ME_PORT_DEFINED
( ^' o$ {# T+ s" o - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
/ x4 b: N4 ]) @1 K: {9 M - {2 C' S# O% I3 U( j
- _DataPin = pin;- }" ~7 c3 Y0 z! ?! j5 C' A
- pinMode(_DataPin, OUTPUT); 1 P3 }+ P# m$ @" F+ e2 x5 n
- digitalWrite(_DataPin, HIGH);
& |; M' d- E2 G, o - }. T6 g5 L; [& A! R& ~5 _+ F
- #endif // ME_PORT_DEFINED
% @9 I' r1 p3 z/ Y1 y - 9 P1 v0 E* O; n
% ~: Z- e- C, j- b- void MeLineFollowerArray::setpin(uint8_t pin)
9 y8 w& I ]( z, K" u - {
" w+ \) v1 u7 i* h! k! x - _DataPin = pin;: p+ M5 `$ A9 X3 p% O, w. f
- pinMode(_DataPin, OUTPUT); , q- B: M" O, x
- digitalWrite(_DataPin, HIGH);
" J/ ^1 ^/ u2 b% z, A6 D) P - # f7 w% A. {% S
- #ifdef ME_PORT_DEFINED! _8 U; B' ^/ O1 s; z; H
- s2 = pin;8 o6 z/ g( Z6 A/ A5 _8 B0 n/ V
- #endif w( X, v6 s% ^0 ? G k/ O) U
- }
8 d. q0 ~$ L( i+ ^: F3 l# r5 g& l - 4 k- `( W5 I+ R; u8 C8 w/ G {# s
- uint8_t MeLineFollowerArray::getValue()( \" i$ p+ B# ~/ ] c
- {9 i0 y' N8 G y5 L
- uint32_t LOW_level_read_time;
# F6 K: O/ I3 F: m! w( w v& C - uint32_t HIGH_level_read_time;* n/ N' V" h3 |$ @9 n" v
- uint32_t time_out_flag;; [; P, D- A( F0 `4 `1 W3 M
- uint8_t Sensor_Data[3];1 n4 {" m1 q" |* Y: {* d; w" Q! D7 B
- static uint8_t old_data = 0xff;7 i2 f4 ?. U: p: F8 U5 N0 H& Q$ K
- ) P! S% u9 Y/ }4 s3 T1 f; t
- pinMode(_DataPin, OUTPUT);
0 X& {, b! s5 N8 }: A - digitalWrite(_DataPin, LOW);
! G' c! n! e7 M; D3 g9 q) e( X8 J - delayMicroseconds(980);
$ U8 }$ c1 Q' H - digitalWrite(_DataPin, HIGH);4 S4 ~. R" {6 t9 _2 [8 y6 |
- 8 `9 i" g( U D' u' _- _
- pinMode(_DataPin, INPUT_PULLUP);
! n6 ?, f9 {0 ^' Q! W( ]9 J - delayMicroseconds(10);
" n* j6 i, s$ ]0 ?) _
9 j" \* ^0 Z* l3 l0 R- time_out_flag = millis();
# o }1 u* T2 v8 G - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
& w$ {7 c1 x8 _4 l& ^ - # i1 e# C6 `2 f0 ]( U3 G
- LOW_level_read_time = micros();- y6 d6 l/ y, f( s1 Y1 F* w0 A) ^
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out _) |) o6 s5 ?9 `+ J
- {8 \4 X9 z0 Y) @( z! l4 r
- return 0xff;
! y+ k9 V% {1 y+ r3 F- a - }
' q' _0 i5 ?. S) z
' {; p& L* T; S2 Q" I' O- N( \8 \2 u- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );) |7 [* F" ~' `* y: |5 c. S- b
4 A1 |$ ^" Z6 `! Z- HIGH_level_read_time = micros();
8 i d- M# h8 O+ c! j' R3 } - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
" e+ ^% O0 I* e# I8 C; Q' d
2 a) n$ G+ Y2 \8 C/ a- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
0 z% ~$ l3 i$ H; l( _ - {4 X% d. [; v% {
- return 0xff;
3 z. }6 O! B5 b) }* X |, m8 F% G - }0 d# e1 A, v$ ]' S9 ]
1 d3 B5 }1 \2 Y+ {/ O. a) X- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
3 _ }: v2 \% D- F3 M - {/ `1 `1 P; F* |) ^5 a5 n
- return 0xff;/ R% K7 _% ]/ N$ c& p" D
- }
& x2 @2 n" c6 F/ z+ R$ \$ C - / S- a. H2 F! a! J1 {
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
, u' G& i1 a: ? - LOW_level_read_time = micros();
! B) Y. f, I/ ~1 }9 K9 l - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
! c* s8 B" c2 b* ~% F6 @' L8 u+ s0 g
" F2 H) R; o% F# s; B- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out( c9 T$ ?0 O3 r2 j; P
- {+ G" s3 U( n7 K! Z: B' C
- return 0xff;# o4 X3 Z) S0 @8 N, C
- }
4 ]: i# U3 ~8 N0 R! b5 G - 2 ?+ ]% A3 ]+ z' p
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
. H/ A' j- @! ~2 |" P - {
& Z# a3 F# C# v - return 0xff;* T! R6 ]* V2 Q4 `
- }/ W' k( G7 }6 D$ G
- ! ? r- |% ^9 {9 X& I0 I* G
- for(uint8_t k=0; k<3; k++)# {9 m' C; Q& \4 Z! F2 g5 k
- {* f$ i/ g, Y* }; }' s' L5 J( ^- ^
- Sensor_Data[k] = 0x00;& W2 ^1 S1 S5 p
- . N: c3 @' Y, w. M
- for(uint8_t i=0;i<8;i++)3 ]$ Y, S4 }0 Q* M
- {+ a% i% A6 R; a7 j' F! f) w
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
8 X3 L$ E* f* W' i2 k - HIGH_level_read_time = micros();
3 Z P) x. F) k. D# i - LOW_level_read_time = micros() - LOW_level_read_time;1 v* P( h9 c! C! x, D- d3 e
- ; z) e! W$ [1 B4 Z- @- h
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
X3 G' x7 p I0 i* C/ | - {' _+ R# \& q6 g; U5 \
- return 0xff;
5 k) W5 J4 E7 r, ]" L9 U& T/ S2 g - }
' K" H- @3 U3 \8 f! b
* m. Q9 Z8 W- X% J- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );8 W) L/ ^; e8 _# C& F3 e1 Y: K& o
- LOW_level_read_time = micros();
/ }+ m7 z3 S& o& r0 C% i$ M - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level7 {( \* k x" Q" p9 r& ~
- + Y6 \% U' b# ~; L6 s
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
( g! W! x6 w6 O9 P - {
) @# o$ j9 W4 s' i e! r5 K - Sensor_Data[k] |= (0x80 >> i);4 i- r# x& b2 n: {
- }
/ [9 k& F6 b! t9 [0 q - else if(HIGH_level_read_time >= 100); u+ U( M% n$ |: M1 }) X" _3 a
- {
9 @+ t/ Z1 J2 R, {; K/ v$ R - return 0xff;3 w) v! s/ i7 O0 w, f
- }
7 I; f, x7 m% {3 q! R/ j- r1 A& _4 G - 0 d) K0 t3 N6 E" ~; y( X7 t7 [' M
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
$ Q4 N# t9 d4 E" t2 z- ~1 K - {
) c; F2 z$ d. A# o; O1 @+ q - return 0xff;
1 I" \* [1 o! N+ y* k - }. B0 y' Z/ v, k2 v: U- E
- }2 P: I4 l) u- Y5 ~
- }! I$ V4 Z# }4 r. U
" h& c& R: H* P2 G8 W1 x- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level; W* W* y- {; f z: d0 z& A$ H
- HIGH_level_read_time = micros();1 G) h Q6 n! X) j
- LOW_level_read_time = micros() - LOW_level_read_time;
7 @4 p+ ^. G) x9 S1 b - : c `+ F$ S! c" h: x. o3 i6 J
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )8 _) `; l( B# B8 j# N, p
- {
, `6 w; Z( L( ]4 d$ b1 E$ @ - return 0xff;
W0 V# w* N% h - }5 \/ M+ I' d% m- _5 m7 Q
- ' A4 Q9 x3 g! g0 R
- pinMode(_DataPin, OUTPUT);8 S% c7 @; ~- f* ]; X
- digitalWrite(_DataPin, HIGH);
% K$ L; T# g. j" s3 f
8 ?8 l. T7 k& X" _7 p- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
; P; |' Z4 j& }1 [2 A" A. F - {# T/ a# @# A/ |: X9 F9 S" r2 \" `8 E" U
- old_data = Sensor_Data[0];# ]+ o) [/ E7 Q8 Z6 T
- return Sensor_Data[0];. S2 g% E5 S3 i/ b' |( q
- }4 q' b; ?7 m% i) r
- else7 D5 R; s- T, I( |
- {
7 `4 I' Q' {( ~9 n7 T) P - return old_data;5 n5 n0 p/ Y1 E; J9 f$ O6 z$ }$ @
- }
9 G0 B* ]. d# G - }
5 M) J* [+ e8 t+ R
複製代碼
* w1 R9 ]. E- l) hMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
$ o1 U2 n8 s$ G( v/ f+ g P
% z5 E+ `/ g2 a! o0 M |
|