|
|
' s6 a7 o* L& P* Q" q2 i
- #include <Arduino.h>) d$ D8 o, l; k4 T. R3 G& ~
- #include <MeAuriga.h>, f/ }* E( E m, L5 N6 a$ D
- #include "MeLineFollowerArray.h"( f' b6 f) u. Z# K) }6 D
- & z: ]" C5 a7 N; b, S) ~
- MeEncoderOnBoard Encoder_1(SLOT1);; o2 S- n# [9 F2 J6 e5 A8 Z2 j
- MeEncoderOnBoard Encoder_2(SLOT2);# g/ \/ ~+ n9 Z# r
- MeLightSensor lightsensor_1(12);$ J( ^1 Q& ?' V' c7 }
- MeLightSensor lightsensor_2(11);
" G$ S; K1 ~) X4 N2 M - MeBuzzer buzzer;
$ ^3 m7 E& x% x - MeLineFollowerArray linefollower(PORT_6);
9 \( M+ Y0 Y% K- I8 U - 9 H) C0 B8 M! W( a3 a
- #define Error1 1+ b2 F( j. x- W9 H; F0 y
- #define Error2 22 k/ g6 \" t+ s3 ]
- #define Error3 3, V3 U9 D- M- U0 C* Z, p3 I
- 5 B% j7 i: Q& K6 W9 ^
- #define Kp 15
- T7 l- N; C8 c" j& j5 W/ P - #define Ki 0.15
' k7 v/ b9 w0 k9 K$ h6 s - #define Kd 0.03
6 p# b( J7 U' Q" P' p- C8 M - 0 C) R0 F* e/ o; P3 c: A* X
- uint8_t sData;
5 w4 ~3 W0 N2 p( z - uint8_t D1;) s4 S) m# Z, R
- uint8_t D2;' p+ R& W }9 D
- uint8_t D3;
2 E) ~; ~) m7 b' }- K% U - uint8_t D4;: R+ g9 K3 O% u! u( A: e, N
- uint8_t D5;
- H3 D% ] z4 _" a' S - uint8_t D6;
" h N& _5 l/ p6 ~2 q; ^. J
* X" S9 D4 F6 q$ S$ K) v- float previous_error = 0;
8 s4 E$ g- n6 r8 M/ y - float integral = 0;7 `: _. z' x7 \: W7 Y
- float derivative = 0;7 J+ [+ H) B- Q) g2 |, z# W0 y
- int Speed = 160;
, w, G$ Y: `+ ~ E/ J* z. ` - float output;
. m, o) ]& U, G5 p8 a6 H. J! f1 h - , \) S' R* x( y# ]9 Z% S$ ?) g
- byte Left;
& I# v* L# X4 F6 p+ T! g: l
$ o) E) p0 n: R% w- void setup() 6 N: p+ c/ d: @/ l$ K& J
- {
) K, w, v% V g6 W. { - //Set PWM 8KHz/ k' ?5 B$ T+ L$ s+ o
- TCCR1A = _BV(WGM10);
% W8 B& ] [" B/ D - TCCR1B = _BV(CS11) | _BV(WGM12);
' q# j( N# x P - TCCR2A = _BV(WGM21) | _BV(WGM20);
- ~9 Z( L. `) ?8 n' N& q/ G2 H/ \ - TCCR2B = _BV(CS21);
9 U, q: [$ G: {) y - Serial.begin(9600);
8 S7 S8 q% }2 M$ w! t$ K. y - buzzer.setpin(45);
* \ _9 ?: y6 N* H% l# U - }
' V* \) l! K7 X! T% } - - ~( v( ?' G0 `- l
- void loop()7 k7 V1 B8 u/ Z! @/ ~* p4 y* P
- {
# P# w g! j) x4 f+ `" Y - while(!((lightsensor_1.read()) < (10)));
. _& u$ a, `4 \3 @, D. s - buzzer.tone(1047, 500);$ k% s) O4 H" u7 _$ O. C9 A6 F, z3 a
- delay(500);, _' L) e4 O# ?8 U+ ?
- do2 Q Y3 F( q' ^
- {
2 N# f9 p! X9 D0 d - int Err = getErr();: e+ m+ q" E ~4 O
- if(D1 == 1)
; C: d/ ~' n" H% { - {
. A& s- ^1 ?7 M - Left = 1;9 E$ ]6 ?6 X. ~( a, N% j0 h, N
- }* E3 X+ [/ x! t7 @
- if(D6 == 1)
% W" ?% O u9 O# h4 W$ @ - {
4 u0 i z9 [/ E; ] ? - Left = 0;; p3 J e) Q+ A$ n
- }, n; s2 T- K+ J1 R
- if(Err == 99)
- n! p4 o; ? \. e - {
+ w4 t8 R- E; {* R H7 M$ w - if(Left == 1). S! P( `1 L- r! T) k
- {
3 |: v- {7 a3 x% h7 J& @0 b- | - Speed -= 5;
& d2 x3 I2 I; M: J8 c' V$ J1 A - moto(0,Speed);
4 j& A4 S9 W3 \0 f! j - do" G; h$ X9 T2 m; D% U
- {$ a( ~: D) Y; M5 b1 u9 `1 N. F
- Err = getErr();
# V* E# @3 f( A; X# R0 l - }while((D1+D6) == 0);7 S; a6 S: ~( z* w P: y2 e6 G/ J
- }
: T }0 s) i3 O: Q9 E7 Z+ z - else
3 z; Q* C( Y" E, c" s) }* B - {
) [& i# d0 w! l; ~ ~0 p4 v& k - Speed -= 5;
_% r3 B: { c1 Q$ [1 ]! G' c - moto(Speed,0); ' ^" q A: q# u2 n9 W& i
- do u1 B2 s+ j2 j
- {* B. w) a7 J3 Q
- Err = getErr();4 Z$ W% u% p% y% T, f- P+ s
- }while((D1+D6) == 0);: W* V5 z) d P2 b- c) o
- } ?" a8 [; [. s3 s. t4 Y6 V$ G. Z
- }* W" ]2 Q9 Y% s+ Q! N3 i% ?, w
- else
( _" Q$ T. |0 R& d [$ A; S% J - {: T' ]- k* |0 {5 s! b( g
- if((Speed < 160) & (Err < 2)) Speed+=1;
1 U, O) p2 P3 D1 F - if((Speed > 100) & (Err > 2)) Speed-=2; 0 r4 r, `" I6 \1 `' a" L4 T2 H
- integral = integral + Err;
0 G4 P3 I7 k1 U2 U - derivative = Err - previous_error;2 k* F; C6 T( s/ h5 y! ~) O
- output = Kp*Err + Ki*integral + Kd*derivative;; O( {/ @' |6 i: C
- moto(int(Speed-output),int(Speed+output));) Y2 L- a2 m8 e$ P
- previous_error = Err;7 I0 n1 g9 D5 G, c2 h9 T7 z0 E
- }
$ V2 m- T" a3 C) i- } - }while(!((lightsensor_2.read()) < (10)));
6 B0 s+ l! r* z( x9 | - moto(0,0);
* E) w, S: m/ V$ l; p S - delay(500);' }/ u: R8 ^# b7 a
- buzzer.tone(262, 500);+ U' U; H6 ~* T! O
- }! A' u; ^" C8 f2 u4 N! l8 B
/ y) |, A+ g: x2 j, l# r8 D- int getErr(), L/ K1 A! J( y+ f) n
- {
. }. j' m: P" q' g& b! A - sData = linefollower.getValue();
$ y3 r" \8 L3 a. |9 f3 H1 Q - D1 = ~(sData>>0)&1;8 Z" p9 e1 J+ g+ O# X( f5 \
- D2 = ~(sData>>1)&1;3 I' I. M$ U+ e f* f. ?
- D3 = ~(sData>>2)&1;
" k( n, d/ J p* K2 b5 @- x - D4 = ~(sData>>3)&1;& ]) }% @- u" R2 y
- D5 = ~(sData>>4)&1;! Y Y* ?+ E7 ~6 F1 L8 G, j& J
- D6 = ~(sData>>5)&1;
! y8 K7 V. }; i4 E4 v - int downD = D1+D2+D3+D4+D5+D6;
2 f' S. `4 ~, q - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);# B+ J# R5 \$ q+ V5 m: c. G
- if(downD == 0)
/ ?: v5 k) h, A - {& t& H- i( @5 ^+ V' @ A( G& k
- return 99;
' S0 ?8 n8 J* R, d$ L - }) p p% Q: g9 |( _7 @! Z
- else
) _2 n! [7 x/ i' ? - {3 i& o& v: }2 p* `3 Q4 ^
- return upD/downD;
7 R/ G, d! k1 d, K4 @+ K C; I1 W - }+ t6 N+ G' w. Z; W
- }; W6 l# r/ `4 m# k3 _ {& T9 Q+ w
3 _+ O% Y+ D4 A7 E( g- void moto(int Speed_L,int Speed_R)
! \$ L; W6 `& e - {
; ~: Z1 U6 H9 A( O - Encoder_2.setMotorPwm(Speed_L);
" M* l; S) p: a- A% F - Encoder_1.setMotorPwm(-Speed_R);
/ `: Y( ]3 f p2 S8 Y2 K. y8 G% O - }
複製代碼 2 H5 c& l' P' Q- P( R; Q& W) y
MeLineFollowerArray.cpp
9 G% S2 j( I$ ?2 ^% V B- #include "MeLineFollowerArray.h"
% j( U% c& K8 a4 }( F% G( H
# U7 Z# c" o3 X, u k- #ifdef ME_PORT_DEFINED
4 x: m9 J8 I! L - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0); {/ p* A2 ^6 L1 s$ D
- {
! Q( {* r2 e t- X' Y v6 O% X, D8 f X - ( |4 V, y4 z/ p: e; C
- }" [* v1 q5 n: ~: ^2 x; g; Q h
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port), n3 x) G: B4 |! M0 D
- {
" ?3 Z: j5 O* U# M5 ]: {3 Q6 ] - _DataPin = mePort[port].s2;, T( B0 t2 | L j8 f
- pinMode(_DataPin, OUTPUT); * l9 O2 T1 ~# m+ O4 N0 {# a( Y
- digitalWrite(_DataPin, HIGH);
8 p3 N3 J+ v( a" }6 C - }
" ^ f( P) F& I4 [( U - #else // ME_PORT_DEFINED: e, M7 q0 \" y9 @* m$ P
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
/ T" T/ o3 n& Q6 \5 T8 H' Z - {
/ P3 v0 B' W, @1 e( h9 p5 k! c+ L( F - _DataPin = pin;$ t% B+ N) e! ]. q0 @; F$ z+ M
- pinMode(_DataPin, OUTPUT); 5 w0 q- |; t! }' _$ x8 h
- digitalWrite(_DataPin, HIGH);! G0 W2 f! c# O9 w* E5 R
- }
2 {7 u3 n0 c7 t - #endif // ME_PORT_DEFINED
; a. \- Z% b! ~7 L6 [5 P
C B4 ]4 b$ s
/ a1 C' U6 P! q, T* H9 G+ u3 Z- void MeLineFollowerArray::setpin(uint8_t pin)
0 D4 q! p5 C- t2 S - {+ [: C" Y$ U9 d4 I+ `" D$ c6 Y
- _DataPin = pin;
9 N5 n1 a1 F' W- a' r" n - pinMode(_DataPin, OUTPUT);
0 w. J7 @# n( a1 C; K - digitalWrite(_DataPin, HIGH);
3 E" I; c {5 ?% o+ k - $ M0 J: f9 q) @ j% i5 P1 t
- #ifdef ME_PORT_DEFINED* H2 u0 v: [# s* Z, T! j9 |; p Q
- s2 = pin;
4 t% n4 L/ U/ _4 z3 @* u6 S' k1 K - #endif" k. B k& P) Z
- }4 S- p" m: g$ n" d% S$ M. s+ [
- 4 ?% [0 q( r6 Z2 A
- uint8_t MeLineFollowerArray::getValue()
. j. d( [9 f" M4 s: t - {
; }3 J Y: l. [6 U - uint32_t LOW_level_read_time;
& M3 `3 u, J4 L3 F& m - uint32_t HIGH_level_read_time;$ X# G0 ?9 A: z+ o' o# @5 y
- uint32_t time_out_flag;5 Z Z$ x6 h+ @" B4 U6 ^. H
- uint8_t Sensor_Data[3];! ]3 p0 j W) h1 u$ t
- static uint8_t old_data = 0xff;
2 J6 \. h8 I7 X! s* r4 ^ - ( X( V/ M% l0 x. p7 o! T2 G: k- I7 l
- pinMode(_DataPin, OUTPUT);
: x8 r; S0 t# W M4 s0 {4 M" s2 D" X - digitalWrite(_DataPin, LOW);; m' q& X1 |5 [% |6 k: \+ u( \
- delayMicroseconds(980);
# G' y: ? J# x - digitalWrite(_DataPin, HIGH);
1 I3 x) a) e' _' @ - # x k( A) Q6 b1 a: Q u) b! P/ N
- pinMode(_DataPin, INPUT_PULLUP);0 D) @! T! b/ M
- delayMicroseconds(10);
5 z0 v2 C% l% u/ _0 \4 Q
$ e% p8 k. e8 p- time_out_flag = millis();
1 f, L- p& G* h. E - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
6 G, d1 Z$ }7 a4 v5 C - 7 b% Z8 t: `# @' R" b
- LOW_level_read_time = micros();
4 _* t$ b6 n4 ` - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out5 C4 h7 u0 P5 l! ?
- {
' d0 E4 ^2 x2 Z# E; [6 @/ z! b/ ] - return 0xff;& n- w/ Z2 I+ ^1 F F& C: P1 u3 G
- }
5 V: b8 c& \* Y, r. V
: p, ^! H0 Y! H9 E- i- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
0 J( I% N2 O9 H - 3 [/ @; }. F* x" j( y. d) R
- HIGH_level_read_time = micros();# b) Y& S. O' u' E
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level& T! |2 W9 [. B" v
4 L( [' p# X2 E/ U; c- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
+ G, E: t$ W3 S& u C" m/ J - {
/ ?# X& C4 P9 p, g2 i# ` - return 0xff;) [: p: e1 g9 |; D5 t% x
- }! R q: V. F' `7 V
# R, S3 G2 b8 E7 F" ^ j% x- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
, ^' n7 Z/ O9 C! o - {: I, M8 K0 c! c9 j. g) N
- return 0xff;7 [8 U; c3 m8 x" f
- }
5 H- K1 p$ d& [- \0 k/ h7 K9 u5 `3 N - 2 a' K l6 f& U& ^
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
1 z% C& ~/ E/ L' m4 a( k) r/ _ - LOW_level_read_time = micros();
7 V3 ~- }9 }( E+ S ` - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level Q) d; a5 s1 W4 t& ^& n
8 v" h* t1 u: S- F- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
* B9 g/ i3 ]! m' p - {" j$ K# \( e g3 E: H- X2 c' X+ V
- return 0xff;
- ?: \3 \4 {+ W* I6 }4 W& H - }
# R. E+ p% `$ \+ M) P; D0 Z
, r5 K6 p% q+ Y* |: @- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
9 P) P$ j7 x, `; C( I - {1 R- o% o2 P1 q/ P% u2 k! j
- return 0xff;
% V. @8 L# W$ x2 ^( H; I - }
) M( n; w; O- y8 `8 o& c
! y0 S+ U) x7 Z* t* P- for(uint8_t k=0; k<3; k++)/ M7 i/ {; L+ Y) n6 a7 g
- {8 D4 |( ^$ E0 N% h: E0 k
- Sensor_Data[k] = 0x00;' i' Y1 n. E. G5 J. y" e$ p3 i# \! l
+ x: Y- U! R* c2 a" j- for(uint8_t i=0;i<8;i++)0 Z3 x, v3 d" ?
- {9 W I) ~5 Q0 ^- j
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level: u, Y: H& V. L5 I% s
- HIGH_level_read_time = micros();# |0 m$ i" g/ _3 j6 d$ p
- LOW_level_read_time = micros() - LOW_level_read_time;
8 w( E- ?) N0 d6 b8 _* M# R, L/ p - - J! }/ v& w' l- s5 a
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
& O: q E @6 I - {" T- m5 @( a* n6 B
- return 0xff;
3 M$ |5 O& C5 ]4 x% j - }9 ~& X2 N v4 z; H5 Y5 x
: y6 _7 F0 d7 m9 s* ?6 J4 V- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
C% r( s2 B. w/ h1 X - LOW_level_read_time = micros();% j4 n* E# m' M2 |% l. ?4 i2 f, z
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
# A0 ]$ s4 K$ i0 [/ r" j9 } - 2 g% l$ Z1 C( J4 _' i j) G
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 11 A: X' E) j B, E/ v* F1 x z
- {% D" L& O6 D- w' p3 k
- Sensor_Data[k] |= (0x80 >> i);1 k+ g. p$ c- b0 B
- }* Q \* v& }5 l3 \
- else if(HIGH_level_read_time >= 100)
& F e+ t: m6 x; z7 j; P- m - {
+ Q3 }9 Z" b# D" `$ q! v" ] - return 0xff;
5 W8 m# T2 B$ t# G - }
2 T$ x ^+ M! S" G8 r6 d. G6 W - ( r' a1 E# J2 _& _7 P; J
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
1 [# r( b. S! U, `5 [9 P$ f - {( t5 q8 x) L* r! c2 ]) J
- return 0xff;7 h# I' \5 x1 K: Y9 G# `" g R
- }
. `5 W" L* G4 X - }% T! q" c8 L- z6 j( D% U6 e! ~6 S% J0 O+ t
- }6 |6 A- h7 k3 m# V7 \6 c
- I7 P- t+ y" c) A5 w6 z6 A- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level. H9 A5 N \% q L4 O! ]
- HIGH_level_read_time = micros();
2 P! B) C! T! b0 M0 L# u - LOW_level_read_time = micros() - LOW_level_read_time;. R+ ^2 U3 T$ G# I" c
* x. Y6 P$ n$ \" z3 C' E% A- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )' p+ Z. g6 z5 L0 k( f2 M7 F' a# b
- {
/ D: |$ @0 I( y1 o) c% y - return 0xff;
3 h/ g3 f7 d8 g - }
6 z' Z3 m2 ?; g+ H - : w- _% O# D; Y1 |7 V% q) `
- pinMode(_DataPin, OUTPUT);
4 a/ z" f2 {3 G/ k9 I) R - digitalWrite(_DataPin, HIGH);2 a" S9 R) a. E8 i
- 9 ~" V. N8 `! A H3 p* ~
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
5 x8 Z! j$ o6 H7 X - {
# }+ C- L! U6 R, D& K; R/ e' W n - old_data = Sensor_Data[0];
2 X" f- w) R. k) T1 N- I+ C9 u, i - return Sensor_Data[0];
0 F4 `$ H y. v9 S - }, U) G, e5 |" a; K1 R+ m* l7 D
- else+ B0 Z% y4 B' a- X1 }% n
- {1 h7 w# L7 Y6 I4 v- ?4 [& u" W
- return old_data;
. n( Z! N+ C8 p' E5 t - }
, \7 r$ v6 P, k. W - }
; V/ z) O0 B; N, J; _$ j
複製代碼 ! [6 Y, M* e' l7 x4 U3 ? c
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
6 e+ q# b; t3 w6 ?- W. G \3 x
|
|