|
|
+ j1 O. ~, m9 }) ? J- #include <Arduino.h>5 P/ L/ J* E/ e3 L
- #include <MeAuriga.h>
0 z& ?$ T2 C) W/ {) k - #include "MeLineFollowerArray.h"& S' h0 [/ x: x! ]
8 ]4 [5 \6 t1 v4 ~- MeEncoderOnBoard Encoder_1(SLOT1);. H( |2 F2 `0 S9 Y {
- MeEncoderOnBoard Encoder_2(SLOT2);
) A& s% L) ]! U/ T+ P - MeLightSensor lightsensor_1(12);( \0 Z( _3 B1 f0 W ]
- MeLightSensor lightsensor_2(11);
1 K0 z# Z% N; w% [# r f - MeBuzzer buzzer;
- q S' Y' F7 C - MeLineFollowerArray linefollower(PORT_6);
" R2 L; {$ f$ M% N- u* G. L* w - ' |4 H. W3 G x$ S' c! h
- #define Error1 1
9 X* Q5 ]8 p' e7 g7 C9 v0 r - #define Error2 2" l I3 G8 i+ ?* M% v3 { N# _
- #define Error3 3
7 L* E4 ~# g3 X! H# g - + a- d5 `- b# f& q9 e
- #define Kp 15$ ]- P5 D, c" E0 x" t4 w# J
- #define Ki 0.15
# {# C8 k6 v! T$ f+ Z5 w - #define Kd 0.03
; O+ V E* F) d) [6 F& f
2 O, [5 l) [4 ^6 Y- uint8_t sData;
0 S8 \. Z+ f, N: s - uint8_t D1;
7 Z ]' G. T: L& V3 L p - uint8_t D2;
M! H& G9 X8 ~& Q - uint8_t D3;
$ [' v' D. R# J, F - uint8_t D4;/ N: K) @3 f, u' y: w& t
- uint8_t D5;8 n5 `6 u8 n% [" E* J1 R
- uint8_t D6;
( p5 {& r) l. Z; s4 K) U: _
& s$ g1 V0 `# n2 k9 v, ?/ O- float previous_error = 0;, ~' X; L5 |4 q) J$ c8 P
- float integral = 0;
O7 F7 |6 t. j. W! f" A - float derivative = 0;
& U- J3 k# _1 A6 t - int Speed = 160;# z( T. h& c3 k4 @6 G0 P( r2 g
- float output;
' W( Z3 Y7 ` n g$ q - 2 K7 Z3 g- `4 P, D( \* K7 t/ b
- byte Left;
. b$ M2 e- q2 x W+ B: ` - L8 A/ K( C2 d+ @) B
- void setup()
& i3 c* G& ^6 F% w* z" j - {
- n8 ?" ?# s9 _ - //Set PWM 8KHz
6 w3 m3 j+ {9 ~* U) d - TCCR1A = _BV(WGM10);: e, y! ]2 j' C/ _
- TCCR1B = _BV(CS11) | _BV(WGM12);
8 {4 j% I) d0 R1 U - TCCR2A = _BV(WGM21) | _BV(WGM20);
( [/ \7 R( R* F- P - TCCR2B = _BV(CS21);
7 G, z. G* w$ j1 G( k+ O; C* L+ a - Serial.begin(9600);, j# p/ p) [. n$ ~+ e/ C7 L/ r
- buzzer.setpin(45);: \5 f( Q( `0 ~, S( x0 z4 X2 Q) P! Y1 R
- }1 n0 B* s8 x6 _
- ! w x( B) C2 b- D7 c: X: Z
- void loop()) G% L( T, | L/ ~
- { \6 _. G1 o5 a- H
- while(!((lightsensor_1.read()) < (10)));* `9 P% Z# |4 ]/ k5 h$ o
- buzzer.tone(1047, 500);
8 m5 b8 d' @& ^& o - delay(500);
1 p0 c& c. T0 T7 n: X - do
: I( r2 L, {* U( h- n" a# Y - {* R+ D' j: y8 t. s- `2 k
- int Err = getErr();
% F0 K G5 _% j$ a - if(D1 == 1)/ W3 G2 P6 X; C0 A0 L" X8 a, O
- {
% s: @" K8 \1 O0 k3 J( A, B - Left = 1;
& k+ |3 |' q( l# q$ t - }
' p' N' o- o1 _/ d - if(D6 == 1)" s( u' b8 J: Y. x+ N
- {
# F( e: F! C$ o# P - Left = 0;
9 h- Z' k" w. r+ V6 { - }- P5 x" }) n# _. @% z0 ^: ?, q
- if(Err == 99)# R; q- D9 w5 P2 L
- {
' d# C' u( h( m0 R# h4 j; _$ H - if(Left == 1)' k* k) S7 u8 a) S8 y
- {' j, e6 O" o1 g$ L( ~
- Speed -= 5;
. \3 W- V- Z$ d# i8 Y - moto(0,Speed);5 `" w) H7 ]" \7 c
- do
: P" _1 S* f2 T& c/ F* p# k; W - {' H& C! B4 _ g9 l6 p) z
- Err = getErr();# _& Z& _+ ]$ z; F/ B
- }while((D1+D6) == 0);& P2 H9 a; a! p
- }' e* O- b4 [ a
- else4 a/ V0 [$ [- P$ P, [0 M1 V
- {+ U# d1 {0 R* s; X
- Speed -= 5;
. h8 @5 F* U4 W* r, f @ - moto(Speed,0); , D y7 Y& x9 F8 E' x; ~: K
- do# {9 n1 f$ V$ y' {" Z
- {' |. q0 H5 s7 v. j+ `9 D! L4 D
- Err = getErr();2 x* z& n& G- z$ x
- }while((D1+D6) == 0);
0 D) P( \7 {' G2 B3 W I: k2 H - }7 _3 P$ U T' e: G( ?
- }
3 j) D& t$ j' a - else1 s: H$ h3 W/ {7 F
- {
. ^4 u& Z/ \' \3 \- @ - if((Speed < 160) & (Err < 2)) Speed+=1; " U7 O0 L ?- j! A- a* y8 k
- if((Speed > 100) & (Err > 2)) Speed-=2;
% M' B3 Q1 H$ x9 c, Y5 g - integral = integral + Err;8 s u" X* z5 r2 |
- derivative = Err - previous_error;$ r7 H8 S2 D8 j' [$ }) E2 v
- output = Kp*Err + Ki*integral + Kd*derivative;
! ]& ^2 `. ?' ? - moto(int(Speed-output),int(Speed+output));
0 V" i# U, \4 W" h6 P9 x - previous_error = Err;
! ~6 C) ]/ u, K) D' Y ?6 C, o. W - }
1 v' A' f5 p' s4 t+ W - }while(!((lightsensor_2.read()) < (10)));
- @1 F# H6 f3 k% o; K - moto(0,0);. T' a) |3 p8 t& {
- delay(500);
/ O! _/ g6 P1 P! ]- N - buzzer.tone(262, 500);! w Q9 o! V1 [* E
- }
, ^! s4 H3 h* d2 B' f - Q; p, }: e7 C8 W$ d' w
- int getErr()$ v( B4 j' P5 X
- { $ b" E3 n' Q0 `" m1 p" N
- sData = linefollower.getValue();) J2 m( ^# ]7 U/ s& J
- D1 = ~(sData>>0)&1;$ ]% _0 n, y( O! X1 j; f9 r7 \% {6 c8 ^
- D2 = ~(sData>>1)&1;8 i" [1 ~2 B3 o/ ^
- D3 = ~(sData>>2)&1;
% V' p: u1 N5 f# | - D4 = ~(sData>>3)&1;
. e" B, l$ m5 k x( O - D5 = ~(sData>>4)&1;+ Q* I6 Q; }, D1 ?6 y* T# [& @) @
- D6 = ~(sData>>5)&1;
" S) d% h: P# O9 P; E - int downD = D1+D2+D3+D4+D5+D6;
?0 E% h) q6 Y" i7 a6 q - int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);4 N& z# |8 H. W- @( w1 K
- if(downD == 0)
% N% B5 o: [+ H9 l6 F; [ - {
) v* P& ~- Z" g - return 99;
5 s$ V; U$ t/ K" X( @$ F - }- S' z" j$ @& W b+ C
- else
5 Z# }9 h# Y" _: L+ E9 n; M - {
- L5 [( L, O7 W; [( T) N1 Z - return upD/downD;
( s' H [4 B; N6 O% T - }- e0 R' R1 V- A( L1 p& C! P
- }
* y# B% @9 J A! | - 9 Q) a8 ^1 F+ @ \; y% R
- void moto(int Speed_L,int Speed_R); U G- L6 o- x3 k
- {
& g5 e) F; F2 ^! ~, ], a - Encoder_2.setMotorPwm(Speed_L);, q i) {7 V3 }" R( e9 q
- Encoder_1.setMotorPwm(-Speed_R);6 c* N- Q, M* g! m
- }
複製代碼 ) N1 B* Y! C& L7 I8 i; Y+ |
MeLineFollowerArray.cpp/ ~8 y$ V4 H" u. `
- #include "MeLineFollowerArray.h"6 }4 R' `0 B, n5 T' Z
, e" L; |1 S2 A- #ifdef ME_PORT_DEFINED' F0 \6 [0 \7 } U
- MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0) K& g, _8 d* ]# s- ^8 C+ |
- {
" V h3 i* ~. M. U - 1 Z; ^6 ^) m1 I. E2 f
- }! a: [# p+ j A8 B3 v$ w$ w' @8 R
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
. A4 j6 }: b( q, P& h [- l - {. N) Z. A8 {# q+ T
- _DataPin = mePort[port].s2;
8 k; Z/ }/ y6 T4 r5 L7 b - pinMode(_DataPin, OUTPUT);
+ }- T+ | g$ ` - digitalWrite(_DataPin, HIGH);4 G) _& `- Z& P5 y5 C- N' j
- }, L6 U1 A. Y' i) c4 ~* {" B3 `
- #else // ME_PORT_DEFINED
& c8 E0 y& A2 \8 S2 n6 K - MeLineFollowerArray::MeLineFollowerArray(uint8_t pin). ]: @9 {# {; w3 X+ ]5 |
- {
1 A+ q6 R$ {2 h4 ]% _ - _DataPin = pin;' x' _( U- q' N" W5 n
- pinMode(_DataPin, OUTPUT);
1 _; G' q: w% }, q - digitalWrite(_DataPin, HIGH);* s, i7 c2 g8 ?( g% H% b I+ G
- }
* Y8 k/ J* N' ^ i. F, |' D - #endif // ME_PORT_DEFINED5 l) F7 y' k+ r |; [
- : B* \( k! t7 @
- 0 F9 K. X }+ e/ x
- void MeLineFollowerArray::setpin(uint8_t pin)% Z' z: C! g, G$ Q
- {
! n ?2 G4 G3 I: P- s$ k5 B - _DataPin = pin;& T8 o9 @' E% j* c0 F
- pinMode(_DataPin, OUTPUT); ; l2 I2 ^5 z) \
- digitalWrite(_DataPin, HIGH);! W' t: U# a4 \5 m" o. @
- 8 m2 h& t" J! ?6 O
- #ifdef ME_PORT_DEFINED
! \0 ]* ^/ y. @4 `3 x2 h - s2 = pin; e% s7 O# t6 M. n. H5 _
- #endif
- v: L5 k- l" v4 A# B5 f0 X - }
# w; v! F6 ~4 Y
$ e1 _- [2 M3 O* `- uint8_t MeLineFollowerArray::getValue()
3 u/ r' N$ t6 z4 Y+ _+ s2 S - {
, ], m7 h# [( t" u, v - uint32_t LOW_level_read_time;& a' b+ S1 h4 t; \. q
- uint32_t HIGH_level_read_time;
' Z- N, F. e# P7 `, |; @( h - uint32_t time_out_flag;
$ a. r. Z8 t9 G9 X7 n/ E - uint8_t Sensor_Data[3];2 W) F) Q/ I# _" `# I
- static uint8_t old_data = 0xff;& y) F8 B5 ~: M3 P
" O* w& v8 Z# O- pinMode(_DataPin, OUTPUT);
, _! i/ T' j. [) N% F - digitalWrite(_DataPin, LOW);( @7 z& u! \8 ?2 [9 }! a
- delayMicroseconds(980);
- r/ w( w' \+ W - digitalWrite(_DataPin, HIGH);# C- H6 P1 u% Z
' ?/ Z* N; @( C0 ^: Q2 _- pinMode(_DataPin, INPUT_PULLUP);! k* N2 \ ]6 H* i, Y. f! z9 [
- delayMicroseconds(10);2 Q. U3 u J! x' M
5 U' ?6 P$ K5 Z x$ u4 ^- time_out_flag = millis();* h$ s q: a7 d) J# K, p
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );/ s" c4 g5 R% G; p7 f
9 e. Y' {/ |% y% y! A- @- LOW_level_read_time = micros();8 `8 e; ?' p9 w6 v$ ]! q1 u
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out( t& G0 F% B' l5 j- R' o9 B
- {
/ n3 s4 j @ ] i# C/ e6 S - return 0xff;
0 W/ U u' j, t5 h - }/ K5 C9 r; m+ I# i. U
: Y% x( p! b2 y+ F/ k, G+ _- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
# l. H2 |0 I' v& \
. E# m+ z3 K* K' ` d. M P- HIGH_level_read_time = micros();$ T" H& t0 d, \+ S' _- Z' ^+ s S5 U
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level
+ f' \% |* W2 H/ ~0 j - 1 x3 X( R% y- o9 h
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out8 H" q2 Z* W0 \1 c
- {
5 Z" @* `: A1 s8 U! y/ ~6 ]; t* C - return 0xff;+ j4 o7 y$ `9 d* S* {( ?
- }+ ~- l- K9 P2 x& R, P
1 S- ^5 @# j: m, O- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
" j( J0 f+ Z! w; T - {
* j B5 i i2 g# q6 ~ - return 0xff;
7 f; }% a! q6 r6 n' s+ Q - }! \( Z$ Z: e5 S4 @4 ?% B! q$ c
- / t6 p$ M5 n, R' L
- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );9 `, g2 y2 E& I9 l$ B
- LOW_level_read_time = micros();: Y$ `: c% {1 }: r
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level( S |4 z$ }9 S1 d0 W- u0 R
8 C4 J# q% u" `2 E- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out) _9 @& T, B$ c( a
- {
% m0 G1 p# q2 l6 s( q: w - return 0xff;: Y* A0 D2 ^; o& N* v4 q3 J
- }( t% W( n L0 x: }9 N
- : K5 B5 I* W" h6 v
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
* t2 n/ T# o& t. U- [ - { J% ]1 f9 b! n/ l
- return 0xff;/ c9 p9 i: Q1 b1 ~
- }: k5 M( B- z5 b0 u8 J
9 j4 `/ I3 c0 m7 Q7 r4 h/ J- for(uint8_t k=0; k<3; k++)
4 d( Q; p F0 t/ G3 p) y, F9 ? - {* A! B4 l( G+ c8 ?+ o9 D
- Sensor_Data[k] = 0x00;
3 a8 U& b. R/ t1 b) u$ X - 1 T( I& `# @) e, J5 K1 E3 S3 d/ B c
- for(uint8_t i=0;i<8;i++)- Y5 ?- T3 \' n: f
- {
) c$ ]+ X# s# ]: [ - while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level; |6 f4 }! T# B1 q9 Q2 F, ]
- HIGH_level_read_time = micros();
9 U4 g8 u6 z' @* Y( { - LOW_level_read_time = micros() - LOW_level_read_time;% u A$ {6 e" g4 Y o
- 5 A& V8 F0 q' a9 g" i
- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) ), w7 |9 C+ O) u' f( w
- {
' B7 g; T2 Z: ?7 a& j' J - return 0xff;: q, v/ D) J8 g1 S
- }
" i b8 p0 f( }
2 D& t4 e0 e; B) s" c5 @2 L$ r. c7 i- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );, d( h4 b4 z4 K d- ~, v
- LOW_level_read_time = micros(); k/ I2 V! M$ ~5 J4 Q0 f0 r
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level5 Q6 W/ c* Y* ~* G/ c
- & g" X4 h0 y: Z4 V: Q# h
- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 1
5 r9 B: \% U2 c( u - {2 |. t" Z a/ p& U0 C" ]
- Sensor_Data[k] |= (0x80 >> i);
0 b- N* b( {2 t% z - }
; C+ |2 l" i: G2 U& I9 E - else if(HIGH_level_read_time >= 100). ]2 `5 }8 Y9 a/ c+ e% Y
- {
: q: @. J8 [/ s3 u2 |, L - return 0xff;9 O2 h- |% t* E3 U4 l8 o% @, \2 X& f, G
- }. Z2 m5 w$ W! ~6 c; V0 A3 S W% N
& R+ t* S' C0 S' P- v6 b0 O- l- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)* N5 z; c6 S% g, ^, S: P
- {
* L8 O5 X$ @- C$ F* w - return 0xff;
: s# ^% R3 c" H6 b& d - }, @* o( H: r8 A$ R
- }$ @4 p: |, c' T# i2 w, G
- }
. d4 }+ i/ N4 i" q$ M
* X8 f2 j# T! c T& r, J6 `- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level
" ~1 Z: j, r7 A6 b$ g4 A - HIGH_level_read_time = micros();1 W, k Q M* w2 H
- LOW_level_read_time = micros() - LOW_level_read_time;
% N4 [5 ?, r5 b# p b" e
3 T( i6 [: d$ J6 K9 X- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
" s: m, x" }6 g, V( `6 m4 {3 a - {( e7 L, r+ j) |* D7 M
- return 0xff;3 D' d3 N5 _4 i# {# L4 B
- }# i" R8 `) `% U$ I- }
3 J% b/ `0 [/ Y* R- pinMode(_DataPin, OUTPUT);' P x7 [4 D+ N2 w( K3 I- x9 v
- digitalWrite(_DataPin, HIGH);
. ^" ]7 q7 a0 ^0 U$ U2 H - * `$ w0 Q) g; x; E
- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
# k3 F( O& h. b$ b - {
& {! o8 s; X3 w6 n' X: B; q - old_data = Sensor_Data[0];
1 z: I8 g1 f9 t6 n3 [ - return Sensor_Data[0];
. A0 E ]: {5 T, }$ W! X$ M - }
0 Y3 A8 K: c0 s, ] - else
8 k" d& ]- w8 p( t' v1 d; b* T - {* z0 a- Q5 {2 t
- return old_data;
4 H+ d/ v1 _, m - }
0 g3 @' _2 R3 |7 i - }
% P. c% F) V( ^( o/ O3 J4 N
複製代碼 6 |) j+ M! C; ]0 }$ ?4 Z. ]
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
/ G3 u6 Q1 K+ a2 e
- v. B- x- l8 V' E* ?, h
|
|