|
( V( z1 u3 y4 U7 c5 I% E- #include <Arduino.h>
7 d" y4 s s; ]# y( }& H, S - #include <MeAuriga.h> W) p5 @. ]3 b% I
- #include "MeLineFollowerArray.h"8 c- N, b9 k+ _; ~ F9 v! G
7 }4 Y, {/ {2 e' b( i: t; u- MeEncoderOnBoard Encoder_1(SLOT1);
/ w5 W4 |' C5 a! M O- Q: ~& x7 v - MeEncoderOnBoard Encoder_2(SLOT2);' m. D% v8 [. Y. J6 K
- MeLightSensor lightsensor_1(12);9 j1 K! A2 }& e! u
- MeLightSensor lightsensor_2(11);
: s, q% F+ J8 c% A" D1 _: ` - MeBuzzer buzzer;
3 D0 ?! g# [0 K+ ~ - MeLineFollowerArray linefollower(PORT_6);
& {' Q6 C! g7 ]
; g6 \" }4 J7 W( f+ w" `$ C- #define Error1 19 u! M3 j" l. x% @ Z
- #define Error2 2- N7 E5 F% d3 D
- #define Error3 3
) v" P# T& X* N- j$ `
2 i. y N& o! v! H1 [- #define Kp 15
% Z3 v* ]1 F3 v9 l - #define Ki 0.15
7 F7 y- `$ Y7 d2 i/ U( m - #define Kd 0.03" a, g! Z2 y1 u5 q7 W& K
+ ~+ i# e2 ]7 f3 Q x- uint8_t sData;
6 o% @8 O0 O$ Z - uint8_t D1;$ Q7 ?; {% G8 S' B" N! s" i
- uint8_t D2;
, @ F+ w' F# H5 x- p4 o; f - uint8_t D3;
2 V0 f9 f2 `6 m - uint8_t D4;7 D2 Z2 P# ?8 z( r8 z
- uint8_t D5;
" N& k- s5 z2 g! A* }( w - uint8_t D6;* ~9 r4 y0 c+ Q
- , B: a; t$ l; ?* a
- float previous_error = 0;. h8 A" O4 h" k0 Q8 c
- float integral = 0;
1 M9 f; _* Z7 \8 R( O5 i - float derivative = 0;
( o* v' b. R# S3 E' \ - int Speed = 160;. d, M) R' k S- {3 _; {
- float output;6 J( H" C5 G4 j0 f f
- 5 B) S! R4 k' O) O( v6 i8 [
- byte Left;8 w& P6 }% A' { J2 s' p
- 6 o" T) \2 \6 k0 `9 ]
- void setup() 9 o) G! c( T! S9 p' g, f, I
- {
, b s2 w8 i& u% }: [ - //Set PWM 8KHz
: a# p: j% s4 K3 T* o, V) c# N - TCCR1A = _BV(WGM10);8 l5 ?4 J. G3 M# ~: B! o
- TCCR1B = _BV(CS11) | _BV(WGM12);! t; C; L0 `" y/ @; x
- TCCR2A = _BV(WGM21) | _BV(WGM20);/ k0 X) Q8 [8 y' e( ]* ?
- TCCR2B = _BV(CS21);
, O2 `6 A o% C - Serial.begin(9600);+ Z0 B+ C8 \7 T* g B
- buzzer.setpin(45);
! J3 f9 b3 h0 c: y: c! ] - }1 u c$ h* @* H/ Y$ m
- / O# O: b) C0 u$ x
- void loop()0 \7 B1 E1 D5 F, O* S
- {! H' l* q3 R6 D" J, G) x; K2 q
- while(!((lightsensor_1.read()) < (10)));
9 s- f0 ?0 e& S; | - buzzer.tone(1047, 500); w6 o6 t, {+ V
- delay(500);; Z! ~% N+ J+ e6 v7 m
- do
% t: \ _0 w, X: e& m, L$ c - {% ~+ S' R0 a5 W6 N
- int Err = getErr();( N/ p) L" a' T4 C# Z0 @3 E3 j6 V
- if(D1 == 1)* W! R3 g0 t% h
- {5 `4 _" h" ^; ], ^' a' \( u$ T
- Left = 1;
/ u* L$ Q' t- H7 R5 N. W+ k0 e - }. p! a" n& x" s/ X' u. ^
- if(D6 == 1)% k, c' [2 a2 O1 }3 I7 D7 H) p" d
- {
# `1 G; v9 x+ t4 L! o, D0 H" o+ { - Left = 0;, U: C# ^5 @0 p9 I, p0 T2 h
- }
, Y' F! X8 g. \9 Q( l - if(Err == 99)5 o* o5 v. U" \, ]3 S; e& G: d
- {
$ o6 T" T4 @9 I, o - if(Left == 1) Z2 v! x [: t6 b1 q/ r) o
- {
3 k3 z6 u# ^- ]% {, b& E; N+ d - Speed -= 5;
" Y- i, N/ J" l0 F/ w - moto(0,Speed);; I4 W4 s) v5 w0 F
- do) ^0 }8 M- U' y% v! F
- {: f1 S' T; B$ Z, l% O5 z1 x" n) R
- Err = getErr();0 i9 ]; W+ Q- Z: @. j' b
- }while((D1+D6) == 0);
! \% O- M2 ?' M/ \4 W - }- @5 L) {2 q$ x" [0 D
- else
}7 ^4 ^! d% c, s3 j& O - {4 b. {. \/ C! M+ z" F
- Speed -= 5;" r* x' _# b& U
- moto(Speed,0);
) j8 I& D4 ^ k0 D4 n - do9 n& F1 m( A$ C* s/ e6 q
- {% \ u2 |" D( T% |1 z; O% ]1 D5 |
- Err = getErr();! A9 P: j* A9 a. |3 a' B
- }while((D1+D6) == 0);6 l2 J. X9 d$ s) F" M
- }
3 ^% O$ x% d3 ]# T - }
$ p# v4 g9 Y2 r! A" x - else
& T+ Y) D1 b: d! h1 R( Z; | - {, X; j1 v! \, y
- if((Speed < 160) & (Err < 2)) Speed+=1;
) [% X. d* l" J% [* ]" j+ N - if((Speed > 100) & (Err > 2)) Speed-=2; % D! J7 H( x5 J, g2 ]7 H5 z
- integral = integral + Err;4 T h, c* S/ ~3 o/ n
- derivative = Err - previous_error;
* s' R1 g% Q0 z r+ T0 E - output = Kp*Err + Ki*integral + Kd*derivative;; |+ g( ~: t4 D" ]3 Z! N% B
- moto(int(Speed-output),int(Speed+output));
. W3 J2 C7 O8 d5 g& n. Z3 s" }5 o - previous_error = Err;
7 U! ~9 J9 C+ J- \, R- `; x. T) T - }
1 f) K: ]! g0 J) s# ? - }while(!((lightsensor_2.read()) < (10)));, B0 N, c% m! n w$ W0 X
- moto(0,0);- }) r2 C' { k$ N8 \% B6 |
- delay(500);) F5 K8 M2 H, ^3 i
- buzzer.tone(262, 500);8 x* W0 k) m9 [( z% X) U8 e
- }
# }3 K2 y: |3 ^- Z- T- o; v
/ y3 ^$ K5 u5 G1 F4 a" U- int getErr()' _. ?# z, i, d7 p F: j% {
- { * ~1 o. A9 u g% j, G+ Q' i
- sData = linefollower.getValue();
; s" a4 h8 a0 k3 l' x - D1 = ~(sData>>0)&1;, b i/ S& A+ T' F; I
- D2 = ~(sData>>1)&1;( x0 o3 V9 ?! }. t: S
- D3 = ~(sData>>2)&1;
& a% U8 O; k9 n& j - D4 = ~(sData>>3)&1;9 }( g5 e- r: b3 K! q; n- I: ?+ l. C
- D5 = ~(sData>>4)&1;
/ K# P, i: C: K* G1 l4 } - D6 = ~(sData>>5)&1;
, X* ?* j* Y- A: C* ] - int downD = D1+D2+D3+D4+D5+D6;* [$ c- [+ P0 E( W" o- W1 }
- int upD = D1*(Error3)+D2*(Error2)+D3*(Error1)+D4*(-Error1)+D5*(-Error2)+D6*(-Error3);2 H6 K9 L# J' |8 l5 ~! N! ]6 l$ `
- if(downD == 0)
/ f# ~: |- r; ^ - {
( o* y* x$ O( o" A' n1 g: m - return 99;3 R. z0 ^/ v: s! {, \/ K! U
- }
8 g- j8 A% C: h - else7 L. m1 {- N5 d& B4 ^! a
- {( j) d8 k- t$ d3 n1 ~1 d
- return upD/downD;$ ]* B7 ^2 f; a% J+ }! f
- }
( K, q* _, A# M - }
4 \ Z6 ?% Y/ m - 4 l! n& w J5 Y( N
- void moto(int Speed_L,int Speed_R)
! W- i$ O8 @% F0 ] - {
) a8 l0 B/ p0 p( A - Encoder_2.setMotorPwm(Speed_L);9 Z1 S F# G$ Y2 B& R# I3 l! V _0 S
- Encoder_1.setMotorPwm(-Speed_R);7 b1 U. q" q; s
- }
複製代碼 & V' c/ |( A; `' Z' j
MeLineFollowerArray.cpp
2 s X, [7 v; n: g1 ^6 E9 |& H- #include "MeLineFollowerArray.h"$ c2 R( K" c/ g
- " L* D0 r" x' A4 c( I. i
- #ifdef ME_PORT_DEFINED
$ q% y" t3 ?! e2 E* [! d2 G: {3 \ - MeLineFollowerArray::MeLineFollowerArray(void) : MePort(0)
0 y" {; F/ U% L3 v - {
3 N" S7 Y5 C8 S7 c8 n2 [. l - 5 q( z2 ?# L. P9 ?. N) m
- } U0 _7 c0 I/ \7 U
- MeLineFollowerArray::MeLineFollowerArray(uint8_t port) : MePort(port)
# j' [7 Q! Q9 F$ Q7 G6 o - {. c5 f1 |: v r
- _DataPin = mePort[port].s2;: e; k3 N5 O' V8 J( \. r$ r
- pinMode(_DataPin, OUTPUT); {+ k/ b& N9 `) S+ z
- digitalWrite(_DataPin, HIGH);
3 m% s* X% f" R0 X& ^7 j3 D - }
; m5 U3 z6 U6 \5 f - #else // ME_PORT_DEFINED3 D% d6 {) @" y/ [
- MeLineFollowerArray::MeLineFollowerArray(uint8_t pin)9 b; q6 ^3 w1 @/ i8 f5 p$ l* H6 U
- {3 |: v y9 P) }# G L0 M' f3 x- I
- _DataPin = pin;
- b5 Q/ B2 K4 \; R# A1 c - pinMode(_DataPin, OUTPUT); 3 a! {8 H* d/ @
- digitalWrite(_DataPin, HIGH);
- J6 Y+ I) H$ ?! x6 w7 K0 R4 | - }, M; G- F% `4 |! P5 r) E- A! V
- #endif // ME_PORT_DEFINED/ ^1 S$ V5 h5 ^7 `; g
, e1 \- C3 }: e9 L
' N5 f* S v# c7 l) n: b- void MeLineFollowerArray::setpin(uint8_t pin)1 H; |* n; e; z5 C) B" i
- {. x8 S, g/ ]: j
- _DataPin = pin;
: `. v1 g$ q% e! d - pinMode(_DataPin, OUTPUT);
3 m/ [/ ~" q7 X& l+ \1 ` - digitalWrite(_DataPin, HIGH);
6 C( q0 D4 ^3 ]# M% f5 u
! E% h! P6 i, x1 U0 I- #ifdef ME_PORT_DEFINED8 G$ ~7 y) D& m2 P3 M* R- _( p* v
- s2 = pin;
2 [7 J! Q5 q9 R2 V - #endif
3 s0 D5 ?! P$ ]4 I4 S/ ^0 Y - }; m7 N' t! n2 y6 g! a' ]4 y
2 Q( ^1 B9 e# w/ z6 u- uint8_t MeLineFollowerArray::getValue()5 D* _ V# s% X
- {
, Y$ F' ~6 Q% Z - uint32_t LOW_level_read_time;+ m! E/ y( H$ I) D% o1 Y2 o) \0 v
- uint32_t HIGH_level_read_time;% F0 ~' ?, C! Z u
- uint32_t time_out_flag;$ q0 Y$ B; m9 ]5 H: G
- uint8_t Sensor_Data[3];- @' Z& h1 Z+ L: ~* ~* v
- static uint8_t old_data = 0xff;
$ L2 g4 u# m" b/ S
/ n# d3 z6 a9 r1 X: Q- F- pinMode(_DataPin, OUTPUT);6 W* h& P4 H7 U. `
- digitalWrite(_DataPin, LOW);
/ c, a! O: l B! x6 ^ - delayMicroseconds(980); Y, V3 q& M' G
- digitalWrite(_DataPin, HIGH);7 i( t7 ?7 z' Z5 e8 p; l5 S1 Y& D
- r2 g8 K" ^7 V4 R$ A- pinMode(_DataPin, INPUT_PULLUP);; K! @, c; t: q u
- delayMicroseconds(10);) J% F* L) b% ?' s, \, [
* B- d& W9 d: E; |3 I9 `/ k- time_out_flag = millis();
; E; l! [- e E* Q# Z$ ? - while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );" W' g9 p' P3 C* Z
8 R( H& Y) Z/ U6 M3 b5 k1 N- LOW_level_read_time = micros(); h3 N1 C' s7 g2 Z7 p, ?
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
1 E8 G5 u) @' Y - {, C/ w! R5 h3 ]3 ]. w1 k
- return 0xff;, e+ d& f& Y/ S3 U3 _
- }
# Q1 S( G# @2 g- b5 @* j - 7 R* _# \0 ~' a' k7 C- S
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );. |# @; l& h' O7 `& v
4 D6 @ f& y/ b, j- HIGH_level_read_time = micros();) I6 c, N. i* b
- LOW_level_read_time = micros() - LOW_level_read_time; //read 100us LOW level9 M4 K7 t5 o( U2 B
$ D1 [* q$ U5 J8 q6 l) \; ~- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
: N3 o$ V: W) m7 w5 ] - { y' b. C, u5 ]: Y% @' E1 s- {5 U
- return 0xff;9 m, q8 ~! z6 s( h& f# y$ X
- }; n8 ~! ~9 N& i
- # A4 E$ d, b8 c) g) Y- @
- if( (LOW_level_read_time < 90) && (LOW_level_read_time > 110))
. t5 ^+ K, B6 i/ m3 P( [7 `. H& U - {
5 ?# n9 i8 Z7 p - return 0xff; _1 Z4 C/ f+ y5 }% P" ?
- }) k- n' k7 v% o. [9 V; r
) v- k8 u/ B5 S% B" u- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );* L! R6 d: `; \( p$ H A% d* u
- LOW_level_read_time = micros();
" m; f% e, m/ o8 ], f - HIGH_level_read_time = micros() - HIGH_level_read_time; //read 50us HIGH level
9 B8 \& y" I |; j9 }) c! T' r. V
9 j: a3 q6 w7 V9 P- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out) //time out
$ L! u( [8 P3 g r - {
/ m, W" @/ D+ c" M - return 0xff;; B( ]+ q; z8 f) t
- }
6 x! e+ C; K0 z3 n2 \ - % g# M4 j9 V. x3 X9 `- A% K
- if( (HIGH_level_read_time < 5) && (HIGH_level_read_time > 55))
. x6 a% j' _8 |% {. f - {
) u$ U6 _8 E0 s2 I' c% F; P - return 0xff;
9 P) D; O* `7 U9 e, _% e8 a6 @ - }
W4 O9 `7 ?7 L/ M) A$ H C( d - - G. ]' f. {1 f& y( o$ _: Q. M e
- for(uint8_t k=0; k<3; k++)
, L$ g2 {# v3 u - {
1 q3 j& }: q' Z) V, u2 m( G# d - Sensor_Data[k] = 0x00;. I$ R! y/ Q4 a0 V! a+ c
, X% n5 V% S* a- for(uint8_t i=0;i<8;i++)7 b: f$ m: h) y8 B' Z2 G2 G8 ~: o
- {0 {# ^' {$ U3 ~- p( c! S& d) e: r. C8 B
- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level5 Z3 ]- `5 b i5 t
- HIGH_level_read_time = micros();- M' t* H( |8 ]5 o. W0 o5 j% M
- LOW_level_read_time = micros() - LOW_level_read_time;
2 `" u( n4 [* I( y# B
+ `7 g/ U' L+ |0 t- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )+ K# K+ |4 D! W$ k* ^
- {- I* ? D" y5 z4 w1 z" K! W5 f
- return 0xff;
+ b- n) f3 h4 K( a* b6 V! G! _/ d - }
# }( u" u" d: N0 J3 }
* X- e W) W( j& l' p u- while(digitalRead(_DataPin) == 1 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) );
$ ~0 d7 l9 n5 Y, k8 x1 }& ` - LOW_level_read_time = micros();7 x- M8 o2 U5 Q
- HIGH_level_read_time = micros() - HIGH_level_read_time; //read HIGH level! k: W" h2 J; \
7 p, B8 L" t4 j2 l- if(HIGH_level_read_time > 50 && HIGH_level_read_time < 100) //bit 18 g0 ~% o9 l9 g9 ^1 J
- {
; @9 _" a5 x2 o+ c - Sensor_Data[k] |= (0x80 >> i); U5 i2 }; D% X, r9 V4 i& O
- }$ d3 j i+ x, x8 A3 l
- else if(HIGH_level_read_time >= 100)
! Q7 y+ ?, U3 {# V! j/ U4 @, X - {; p* }! ]/ O4 r9 u
- return 0xff;2 l0 j$ u1 x) A8 z% @9 n* N
- }
# f- p0 H f( I" \9 Z - + @, y: P4 U1 {; e0 L8 h" b# c
- if((millis() - time_out_flag) >= MeLineFollowerArray_Communication_Time_Out)
# |0 s0 m v9 X, W - {; z, U7 {/ y( i
- return 0xff;7 _3 v5 R, c$ M- p* a9 k) F
- }+ a9 L$ o. o# c& f7 \
- }
5 d$ i& {0 y7 O; |0 F: D) n1 K - } Y t3 v- Y$ u9 ?
* A% B# m* t p" L- while(digitalRead(_DataPin) == 0 && ((millis() - time_out_flag) < MeLineFollowerArray_Communication_Time_Out) ); //read 50us LOW level9 h+ I0 b9 D" n' N4 c+ k; W0 _, o+ ^
- HIGH_level_read_time = micros();! r* `3 q$ h( `6 S, V
- LOW_level_read_time = micros() - LOW_level_read_time;( I) {- |5 |# W* O. R% i
! { ?8 ?3 r* k- if( (LOW_level_read_time < 5) && (LOW_level_read_time > 55) )
+ {9 s1 }2 h5 O# H: c9 | - {
, s1 Z% q" W) q6 S - return 0xff;
7 Q8 t- a: Y D# _( M6 B - }4 J/ v0 {4 C2 r( h8 r* w9 ^
- 4 x$ |+ z( d d; G. K' G
- pinMode(_DataPin, OUTPUT);
+ }; o" a; b: {& D - digitalWrite(_DataPin, HIGH);! b+ \( e: f1 U+ ]: c
( j0 i7 }+ |& t" Z/ N- if( (Sensor_Data[2] == (uint8_t)(Sensor_Data[1] - Sensor_Data[0])) && (Sensor_Data[1] == (uint8_t)(~(uint8_t)Sensor_Data[0])))
( n9 y& j( u4 C4 g0 z- ] - {
* n% [/ V) H9 e3 d% F: C& f- | - old_data = Sensor_Data[0];' C2 W2 U! \: e1 k
- return Sensor_Data[0];
8 j: G2 \) q) [% s+ S {( b - }: Q! Z$ F2 o Z; m2 k
- else
1 q* \- t# O2 L) L% Z1 H* { - {: k7 t K7 |0 @& |1 x# o
- return old_data;. h1 f4 k0 P9 s: A- r4 @
- }$ t6 D0 p4 d- o+ F! j
- }
, r# I/ L' Y9 {7 d
複製代碼 / H- `# c# p' I. F
MeLineFollowerArray.h
MeLineFollowerArray.h
(731 Bytes, 下載次數: 10, 售價: 100 個銅錢)
: E ]+ [1 C$ o) g8 T7 @; ^* M3 b5 V; ^+ _) I: W: P. N
|
|