|
|
) R" C! G" O9 B0 f- g6 z- #include <Arduino.h>& @1 l8 N0 l9 G0 H/ w/ J: J& j
- #include <MeAuriga.h>
6 x/ _8 J. K" ? P, ^ - #include "MeLineFollowerArray.h"7 O/ G! E! m8 x% \! H' l
- # e% V9 [. p( \6 I# @9 `- b' K* P2 D
- MeEncoderOnBoard Encoder_1(SLOT1);% |% d/ j% h# V2 w
- MeEncoderOnBoard Encoder_2(SLOT2);* p3 b- O) B( @: n4 q6 W' ^1 a
- MeLightSensor lightsensor_1(12);5 s8 \" ?+ r+ j( |
- MeLightSensor lightsensor_2(11);
" v. |9 g1 v9 i& t - MeBuzzer buzzer;/ f* X) E' x; h# t6 p
- MeLineFollowerArray linefollower(PORT_6);* a5 @9 p& y" H3 l
* b4 C7 o4 Q5 }- #define Error1 1
' d+ d3 A- s6 Y0 L# g$ V - #define Error2 27 t: b' N* l) j" V2 |
- #define Error3 33 D8 K0 ]- }5 y! m+ e+ ~
* Y# M0 h4 }; T) x2 @- #define Kp 15
* }7 r% l' w4 n0 l/ W - #define Ki 0.15
6 Y4 h! L8 f+ g* k8 M0 j - #define Kd 0.03
: N: k' [; ~. \ t - / V+ j. D: F4 Z t5 u
- uint8_t sData;# v3 w- O: F v8 j1 R9 \7 l+ d0 }
- uint8_t D1;
, a5 H, w6 N5 K7 c @! R - uint8_t D2;
! ^# A E; J+ z+ S$ l9 h - uint8_t D3;
/ u. f; ^) m' P" e8 f% F - uint8_t D4;$ E/ D/ h& `- U( W7 g! N
- uint8_t D5;
0 ]: m* B$ z5 X. K; ]" S% U$ v# }4 K9 b - uint8_t D6;3 T4 t7 h- z4 V
- . ~3 Z/ r" ^% d! ^1 ]
- float previous_error = 0;! v6 _( A |$ }* G. Z* t
- float integral = 0;
! a- W J1 f# H6 j; `/ Z - float derivative = 0;% N( E5 X( j* R# V0 {' R
- int Speed = 160;
' N" r t4 f, N/ S6 e3 t) w' c - float output;8 \' K% \& t% w+ Y( Z1 l$ P0 Q
- - x5 {. j' U* ~. A) h
- byte Left;* n1 I: \: `9 E, L- L
, |; e( O0 z1 z+ [2 @; H8 e2 S! z- void setup()
; z& u# d1 b* B. d2 }# z# X - {
{& ?/ @% n+ Z: S - //Set PWM 8KHz
/ f+ H( k" z5 h6 m - TCCR1A = _BV(WGM10);
3 c* ^, I/ d: b2 V: W) M* Q d - TCCR1B = _BV(CS11) | _BV(WGM12);
3 |7 C! X: c# d; \ - TCCR2A = _BV(WGM21) | _BV(WGM20);- {5 j; h' I# t8 |
- TCCR2B = _BV(CS21);
6 A1 ^1 F5 _2 b - Serial.begin(9600);- u! n& z" R9 k3 k- X9 ?8 [; n0 w
- buzzer.setpin(45);
% ]7 \. F# n! j1 P: Q - }3 O" q# I% Q8 T3 ~
$ M! S" x- ~) z- void loop()/ x/ t) A" a. G9 C$ `6 F
- {
{" \* b3 @& X - while(!((lightsensor_1.read()) < (10)));
0 _6 ^ `5 t( @& r1 o7 k; P% ^ - buzzer.tone(1047, 500);
* U% F3 @5 }# F: @& ?+ v% y9 k - delay(500);
' i) I8 s6 h9 g8 C u; t - do
" e4 C0 ~& n4 x, Q - {% p/ w- b2 m) q1 A2 n& Q3 b4 Y
- int Err = getErr();7 Y: w" g- O, x# ~7 V% T; Z9 M
- if(D1 == 1)& I g5 c+ }* v& P0 w0 r
- {3 l4 R' y& O/ i, |/ C' F
- Left = 1;
* t3 ?& t! Y0 Q& y5 p% Q - }" U* ?# E6 m; q
- if(D6 == 1)/ v# S4 R/ s) h$ D* x8 ^
- {
4 A+ t5 X- @1 C6 \/ G - Left = 0;
4 \1 I0 {' b& I - }4 T% e8 ?( E7 B* p; {2 v
- if(Err == 99)# C# H( O/ \$ w5 d2 a; Z
- {
% F* A+ l! f- B+ x& [. Z; w - if(Left == 1)# c$ L* @0 Z! B8 E
- {7 M& W2 m5 g" L" P7 C7 s
- Speed -= 5;
" `4 N3 z, S( u& A - moto(0,Speed);
( O5 T Y+ j; N" v6 j% Z4 X - do2 M9 T( X% U6 i0 j9 B O
- {3 H( v' [7 ?& k4 v% b% ~' w
- Err = getErr();; m. a f3 ]8 k" g* g z
- }while((D1+D6) == 0);
% j1 }) M9 ~3 g9 Q7 g$ I, u1 L5 ] - }
* Z" w4 X: b3 K. w7 U3 s8 @ - else" x$ N; m9 T, R3 G1 g+ s- p4 N, \
- {% H# j L4 r; p+ V7 W5 ^
- Speed -= 5;/ d! s3 Q j* J3 M5 Z
- moto(Speed,0);
; i1 o3 R, k$ j6 S2 Y5 R( K - do* x* \, F6 \6 F1 @
- {
6 u8 A! l% F3 `: i1 k/ j - Err = getErr();2 \2 n7 \( V) k4 @& |; R% Y
- }while((D1+D6) == 0);
7 t0 @/ E- y) Z1 o, r5 g - }% O$ t5 S. B6 I. i4 z/ I {
- }
, T# j: V: {( t: a _5 j - else% R8 V4 q/ V _5 R+ C4 q3 e) |0 {
- {! t# b9 W# W" V6 o
- if((Speed < 160) & (Err < 2)) Speed+=1;
8 o! T5 ]! l; l$ [: C- v! P - if((Speed > 100) & (Err > 2)) Speed-=2; : h+ \- H1 V4 t' h6 W# O p
- integral = integral + Err;
# h% L7 s: y* \ - derivative = Err - previous_error;
6 F0 r$ T9 V; F/ n6 `# e! R/ d - output = Kp*Err + Ki*integral + Kd*derivative;2 f( D" s4 T0 Y. m" s9 p
- moto(int(Speed-output),int(Speed+output));/ v8 q- ]) p1 r7 v' u7 P
- previous_error = Err;
: ]9 d$ a/ B7 r% m - }
+ c5 o r* G l6 O) N) B - }while(!((lightsensor_2.read()) < (10)));* k4 i; L$ P5 h3 \# q) i
- moto(0,0);
% M/ b: [6 q& R/ W$ D - delay(500);
# M$ I6 [; n# J: t( | - buzzer.tone(262, 500);* u. s' _6 s8 i% Z) H2 i" P4 T
- }" D) L9 h) \5 g3 j" j
; b1 w$ T' S8 |# l7 H# d$ ^- int getErr()! @' u) x- k; i
- {
4 W3 c% ^, b2 ^7 i - sData = linefollower.getValue();- `4 D- J+ _' P* C ~# I( h3 o* |. T9 D j
- D1 = ~(sData>>0)&1;
+ F1 b6 f8 {& L; k" M - D2 = ~(sData>>1)&1;6 z! `: f! n' f6 [! r0 `
- D3 = ~(sData>>2)&1;
6 h* _8 J# f* n7 l# ~ - D4 = ~(sData>>3)&1;6 J* w) a* z* A/ z$ S
- D5 = ~(sData>>4)&1;
, g) w% P( R+ K- w$ P, S8 A - D6 = ~(sData>>5)&1;
9 ]& P, a6 ~; [ - int downD = D1+D2+D3+D4+D5+D6;
- u! _; W8 W# O9 [# O3 J$ q - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);" W$ y: {. N6 v2 M$ ~
- if(downD == 0)# M% f8 e: |$ f: \
- {2 u" \4 s8 E8 [/ E8 s- t$ `
- return 99;
% |) f! E% K- U! A: [ - }
, I9 K# P. s7 [' P8 j - else7 m+ x% q1 c4 }0 [( j6 g
- {! u" ~3 n( w- b- k/ ~# I
- return upD/downD;
5 _. l5 }. c3 G% Y - }% a) W, d* L9 x- e) `
- }+ t& Q3 ^. }8 p- S2 I
; T' M5 i3 ]/ [0 [5 E4 k$ g1 N- void moto(int Speed_L,int Speed_R)
9 c& \ z& {; ?- t1 @/ Y - { I. @3 B9 [0 @- q* K. {8 P) K
- Encoder_2.setMotorPwm(Speed_L);# ` f! h* ^ U0 m* Z
- Encoder_1.setMotorPwm(-Speed_R);& N/ U, t3 w/ X0 B7 l1 H( `
- }
複製代碼
" p+ z9 k1 [5 ?7 j, jMeLineFollowerArray.cpp5 j: x3 l4 `8 B6 e" [& z7 L5 k
- #include "MeLineFollowerArray.h"# h: ~3 S, {5 i2 P
1 X" H, O& Y; K; A1 U( {& |) ]- #ifdef ME_PORT_DEFINED
- z n1 E( C B) z- V - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
4 n' p# F! A' `$ N - {
( c; M9 m6 o8 D' `; E/ s/ A% j
( l- K$ r' K0 C! F- }
. T6 a0 `/ m4 G |9 s5 _ - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
9 Z) I3 c7 j3 C$ \ - {6 R g8 N5 W9 t6 \7 h
- _DataPin = mePort[port].s2;4 R5 O9 A- U) c8 x6 {% a9 ?
- pinMode(_DataPin, OUTPUT); # M. }, d, T2 W) H. u. F
- digitalWrite(_DataPin, HIGH);9 _3 ~# \$ f s+ U
- }3 I" |7 i" ^- `" m2 Q7 G. \
- #else // ME_PORT_DEFINED
% K6 l3 A+ R, `% z ~: G) z - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)9 i" ]4 x; S; E' U" C
- {
9 B+ [0 x* C6 j& m - _DataPin = pin;$ b+ k0 w+ U. y0 }" ~" r
- pinMode(_DataPin, OUTPUT); 1 A' g; d# n9 W. W
- digitalWrite(_DataPin, HIGH);6 d$ @8 f: h' r+ U7 J
- }
$ H& F) A; j6 K. I4 U3 R - #endif // ME_PORT_DEFINED
6 e$ v5 j4 m: U; J4 h
9 j' {- F& |0 y T2 r) X- 5 h& |9 y- L6 s+ [* ]- v
- void MeLineFollowerArray::setpin(uint8_t pin)
" ]3 ~( b7 X6 P. x! p; R - {
: J5 x: B- ?6 t; I7 u - _DataPin = pin;
- w; m( u2 h+ K - pinMode(_DataPin, OUTPUT); % i# a' g7 m/ o, k
- digitalWrite(_DataPin, HIGH);
6 Y* ?8 ~7 e- H" Q. \; b1 L4 [
% v4 R& Z) y" b1 ]4 d- #ifdef ME_PORT_DEFINED9 G0 `& e0 k" {
- s2 = pin; n0 r0 K* J" T, R; t+ f5 _
- #endif, d" x! h. T ~$ l0 m
- }8 d* C! w+ C# U7 u
- & k4 g/ r, B# [. {
- uint8_t MeLineFollowerArray::getValue()
8 G2 y, [1 m- S- x, m2 Z2 _# [. l - {
4 p3 ]7 w/ o( \5 @0 @: x- w; p) O4 | - uint32_t LOW_level_read_time;( {( e/ }1 n8 Z R6 s5 u v
- uint32_t HIGH_level_read_time;3 Y4 b/ r. ?$ B/ N% j* J, B
- uint32_t time_out_flag;
$ X0 K8 G8 @) O9 w4 \/ o/ s) J: V; @ - uint8_t Sensor_Data[3];! Q( n' d- L- |
- static uint8_t old_data = 0xff;
( R* E5 ^( ]" h. p - 9 }, T* ~' l& j$ v$ |
- pinMode(_DataPin, OUTPUT);
5 W6 u5 ^- Y& d- b7 K+ ]$ V1 _0 L4 U - digitalWrite(_DataPin, LOW);
! ?% {# p7 d$ V6 ?" p B" l - delayMicroseconds(980);$ K" p. Z: }, x
- digitalWrite(_DataPin, HIGH);3 r2 e3 @+ L4 s" i
4 `' H0 t0 a$ a1 m% X0 ?- pinMode(_DataPin, INPUT_PULLUP);' w) y) E9 G. {( i) U Q0 k
- delayMicroseconds(10);
! l* h( ~) f' R2 p; M+ R7 a - " y5 e6 t2 l4 M) @# S2 P
- time_out_flag = millis();/ p# c% L5 I/ u. S% }" M
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); o' Z8 l* {# R2 \) z
c7 W" y8 e* g/ ?, D6 G4 m- LOW_level_read_time = micros();' q. G+ X+ V; C- U0 t6 J
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
6 m) G2 T, v6 _! _/ {: g( b9 Y - {+ v3 v1 }" u. Y0 p' \! @- `% M
- return 0xff;
" l- |7 {' v0 T( d - }$ [; z" X3 ~" H' C7 m
- : H$ ^. a- {+ W) W; @; Z
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );2 X, H; e, ^& p J
- 5 y8 e' S# ]( Q; ^/ o" L2 ~
- HIGH_level_read_time = micros();* k: F& V1 L: e
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
$ W& A0 T* Q0 l7 Y/ {9 P, [- H8 ] - & E% f* [! q1 U
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out: z# u) ?3 X( G) w7 V
- {
" G' E' [/ |0 F! k - return 0xff;
a/ B; f' Y% T1 _( h - }
0 r$ _5 [. e S9 O8 v - " g3 r2 r; d. z' r2 {( Z
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))9 F Q/ o% F9 i) S
- {
2 H( H& C5 a% v - return 0xff;
, F' a' N$ |- N0 p1 C - }. b8 k- e% w# J r" D3 K& `
$ R4 U1 z; t; g0 m0 Y- K+ x- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );8 a. x& Q4 Y( U6 V+ D: T7 I
- LOW_level_read_time = micros();
" P5 f( W: ^4 {5 j - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level7 S# v, ~8 g4 p' O$ X/ h% w
- ! T g# H' Z+ A: _
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out' ~8 ?/ J3 m: ?( _ U
- {4 P% i. N* @- C$ o5 s: m- a
- return 0xff;# _! C4 c7 i6 i7 d5 m! e
- }
7 {0 ~: ?0 w+ }* ?
- h+ S) `3 X) z% j( p- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))0 ?. G1 E& c* o! Y# x; ]& n' Y8 G3 O
- {
0 M6 a7 Y& R3 y3 d- Y7 A) z - return 0xff;
) p) F) K1 z7 U7 c3 g3 O - }
# f" S8 V: B3 I K a
! r, t1 R& P% V0 j( e8 N: a6 S- for(uint8_t k=0; k<3; k++)& X/ b9 m1 s& B$ W( W
- {
9 x, [% z. e# m8 L - Sensor_Data[k] = 0x00;
3 k7 i6 }4 R' h2 L3 h6 `) g4 \+ i - ; n1 ~) C# d1 [! { O; N: D
- for(uint8_t i=0;i<8;i++)' H$ M, e0 O( g- ~, X
- {
. ` }) }/ M( h; Q - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level1 o5 b7 s7 c* I) L% Q& @
- HIGH_level_read_time = micros();0 b e m% ]4 a( _
- LOW_level_read_time = micros() - LOW_level_read_time;
# i' \# J6 y4 U - 3 _6 T- r* }- i7 c! y, y
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
7 J8 H7 p0 u2 @# q' ]2 R l - {
) S; N, K- M h8 p - return 0xff;+ v4 K- a" p2 l
- }
9 d9 Y, |5 ]2 X; h
4 H0 P( z% b# R& j$ g- I- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
* E+ ]- w6 _$ C0 j2 G - LOW_level_read_time = micros();/ l& n: h# ~* d' Y5 r
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
5 w0 ~2 {$ v; {% W; m: P- U - * w4 c! \& M! ^1 h& X
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
: k( ~' r! ?, I1 p, D' {; |4 l3 p - {
6 D+ o5 F' |& b7 O F7 @ - Sensor_Data[k] |= (0x80 >> i);% ?; _1 s+ u- x- Q5 k( P, Q1 w
- }
4 \ J3 Z6 Y }0 m5 A- N - else if(HIGH_level_read_time >= 100)! m- a) D+ s* {$ z4 Z* C3 M; J" c
- {; }% _( S, \2 f! K8 J' j$ _, ], y
- return 0xff;1 j) [, I$ B" Q! H, R
- }
3 g& G+ N6 N; c D% ^9 ~+ b
7 C! I0 L6 x- H2 e- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
3 L% V2 |* ^5 t - {
}- e2 |6 k& A( \# X - return 0xff;7 M7 `' V8 `; k2 c0 J5 M
- }( G( G/ d5 [8 P1 i8 x0 a) n% n2 O
- }$ h: }# t7 \( H* I3 H4 C
- }4 R) l- ?5 l/ [0 H3 O
- , U6 y5 N. q+ o& \- i2 Q
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
6 m7 v8 Q6 Q% e3 w- ?1 a: w - HIGH_level_read_time = micros();
. d0 p" [* [2 X( Z - LOW_level_read_time = micros() - LOW_level_read_time;
! u6 \7 }5 t3 {$ A6 g; _( V
1 m- K, w3 ]+ ]2 q& M9 f F% p- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
: N3 Y/ a1 G: [. }* v, f - {
& n4 W5 m, o$ J* v) f2 }7 k - return 0xff;
# \! p( `& |! ], n' { - }
; V7 n. V& j6 w: g; m
$ i9 \/ c5 v( m, p- pinMode(_DataPin, OUTPUT);' {* C6 S0 d) E7 v5 l; T, \
- digitalWrite(_DataPin, HIGH);' g: c$ l! R# W1 `5 v
8 m5 d4 T' X9 Y4 s( S- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))) {+ T; m2 R0 Q3 s& e, k
- {# U* E5 _5 ~. D A; r
- old_data = Sensor_Data[0];) c8 T% k9 s* o: Z
- return Sensor_Data[0];
1 v; t2 N2 g. K1 H: h! R" X - }7 y# Y5 a. ]1 a- Q; @( B/ j: E
- else
' z2 L. {# y* c( x" E' r* s k% f$ \ - {, U! m( Z q3 |. i+ h3 N( V
- return old_data; \% K# B, k. Q9 c
- }% G- _; H$ H- ]( F8 W* u
- }7 k1 X3 ]* H# \0 Q. G0 M, _
複製代碼
) J0 @) |3 _( X6 y& m% w, tMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
0 o) A* w# J( O( i2 w, B2 f5 p$ s% A6 G
; o! j! q1 u2 l i* z2 K |
|