|
|
( V( n) i/ q9 K- D$ B4 G3 C; z B
- #include <Arduino.h>
; _2 P4 E. [ @( a - #include <MeAuriga.h>) v$ R7 J5 q, r; j0 s) s4 N1 m# Q: \
- #include "MeLineFollowerArray.h"
5 f" j8 @& g# P3 | E* ~9 O - 1 K1 T( j2 s8 m( J" _4 G
- MeEncoderOnBoard Encoder_1(SLOT1);; z" l% _( O& k0 e$ Y% |/ x5 ?' t
- MeEncoderOnBoard Encoder_2(SLOT2);
4 U M) [- R7 V) m9 q5 W - MeLightSensor lightsensor_1(12);
. M8 U. @2 M4 y2 t - MeLightSensor lightsensor_2(11);0 P+ w+ c( z5 D' `) \
- MeBuzzer buzzer;
5 n7 S k9 K$ \) T" l - MeLineFollowerArray linefollower(PORT_6);
% L- o1 L) W9 p+ l) v6 m- d
( H! J3 y# o9 b( M) b- #define Error1 1
) O7 b' `! @$ l) G - #define Error2 2; c* i2 v# [, {+ Q
- #define Error3 3: q" [ f$ ~# }4 b
- ( L/ { E8 e, g$ n
- #define Kp 152 J# W& D' I9 d, j+ U
- #define Ki 0.15" n8 Q9 r/ ^6 w8 p
- #define Kd 0.03
1 n( T1 Y2 t- n, } k* Y( C0 u
- F8 _0 h5 j9 y% Q7 s! T5 a1 N) F- uint8_t sData;+ ~7 q9 y4 [6 G8 @& G" |
- uint8_t D1;
7 i& k6 N- _% z* m" I) g$ t - uint8_t D2;
+ [9 O) Y! s0 B' d- H! U - uint8_t D3;5 e1 \3 p+ R2 P# K- p7 Q
- uint8_t D4;
& P# y/ [( F8 W3 K5 q9 F0 W - uint8_t D5;7 a1 ?# m# s: o7 g
- uint8_t D6;; a+ O' ]2 i! d5 U' Z' H
6 y& s0 s7 D) C% ]" ]- float previous_error = 0;7 L: n8 l! v9 F: b0 v0 Q9 X# }
- float integral = 0;1 d' u5 n; W) J4 {8 X1 A# _# a8 x
- float derivative = 0;
; P" r' v, A% t' J4 ?( T - int Speed = 160;& f1 q1 Q5 @* W& B8 g# @4 j+ M, M
- float output;
" n' e* u ^4 b1 b' W - : r$ Z- \1 d& H z; j* T5 X* A
- byte Left;
1 r' n6 }$ q" P4 k
9 A8 u5 n; G( N5 {6 [0 }" J. T- void setup()
- D9 O* |6 ?' S* q0 e+ Q - {7 @+ V* Q, s- j/ I4 Q) S
- //Set PWM 8KHz- v) [+ [ I$ ?' @- O9 ~& P* I, ^/ w
- TCCR1A = _BV(WGM10);9 O0 E' A' p) _- W1 M W
- TCCR1B = _BV(CS11) | _BV(WGM12);
& Q6 P5 H z% I& P - TCCR2A = _BV(WGM21) | _BV(WGM20);
8 L$ R! l: v' h) E - TCCR2B = _BV(CS21);/ M$ j2 M' b- A7 _% A! v& [) h
- Serial.begin(9600);
, a. ?' h& l. h& M - buzzer.setpin(45);
2 U$ B7 N9 r0 a* Y2 k5 S0 J - } j0 A. Y/ Q C# y
& Q- y, ~' @7 l& s- void loop()5 _, X# Q3 z6 q* ^5 n }
- {' E: r' e, ]6 F: X; c
- while(!((lightsensor_1.read()) < (10)));
/ S$ o$ i' k, { C - buzzer.tone(1047, 500);
3 W0 N& J- w* b - delay(500);2 z/ J* N: B1 E
- do
* k, K3 M" `; M9 f$ o. z* ] - {0 c* d! [! O6 I" I) P
- int Err = getErr(); u$ a8 j( x1 ^! N
- if(D1 == 1) k* e3 g/ T" S7 A, j. V; Q! O6 Z
- {
: f" `! d& ]# t: Z/ Y- M) F% B - Left = 1;
4 k: T# f, e+ {6 t, p7 Q$ ]5 ~ - }/ V4 x6 `8 {: ^% p: k) h
- if(D6 == 1)% f& n+ n- K/ y0 J5 L! B
- {
5 Q' u- V$ `; c- w2 {7 n. ? - Left = 0;; Z6 K/ w: Z1 |& ^
- }+ B& }( [5 y6 O, |
- if(Err == 99)2 a8 a# M! O1 R6 e: k# @) A h
- {
& P1 W8 r( V# W: d - if(Left == 1)
4 \3 d- a. N- l, f; {$ F* s - {$ s, ~1 D1 w$ f0 ^; S0 {
- Speed -= 5;
1 F9 G* j6 x: T# ]6 L - moto(0,Speed);- ]) @0 v0 D5 m
- do
9 K9 W$ P/ y v- g6 } - {
$ Y! T9 m2 O+ p/ S - Err = getErr();( P: E1 T. c e ~' t
- }while((D1+D6) == 0);
) R* u0 N8 f d# n5 [4 v. A - }, t! B) u' x0 t0 i3 P! ?! w5 f; n8 j
- else5 S9 H( z, c4 d6 v) F) e* K* ~. Z4 F
- {
) L/ Q" Q3 |# b5 P: p8 f - Speed -= 5;1 J, B! w+ S( Y j+ d
- moto(Speed,0);
a; n1 s1 p$ W+ O9 I - do
. T% K8 z- n8 ?1 G! s- _* O - {
: N' z* a2 [4 ^0 M$ u. L - Err = getErr();9 F# ?& t& y8 H5 [- D0 n
- }while((D1+D6) == 0);% w$ @' w K7 R$ p9 A, s' c. R
- }
8 V; ?- ~+ F1 o6 [3 T% { - }
2 s$ l* _; ?4 _, ^' d - else0 ^* b+ x/ Q( {5 K& ]2 h ~. F
- {% _( {0 Q0 g% n; k
- if((Speed < 160) & (Err < 2)) Speed+=1;
. m- n- A7 M- c. `" g0 _2 d - if((Speed > 100) & (Err > 2)) Speed-=2; $ p' K+ q0 Z* }1 {
- integral = integral + Err;
9 p+ |1 r5 S6 `4 b - derivative = Err - previous_error;' W' v. o$ f# w, s/ [# I- c* X
- output = Kp*Err + Ki*integral + Kd*derivative;9 K! w' v( O$ l- q. b: c
- moto(int(Speed-output),int(Speed+output));
2 q2 \6 i- Q- O r0 P6 f! c1 j9 V - previous_error = Err;+ v! \) {, @- m: A: s1 c
- }
4 O! q: x' K+ L1 O5 v: q+ f - }while(!((lightsensor_2.read()) < (10)));" D( p4 E" w6 W, J. ~3 W1 {
- moto(0,0);5 A' x3 h) y7 t+ n# c
- delay(500);
- ?3 ^( y- v7 ?0 [) }$ v& { - buzzer.tone(262, 500);
6 l$ z; P( \- n9 [ - }1 G/ q8 e9 K' l0 A9 ^
7 c! o6 Q* p! a) {1 W9 p- int getErr()
8 x- L- U9 h/ O - {
8 I& k2 W) p; ~5 @0 K - sData = linefollower.getValue();6 f! W1 O# j `( F
- D1 = ~(sData>>0)&1;8 [1 V8 G) v) p5 A" w, o
- D2 = ~(sData>>1)&1;
- r& `$ F- ?1 H# i. L$ A& c( G - D3 = ~(sData>>2)&1;. P3 H# e1 s# ~
- D4 = ~(sData>>3)&1;
3 o# l/ s8 s# J# D - D5 = ~(sData>>4)&1;1 O. m' v. r |3 g/ w
- D6 = ~(sData>>5)&1;% a6 y, m3 s' ^! j" K1 {* d
- int downD = D1+D2+D3+D4+D5+D6;9 b5 l" U) R1 P% k
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);4 i, ^3 T# ~8 ?" y! D' E
- if(downD == 0)0 u, B* V* S; g) w+ w
- {( e. _! _7 u- ?) B
- return 99;
- @4 v& K+ d" ~5 h - }3 _3 i4 k: R- r9 h& j; n
- else
+ k, D/ H& ?: O% G - {
( R d1 }5 K1 @# x( n - return upD/downD;2 g7 |/ V5 g0 i/ U; I9 D- L" x' u5 a
- }
- X( V: o t# s. v' y - }
$ c. }4 V: W. E S0 t
9 i' w5 L% x% o) D5 L- void moto(int Speed_L,int Speed_R)% q1 t w2 y' h$ ?' R: u) w9 C. a
- {
: l3 d" T4 i O! F% I8 H# x - Encoder_2.setMotorPwm(Speed_L);
& _8 Z& [$ C; @) h' K2 y- o W6 D0 F6 Y% L - Encoder_1.setMotorPwm(-Speed_R);
) o9 {" y- N c2 \" E v! g - }
複製代碼 ) A/ N8 m1 F5 T* Q7 n3 B
MeLineFollowerArray.cpp. J1 {3 d0 _, x5 v/ w* G, i
- #include "MeLineFollowerArray.h"! `) k: E: V+ p: {' A# K
! I6 k( j7 ]* f/ n- #ifdef ME_PORT_DEFINED1 |9 ?% z0 h0 L2 Q
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
3 ]; @ S! S. n4 g1 m - {+ v7 d, a" C. h0 w5 k2 ?, f
$ d. Q& C+ m2 A" T8 L- }
7 A* r \$ K' R; o9 h - MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)2 r0 l: L. d% @
- {
6 S& s; L7 y J* j# {: ~ - _DataPin = mePort[port].s2;6 Z. R. ^" [* q# u: a" u
- pinMode(_DataPin, OUTPUT);
2 O6 I" C- ?6 j- R! w8 E7 e! A# I - digitalWrite(_DataPin, HIGH);3 K/ |! e/ j/ k( |6 P
- }5 A8 D( \, \. O
- #else // ME_PORT_DEFINED
/ C9 c7 f9 E4 q W5 i8 a - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)# K5 \! b9 l1 V: v3 `+ a: G M
- {- h9 {: c/ [) z0 }5 Z3 }* \
- _DataPin = pin;
2 Y$ ^4 m% Y# T% b+ ^ - pinMode(_DataPin, OUTPUT); - t* S7 {1 r: Y. P6 A1 T
- digitalWrite(_DataPin, HIGH);
8 P/ A% @9 S$ v9 n5 u# j - }
. k' C# D! F S& p N2 t - #endif // ME_PORT_DEFINED3 G, e" v/ r/ V$ g& S
7 d* M- f3 d2 Z& t- : }% S# {8 t9 ^, C
- void MeLineFollowerArray::setpin(uint8_t pin)% Q# y7 ?; i8 T3 ?2 C
- {
, x$ [8 x r2 f1 `3 c* R - _DataPin = pin;
* J$ W/ i: K4 R5 W4 ^' n - pinMode(_DataPin, OUTPUT); : R+ B$ X T' |4 Z; L
- digitalWrite(_DataPin, HIGH);
4 t! \! a+ D" a6 K - % f0 G* R7 ]; N3 O- F) J
- #ifdef ME_PORT_DEFINED
( F1 \. X# `. _; H# g1 O8 a% g - s2 = pin;+ U: U3 ]( N( e
- #endif6 s" f" S8 q- r% f
- }
T$ Y9 d' ?/ D1 B
$ I8 c0 r9 G3 j3 a9 A- uint8_t MeLineFollowerArray::getValue()- G* Q. S, u( p5 H) [
- {
9 z$ `: p) v+ p: i, z, |" G1 y2 D - uint32_t LOW_level_read_time;
! X& B) C7 k+ q2 G/ n; m - uint32_t HIGH_level_read_time;& M- k7 S( V% ^$ j& v
- uint32_t time_out_flag;
D: W1 q, ^5 W2 @5 v7 H/ c& Z - uint8_t Sensor_Data[3];
2 A% X4 I& m+ L- Y - static uint8_t old_data = 0xff;8 |* M# ]1 ?' j5 l
- B3 J) f! s9 n+ X
- pinMode(_DataPin, OUTPUT);
$ |4 h2 o8 M& t4 z- Y - digitalWrite(_DataPin, LOW);% p9 t" X4 ^# E7 O- M3 S
- delayMicroseconds(980);
+ p4 V, L* S( e: e# a& A - digitalWrite(_DataPin, HIGH);( P; [9 E1 x" m9 A. p
: ]4 g& Q+ {6 o2 O1 u- pinMode(_DataPin, INPUT_PULLUP);; y. T- n% M# w) M9 |# Q
- delayMicroseconds(10);" z$ p$ e2 d# e( T+ W
- ' M0 I' n/ O; S
- time_out_flag = millis();) ^8 O7 S+ I T) h5 h
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );' m7 h+ s& T4 C( ?/ J
- ( |6 v. s: c1 ?4 ?
- LOW_level_read_time = micros();
* b0 \% _. i* m; ]8 ]# _! w; r& c - if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
+ P! ?: _3 {7 q, I& S - {
9 o2 F' U, R' s - return 0xff;- P1 ~% j1 U0 a+ ^) S i
- }7 Z1 j. l7 A% j
8 _3 \3 c& k: R7 m1 Q( X- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
. E) K- O) \4 O. y; D1 o
z3 G' s( `5 ]- HIGH_level_read_time = micros();
) Z: {& t% a- ]% ` - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level* a w3 S% @1 b. F5 V
- / s+ J/ d7 ^, B: F: q5 G2 y* h$ ^
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out3 m% B5 _" Y2 M: T9 h; j3 Y
- {
' Y3 b/ |2 p; x( j2 z; t! b9 ^ - return 0xff;( T2 T+ q0 X* ^' K. a
- }7 y$ Z" h0 @" y5 Q$ M% Y, q0 L5 A5 }
6 X5 C. t. L6 R, ]7 k3 d M- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
8 B: O1 ]7 p$ _( t, ^ - {
! @0 }; \2 I' k! d1 N, L - return 0xff;" M, u$ O0 H. R% X0 U
- }' R- N" o9 C3 R6 O0 F1 \
- T" Z1 C n) ^! h M! G# Z( S
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
7 S( f B5 S; {! y - LOW_level_read_time = micros();
/ X0 M0 C# }* g$ Q4 e# d - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level; G' _0 t( u( V
6 h1 n, O' J0 ]. ^4 \- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out7 t9 `1 n$ {6 b) v0 n
- {0 H* o' K: n( f+ q: r% h' @
- return 0xff;' c- R: q* F7 ?4 Y( P7 ~) K
- }
' N+ g% R1 x( D! [
0 Y$ e5 ], j6 _9 Z, G# r; I- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
& e( ^8 ?5 x* k p) [/ G - {
7 _4 ^+ {$ b: Y( \: Q$ m - return 0xff;' k/ c7 s0 L5 A1 K! ?
- }
2 I* u( ^' p' `) b. L2 x - . J- r) J9 u& I) |# Q% o0 B
- for(uint8_t k=0; k<3; k++)0 P' ~. {9 X& v) E5 k0 l+ n
- {5 A/ {% x1 X B: f) N: A% N" a
- Sensor_Data[k] = 0x00;7 D- K4 ~" }6 p- [
- 0 \1 Z# o/ ]3 \- f$ P2 k+ }+ r
- for(uint8_t i=0;i<8;i++)
$ U. C4 Q: s4 v - {1 l& r# O* n4 y0 `; g% ^' p I7 w
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
9 N3 X2 ^) M" _/ ` - HIGH_level_read_time = micros();
# T7 O4 x9 }9 G( m$ k+ R - LOW_level_read_time = micros() - LOW_level_read_time;
8 @# E" e; H! _3 G4 ]3 k3 {) |
* Y. M' L- o# V- f) \4 [# K1 Q- X- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )+ E) W" L. M e1 Q; B9 i" m
- {
& X$ z% E2 ~9 V& l - return 0xff;
4 S/ v: A* f& q( h - }% p5 I5 p) n, ]* K3 k) w
- 6 p5 |7 r* k7 ]3 B! d9 o0 ]3 G1 [
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
* S8 m. r' B, S- q0 m4 ^; i: e2 l+ x - LOW_level_read_time = micros();( z7 z+ m& S; Q w/ i4 I+ ^
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level0 g. k) T) e6 l, x+ W
- 2 x- Z0 D- b& E( H! X* |6 o( D
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1+ b) u- p) z. P3 I+ \$ d% f0 \
- {3 K" |% ]0 B' \9 r. V* I
- Sensor_Data[k] |= (0x80 >> i);
0 j A8 a% ~! S- s4 J% _# e - }
, F5 B0 {3 k6 _: R4 m9 E - else if(HIGH_level_read_time >= 100)* b# G8 r& E V0 [! g
- {" w+ ?0 I6 R/ S ~ o( s% W
- return 0xff;
9 H* @0 W5 x; I% |7 P - }" d8 @/ I' A# Y+ N9 |3 T/ c$ x- P
" z& |$ F3 M' k4 _: k- o3 E' z- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)8 Q' W, f* w2 Y& w
- {' c; P3 o8 f9 Z) [5 _9 Z
- return 0xff;
' t$ }) V, g6 u' M - }. o: z/ E: Q# W6 h! S4 G
- }. l }6 T1 d; M+ I K" ?7 b# U
- }
" w! E) v5 N! _. U. w
# F8 t" \; a" I! C- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
' P2 e6 ?: E0 L% j0 F - HIGH_level_read_time = micros();
6 c- o* P. N; h0 L - LOW_level_read_time = micros() - LOW_level_read_time;
3 h- i+ \! k- }0 {" n; { - / T# ?+ c$ p- ]/ H' x9 B
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )# O4 G: F" R+ o, P s: G0 i( y
- {
, p" @! c2 b5 ~. s - return 0xff;
# z' `5 H1 g/ V# ?1 [; m% O# n - }
% R0 h- n8 Q4 M( l. Z7 i7 y - / V+ o9 F Y' Q; ~1 T& X) k8 s
- pinMode(_DataPin, OUTPUT);2 ]) t+ D0 G% F2 j$ s
- digitalWrite(_DataPin, HIGH);" f/ @6 b7 b( i. L& d- {
& K1 M, h7 i0 h$ Z, R6 f' q5 e `- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))6 n0 P* a6 r, F- v/ J3 {2 k
- {
! `' Y6 `( C2 ^ - old_data = Sensor_Data[0];9 f% i. c; Q# n
- return Sensor_Data[0];4 ?! N% ]$ ]" R4 Z4 n( K* ^0 _
- }* A- N3 [6 P: G& w9 T9 T$ O1 U. i( }
- else
4 R: T6 g# @9 ^ G, c1 x" g2 I; Q Q - {
3 u5 m! T! p4 P9 q* r - return old_data; S" T$ C$ E; f' B5 n/ h
- }+ {+ k' C# t6 t$ O+ y
- }
- c! G( A1 _' A+ p; y! F% h
複製代碼 1 [* f$ U4 K" V- K7 u
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
: H+ d6 ^# \8 X7 q% \! \0 T/ T0 W2 F( K! {7 _, `. H+ o, p5 \
|
|