|
/ c* T( f# p- w P/ u- #include <Arduino.h>, |; q! G0 f4 T. k! o! h' ^
- #include <MeAuriga.h>( @) b% B. f) ~; K, k
- #include "MeLineFollowerArray.h"
+ w' M" ^' ]5 }: P5 L! X n - D3 x \ _5 L* | c2 b
- MeEncoderOnBoard Encoder_1(SLOT1);
4 b z0 f' T+ y5 I& y - MeEncoderOnBoard Encoder_2(SLOT2);
% X7 R! x1 s/ I7 W4 N, @7 U" i - MeLightSensor lightsensor_1(12);. u: L. f, h+ C G, Q
- MeLightSensor lightsensor_2(11);
& f7 s0 Q/ ]& W) |! a' B - MeBuzzer buzzer;: \. f: w' s5 g0 t1 F7 ]. u
- MeLineFollowerArray linefollower(PORT_6);1 e. t7 P; R2 Y+ [
" e6 b0 d+ I; l8 y- #define Error1 1/ _- e+ v+ k! t. n6 G% d3 C
- #define Error2 2
4 ]. j) `( F& j+ x6 w5 B' Y - #define Error3 3: Q6 Q9 e* i! ?7 E! |
& _* q) e; ?% Q" r- #define Kp 15
& s4 b* p: N# ^: K: C - #define Ki 0.15
# c7 l" A; }) L4 X - #define Kd 0.03
N! O6 h8 i! l% A* J% c
+ ^9 W$ |0 J0 J- uint8_t sData;: L* b$ q) w# j* Z5 {4 t* }, \) s
- uint8_t D1;4 g& H A! j3 d& O5 z6 D
- uint8_t D2;
; @ b8 _7 ]9 H" _6 B, G - uint8_t D3;
# C& S% @- H, S }7 `- Y) J - uint8_t D4;
8 ^% j! y' J, e9 R# |* o - uint8_t D5;
- n0 B+ W4 y/ R+ p# m0 i6 v% L - uint8_t D6;
! [( g7 t$ X u$ ~ _$ m - 7 ^' p1 f# b" E, Y" D
- float previous_error = 0;
. Y2 t3 P9 Z3 z6 l& ~5 A3 t - float integral = 0;8 V- y: ~; A x0 V6 w; w
- float derivative = 0;
! r; F0 U/ E0 C$ L/ ]# `0 F - int Speed = 160;" R+ Z5 M4 J6 a
- float output;
( X0 i5 l" L9 n8 A0 f' ]6 t2 a# j
8 j4 f- ^$ f. o8 a- byte Left;
5 B# r: c, m& G6 F6 x) s+ x/ d
+ Q' [. D7 A" P: M- void setup() S% H+ e/ e0 f, S; _8 m: J* u8 _; l/ r
- {
3 Z9 j) E- V1 x* q' n7 y" x - //Set PWM 8KHz
# D6 ~2 y2 _. N. r - TCCR1A = _BV(WGM10);
( r7 f* a; [3 c n& v - TCCR1B = _BV(CS11) | _BV(WGM12);# z& p& H: K8 g+ X( P
- TCCR2A = _BV(WGM21) | _BV(WGM20);& o" q% r/ b$ }0 D% S s: |) J# c
- TCCR2B = _BV(CS21);1 k+ i, Z; W$ |1 q, s
- Serial.begin(9600);
+ A- M4 b$ v; H9 L - buzzer.setpin(45);: a/ K y8 O& Z* B1 r. L
- }" U) ]0 L3 N/ I. ] }/ d
- ! P0 |5 T9 C( i
- void loop()0 X8 D, O# r1 [, i! H" d
- {
3 P( o# c, Z. z% n: [ - while(!((lightsensor_1.read()) < (10)));
, P$ L/ V9 n3 _! S) n1 V$ X5 [ - buzzer.tone(1047, 500);
* E, l! C" y* c& H; B( R - delay(500);' |3 C6 j: i4 k
- do
; r8 r S1 b+ |% R - {- J& p8 ~- U4 L2 ^4 x
- int Err = getErr();
4 L& L. R' @9 b% B' V+ I* C- T% w2 U+ o9 O - if(D1 == 1)
4 F! W8 ~$ M0 U - {
^: |. X- d8 [ - Left = 1;
1 C& m' h$ |% f - }3 O% f2 P3 H4 z: k
- if(D6 == 1)1 c- H% P6 _' Z1 i
- {
4 T9 @7 Z. Q u- B; S6 g - Left = 0;, `6 T4 ]: t4 g ~
- }
; ~! j j5 c; t5 F& E" t2 o+ W { - if(Err == 99), m, z" t. Z0 W& k
- {
& ^2 ?+ E5 {+ n$ C; X - if(Left == 1)1 c3 c& ~1 ?2 k {) j
- {8 H( j* j1 Y$ r! {- N W! G) s5 A5 j
- Speed -= 5;
$ H* e# |% G2 x- i - moto(0,Speed);
" W( m7 R% H- L }- W+ i' D - do
8 @ W L$ I7 H3 P& [ - {
q; C2 B2 \: p t# i - Err = getErr();
8 ?; u: Q# N. H) a4 G# W; i$ ] - }while((D1+D6) == 0);
, v! k; V9 Z3 Q! r - }
2 x5 w3 q- P# y - else. E& N+ p+ i; M/ u2 U
- {
: d4 J- ?0 K& W. W r - Speed -= 5;
, K& G+ g: H; o L7 ^7 {" N - moto(Speed,0); 0 Z# Y5 U2 G; z0 R
- do" l) u2 R' p8 ^6 o) G0 B
- {1 g1 o( r9 R8 S8 \3 L$ c" o
- Err = getErr();
$ Q( S0 u3 f- h/ e- F2 Q - }while((D1+D6) == 0);
8 W9 s0 m0 |8 R+ Q: u$ { - }; S) Q/ a5 @ c0 O" |* {1 K0 k5 H
- }- p% p. x: C: w; X. E% y
- else
9 o" \& n" v! ~0 Q- J: V - {/ K- I# U5 u1 M7 D9 }" ]. X; y
- if((Speed < 160) & (Err < 2)) Speed+=1;
* R4 w* E- B6 T N, ~' b - if((Speed > 100) & (Err > 2)) Speed-=2;
- a$ _6 V6 U- c1 K( w - integral = integral + Err;
* h" d( _' R5 T X - derivative = Err - previous_error;, f" O8 Z( \5 s6 V$ K7 [, z. |
- output = Kp*Err + Ki*integral + Kd*derivative;
( t3 p* y+ m7 F+ z& M4 c7 h. k - moto(int(Speed-output),int(Speed+output));6 i8 ~+ @ r4 T& c. T/ E- u5 E
- previous_error = Err;
+ B; ^2 ^. n/ G9 e- c* y, B - }5 `; h. b5 u3 B. E& |
- }while(!((lightsensor_2.read()) < (10))); M% f \* p$ O# c) H+ a- B; p6 K
- moto(0,0);
0 c8 A/ e, m+ m - delay(500);
) p! K, f' U9 T3 o8 n - buzzer.tone(262, 500);
) _/ B" k! H0 ~& R6 Q - }" {# V" O* f) U6 H
- ; Y w9 A8 R# z
- int getErr(); T& b5 p" E: v* B& n
- {
$ X6 \1 [ j7 ~ - sData = linefollower.getValue();. E1 @. t0 ^: N' J0 z5 |
- D1 = ~(sData>>0)&1;
2 \. b: F, i7 h, a - D2 = ~(sData>>1)&1;
3 |+ c1 k$ S* `5 f - D3 = ~(sData>>2)&1;
# o; a" r( b1 Z; u5 i - D4 = ~(sData>>3)&1;" L; p2 {9 l& q9 O1 Y
- D5 = ~(sData>>4)&1;7 ]9 J' H5 O/ ^- o
- D6 = ~(sData>>5)&1;
* K4 H' \8 k" v9 S( f - int downD = D1+D2+D3+D4+D5+D6; N% E9 P1 m3 ~% b7 O, e. X
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);0 T! A* t2 c m. Q. |
- if(downD == 0)
3 u& x7 ^+ A3 y8 P4 x) I - {
$ v# P$ b6 I I; W1 w6 U6 }7 G1 [* f' T - return 99;
' m' G X2 @" l9 m) [0 M. u - }* A. n0 C/ h2 `7 M
- else5 G$ r" \; c! h9 n( a* f: ]: E" f0 P
- {
" X7 Y3 A! O9 Y" p" T$ Q1 E - return upD/downD;
; J) j- f) G4 A# T - }0 L4 u7 d- Y# B6 n
- }- n: n) g( {2 s& H# [0 g
4 j0 G: j& f% X, y, U( m3 R5 C- void moto(int Speed_L,int Speed_R)
$ ] H' U" r$ R) \ - {8 Q- { R$ w( O9 K% _
- Encoder_2.setMotorPwm(Speed_L);
+ r& P( D1 f1 P( Y W- } - Encoder_1.setMotorPwm(-Speed_R);
3 b& w6 v! U& f$ {8 d9 K - }
複製代碼
, b0 M/ j( k( p; p1 f% L# a" _: pMeLineFollowerArray.cpp
6 z/ ?" U8 D4 N- #include "MeLineFollowerArray.h"
3 G+ x$ P: m- O: @
0 @: t4 `2 i: w, p: L- #ifdef ME_PORT_DEFINED) X4 {3 C: o- v, e" i" P6 U9 O m7 i
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
: o3 p! |8 ~% E3 L- G! N" e - {# p% S1 M8 g$ d
- 7 G8 f; Y V7 w9 ?) H
- }
* J9 j& }0 d+ \5 v - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
, d! k% ~- |2 A& j - {
8 r6 C$ q/ L* A1 U/ t - _DataPin = mePort[port].s2;; m' I i, }2 b! g* r
- pinMode(_DataPin, OUTPUT);
: d6 k5 x2 z w - digitalWrite(_DataPin, HIGH);+ n' V: i' l9 V# h7 Q: L
- }* T" M% f) H% Z7 s2 c- C! O
- #else // ME_PORT_DEFINED
+ @ y) G- L# F - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
" Q* K7 \7 ~7 H) { - {
/ Q9 r8 V/ F# u5 U4 t" Y - _DataPin = pin;7 r9 E/ X N; _$ w+ I7 T9 F
- pinMode(_DataPin, OUTPUT);
. K m$ m+ E( x" O$ \( F - digitalWrite(_DataPin, HIGH);3 q. C/ A5 W- \% A
- }: S4 i$ ?3 I- h0 V: E. [4 u
- #endif // ME_PORT_DEFINED
! Q4 f* [2 L' W# r% a( a6 T Q
# l. P) L* R( _- J/ F3 g1 Z1 X
" r" w! T4 ]( c7 \4 X- void MeLineFollowerArray::setpin(uint8_t pin), m7 i6 L h, b# F" r, e
- {
* o' o: F+ r6 H4 Z6 G - _DataPin = pin;/ a/ |# w' T8 E* S. a) c. F
- pinMode(_DataPin, OUTPUT); ) O7 A0 I2 p1 ]* J: N1 e
- digitalWrite(_DataPin, HIGH);
, I0 _# B( G Y% n- x* H - e8 J( t/ Q- G
- #ifdef ME_PORT_DEFINED. o! b/ _+ l5 w2 O% S
- s2 = pin;6 V- H* U' P4 R0 `1 s; L8 l
- #endif
s( H. w" q1 w' \+ f6 o- C - }
2 C M( c7 e/ H) O% d: ` - ) R' S% J0 M d
- uint8_t MeLineFollowerArray::getValue()/ V- g# x5 ~# V* q4 F% G4 R
- {: C" @- e0 a0 A
- uint32_t LOW_level_read_time;
' V, B( k4 i% |& E/ `3 } - uint32_t HIGH_level_read_time;
V( Z1 l2 N3 E9 e8 L - uint32_t time_out_flag;8 }3 P* W j7 }! I0 U
- uint8_t Sensor_Data[3];
- `: t6 X* w2 {/ i7 s2 u - static uint8_t old_data = 0xff;
- E( D$ d7 v l2 S - 4 A: I) D+ g4 n
- pinMode(_DataPin, OUTPUT);
) f0 M) T' h8 L, B - digitalWrite(_DataPin, LOW);8 j; ?4 V5 ?* h6 t
- delayMicroseconds(980);
+ O! g, L7 A# T$ `! ? - digitalWrite(_DataPin, HIGH);0 f$ t- p3 s6 P! \9 ~1 {. x
- . C0 ~. a7 |- u% J6 G4 h' A# b
- pinMode(_DataPin, INPUT_PULLUP);
6 g$ Y7 x, q* x, f! q" z1 U, d" n - delayMicroseconds(10);
: T! _2 j+ F5 r* I" J9 o
0 X: y8 M, c3 {5 J- time_out_flag = millis();
- ~. x7 e( F7 N, g9 m0 E$ u - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );8 b# F9 W& x0 T0 n6 W7 S% W
3 C' ?; t$ Z( {; D N( [- LOW_level_read_time = micros();
/ X; o& _5 c. D6 I/ z: { - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
6 X, L8 x) }& M - {6 ~; x, c) W3 o9 K" z2 _9 c
- return 0xff;
/ D. r* g2 X/ o) Y% W K - }# p+ k/ C3 k' H! K K
7 O$ m0 N4 Z% Z# H) K' n, f- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
! ^6 G5 A9 a4 a! E5 U0 x+ {( H9 j
" I! C, a; ]# A. o- HIGH_level_read_time = micros();$ ?+ k4 ^& k" s+ K
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
+ \( a/ k6 R0 e+ e) s6 S
9 y2 C5 [: ]# @0 i/ o& Y4 Q- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out6 ]$ J$ G: x( Y7 I* r ]5 q
- {
) Q: n, x+ \1 W6 T) U - return 0xff;+ u+ w$ B7 h4 p
- }
7 @4 s; Q, }) ], E - , @! q6 }% y% j# b. H
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
2 |( _* {4 r, B' V - {3 g) _ x& v# P! o$ o& R7 b
- return 0xff;4 U8 [/ e! d+ e$ C: X- V9 J9 z
- }8 S# b% e1 a( L X% E! y( d
( M3 v" [4 k: k. e) j* L- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );7 M) V0 u$ d9 ]: Y8 W$ |
- LOW_level_read_time = micros();' M; U5 |6 ^, z
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level; G' i& N% h2 y; t b: D* _
6 V# @7 M% n& O% q- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out1 [( N' p% y; \1 D0 h7 Z% M: Q o
- {
. K( Z/ h' b) N: M3 }% i i: F - return 0xff;
1 l; Z/ Y, M' [4 h - }
, x5 i; e, @! A5 v. m% `6 p
; J- C4 C# v. S9 i- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
1 p; r9 P7 ?" Z5 U' w1 W0 H - {
) B: H5 h' T! V$ Q - return 0xff;& F& O* _( T% _+ V+ j( F
- }
5 J ]7 T# }. r9 k; O - 7 z, t. I7 Z0 Y& f# ~- v4 ]* Y% I
- for(uint8_t k=0; k<3; k++)9 K, l! B& F7 v1 @
- {
8 [+ c5 |3 j1 s/ s3 o7 H' K - Sensor_Data[k] = 0x00;' G) L/ \6 s! m/ G5 S! v/ b
- 8 v; O/ x4 |+ k" W/ k/ a5 {3 a
- for(uint8_t i=0;i<8;i++)& H4 P8 V |. q
- {
# `2 q' q- y9 I6 z - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
3 k' s% y/ \2 k8 _% D* b* \' K - HIGH_level_read_time = micros();
6 ?+ H l. z' Q( r7 S5 U - LOW_level_read_time = micros() - LOW_level_read_time;5 X9 q7 s" c5 I# q# L
- 9 B5 w% S1 o5 q% k" C! g
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )( a8 s* M/ P7 k
- {
) J4 w; a) i" R - return 0xff;
: t- K, x) ~" g$ A; k y% j% K" r - }
- {8 Q3 o( ^' z- x8 V5 [# j( a5 g - 5 P. z0 K H, ?2 m; ~6 Y
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
6 U, t9 x& v$ D - LOW_level_read_time = micros();
# Z0 u( ]/ \: N - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level Y" u; Y1 K# g( V
8 v. Z9 Y1 B5 E. k* @/ C- T0 i* v- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
# O9 W3 E2 H, Z* H( S7 k/ ?6 P - {
9 N. F9 ]1 g; B1 Z - Sensor_Data[k] |= (0x80 >> i);
0 K+ G0 ^; [ K - }
& i+ j% w9 M& G. o3 f - else if(HIGH_level_read_time >= 100)/ C& u9 f6 o0 c, @$ p+ O9 Y
- {
" w, P: A h; d1 Z - return 0xff;' z) [, t. L- B4 z
- }
! J+ R) B- y$ L6 \8 U5 u - 5 }8 e0 X+ j0 L: `5 [" u2 d! f
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
8 n4 S' ?9 ~4 q5 ^* X( u - {
. ~; z* e( B! S( w8 S( _ - return 0xff;
5 ^" [' |6 @2 |" ~& ` - }
; b: a% T& U. y) x8 ~6 p9 y - }/ m& z" [5 Z/ _) U
- }
1 l7 a) `. j$ B/ C
9 |0 p r9 b; H5 u% S4 G0 a- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level. n, X3 _2 J/ Z
- HIGH_level_read_time = micros();
$ F& u3 |) ~0 s0 N! M - LOW_level_read_time = micros() - LOW_level_read_time;
5 r" B0 U( N( d% W" x - 8 Q. S$ K2 j+ ^- g) i- v; e
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
+ q E! Z% x* f' c+ I2 l) \. K - {9 n; X' q% i" S# P$ l! G; f
- return 0xff;0 ?& I/ x' H+ Y! N
- }0 ^2 {/ a9 `: ~9 e4 O
( d! ^3 g3 Z% Z: T& m- pinMode(_DataPin, OUTPUT);' O9 G% D0 p6 w0 I& u, {
- digitalWrite(_DataPin, HIGH);
8 b5 {4 s! ^; k7 `- x) f
+ p( P/ p! W; c! M9 E7 E1 m- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))9 w6 }/ v. i1 D# i
- {: Y. |# V- n& s: p
- old_data = Sensor_Data[0];
( y g; Q1 J7 m* s) G) u - return Sensor_Data[0];
$ \3 @* I0 G+ Y% G; ]' z* _- C - }$ Z$ E, d! M7 K D) Q
- else9 o8 o+ |) ?3 y: F' m
- {
+ ] o6 H' A" @5 k2 z2 U* o' u- V - return old_data;" b, j N; E: i) h0 V
- }
& @* o% d2 S# A1 m$ `! T+ F9 [; l1 D - }
: \; z9 x" R+ E( u; @" s6 A% B
複製代碼 ; K! b2 v" {! S7 z ?* F
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
& |8 r/ |1 s% l, j7 S8 \! Y. s
- J% p7 i8 A; Z6 E3 F
|
|