|
|
6 I; ], d4 b' F3 }# C( d7 T- #include <Arduino.h>% l% A0 K! m+ |
- #include <MeAuriga.h>
0 b3 s# c1 J) R8 k$ h* M9 V2 t - #include "MeLineFollowerArray.h", U3 L% K9 T' N3 l9 i! Z
6 L% Y( Z3 L" T- r- MeEncoderOnBoard Encoder_1(SLOT1);. c- O+ d1 [: P( Z
- MeEncoderOnBoard Encoder_2(SLOT2);
+ ?" U. S2 N/ y - MeLightSensor lightsensor_1(12);. d9 E) A2 M5 Z/ [* _3 B. W$ I
- MeLightSensor lightsensor_2(11);0 ~" c: X6 u: Q8 `0 ?$ g! ], w
- MeBuzzer buzzer;
) h- \) ~3 k7 J* u! h7 x - MeLineFollowerArray linefollower(PORT_6);( f) J' ~5 t, a7 N( F6 Z" }
& N2 d" F7 o3 e6 E# g- #define Error1 1- a' F6 U0 x: V7 T1 q$ ~
- #define Error2 28 G$ \" b. Z& e( E9 ~. i. j, n
- #define Error3 3
! ^6 C& ]5 Q1 ~ - 4 i$ w; ~9 A9 U6 W V/ m* A
- #define Kp 156 X. x( E7 X3 o; c& m2 f! K
- #define Ki 0.15
( R/ O! p9 k, D/ K - #define Kd 0.03
- j/ y5 b( C+ q3 v; Q4 R
/ s. I3 Y9 _1 n( ^$ T8 l7 i- uint8_t sData;1 G" Z$ B, @' D2 E
- uint8_t D1;- }) h1 C1 z4 Y# @
- uint8_t D2;( o( y* j8 j3 c2 S- q
- uint8_t D3;, H6 b8 @5 O2 y5 ]0 a. _& D
- uint8_t D4;( [& B: U9 M7 ~9 G4 L
- uint8_t D5;
' n$ u' C* Z; Z9 U, Y3 j - uint8_t D6;0 Q/ ?, l" k7 T$ O/ P
( Q, l- w i; I G) S' y8 X8 [- float previous_error = 0;4 b. v8 _5 n$ L) M
- float integral = 0;1 |) g6 t4 R: X' m) v8 A* |6 l3 ^7 a
- float derivative = 0;
) |* V/ Q w9 l4 e8 E - int Speed = 160;
7 H2 q' p0 B m& V - float output;
" y0 O% ?; ~) V* k- y - # ?/ i; e+ Q/ P5 n: q+ b* H
- byte Left;
/ s( i! ^5 y- t& x, i- p1 n3 S - / h' \" k3 v4 _4 U
- void setup() ( s5 f1 _5 c: L) k7 T3 G
- {
4 U' N' T( k$ C% u: |2 O$ s - //Set PWM 8KHz
- S0 P4 z+ e T+ _; x* Y( } - TCCR1A = _BV(WGM10);
/ a3 W7 T, U& m* Z2 e6 s - TCCR1B = _BV(CS11) | _BV(WGM12);& x& n# C8 p I! b |2 z ?
- TCCR2A = _BV(WGM21) | _BV(WGM20);7 F0 J$ X3 v7 {/ h; @3 Q: K8 ^: W
- TCCR2B = _BV(CS21);
# ]# W8 j, I1 y. b - Serial.begin(9600);
; y" r& h3 U( t) Y - buzzer.setpin(45);
6 O9 x/ D0 S# F - }# }9 q( D" p3 n! _
- |" A$ m" L& E; p' i D% C7 Z- V- void loop()3 f1 u2 l! `2 z4 C- x0 b7 i4 H
- {: T3 c3 o& c, v" X
- while(!((lightsensor_1.read()) < (10)));7 x/ \1 y/ t; ~* d5 G
- buzzer.tone(1047, 500);- J# Y/ @# G# C- x6 N* [
- delay(500);) ?8 x) c/ }! n, `# w
- do; ~+ K% H4 d( z* Z6 @( u5 T% f
- {
[# T3 [1 h4 j4 ]! `% R - int Err = getErr();
b) ]7 W. y' N - if(D1 == 1)
; R2 Z/ Z) ~$ _" u4 f' s - {; z$ C: g% _9 C9 Z8 U1 Q6 h& ]
- Left = 1;
1 F; w/ {+ g6 O5 Q9 q0 W - }6 E3 X, a$ `3 Q- d$ d5 d' z
- if(D6 == 1) F. ^" }$ `$ g3 R7 r8 o: Q
- {& b8 c1 b5 U f1 M0 R* D8 X. n
- Left = 0; b8 J6 V5 P* s C2 f
- }4 Y+ v7 L7 P: Z- s, E* S
- if(Err == 99)( e' g7 b' a, u
- {
, r4 Q5 l; ]% h9 y& I+ { - if(Left == 1)9 f6 j! f& s; n- u# Z6 g/ t; ]
- {, ]$ {9 i& O! c# U
- Speed -= 5;' l* x" t8 v, g1 N* k2 J
- moto(0,Speed);0 u* p; h- `7 |' i
- do
% @4 o, R2 X N7 v I1 U - {6 ]- v8 Y; i+ J3 R7 H4 N% i' i) f
- Err = getErr();; A; E. X6 p; g% A
- }while((D1+D6) == 0);3 [1 T# M# }7 w# d* X
- } t6 ^6 R: d3 J3 S1 Q
- else2 a# h5 k8 M+ D0 U3 E0 P0 J
- {
# [' n) j) h4 \( D% v. Q# |. i& \ - Speed -= 5;$ S" ]2 |3 s8 m% U( w6 `0 F
- moto(Speed,0); 6 V0 J. U: U! e- N+ a
- do4 Z# w2 l# i# ]: O( s
- {+ e4 ?7 e4 F" K4 V. o
- Err = getErr();5 q% h6 ?" B& i# H, Z+ I0 m. S. c
- }while((D1+D6) == 0);/ q3 z1 s+ f: B& U9 W0 u9 n* K% h
- }
$ ] t" M( f% [6 c- s - }5 k8 G. r, r' C) d
- else
. R" |" n5 E* w- V n3 @3 `' O6 s - {3 [: u: t& Q4 l o! K& k: q
- if((Speed < 160) & (Err < 2)) Speed+=1; 9 G8 J0 V' e! e' o+ o: w
- if((Speed > 100) & (Err > 2)) Speed-=2; 2 x; s7 K1 Z' Y: @ v; b
- integral = integral + Err;
. ?: j9 I/ d4 d - derivative = Err - previous_error;
8 k L X( R) [1 l - output = Kp*Err + Ki*integral + Kd*derivative;' \# v0 B ^# A7 Y" ^
- moto(int(Speed-output),int(Speed+output));
, i2 M: H$ C3 n) j- a8 j5 a - previous_error = Err;, a1 d: W+ v8 u* d& U
- }
3 Y% u/ D8 B# j. V& ] - }while(!((lightsensor_2.read()) < (10)));
$ P' J) r8 R4 p6 f- M5 m: T$ A5 o( P - moto(0,0);" C8 ?/ d- w" K- v$ a
- delay(500);5 c3 T" u7 M: g* ?/ X1 E
- buzzer.tone(262, 500);& \6 t* Z7 t8 J3 z8 h" z% C7 _- i
- }
' F$ e" K: ?6 s" p8 k - 5 v& \2 x0 r$ b2 r) m) M
- int getErr()4 {; A. K- o1 T9 C# r0 u* ]
- { & ^ }6 d' B+ {6 p! E# {
- sData = linefollower.getValue(); Y$ S1 x; V* h( R: }
- D1 = ~(sData>>0)&1;
3 M+ t2 N9 L3 H. u/ I( ? - D2 = ~(sData>>1)&1;8 z: \- l# S7 \# U7 \: w) D
- D3 = ~(sData>>2)&1;( t. g1 x4 F! Q( z9 S' j$ k
- D4 = ~(sData>>3)&1;
0 V v% ^3 ]0 ~& T - D5 = ~(sData>>4)&1;
. z c& N0 N" u - D6 = ~(sData>>5)&1;
" p% D( v" m+ d - int downD = D1+D2+D3+D4+D5+D6;
9 u8 _' v; r8 u( h7 |3 x - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);- P' v6 m# S! V: U R0 e
- if(downD == 0)* \1 D* z {, d+ w% v% O
- {" k' P: U$ ?3 q$ a6 `+ }0 k2 _; _' x
- return 99;1 ? b. Z/ k$ b* v4 t- X) x
- }+ O# h) i O- B4 L! G* m* y
- else9 @* ~: w- F! Z3 @8 q2 p. I
- {
5 g. v$ Z) i6 i" A - return upD/downD;7 {3 R+ V7 z7 |) Y) d: e
- }
+ y1 K- Q7 b. Y7 c# s3 Z - } \- X/ b+ a+ U) e- p: Y
2 d4 w2 `# [- z: j8 _+ ^0 Y- void moto(int Speed_L,int Speed_R)
0 D% R( f$ l$ }6 p1 b$ B% V - {2 g$ D7 \+ M" m6 n4 \
- Encoder_2.setMotorPwm(Speed_L);3 @& L- J( I! m
- Encoder_1.setMotorPwm(-Speed_R);
9 Y: J5 o. o: F1 c" x6 d) Z' H - }
複製代碼
' z0 K! g3 Z. T, k& c8 r1 i' o! h( wMeLineFollowerArray.cpp/ F4 a6 f- [& l6 R# v
- #include "MeLineFollowerArray.h"2 B; Q Z2 }+ j& g5 _- R0 X
- n* g h( e3 s( d- #ifdef ME_PORT_DEFINED& Z) k' i4 U' q8 I* i
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0). E* {$ d; u! @8 |
- {
6 V4 ?1 b! G# P% F+ u5 l
/ a/ H. X7 E% N; B- }4 D9 i( W& `6 D
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
$ k) n& @: d' U5 r - {, m, p, X7 N N9 e4 M: K
- _DataPin = mePort[port].s2;
! I: I- n8 B, P1 A+ P' O - pinMode(_DataPin, OUTPUT);
$ y; b( R/ i# F# C, ] - digitalWrite(_DataPin, HIGH);9 G& q! F2 ]4 }: C5 b: E
- }
6 t( r- f5 z1 T - #else // ME_PORT_DEFINED; c6 D6 T8 g: \% a
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)
1 b2 Z$ p, ^, P4 }( n. H, P4 K3 f - {
1 Y7 ]& f9 z4 {: J' w - _DataPin = pin; W) n" J3 ^8 d6 c
- pinMode(_DataPin, OUTPUT);
7 d+ ~! H% L, ]6 I - digitalWrite(_DataPin, HIGH);
5 n# o1 D. {4 j+ G# V+ O- i# _, i - }9 M3 d( I& j- ?1 u3 w! E2 M. L
- #endif // ME_PORT_DEFINED; D6 ~8 I+ k+ ~( [
- $ d3 Y2 u: I( Q) b
- - K3 Z0 e1 |9 N8 ?7 y# U
- void MeLineFollowerArray::setpin(uint8_t pin)( k4 n0 U2 X% R9 s# l, [9 N
- {
& V6 u/ I, ]; p( H" g; @3 p - _DataPin = pin;
% r7 e- A$ L% s* L, B6 p' m1 L - pinMode(_DataPin, OUTPUT); % e/ _9 k" K2 M6 t0 Y8 M: y
- digitalWrite(_DataPin, HIGH);, p$ t) u1 B% X" e
- ) z5 N& u; f( }' M6 X
- #ifdef ME_PORT_DEFINED! n, P3 G# s' A& b7 a; i
- s2 = pin;% H4 b! @: a/ l! V, a& b- O
- #endif% L8 ?2 [6 t/ N' z6 W/ u/ m) U6 T
- }1 }6 [4 y8 @: j7 e/ J6 x4 [
- 3 b" J- G- ]$ h+ [! _; k6 u6 o
- uint8_t MeLineFollowerArray::getValue() I* G$ y* l* J8 s; Z. \0 i+ F# c9 }
- {" ~9 k0 c6 V7 X
- uint32_t LOW_level_read_time;
6 J4 {( j- M; X' l) j# c5 h& w - uint32_t HIGH_level_read_time;
3 g4 G0 t* K" D5 q* }5 `0 m - uint32_t time_out_flag;
( U9 i4 r9 f: S7 ?* m# l - uint8_t Sensor_Data[3];
' s; ^! ?: B; \2 [# h" z. P9 T - static uint8_t old_data = 0xff; Y4 M- k. v: {0 d) g7 V# D+ P9 G2 N
3 h2 ?2 o" ~$ G" T) L- pinMode(_DataPin, OUTPUT);
- w5 w+ |6 ~# _( f2 I. V - digitalWrite(_DataPin, LOW);
: I% J0 g$ {+ g, ] - delayMicroseconds(980);) H) v7 G) ^. y
- digitalWrite(_DataPin, HIGH);
0 I: g4 o9 I# L
6 o% I1 E9 s2 ^- pinMode(_DataPin, INPUT_PULLUP);
! Q; m% [5 A/ L' O1 ]6 X - delayMicroseconds(10);& Q7 M9 Z2 l L% |6 B8 o
- ! M8 D. w1 z9 |( L+ z2 v5 z
- time_out_flag = millis();4 [8 C! b+ C& u
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
% [9 r& r- \4 }8 d8 _ - 0 O* ?7 R8 N; p$ I& r0 _$ W2 J
- LOW_level_read_time = micros();' K1 Y1 [ }" K5 V5 w+ I
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out2 v; e4 p1 |2 x2 \- h1 a+ J9 Y
- {
$ m) i5 |1 z* e) i; E8 ? - return 0xff;
* {: ]5 j# m8 J - }& N* R/ H. h/ X7 z2 @; S
- 8 w$ [% z ^- Y& ^
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
6 G7 s1 g* _$ K$ s4 d# S4 u - 2 l n$ c) C0 ]4 m# e* T8 }
- HIGH_level_read_time = micros();
; I& f! O; ^( U1 K! G6 O: p2 \ - LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
5 G0 U' x8 ]4 s7 A- [
C/ g; y' B5 ? T+ m( i- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
! j$ ?- \& K7 e5 w+ {' a! o8 e# ^ - {$ g. k- T8 a6 n( v3 X3 J
- return 0xff;
* g8 j9 @4 o# Z, L' r. |5 Y - }
) d; A0 y% K$ i' {7 Q d - * l0 L1 |9 i1 e K& a
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
9 e6 ]% N/ J/ a0 E: j8 N* O: o - {
" ]4 L$ Z7 q# l% u3 Y0 M - return 0xff;# L! X9 J5 `% N2 Q8 y4 J
- }$ M6 ~" e3 V9 f
- 6 d# J2 B* r, f
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
( X( u# E4 ~* R9 D* B; s0 h9 l - LOW_level_read_time = micros();: ]$ T1 H T- w7 A) C
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level" }# Y+ `: l, T( u/ I
# x& D; n; d1 W ?/ g- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
) \) @- C. Z7 I7 Q" I$ r* N$ } - {
$ I9 P0 i2 g6 h - return 0xff;2 Q2 ~* u$ ~, M
- }
3 @% K5 i4 z$ m; \: A
( _: o$ U, i+ L9 e) E: r- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
9 W9 ~3 e/ n7 r4 x8 y - {
- s: C5 B! i: j1 t) i - return 0xff;
" K9 |" O0 p7 ]" ]( O - }
# f& |+ V' o1 ?0 H- M4 x
- \+ n3 V h% g! X, t" j- for(uint8_t k=0; k<3; k++)) u2 M( E6 u0 |
- {& M3 Z, Q8 @1 @, e+ c
- Sensor_Data[k] = 0x00;' w4 @/ ~( ^+ k" X- }0 G
0 x- N' L) g0 H! P1 M2 R) ^- for(uint8_t i=0;i<8;i++)
. O* E! _6 k. R6 l2 g* _7 Q - {8 w# G/ Y( M3 b# O' \( Q& {8 U, M
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level) w& W$ A! h' b* v4 U. k
- HIGH_level_read_time = micros();0 y& O1 D$ l: d( {* I
- LOW_level_read_time = micros() - LOW_level_read_time;# h, ?) B4 q% x! r3 p; b
- : v% A. ~: p+ b' z
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) ) d8 f' C5 q5 m2 e" X0 o* ^
- {$ |) e! ]' d; D. F+ h
- return 0xff;* x( x; c4 ^, K& a: ], O& J% |, t
- }
! s4 ?4 T- J3 h9 n
6 v0 V: k- n* c- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
+ o# ?0 D+ F' u" T. o/ C - LOW_level_read_time = micros();
/ i# ~) R9 `1 o/ R/ _! h6 f, M - HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level- a) n6 B* ?8 X( P9 a6 T s. ]* `
8 S* @- c: Y( k3 m; o M( Y- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
9 G8 f7 j4 N# x4 W2 H1 } - {5 X% e# H, E* H1 V
- Sensor_Data[k] |= (0x80 >> i);0 n/ |; u, k/ d5 L y. [
- }4 b" k) \; p8 u
- else if(HIGH_level_read_time >= 100)2 f" M& p2 w! E% s, o& k5 z* ^
- {3 D& l- O% T0 `2 D
- return 0xff;
4 S$ z0 ^* U5 w( t( Q - }
3 q& t, w3 U$ j4 _1 p3 S4 y$ j$ e4 U
9 ^( h1 p. S4 J# t9 V) b- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out); A5 K$ [2 R- `
- {
1 ^$ b: m9 N, s5 P- t* N - return 0xff;
- f6 Z- n2 U* B- Y - }
9 C" k" N( ~- n, \' g) J- }- O - }: x8 g9 c: [& l; _/ H' F3 H& L+ ]
- }
0 `1 V3 J: L( o, [- ]$ h - % G& e7 ~9 H6 z! f& U- T
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
# @, _4 g1 q! y4 S8 J3 | - HIGH_level_read_time = micros();4 G4 s9 u* g3 d7 j+ h/ U
- LOW_level_read_time = micros() - LOW_level_read_time;
& J$ o9 c# I1 t9 S i - 3 }9 E( i- U6 @3 g+ V
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) ): |5 D2 Z; b/ q
- {5 U/ y; i$ Y: V! M9 ~
- return 0xff;" Q1 i1 w ^, d& G3 r; Q" y' d( N
- }
4 S! o1 @$ L: f# I - : h' o5 r+ J r4 N' Q$ ]2 U6 ?! |: n
- pinMode(_DataPin, OUTPUT);
2 O, D2 i* x! t2 C' t; h5 c - digitalWrite(_DataPin, HIGH);4 \1 R( s2 k% V6 i: P. C# X, _
4 n5 v6 {1 E9 b1 A: R- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))5 H& b" g$ U: a. z/ m" a
- {
, v% `4 }1 R" ? - old_data = Sensor_Data[0];3 _: D) K2 Z2 Y. O
- return Sensor_Data[0];
6 z9 ?, T X2 \2 z' t$ y. D - }$ Q1 y$ S+ K, _8 p. i, ]' x/ h
- else }2 A8 f. P+ k/ q/ F4 _
- {5 R* @' q: J- J& F2 P$ D( f
- return old_data;4 O/ Q5 j0 {' Y$ ]8 U
- }$ i0 T! L( G( l! S! p
- }( f1 m9 |0 I! p* X8 I
複製代碼 4 O Q! E. X: O M7 W
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
# ~& j" N6 r/ @; L- i3 Z" v
$ m% \# b5 t* C: M z' X |
|