|
; R6 `. H% T7 {- U
- #include <Arduino.h>
/ |! F Y1 y8 e2 ~7 g - #include <MeAuriga.h>' P1 _0 P" b [/ z
- #include "MeLineFollowerArray.h"" r% c& T7 ] m* F
- 8 N: L3 `1 j/ b1 f* W w
- MeEncoderOnBoard Encoder_1(SLOT1);& C9 h) K* V. Y) D& t7 ~
- MeEncoderOnBoard Encoder_2(SLOT2);
& t! D9 c1 S; v% \ - MeLightSensor lightsensor_1(12);# B7 i, I% l7 h3 Z( S& e; \
- MeLightSensor lightsensor_2(11);+ g {& q9 Q, g& ]
- MeBuzzer buzzer;# b7 B0 |5 N3 a- f) X9 ^! l* I7 m
- MeLineFollowerArray linefollower(PORT_6);
* r- P7 n3 t2 ~4 k7 g
! y6 v9 f# @4 A/ ?, x3 }- #define Error1 1
* n; Q" C& T0 J: D. o( f4 P - #define Error2 21 X& x0 O: h$ `
- #define Error3 3" [1 y# U) {! }
I" S/ f6 Z0 B! V- #define Kp 154 V+ a4 b. E: b( Q" E: J, E
- #define Ki 0.15, F* j& X2 U% A e! y
- #define Kd 0.03* o4 e/ A }7 z ^( [
7 }9 l! I$ k7 _& X8 s |9 G- uint8_t sData;
' h- g2 y( T3 T" j - uint8_t D1;/ ~: V! n# u* Z( p
- uint8_t D2;/ x; s% ~1 \+ ?( i: N& I
- uint8_t D3;
9 ]( V$ L7 Z8 T5 B3 U - uint8_t D4;
% T- T: Z% V; D4 s: q9 |- Q: w - uint8_t D5;
% _% {6 b$ B9 A( F# ?0 _ - uint8_t D6;: }7 p- m! Y5 F5 |/ d
/ c- R; }" O# a4 j+ |- float previous_error = 0;
a1 W. ^0 U* A0 U5 U - float integral = 0;1 g/ _+ E6 d$ P- D. N/ u
- float derivative = 0;
, f- Z x; A4 ~/ E1 Z7 K% J - int Speed = 160;
) x/ Q7 ^7 I/ h! S0 @% H - float output;
% |7 g8 x. _2 Z# ^* f - ) u0 R! |7 u! `& Y. b M n* r* g
- byte Left;# i1 g' F# f1 h5 I& K
* ]1 e2 y* i) x0 H- S% M; \9 `- void setup()
+ y6 F+ d" d1 @: F - {7 w0 `, d; Z G5 D
- //Set PWM 8KHz. S: ?5 P5 v- A y K( ~) V6 v0 f
- TCCR1A = _BV(WGM10);
3 U% d( ]& y' o - TCCR1B = _BV(CS11) | _BV(WGM12);
5 R! `5 J2 h- |1 ^ - TCCR2A = _BV(WGM21) | _BV(WGM20);* J% h* ~) X) N: T2 ?) e
- TCCR2B = _BV(CS21);
) b! r% O7 j; z0 T7 | - Serial.begin(9600);
' T: b/ [# B$ X% n - buzzer.setpin(45);
& x- [( `3 b# v - } W5 m* ^( ~3 e- D6 M7 }! r
- % P/ o( R! g! N+ }. { q
- void loop()
J* ~" Q. Y3 ] - {) a& u- @% [; Z9 u; @0 S) E8 e
- while(!((lightsensor_1.read()) < (10)));8 \ `' c Y/ T$ G! }
- buzzer.tone(1047, 500);
& J( R* K8 N! D2 ~& f/ q - delay(500);( ]0 |& y3 [! _. o! A) ]5 w3 ?
- do
; `8 l/ d$ R# ~1 V' M+ S - {
3 o4 T8 h- _7 I5 t - int Err = getErr();
6 \9 d8 V# ]; N# ~) F6 B# g - if(D1 == 1)
) `, v* o2 r0 h: [! j! q - {+ w: S/ Y" g4 i9 V4 d# c# C
- Left = 1;
. [. k! ?( `/ B. `# g% _1 p - }3 ^# l' s8 i& K
- if(D6 == 1)
6 Y0 U4 o w0 w: y: w/ D* J9 C, @ - {% \* b+ f9 B( u' v+ v4 T# J
- Left = 0;4 C8 N4 B& t1 U* h1 n- p) p
- }* a" R: X) I% Y# O! N F: {
- if(Err == 99)- m6 v& O' b, r, e
- {8 }2 f! t9 O5 Z+ P& T) A |
- if(Left == 1), R& Q4 s' s$ p% Z% \& r; x
- {+ \& C( f8 ?/ m2 a: z7 |7 s
- Speed -= 5;5 Q7 e R3 Z+ N/ u) M) d# b9 `
- moto(0,Speed);
m+ K* r1 Y' _ - do7 k, \# l, g7 R) @- d' D6 R) _; u& {
- {
" m2 w! t7 o: X - Err = getErr();) S. h' x4 G- S
- }while((D1+D6) == 0);
7 C( q9 v# Z; M( g9 T - }. a7 ?. F- q7 I
- else( [/ ?% p" _" V0 r7 E$ J
- {
4 {( O0 ~% L9 C- M. @2 Q" R8 T - Speed -= 5;
- T1 e" U) Y! Q* @8 \. T - moto(Speed,0);
5 w! M+ f* v1 p( k; \% T - do! O8 G- P g/ K& J
- {
$ J: Y& e$ K/ u6 d7 e' }# m, a* B - Err = getErr();7 u: b# {( d; L, `1 @: c
- }while((D1+D6) == 0);4 q% ^- G, U' g8 t
- }
2 {4 ~$ F! q* D2 o3 s! s( j - }
" a' N$ h7 w+ M4 D5 P: A - else
P6 }1 t3 R2 ?2 S& W - {: [" f1 k. B" D# M
- if((Speed < 160) & (Err < 2)) Speed+=1; 0 i4 S' ^, `# Y* e$ r% E
- if((Speed > 100) & (Err > 2)) Speed-=2;
; O/ q( N! C* E$ g- m% ^ - integral = integral + Err;" O+ B) ^, I9 m. ^' R
- derivative = Err - previous_error;
. Q% f- ]* }+ F, a* |' T9 R - output = Kp*Err + Ki*integral + Kd*derivative;, c R1 Y3 d! Y! J" D5 K
- moto(int(Speed-output),int(Speed+output)); F O6 ]9 x5 T+ I) Y
- previous_error = Err;* u: _& W: I' g* d* u
- }. T* G5 D$ B# U+ F2 S) E
- }while(!((lightsensor_2.read()) < (10)));
& Y# f' _, o0 W0 k+ s - moto(0,0);+ b1 n, t: M' F2 I; f! h, @
- delay(500);: T* F2 h% {, {( ]' x- f( U6 H* @- [
- buzzer.tone(262, 500);3 b$ ]1 u: a" `9 L& F* B5 ^8 p
- }0 I6 L. m! d% u+ b
6 n, }9 E5 S2 D+ A% c- int getErr()
! X1 A1 x a% X - {
4 f7 J/ }, U' f+ l - sData = linefollower.getValue();3 s- }- |2 \4 H3 d5 l" z3 ]+ a
- D1 = ~(sData>>0)&1;
. g4 |! k+ o$ z* y - D2 = ~(sData>>1)&1;9 o/ S$ S8 D; T! o! z; e# P1 {
- D3 = ~(sData>>2)&1;% w0 k- e. v* e2 b) C
- D4 = ~(sData>>3)&1;9 T! _& A1 L! H; D. g" q9 [
- D5 = ~(sData>>4)&1;
0 U7 `8 X( l; K* M% W! B$ l& w - D6 = ~(sData>>5)&1;: t$ E# J4 R" Y6 H' b2 g5 x
- int downD = D1+D2+D3+D4+D5+D6;1 Y# Y; B2 I' o; Q i; _2 T* {4 `
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);9 i, D& |4 E3 l5 r/ {
- if(downD == 0): {' F! t2 a9 Q6 N- P# |4 A
- {
- t7 p3 E+ s x( C: V9 M/ h - return 99;( c0 l4 P I. t) _( [
- }' x9 R$ P; M& O
- else' k" b' r% p/ i7 _7 _# D+ `5 r
- {
5 I* z: C+ Z2 I' s$ W - return upD/downD;
z2 `+ x/ d& ?; M3 b7 r: ?7 F - }4 R; F2 R1 ^! G! F! V& [0 r4 {
- }4 u7 l( m& g3 f) o8 f+ Z
- 0 H( o* n+ p9 x- Q6 N8 `
- void moto(int Speed_L,int Speed_R)
. p% N# x# o: S, \3 U) r - {
$ @. M8 e4 q: ~ - Encoder_2.setMotorPwm(Speed_L);2 S: c" Z3 L; m! P5 w- ^
- Encoder_1.setMotorPwm(-Speed_R);
& r4 ~ q+ H$ o4 R - }
複製代碼
: X, a7 i/ ?: h6 V; DMeLineFollowerArray.cpp; R8 B! G( L7 e/ i7 O: k1 @3 I
- #include "MeLineFollowerArray.h", B( I) K8 ` k2 d+ S1 [# `, f
1 E8 d" Y# q* c, A6 T$ v7 f5 s- #ifdef ME_PORT_DEFINED& H; q( Z" Z' r, A# n# m
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
- C' K2 y4 L% c! f, n: T/ g - {7 h4 k7 D" u# {% Y" {
- + [& z. J# ? O! J7 t
- }3 O- W! H" M( i
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)" [" M5 Y b* n$ X0 D
- {
0 L M1 {5 M/ ^! F) K - _DataPin = mePort[port].s2;! M* n. t M, N1 ^! Y/ y" s
- pinMode(_DataPin, OUTPUT); * i- }, D9 @% g( u) |* ]# ]' m
- digitalWrite(_DataPin, HIGH);
; @& X5 `4 p% }/ S/ ?5 i - }
, ]) z" C5 E1 [( S% D - #else // ME_PORT_DEFINED
& s, c/ [/ n7 U& Y- [ - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
; o6 C0 H) L' y/ v7 b - {6 s/ {' b' c6 E7 o5 _, ~- a
- _DataPin = pin;4 U6 a2 D4 \1 v! Q
- pinMode(_DataPin, OUTPUT); 2 G' {1 g3 y& R& t; C
- digitalWrite(_DataPin, HIGH);9 _$ Z; z8 e, C& l5 x
- }
0 k1 W+ e# l# ^. l9 D - #endif // ME_PORT_DEFINED; M) g0 w# b- P
- 1 L7 n% x$ U/ G+ L9 b6 j1 [
- . z/ ]% J, ], V" e3 K
- void MeLineFollowerArray::setpin(uint8_t pin)% S, k6 s7 X0 V1 _! \ r, v; d
- {4 ^4 t* d- H* u3 c8 s D" z
- _DataPin = pin;& x2 y# h7 g; X( Q$ \+ |$ R3 O8 K8 s
- pinMode(_DataPin, OUTPUT);
0 ?! Z) l% z# d, w- \( s - digitalWrite(_DataPin, HIGH);6 \ M' i% t# F' S' X% b
- r$ |: Q8 a" w' g& v T
- #ifdef ME_PORT_DEFINED, o9 t& V M! W: q% T4 r4 }
- s2 = pin;
2 D7 {6 \7 Q* k# D0 i - #endif
, z& W1 a! d" E# Z - }
* F1 _2 ~* g2 W* I$ [' _
/ W# v3 c O. C* ~7 |5 J' f- uint8_t MeLineFollowerArray::getValue()
, z! }& S+ V, N: ^! M - {
0 l( l, f. I0 l+ Q - uint32_t LOW_level_read_time;) C( K5 r- |! d2 X! G% s7 x$ F
- uint32_t HIGH_level_read_time;
1 w' ~; y4 v. m+ X - uint32_t time_out_flag;
" @+ X% Y9 G" S2 ^% a+ e - uint8_t Sensor_Data[3];
& ]$ ^/ V4 A1 s - static uint8_t old_data = 0xff;) {2 d& _! i* ^+ r
( q C1 n! c! ~. j* @" {! r+ a- pinMode(_DataPin, OUTPUT);1 J9 |+ t( l) l" A- \
- digitalWrite(_DataPin, LOW);# b9 F4 Z5 g9 L. F2 B
- delayMicroseconds(980);& E T; {+ h/ ]$ @ ^7 J
- digitalWrite(_DataPin, HIGH);5 }8 X1 Y+ `& Q# I/ o( O
- # \. q$ H! @! k; X$ L c) u K
- pinMode(_DataPin, INPUT_PULLUP);" O4 b) _( X0 s( j' u4 x8 l
- delayMicroseconds(10);" j* ^/ ^. w9 n% O; ^
- " r& n& z0 Q; j3 S8 }, U, j
- time_out_flag = millis();
2 y3 a4 w$ e) n7 k - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );; f2 v. i/ B/ T2 w: A4 u
+ ]' c$ ]: |7 D# Y- o1 ]( C- LOW_level_read_time = micros();' R2 H8 c- ^ a+ l# p9 {! f
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out' f; t* ` @8 p4 X# R( k* ]
- {
( A0 I2 M. |$ b4 V; @ - return 0xff;
/ \( @8 A8 N3 T/ D - }
0 X9 I8 f5 M" P: \% {. C - $ W; V; f: q- r8 B& D" u/ i: e
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
; X# f' E g* s6 D4 W+ j
5 Z4 B) E0 V0 q" h& q- HIGH_level_read_time = micros();0 [" A) U* {1 P! u4 ]/ R# @
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
* n$ e4 D, G4 `. O" S - " W5 H3 Y* J: ]- C% {& [
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out9 F3 ]9 ~! X- I; I
- {
/ J7 K+ e: O+ T8 J0 @ - return 0xff;1 r* m- T. Y! J. j7 z0 |8 R& N
- }& Z" s% f0 i; |3 k# x
/ {" [2 s# R3 E% D% ?. j. c- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
& D2 _0 m) M' H# l$ T - {
$ K" ^# K, Z& a! @ - return 0xff;
# }0 l7 I+ O' o8 ? - }! ^, Z6 N) |4 e
% Z% E" ~( _7 q; @2 l" k- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
3 r, ^- T) X' k9 a2 H - LOW_level_read_time = micros();$ \- }/ k- A2 l' \. v/ {( Y
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
w" G9 ~6 G4 ?+ F1 o0 e5 x
7 R8 o& a+ a2 v: D- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
# V9 `7 ^. z" d3 @& [0 V& ~9 |2 { - {
8 D9 _: i* N% \3 ^9 ` - return 0xff;
! S* h- |$ C0 x! L. d - }
y2 D2 c, w" w
2 _% R& s. A# \8 R" _: C; r, w* P- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
) P c* y. h# \2 H% S - {
! q+ b ?( J; H, s - return 0xff;
6 A6 H# {0 f n+ V" |7 ?4 o. e - }
3 r. N5 V3 Y' K0 Y+ [ - : [3 v+ e' j9 W" g8 M
- for(uint8_t k=0; k<3; k++)/ I4 ]0 O+ Z% r' V: Y8 a" y9 Z
- {
3 O& H0 f( R0 P8 e7 y% R - Sensor_Data[k] = 0x00;
: Q$ u4 W% H; l' | - * |" Q6 R/ A0 |4 I/ X
- for(uint8_t i=0;i<8;i++)* ~+ T. j0 @' @/ |2 g( M$ Q6 O
- {
: J5 z4 R+ O2 @; U1 q3 E - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
# y0 x `0 G! k - HIGH_level_read_time = micros();/ f0 [( f. J( @- N' D, r* h
- LOW_level_read_time = micros() - LOW_level_read_time;
9 v5 d; B2 w+ B" `7 o% [ - ! X( p; N1 a) {0 x7 P/ @
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
4 _5 _( s d* o" i4 s9 X3 w( [ - {% ~8 w2 v9 E1 F, L2 T
- return 0xff;/ W( Z0 T3 L' r9 a
- }2 Z3 @% h% D1 K& @
( f7 @9 U$ M6 R8 D- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
5 [9 S3 l% h9 e/ q% W - LOW_level_read_time = micros();" x9 n# P8 A( ^; G. @' [" r. S* s6 L
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
' P- [( S1 g3 [" ?1 m h
1 `, t4 t: e# d% @7 R$ H' }- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 14 L. |0 t9 J7 x4 t
- {
' M6 H* S2 u. l' ^ - Sensor_Data[k] |= (0x80 >> i);1 Z+ s7 o; }1 _* { h' e, |% J
- }& q: Q, }" u# g u
- else if(HIGH_level_read_time >= 100)
1 B* |( N% ?$ C% X( G4 N& t - {! S1 j+ V+ n4 C6 B. ]" l- d2 d
- return 0xff;2 n& q4 B' }6 |: i) H) ^) W, ^
- }
$ f6 ^7 _/ s% x6 V& [, j9 Z - 0 k: j/ C0 e8 p& [$ B* y
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
, r5 o( D) T3 T$ }4 s! L - {
! Q& [9 Y. C ?- a: Q - return 0xff;
+ c/ \, L2 U3 Y - }
8 o8 X1 m. R% k. A( d" C6 S; t# G. D - }
3 n2 ?2 T" Q0 U2 c - }$ F0 }. ~( p" j) Z
- 3 n* e; q2 W) j$ s
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level. R6 N1 H, X- M, O: D
- HIGH_level_read_time = micros();
, B$ I7 E+ h/ u( N3 b - LOW_level_read_time = micros() - LOW_level_read_time;2 r! q8 p% s7 x# ^9 {5 D5 P
- . T! O2 n( S- {# e$ N2 K4 Z+ W! P6 k! l
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
T0 U+ K+ M# K0 C - {; A( g6 \$ o% o8 x( h& t% u( \
- return 0xff;$ a# V Z4 s7 U5 X+ I
- }4 H4 G1 j, B" p9 K7 |- k
' a2 {2 X9 h8 e- n: C. U- pinMode(_DataPin, OUTPUT);- A* P7 e! B9 Y# ]' l& ~8 ~
- digitalWrite(_DataPin, HIGH);
. n) y! o( i: V; E
) {% D( U& Q) j/ t- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))2 k8 z' i% A$ P; [# c; P# v# I
- {
) g! J$ b }" Q2 U- c( a0 B - old_data = Sensor_Data[0];! U: ]: ?+ E" a! z
- return Sensor_Data[0];/ u7 Z2 y- x) _9 G! L' Y
- }6 ^' ]8 i; \& X& V0 B. p
- else" t, ?' g" ^2 h. a9 p
- {# X8 G3 P# ?9 B
- return old_data; [* X8 u6 ]) r0 j* n/ B
- }
1 _) ]/ ` `- o# o! p( p" I - }+ Q. B$ m; ]7 c
複製代碼
% |% i9 N9 ]4 D, W9 C/ O, FMeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
, K" A; T' m" o7 n" Q
8 Y8 Y r7 j E. J% m) T) B. j |
|