|
|
: k3 B/ m+ e5 ~/ H, H) V5 T# H- #include <Arduino.h># Y6 G+ d: g* y* B9 @, v- ~" D
- #include <MeAuriga.h>
5 N$ o! x( P2 K, m- q - #include "MeLineFollowerArray.h" c9 i6 M, W* B2 ]. [; M4 H
- J8 s7 b% N- z- MeEncoderOnBoard Encoder_1(SLOT1);
. s. \. r9 W3 u( k - MeEncoderOnBoard Encoder_2(SLOT2);# m. ^; x' B8 C: \9 n
- MeLightSensor lightsensor_1(12);4 r8 ^, ]9 K! Y( o7 C/ J( m
- MeLightSensor lightsensor_2(11);! V2 [8 M0 w% K
- MeBuzzer buzzer;
. E s7 X7 z% i$ o! U; n) Y - MeLineFollowerArray linefollower(PORT_6);
9 X/ h6 s& t' x% \( c8 ~
% c6 j# f/ @- i6 l! e- #define Error1 1
T* {9 P5 k7 m4 c% C+ a: W7 z; O - #define Error2 2
* @0 I: B8 `, { - #define Error3 3
7 ~( N# @- N$ g& A. {" k
9 E3 ]( ^5 d* f; K- #define Kp 15
+ R6 J( ^3 A3 A) z( ` - #define Ki 0.15
2 S' Y: P. r- L8 N. X, I - #define Kd 0.03
: c% X# ^7 \7 R4 F T% A
1 v/ a5 V# |+ L% I: g- uint8_t sData;
, d$ x' v/ z: Y - uint8_t D1;* `, I; y7 N2 O- v- ]1 O" T
- uint8_t D2;- a% o ~9 h( c7 b; m C+ ^
- uint8_t D3;! C. A5 q( P- X3 M" I- ~$ D! V
- uint8_t D4;
5 d! Z( V7 P. |8 i; h - uint8_t D5;0 v2 S$ c7 Z- s( Y+ E+ x) l
- uint8_t D6;
/ x, V# A2 H* e: h6 `% s - ( D$ u6 G9 X2 Q2 v m/ e
- float previous_error = 0;' b/ q' M8 H7 J5 X0 c, s# q0 P
- float integral = 0;" E* F* {& B# V4 ~! t* Y4 M
- float derivative = 0;
1 ^- M1 X& E0 d/ y! g0 | - int Speed = 160;
% }$ s2 n7 V. _8 K1 V - float output;
, V, q6 B3 L, t - / q7 B& ~* @! H2 y9 P
- byte Left;
0 k! y- j& S/ D5 t8 z& e" ]3 e: i
4 g2 T7 p5 {1 y; t7 o4 u$ p- void setup()
5 }( |1 J7 p( {; ]: \$ B a7 j - {6 D. @' v6 x: v& P) Z& F
- //Set PWM 8KHz
- t+ G- O+ L: T* J- L) h$ J. @ - TCCR1A = _BV(WGM10);" A/ I" z% P# B4 [
- TCCR1B = _BV(CS11) | _BV(WGM12);& k! }% O) {! k
- TCCR2A = _BV(WGM21) | _BV(WGM20);0 V5 |: `% [1 D1 X
- TCCR2B = _BV(CS21);
: ^; Z) C6 c. ?. P: H - Serial.begin(9600);
- D$ X; \8 z: D7 _( M1 @ - buzzer.setpin(45);
! `" t& t! `0 M0 ~& [; k. k - }
5 {$ Z P S/ ]8 a - 3 R% S% @& I/ Q
- void loop()) d/ q- w& `& g3 P, W
- {
) P. b% d# v% n# k7 T - while(!((lightsensor_1.read()) < (10)));) z1 { x1 e8 m% K" m
- buzzer.tone(1047, 500);
9 t J/ I7 A8 T+ Y3 r- Y4 i - delay(500);6 p5 k) V* l5 K Y; t" k
- do
' W/ O1 Y E0 u7 T( G! f# H; d - {, }9 h8 k% y: O0 d. ~# }% K
- int Err = getErr();
! n X9 B) l0 l! _ - if(D1 == 1)) }3 Q% ?8 N4 R" A) T3 Y* @- q4 o; x
- {
. q1 x7 ?& _: ?( L0 ? - Left = 1;
. M" X6 E$ O: c9 v H C, \, S3 } - }( [$ L9 Z* {( l4 A( U* ^
- if(D6 == 1)
& X$ j4 J3 \3 @4 w - {
$ I3 ^ b+ H3 |. v1 y- j+ K& h3 Q - Left = 0;4 o9 r6 u: P& {$ _
- } P& L5 b6 N' K( r/ J) j% X
- if(Err == 99)& N+ l7 k) @+ d3 O: i! `! e
- {
& m5 [+ N. k# c. Q$ c6 B! R5 e! {2 a - if(Left == 1)
k0 T- g$ M. e1 X - {
$ g7 i3 b0 s1 m, p8 a6 v - Speed -= 5;7 z5 y7 k! z8 T+ \
- moto(0,Speed);
; Y% q; I1 I9 h - do# i- X0 P4 _& P% y+ D% f2 e1 y! m4 L% r
- {
$ f: A3 S" `* G$ Q. K - Err = getErr();
: S5 l. F6 N+ c - }while((D1+D6) == 0);
4 U% j2 y1 S" D$ L8 Z. |9 y - }
/ k* I3 {8 V4 O) ]1 J# T$ q" L - else
) ?, J# _& x/ C/ q - {5 u. _+ O$ X- H5 E h0 f
- Speed -= 5;' p+ I9 H5 @$ f3 r- i
- moto(Speed,0);
6 i9 ^$ ]. v5 H5 m - do
( v z% {$ ]- w2 ? - { L" @8 A8 [& M- L
- Err = getErr();8 ~/ o4 K1 o5 F- w" b+ M
- }while((D1+D6) == 0);# C7 f. j) @* X& _: N7 p7 F% E
- }$ V5 l; _9 w$ Z( i
- }
6 C2 Z8 ~* O1 w3 v7 p0 j - else( v. D& {* L4 U3 D5 }1 `
- {) @0 A/ B# Q; } I* H" E9 v1 a
- if((Speed < 160) & (Err < 2)) Speed+=1; 6 s- P* v( O; M
- if((Speed > 100) & (Err > 2)) Speed-=2;
/ k" F" g1 S# z: o: @ - integral = integral + Err;
; e8 k# a/ ]9 |5 b h - derivative = Err - previous_error;! x' R* s: O- l* C% P8 ]$ \
- output = Kp*Err + Ki*integral + Kd*derivative;$ Q# S$ b' e) t# E+ q9 p+ q) y" h
- moto(int(Speed-output),int(Speed+output));0 W* @; V( R2 [! d
- previous_error = Err;
; B8 A& Q$ {+ ^0 r* V; n* j$ M( l - }
2 |' [# v* N+ w n0 ^ s* c - }while(!((lightsensor_2.read()) < (10)));, i; P+ b: c$ J1 `) v$ `
- moto(0,0); b& ~/ ~# c- f: Q. |7 m4 h
- delay(500);
, C7 ?3 H) `; h' y - buzzer.tone(262, 500);
' J- ~; n! f H7 r - }
1 @7 E) G+ v- k8 v% q3 W. G
% n+ A3 V+ w3 L1 a! I- int getErr()+ Y, P9 h7 s" ~) C% f; Q# j' Y. R
- {
& Z5 v3 N0 E, ~% D0 W( N - sData = linefollower.getValue();
. y! k& U& ~ i - D1 = ~(sData>>0)&1;* Q8 Z4 M& X* v' _+ X
- D2 = ~(sData>>1)&1;
1 f7 B* {9 k \ - D3 = ~(sData>>2)&1;
4 h# P6 n r) j- A% B6 Z - D4 = ~(sData>>3)&1;
' V+ ?& C3 k; E1 h) K - D5 = ~(sData>>4)&1;3 D1 K5 F2 D9 k5 o: k' M4 l
- D6 = ~(sData>>5)&1;: E; o2 e6 s, M0 ?* M0 a* A( l/ k
- int downD = D1+D2+D3+D4+D5+D6;
0 S, {1 o/ J( |3 o& a; N' f5 [3 e- d - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);
& o' Q0 R% E9 D" `) r. F* O% |+ q - if(downD == 0)
% p5 ~! g0 ?2 z* G* r5 d - {1 ~' L G# Z/ S- e8 O
- return 99;5 v7 M: S! t( t( G6 T9 O
- }' @& `2 }# i' o5 a8 J7 p
- else
; T" w4 u N$ I" ?+ x# x - {5 R; o0 n+ r% a8 T4 [
- return upD/downD;
. v, t7 p. o& \, B3 v4 s1 B9 ? - }
- c/ d; t9 N+ n( [3 E9 U - }
: _% q& W0 s2 @ i! I4 Q4 F% C - 4 D2 z e" j( b+ s3 A# G
- void moto(int Speed_L,int Speed_R)
0 d, }8 b! G0 D) \. b1 l - {
5 j6 M/ r3 C- O/ X# x - Encoder_2.setMotorPwm(Speed_L);0 |% {9 Q6 Z5 u2 g/ w# j0 j( H, W+ O
- Encoder_1.setMotorPwm(-Speed_R);
8 d7 n5 f6 M6 e( T8 B* L) _ - }
複製代碼 ' c! b* F* j# b
MeLineFollowerArray.cpp
" W: T9 h' j7 F$ F& o% E3 k0 w- #include "MeLineFollowerArray.h"( h; `) N" U# a+ c. i/ p P8 h/ ?' a
3 p5 d+ e5 k2 Y. X" }2 [( O- #ifdef ME_PORT_DEFINED0 H3 b. G- R7 k; N; E
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
% g* \) @- T7 t& |/ }' s - {
& G& P) {! J0 d$ M9 i R - 9 H2 T, p2 n$ L' G
- }# {5 ^8 j& F# ]
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
V+ U6 F6 J( G1 s3 q - {
7 Z7 f6 X" I, x* u, E; P4 H; R - _DataPin = mePort[port].s2;
5 S# x3 ^6 |6 h4 ? R - pinMode(_DataPin, OUTPUT); 4 J+ S% S& a# V( ~* I8 V
- digitalWrite(_DataPin, HIGH);
, x5 i% A, R1 Y8 V. Q; Q - }, Q# H6 a* C( q4 q, u1 ?+ c& ^. [
- #else // ME_PORT_DEFINED
/ \0 x0 \4 y5 T, n+ [* K! R - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)$ x8 o( P0 C% }# p( r1 T2 x/ K
- {
6 P2 a `5 g0 N& ]2 ~4 d8 g - _DataPin = pin;+ s( }4 D Y, x5 B
- pinMode(_DataPin, OUTPUT); : g" l6 R4 k- d3 O
- digitalWrite(_DataPin, HIGH);
X4 ]6 ]7 d7 V - }0 [& B6 v6 x. |7 T
- #endif // ME_PORT_DEFINED
" i: s$ v/ {( s! Q! X
. y+ \: V, V, a4 N( k, O' L4 m3 J
" D' I' E8 N/ E3 c9 _; Y/ q- void MeLineFollowerArray::setpin(uint8_t pin)
" G4 V% |! V- l+ J! D* ~ - {
) g4 s; ?+ y0 A; L/ c# u - _DataPin = pin;
0 y, x5 W5 {+ u% J - pinMode(_DataPin, OUTPUT);
- H3 [) t" P- m Y2 K# T2 n1 K - digitalWrite(_DataPin, HIGH);
& H0 V5 C: k9 m, G6 a1 C- R
9 W& b; J! y4 \2 [- #ifdef ME_PORT_DEFINED& K4 W0 ~$ i# I. V0 \" K4 z
- s2 = pin;" h4 T$ n: O) e8 a, n0 T( C5 ~
- #endif. ^5 M5 h& x ^! s, ?
- }+ _4 }+ v% @! e! V' j! ^
- ; _- m$ |9 G, w: {2 |" a0 \
- uint8_t MeLineFollowerArray::getValue()9 D: a6 S5 b/ K$ {4 c1 r R8 k! g- j" F
- {
1 @- h* E3 q+ \+ S% }- {: { - uint32_t LOW_level_read_time;! I2 i6 Y9 d) Q
- uint32_t HIGH_level_read_time;
3 G& z; k O- M/ W8 N - uint32_t time_out_flag;* h3 y, h6 n. h# G# J( c3 V
- uint8_t Sensor_Data[3];& ^* ?9 V2 t; m' E# K
- static uint8_t old_data = 0xff;% |/ K: E1 y6 z2 w1 z% [7 J
- ' F5 c6 D7 `% L8 R% t1 I" T; o- h
- pinMode(_DataPin, OUTPUT);
& u5 d; W1 P5 q - digitalWrite(_DataPin, LOW);
* j! t: ?: i9 b8 N5 f/ A" v/ O% F - delayMicroseconds(980);
! ^ ?2 G. o8 {6 U - digitalWrite(_DataPin, HIGH);) ]# w* O8 H) [
- : F- t" V; l) N0 l) o0 f+ u& b1 \
- pinMode(_DataPin, INPUT_PULLUP);9 x1 z) v% O7 W1 X6 L
- delayMicroseconds(10);: J1 P: |! Q: ~, h
- ; d. Z [9 q3 o
- time_out_flag = millis();
# k& z: O4 h6 K J) a! N2 {- s - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
( B! @) k$ A. l; a7 \& E. p
L7 @" i- O6 C- LOW_level_read_time = micros();5 a7 |% ~, N$ o% ]' y, e4 k* w
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out J- _6 V& x O6 i& C4 M& d
- {
) y0 r& F: n1 Y" V5 i. i& `+ a - return 0xff;& s, M) f. ^7 w# I
- }. r4 @( A9 B+ B/ E6 R/ _
2 w4 D% h+ @( v- y- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
# c# X5 b& Q* t( U( D& s
) W- G/ ^4 ^! P& X) s6 Y, h% k- HIGH_level_read_time = micros(); A! ]0 a' e( S+ C, X" `
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
/ A4 [1 M: f1 f. x
* f& D4 |$ K* M2 V" u# t- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out8 X h2 |1 G7 X1 N1 ^3 q
- {
) G/ {3 x! A8 W. ? - return 0xff;
* Q. x' y, S9 }! y3 ] - }# B- k8 r. d# r; q( E
5 {$ O: c: q/ C h+ X8 M# W- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))# m' i6 E" X- F2 T! E+ Y( }
- {
/ _0 m# X. C3 d! V% @, l' Y# Q1 X - return 0xff;
% [6 M0 {7 Z9 F: W$ I - }
! P) u6 D0 v2 Q; o% F* d4 N - + \: L* D. j, }
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );3 h$ f. C+ A- g% l- C0 A4 p$ E5 i
- LOW_level_read_time = micros();
2 R; a0 O8 @# f5 }( B - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level) y% J3 U. R F' v+ R
- ( d; b8 ~ |1 W
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
0 D/ |/ X+ z H$ N7 | - {
7 W9 c5 G0 {9 x - return 0xff;9 C$ ^+ V3 L* G1 o" L
- }- C" {( U4 S( `- m7 d
# T2 z6 | E; t6 p7 L# C- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
) _' U1 l. v- S( m/ K% D1 N1 v3 j - {9 n, n( G* G0 ^
- return 0xff;
5 e% a2 T2 G; N7 G3 j# W2 M - } T* c6 r8 b: d
$ X( t. V A0 j- for(uint8_t k=0; k<3; k++)9 |, m/ Y4 F5 t3 D
- {
) `0 _$ Y6 o1 p [8 K3 C# @3 H - Sensor_Data[k] = 0x00;
7 k7 C6 s" I! G& ~1 L7 x& A9 L+ o
) _7 [7 [ P- P- for(uint8_t i=0;i<8;i++)6 J2 z/ ^# ]. x5 z. p4 }7 {- ~
- {0 W, n* d, p# ^& b6 `- ]
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level9 l/ L6 H, [6 q! h
- HIGH_level_read_time = micros();" \9 y! n! T( F J4 ]
- LOW_level_read_time = micros() - LOW_level_read_time;, j! E4 j5 M: @$ E
0 H2 o- Y, M0 ]- `0 M/ n- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )0 Z v0 D$ ?: t
- {) [6 C: l" r, L! P9 r+ T, P# g6 l8 C. E
- return 0xff;4 h2 x- V( p: u: E0 V
- }
5 P- t7 b: k9 s7 o4 f) ` - + `+ K9 n# F$ a5 _8 \1 V
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
1 o4 O6 x5 i8 {' H" ` - LOW_level_read_time = micros();( n8 [* g/ ]% E1 N9 Z# }
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level
; Q2 Z. O- j7 X- p: l
* \! F0 d, l* c+ | K) t% ?1 T- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
) t- P3 Z% Z: j/ `9 ] - {
" j! j: c) `& t9 J' W: i: _ - Sensor_Data[k] |= (0x80 >> i);& R8 C) e# F) m4 i! ^) }
- }2 ?7 A; F9 l/ f* ~
- else if(HIGH_level_read_time >= 100)3 S3 v: N- G( u+ m& @
- {
8 `( @1 R- y; L6 V$ j2 b% Y e$ d( [ - return 0xff;
) m/ u7 z# x9 ^6 q. [* ^ - }
/ B0 N6 j2 v$ R/ _
) p0 Y- U# s0 k- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
/ x5 h# p( c ~5 p - {: _% [: o$ n* r. a# N Q" T
- return 0xff;
. E3 P1 @. _7 J: K c% G, k) [0 \5 k - }
! A* J+ P# ~8 E$ @+ d' ?3 ]" ~ - }
- `$ m4 y) s$ u* p2 K/ e - }: E7 K& b# Y- x! ~
- $ U& {+ M, x. [( h/ n8 o& {2 w' c
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level; F1 ]! K. Z* v' b/ U5 I! y, h
- HIGH_level_read_time = micros();( k, S2 r* b% r$ R! E9 t2 n. |
- LOW_level_read_time = micros() - LOW_level_read_time; d8 \9 `; g4 J+ \
$ T2 ?& Q3 a, \- L. i7 E- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )2 i5 g. {. U) ^
- {
% ]: A: u, l( I% J& f) k - return 0xff;
( f. P; _! S U, Z1 ?. I - }
8 P- s2 C$ N! g8 U - ; H1 \' U* _3 u: h/ l( @" E0 r
- pinMode(_DataPin, OUTPUT);
0 N8 ]- `& K/ `3 [: S" {" `8 F - digitalWrite(_DataPin, HIGH);
+ F/ i1 M) R6 L$ \, V) \# n
& `* z) K- z: G. ~ m9 z- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
9 ?& |. U! ]: s. P1 e3 \ - {- J5 p5 I# @9 ?
- old_data = Sensor_Data[0]; i, s( Q4 g( m0 o
- return Sensor_Data[0];
! g& o. I9 I' P4 R* w/ p6 u - }( r5 C0 \/ [* N
- else
% \# G$ k: @: B* l1 c% x4 B0 t( w - {
) F/ f# |0 J5 Q. i - return old_data;" {7 z' ]6 w! G
- }( s, a! C; @ e$ d
- }, o( C {9 o: z- H" G/ P
複製代碼
7 s* [3 o" M1 R+ ]) X/ a) \MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
8 D1 K9 F- _2 o. @$ v
' o' U0 g0 |, z: z/ S |
|